Zag-基于狀態(tài)機的組件庫
本文適合對狀態(tài)機感興趣的小伙伴閱讀
歡迎關(guān)注前端早茶,與廣東靚仔攜手共同進階~
一、前言
官方介紹:
Zag 是一個與框架無關(guān)的工具包,用于設(shè)計系統(tǒng)和 Web 應用程序中實現(xiàn)復雜、交互式和可訪問的 UI 組件。
可以在 React、Solid 和 Vue 中構(gòu)建可訪問的 UI 組件,無需為邏輯而煩惱。
過去我們經(jīng)歷了太多與我們?nèi)绾螀f(xié)調(diào)事件、管理狀態(tài)和副作用有關(guān)的小問題和錯誤。大多數(shù)這些錯誤都與useEffect編排useMemo有關(guān)useCallback。
二、Zag的特性
Why Zag?
三、安裝
Zag 可以在大多數(shù) JS 框架中使用,例如 Vue、React 和 Solid。
@zag-js/vue

@zag-js/react

@zag-js/solid

為自己感興趣的組件安裝機器
npm?i?--save?@zag-js/{component}
#?or
yarn?add?@zag-js/{component}
#?react
npm?install?@zag-js/react
#?or
yarn?add?@zag-js/react
#?vue
npm?install?@zag-js/vue
#?or
yarn?add?@zag-js/vue
#?solid
npm?install?@zag-js/solid
#?or
yarn?add?@zag-js/solid
四、demo
import?{?useMachine,?useSetup?}?from?"@zag-js/react"
import?*?as?toggle?from?"@zag-js/toggle"
export?function?Toggle()?{
??const?[state,?send]?=?useMachine(toggle.machine)
??const?ref?=?useSetup({?send,?id:?"1"?})
??const?api?=?toggle.connect(state,?send)
??return?(
????<button?ref={ref}?{...api.buttonProps}>
??????{api.isPressed???"開"?:?"關(guān)"}
????button>
??)
}
import?{?computed,?defineComponent,?h,?Fragment?}?from?"vue"
import?*?as?toggle?from?"@zag-js/toggle"
import?{?useMachine,?useSetup,?normalizeProps?}?from?"@zag-js/vue"
export?default?defineComponent({
??name:?"Toggle",
??setup()?{
????const?[state,?send]?=?useMachine(toggle.machine)
????const?ref?=?useSetup({?send,?id:?"1"?})
????const?apiRef?=?computed(()?=>?toggle.connect(state,?send,?normalizeProps))
????return?()?=>?{
??????const?api?=?apiRef.value
??????return?(
????????<button?ref={ref}?{...api.buttonProps}>
??????????{api.isPressed???"開"?:?"關(guān)"}
????????button>
??????)
????}
??},
})
import?{?createMemo?}?from?"solid-js"
import?*?as?toggle?from?"@zag-js/toggle"
import?{?useMachine,?useSetup,?normalizeProps?}?from?"@zag-js/solid"
export?default?function?Toggle()?{
??const?[state,?send]?=?useMachine(toggle.machine)
??const?ref?=?useSetup({?send,?id:?"1"?})
??const?api?=?createMemo(()?=>?toggle.connect(state,?send,?normalizeProps))
??return?(
????<button?ref={ref}?{...api().buttonProps}>
??????{api().isPressed???"開"?:?"關(guān)"}
????button>
??)
}
面試題庫推薦



五、最后
關(guān)注我,一起攜手進階
歡迎關(guān)注前端早茶,與廣東靚仔攜手共同進階~
評論
圖片
表情
