@zag-js/tabs 0.69.0 → 0.71.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/tabs.anatomy.ts","../src/tabs.connect.ts","../src/tabs.dom.ts","../src/tabs.machine.ts","../src/tabs.props.ts"],"sourcesContent":["import { createAnatomy } from \"@zag-js/anatomy\"\n\nexport const anatomy = createAnatomy(\"tabs\").parts(\"root\", \"list\", \"trigger\", \"content\", \"indicator\")\nexport const parts = anatomy.build()\n","import { getEventKey, type EventKeyMap } from \"@zag-js/dom-event\"\nimport { dataAttr, isComposingEvent, isSafari, isSelfTarget } from \"@zag-js/dom-query\"\nimport type { NormalizeProps, PropTypes } from \"@zag-js/types\"\nimport { parts } from \"./tabs.anatomy\"\nimport { dom } from \"./tabs.dom\"\nimport type { MachineApi, Send, State, TriggerProps, TriggerState } from \"./tabs.types\"\n\nexport function connect<T extends PropTypes>(state: State, send: Send, normalize: NormalizeProps<T>): MachineApi<T> {\n const translations = state.context.translations\n const focused = state.matches(\"focused\")\n\n const isVertical = state.context.orientation === \"vertical\"\n const isHorizontal = state.context.orientation === \"horizontal\"\n const composite = state.context.composite\n const indicator = state.context.indicatorState\n\n function getTriggerState(props: TriggerProps): TriggerState {\n return {\n selected: state.context.value === props.value,\n focused: state.context.focusedValue === props.value,\n disabled: !!props.disabled,\n }\n }\n\n return {\n value: state.context.value,\n focusedValue: state.context.focusedValue,\n setValue(value) {\n send({ type: \"SET_VALUE\", value })\n },\n clearValue() {\n send({ type: \"CLEAR_VALUE\" })\n },\n setIndicatorRect(value) {\n const id = dom.getTriggerId(state.context, value)\n send({ type: \"SET_INDICATOR_RECT\", id })\n },\n syncTabIndex() {\n send(\"SYNC_TAB_INDEX\")\n },\n selectNext(fromValue) {\n send({ type: \"TAB_FOCUS\", value: fromValue, src: \"selectNext\" })\n send({ type: \"ARROW_NEXT\", src: \"selectNext\" })\n },\n selectPrev(fromValue) {\n send({ type: \"TAB_FOCUS\", value: fromValue, src: \"selectPrev\" })\n send({ type: \"ARROW_PREV\", src: \"selectPrev\" })\n },\n focus() {\n dom.getSelectedTriggerEl(state.context)?.focus()\n },\n\n getRootProps() {\n return normalize.element({\n ...parts.root.attrs,\n id: dom.getRootId(state.context),\n \"data-orientation\": state.context.orientation,\n \"data-focus\": dataAttr(focused),\n dir: state.context.dir,\n })\n },\n\n getListProps() {\n return normalize.element({\n ...parts.list.attrs,\n id: dom.getListId(state.context),\n role: \"tablist\",\n dir: state.context.dir,\n \"data-focus\": dataAttr(focused),\n \"aria-orientation\": state.context.orientation,\n \"data-orientation\": state.context.orientation,\n \"aria-label\": translations?.listLabel,\n onKeyDown(event) {\n if (event.defaultPrevented) return\n\n if (!isSelfTarget(event)) return\n if (isComposingEvent(event)) return\n\n const keyMap: EventKeyMap = {\n ArrowDown() {\n if (isHorizontal) return\n send({ type: \"ARROW_NEXT\", key: \"ArrowDown\" })\n },\n ArrowUp() {\n if (isHorizontal) return\n send({ type: \"ARROW_PREV\", key: \"ArrowUp\" })\n },\n ArrowLeft() {\n if (isVertical) return\n send({ type: \"ARROW_PREV\", key: \"ArrowLeft\" })\n },\n ArrowRight() {\n if (isVertical) return\n send({ type: \"ARROW_NEXT\", key: \"ArrowRight\" })\n },\n Home() {\n send(\"HOME\")\n },\n End() {\n send(\"END\")\n },\n Enter() {\n send({ type: \"ENTER\" })\n },\n }\n\n let key = getEventKey(event, state.context)\n const exec = keyMap[key]\n\n if (exec) {\n event.preventDefault()\n exec(event)\n }\n },\n })\n },\n\n getTriggerState,\n\n getTriggerProps(props) {\n const { value, disabled } = props\n const triggerState = getTriggerState(props)\n\n return normalize.button({\n ...parts.trigger.attrs,\n role: \"tab\",\n type: \"button\",\n disabled,\n dir: state.context.dir,\n \"data-orientation\": state.context.orientation,\n \"data-disabled\": dataAttr(disabled),\n \"aria-disabled\": disabled,\n \"data-value\": value,\n \"aria-selected\": triggerState.selected,\n \"data-selected\": dataAttr(triggerState.selected),\n \"data-focus\": dataAttr(triggerState.focused),\n \"aria-controls\": triggerState.selected ? dom.getContentId(state.context, value) : undefined,\n \"data-ownedby\": dom.getListId(state.context),\n \"data-ssr\": dataAttr(state.context.ssr),\n id: dom.getTriggerId(state.context, value),\n tabIndex: triggerState.selected && composite ? 0 : -1,\n onFocus() {\n send({ type: \"TAB_FOCUS\", value })\n },\n onBlur(event) {\n const target = event.relatedTarget as HTMLElement | null\n if (target?.getAttribute(\"role\") !== \"tab\") {\n send({ type: \"TAB_BLUR\" })\n }\n },\n onClick(event) {\n if (event.defaultPrevented) return\n if (disabled) return\n if (isSafari()) {\n event.currentTarget.focus()\n }\n send({ type: \"TAB_CLICK\", value })\n },\n })\n },\n\n getContentProps(props) {\n const { value } = props\n const selected = state.context.value === value\n return normalize.element({\n ...parts.content.attrs,\n dir: state.context.dir,\n id: dom.getContentId(state.context, value),\n tabIndex: composite ? 0 : -1,\n \"aria-labelledby\": dom.getTriggerId(state.context, value),\n role: \"tabpanel\",\n \"data-ownedby\": dom.getListId(state.context),\n \"data-selected\": dataAttr(selected),\n \"data-orientation\": state.context.orientation,\n hidden: !selected,\n })\n },\n\n getIndicatorProps() {\n return normalize.element({\n id: dom.getIndicatorId(state.context),\n ...parts.indicator.attrs,\n dir: state.context.dir,\n \"data-orientation\": state.context.orientation,\n style: {\n \"--transition-property\": \"left, right, top, bottom, width, height\",\n \"--left\": indicator.rect?.left,\n \"--top\": indicator.rect?.top,\n \"--width\": indicator.rect?.width,\n \"--height\": indicator.rect?.height,\n position: \"absolute\",\n willChange: \"var(--transition-property)\",\n transitionProperty: \"var(--transition-property)\",\n transitionDuration: indicator.transition ? \"var(--transition-duration, 150ms)\" : \"0ms\",\n transitionTimingFunction: \"var(--transition-timing-function)\",\n [isHorizontal ? \"left\" : \"top\"]: isHorizontal ? \"var(--left)\" : \"var(--top)\",\n },\n })\n },\n }\n}\n","import { createScope, itemById, nextById, prevById, queryAll } from \"@zag-js/dom-query\"\nimport { first, last } from \"@zag-js/utils\"\nimport type { MachineContext as Ctx } from \"./tabs.types\"\n\nexport const dom = createScope({\n getRootId: (ctx: Ctx) => ctx.ids?.root ?? `tabs:${ctx.id}`,\n getListId: (ctx: Ctx) => ctx.ids?.list ?? `tabs:${ctx.id}:list`,\n getContentId: (ctx: Ctx, id: string) => ctx.ids?.content ?? `tabs:${ctx.id}:content-${id}`,\n getTriggerId: (ctx: Ctx, id: string) => ctx.ids?.trigger ?? `tabs:${ctx.id}:trigger-${id}`,\n getIndicatorId: (ctx: Ctx) => ctx.ids?.indicator ?? `tabs:${ctx.id}:indicator`,\n\n getListEl: (ctx: Ctx) => dom.getById(ctx, dom.getListId(ctx)),\n getContentEl: (ctx: Ctx, id: string) => dom.getById(ctx, dom.getContentId(ctx, id)),\n getTriggerEl: (ctx: Ctx, id: string) => dom.getById(ctx, dom.getTriggerId(ctx, id)),\n getIndicatorEl: (ctx: Ctx) => dom.getById(ctx, dom.getIndicatorId(ctx)),\n\n getElements: (ctx: Ctx) => {\n const ownerId = CSS.escape(dom.getListId(ctx))\n const selector = `[role=tab][data-ownedby='${ownerId}']:not([disabled])`\n return queryAll(dom.getListEl(ctx), selector)\n },\n\n getFirstTriggerEl: (ctx: Ctx) => first(dom.getElements(ctx)),\n getLastTriggerEl: (ctx: Ctx) => last(dom.getElements(ctx)),\n getNextTriggerEl: (ctx: Ctx, id: string) => nextById(dom.getElements(ctx), dom.getTriggerId(ctx, id), ctx.loopFocus),\n getPrevTriggerEl: (ctx: Ctx, id: string) => prevById(dom.getElements(ctx), dom.getTriggerId(ctx, id), ctx.loopFocus),\n getSelectedContentEl: (ctx: Ctx) => {\n if (!ctx.value) return\n return dom.getContentEl(ctx, ctx.value)\n },\n getSelectedTriggerEl: (ctx: Ctx) => {\n if (!ctx.value) return\n return dom.getTriggerEl(ctx, ctx.value)\n },\n\n getOffsetRect: (el: HTMLElement | undefined) => {\n return {\n left: el?.offsetLeft ?? 0,\n top: el?.offsetTop ?? 0,\n width: el?.offsetWidth ?? 0,\n height: el?.offsetHeight ?? 0,\n }\n },\n\n getRectById: (ctx: Ctx, id: string) => {\n const tab = itemById(dom.getElements(ctx), dom.getTriggerId(ctx, id))\n return dom.resolveRect(dom.getOffsetRect(tab))\n },\n\n resolveRect: (rect: Record<\"width\" | \"height\" | \"left\" | \"top\", number>) => ({\n width: `${rect.width}px`,\n height: `${rect.height}px`,\n left: `${rect.left}px`,\n top: `${rect.top}px`,\n }),\n})\n","import { createMachine, guards } from \"@zag-js/core\"\nimport { clickIfLink } from \"@zag-js/dom-event\"\nimport { nextTick, raf, getFocusables } from \"@zag-js/dom-query\"\nimport { trackElementRect } from \"@zag-js/element-rect\"\nimport { compact, isEqual } from \"@zag-js/utils\"\nimport { dom } from \"./tabs.dom\"\nimport type { MachineContext, MachineState, UserDefinedContext } from \"./tabs.types\"\n\nconst { not } = guards\n\nexport function machine(userContext: UserDefinedContext) {\n const ctx = compact(userContext)\n return createMachine<MachineContext, MachineState>(\n {\n initial: \"idle\",\n\n context: {\n dir: \"ltr\",\n orientation: \"horizontal\",\n activationMode: \"automatic\",\n value: null,\n loopFocus: true,\n composite: true,\n ...ctx,\n focusedValue: ctx.value ?? null,\n ssr: true,\n indicatorState: {\n rendered: false,\n transition: false,\n rect: { left: \"0px\", top: \"0px\", width: \"0px\", height: \"0px\" },\n },\n },\n\n watch: {\n value: [\"allowIndicatorTransition\", \"syncIndicatorRect\", \"syncTabIndex\", \"clickIfLink\"],\n dir: [\"syncIndicatorRect\"],\n orientation: [\"syncIndicatorRect\"],\n },\n\n on: {\n SET_VALUE: {\n actions: \"setValue\",\n },\n CLEAR_VALUE: {\n actions: \"clearValue\",\n },\n SET_INDICATOR_RECT: {\n actions: \"setIndicatorRect\",\n },\n SYNC_TAB_INDEX: {\n actions: \"syncTabIndex\",\n },\n },\n\n created: [\"syncFocusedValue\"],\n\n entry: [\"checkRenderedElements\", \"syncIndicatorRect\", \"syncTabIndex\", \"syncSsr\"],\n\n exit: [\"cleanupObserver\"],\n\n states: {\n idle: {\n on: {\n TAB_FOCUS: {\n target: \"focused\",\n actions: \"setFocusedValue\",\n },\n TAB_CLICK: {\n target: \"focused\",\n actions: [\"setFocusedValue\", \"setValue\"],\n },\n },\n },\n focused: {\n on: {\n TAB_CLICK: {\n target: \"focused\",\n actions: [\"setFocusedValue\", \"setValue\"],\n },\n ARROW_PREV: [\n {\n guard: \"selectOnFocus\",\n actions: [\"focusPrevTab\", \"selectFocusedTab\"],\n },\n {\n actions: \"focusPrevTab\",\n },\n ],\n ARROW_NEXT: [\n {\n guard: \"selectOnFocus\",\n actions: [\"focusNextTab\", \"selectFocusedTab\"],\n },\n {\n actions: \"focusNextTab\",\n },\n ],\n HOME: [\n {\n guard: \"selectOnFocus\",\n actions: [\"focusFirstTab\", \"selectFocusedTab\"],\n },\n {\n actions: \"focusFirstTab\",\n },\n ],\n END: [\n {\n guard: \"selectOnFocus\",\n actions: [\"focusLastTab\", \"selectFocusedTab\"],\n },\n {\n actions: \"focusLastTab\",\n },\n ],\n ENTER: {\n guard: not(\"selectOnFocus\"),\n actions: \"selectFocusedTab\",\n },\n TAB_FOCUS: {\n actions: [\"setFocusedValue\"],\n },\n TAB_BLUR: {\n target: \"idle\",\n actions: \"clearFocusedValue\",\n },\n },\n },\n },\n },\n {\n guards: {\n selectOnFocus: (ctx) => ctx.activationMode === \"automatic\",\n },\n\n actions: {\n syncFocusedValue(ctx) {\n if (ctx.value != null && ctx.focusedValue == null) {\n ctx.focusedValue = ctx.value\n }\n },\n selectFocusedTab(ctx) {\n raf(() => {\n set.value(ctx, ctx.focusedValue)\n })\n },\n setFocusedValue(ctx, evt) {\n if (evt.value == null) return\n set.focusedValue(ctx, evt.value)\n },\n clearFocusedValue(ctx) {\n set.focusedValue(ctx, null)\n },\n setValue(ctx, evt) {\n set.value(ctx, evt.value)\n },\n clearValue(ctx) {\n set.value(ctx, null)\n },\n focusFirstTab(ctx) {\n raf(() => {\n dom.getFirstTriggerEl(ctx)?.focus()\n })\n },\n focusLastTab(ctx) {\n raf(() => {\n dom.getLastTriggerEl(ctx)?.focus()\n })\n },\n focusNextTab(ctx) {\n if (!ctx.focusedValue) return\n const triggerEl = dom.getNextTriggerEl(ctx, ctx.focusedValue)\n raf(() => {\n if (ctx.composite) {\n triggerEl?.focus()\n } else if (triggerEl?.dataset.value != null) {\n set.focusedValue(ctx, triggerEl.dataset.value)\n }\n })\n },\n focusPrevTab(ctx) {\n if (!ctx.focusedValue) return\n const triggerEl = dom.getPrevTriggerEl(ctx, ctx.focusedValue)\n raf(() => {\n if (ctx.composite) {\n triggerEl?.focus()\n } else if (triggerEl?.dataset.value != null) {\n set.focusedValue(ctx, triggerEl.dataset.value)\n }\n })\n },\n checkRenderedElements(ctx) {\n ctx.indicatorState.rendered = !!dom.getIndicatorEl(ctx)\n },\n syncTabIndex(ctx) {\n raf(() => {\n const contentEl = dom.getSelectedContentEl(ctx)\n if (!contentEl) return\n const focusables = getFocusables(contentEl)\n if (focusables.length > 0) {\n contentEl.removeAttribute(\"tabindex\")\n } else {\n contentEl.setAttribute(\"tabindex\", \"0\")\n }\n })\n },\n cleanupObserver(ctx) {\n ctx.indicatorCleanup?.()\n },\n allowIndicatorTransition(ctx) {\n ctx.indicatorState.transition = true\n },\n setIndicatorRect(ctx, evt) {\n const value = evt.id ?? ctx.value\n if (!ctx.indicatorState.rendered || !value) return\n\n const triggerEl = dom.getTriggerEl(ctx, value)\n if (!triggerEl) return\n\n ctx.indicatorState.rect = dom.getRectById(ctx, value)\n nextTick(() => {\n ctx.indicatorState.transition = false\n })\n },\n syncSsr(ctx) {\n ctx.ssr = false\n },\n syncIndicatorRect(ctx) {\n ctx.indicatorCleanup?.()\n\n const value = ctx.value\n if (!ctx.indicatorState.rendered || !value) return\n\n const triggerEl = dom.getSelectedTriggerEl(ctx)\n if (!triggerEl) return\n\n ctx.indicatorCleanup = trackElementRect(triggerEl, {\n getRect(el) {\n return dom.getOffsetRect(el)\n },\n onChange(rect) {\n ctx.indicatorState.rect = dom.resolveRect(rect)\n nextTick(() => {\n ctx.indicatorState.transition = false\n })\n },\n })\n },\n clickIfLink(ctx) {\n clickIfLink(dom.getSelectedTriggerEl(ctx))\n },\n },\n },\n )\n}\n\nconst invoke = {\n change: (ctx: MachineContext) => {\n if (ctx.value == null) return\n ctx.onValueChange?.({ value: ctx.value })\n },\n focusChange: (ctx: MachineContext) => {\n if (ctx.focusedValue == null) return\n ctx.onFocusChange?.({ focusedValue: ctx.focusedValue })\n },\n}\n\nconst set = {\n value: (ctx: MachineContext, value: string | null) => {\n if (isEqual(value, ctx.value)) return\n ctx.value = value\n invoke.change(ctx)\n },\n focusedValue: (ctx: MachineContext, value: string | null) => {\n if (isEqual(value, ctx.focusedValue)) return\n ctx.focusedValue = value\n invoke.focusChange(ctx)\n },\n}\n","import { createProps } from \"@zag-js/types\"\nimport { createSplitProps } from \"@zag-js/utils\"\nimport type { ContentProps, TriggerProps, UserDefinedContext } from \"./tabs.types\"\n\nexport const props = createProps<UserDefinedContext>()([\n \"activationMode\",\n \"composite\",\n \"dir\",\n \"getRootNode\",\n \"id\",\n \"ids\",\n \"loopFocus\",\n \"onFocusChange\",\n \"onValueChange\",\n \"orientation\",\n \"translations\",\n \"value\",\n])\n\nexport const splitProps = createSplitProps<Partial<UserDefinedContext>>(props)\n\nexport const triggerProps = createProps<TriggerProps>()([\"disabled\", \"value\"])\nexport const splitTriggerProps = createSplitProps<TriggerProps>(triggerProps)\n\nexport const contentProps = createProps<ContentProps>()([\"value\"])\nexport const splitContentProps = createSplitProps<ContentProps>(contentProps)\n"],"mappings":";AAAA,SAAS,qBAAqB;AAEvB,IAAM,UAAU,cAAc,MAAM,EAAE,MAAM,QAAQ,QAAQ,WAAW,WAAW,WAAW;AAC7F,IAAM,QAAQ,QAAQ,MAAM;;;ACHnC,SAAS,mBAAqC;AAC9C,SAAS,UAAU,kBAAkB,UAAU,oBAAoB;;;ACDnE,SAAS,aAAa,UAAU,UAAU,U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AI,MAAM,CAAC;AAAA,EAC1C;AAAA,EACA,aAAa,CAAC,QAAwB;AACpC,QAAI,IAAI,gBAAgB,KAAM;AAC9B,QAAI,gBAAgB,EAAE,cAAc,IAAI,aAAa,CAAC;AAAA,EACxD;AACF;AAEA,IAAM,MAAM;AAAA,EACV,OAAO,CAAC,KAAqB,UAAyB;AACpD,QAAI,QAAQ,OAAO,IAAI,KAAK,EAAG;AAC/B,QAAI,QAAQ;AACZ,WAAO,OAAO,GAAG;AAAA,EACnB;AAAA,EACA,cAAc,CAAC,KAAqB,UAAyB;AAC3D,QAAI,QAAQ,OAAO,IAAI,YAAY,EAAG;AACtC,QAAI,eAAe;AACnB,WAAO,YAAY,GAAG;AAAA,EACxB;AACF;;;ACtRA,SAAS,mBAAmB;AAC5B,SAAS,wBAAwB;AAG1B,IAAM,QAAQ,YAAgC,EAAE;AAAA,EACrD;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,CAAC;AAEM,IAAM,aAAa,iBAA8C,KAAK;AAEtE,IAAM,eAAe,YAA0B,EAAE,CAAC,YAAY,OAAO,CAAC;AACtE,IAAM,oBAAoB,iBAA+B,YAAY;AAErE,IAAM,eAAe,YAA0B,EAAE,CAAC,OAAO,CAAC;AAC1D,IAAM,oBAAoB,iBAA+B,YAAY;","names":["props","ctx"]}
package/src/index.ts DELETED
@@ -1,15 +0,0 @@
1
- export { anatomy } from "./tabs.anatomy"
2
- export { connect } from "./tabs.connect"
3
- export { machine } from "./tabs.machine"
4
- export * from "./tabs.props"
5
- export type {
6
- MachineApi as Api,
7
- ContentProps,
8
- UserDefinedContext as Context,
9
- ElementIds,
10
- FocusChangeDetails,
11
- IntlTranslations,
12
- Service,
13
- TriggerProps,
14
- ValueChangeDetails,
15
- } from "./tabs.types"
@@ -1,4 +0,0 @@
1
- import { createAnatomy } from "@zag-js/anatomy"
2
-
3
- export const anatomy = createAnatomy("tabs").parts("root", "list", "trigger", "content", "indicator")
4
- export const parts = anatomy.build()
@@ -1,201 +0,0 @@
1
- import { getEventKey, type EventKeyMap } from "@zag-js/dom-event"
2
- import { dataAttr, isComposingEvent, isSafari, isSelfTarget } from "@zag-js/dom-query"
3
- import type { NormalizeProps, PropTypes } from "@zag-js/types"
4
- import { parts } from "./tabs.anatomy"
5
- import { dom } from "./tabs.dom"
6
- import type { MachineApi, Send, State, TriggerProps, TriggerState } from "./tabs.types"
7
-
8
- export function connect<T extends PropTypes>(state: State, send: Send, normalize: NormalizeProps<T>): MachineApi<T> {
9
- const translations = state.context.translations
10
- const focused = state.matches("focused")
11
-
12
- const isVertical = state.context.orientation === "vertical"
13
- const isHorizontal = state.context.orientation === "horizontal"
14
- const composite = state.context.composite
15
- const indicator = state.context.indicatorState
16
-
17
- function getTriggerState(props: TriggerProps): TriggerState {
18
- return {
19
- selected: state.context.value === props.value,
20
- focused: state.context.focusedValue === props.value,
21
- disabled: !!props.disabled,
22
- }
23
- }
24
-
25
- return {
26
- value: state.context.value,
27
- focusedValue: state.context.focusedValue,
28
- setValue(value) {
29
- send({ type: "SET_VALUE", value })
30
- },
31
- clearValue() {
32
- send({ type: "CLEAR_VALUE" })
33
- },
34
- setIndicatorRect(value) {
35
- const id = dom.getTriggerId(state.context, value)
36
- send({ type: "SET_INDICATOR_RECT", id })
37
- },
38
- syncTabIndex() {
39
- send("SYNC_TAB_INDEX")
40
- },
41
- selectNext(fromValue) {
42
- send({ type: "TAB_FOCUS", value: fromValue, src: "selectNext" })
43
- send({ type: "ARROW_NEXT", src: "selectNext" })
44
- },
45
- selectPrev(fromValue) {
46
- send({ type: "TAB_FOCUS", value: fromValue, src: "selectPrev" })
47
- send({ type: "ARROW_PREV", src: "selectPrev" })
48
- },
49
- focus() {
50
- dom.getSelectedTriggerEl(state.context)?.focus()
51
- },
52
-
53
- getRootProps() {
54
- return normalize.element({
55
- ...parts.root.attrs,
56
- id: dom.getRootId(state.context),
57
- "data-orientation": state.context.orientation,
58
- "data-focus": dataAttr(focused),
59
- dir: state.context.dir,
60
- })
61
- },
62
-
63
- getListProps() {
64
- return normalize.element({
65
- ...parts.list.attrs,
66
- id: dom.getListId(state.context),
67
- role: "tablist",
68
- dir: state.context.dir,
69
- "data-focus": dataAttr(focused),
70
- "aria-orientation": state.context.orientation,
71
- "data-orientation": state.context.orientation,
72
- "aria-label": translations?.listLabel,
73
- onKeyDown(event) {
74
- if (event.defaultPrevented) return
75
-
76
- if (!isSelfTarget(event)) return
77
- if (isComposingEvent(event)) return
78
-
79
- const keyMap: EventKeyMap = {
80
- ArrowDown() {
81
- if (isHorizontal) return
82
- send({ type: "ARROW_NEXT", key: "ArrowDown" })
83
- },
84
- ArrowUp() {
85
- if (isHorizontal) return
86
- send({ type: "ARROW_PREV", key: "ArrowUp" })
87
- },
88
- ArrowLeft() {
89
- if (isVertical) return
90
- send({ type: "ARROW_PREV", key: "ArrowLeft" })
91
- },
92
- ArrowRight() {
93
- if (isVertical) return
94
- send({ type: "ARROW_NEXT", key: "ArrowRight" })
95
- },
96
- Home() {
97
- send("HOME")
98
- },
99
- End() {
100
- send("END")
101
- },
102
- Enter() {
103
- send({ type: "ENTER" })
104
- },
105
- }
106
-
107
- let key = getEventKey(event, state.context)
108
- const exec = keyMap[key]
109
-
110
- if (exec) {
111
- event.preventDefault()
112
- exec(event)
113
- }
114
- },
115
- })
116
- },
117
-
118
- getTriggerState,
119
-
120
- getTriggerProps(props) {
121
- const { value, disabled } = props
122
- const triggerState = getTriggerState(props)
123
-
124
- return normalize.button({
125
- ...parts.trigger.attrs,
126
- role: "tab",
127
- type: "button",
128
- disabled,
129
- dir: state.context.dir,
130
- "data-orientation": state.context.orientation,
131
- "data-disabled": dataAttr(disabled),
132
- "aria-disabled": disabled,
133
- "data-value": value,
134
- "aria-selected": triggerState.selected,
135
- "data-selected": dataAttr(triggerState.selected),
136
- "data-focus": dataAttr(triggerState.focused),
137
- "aria-controls": triggerState.selected ? dom.getContentId(state.context, value) : undefined,
138
- "data-ownedby": dom.getListId(state.context),
139
- "data-ssr": dataAttr(state.context.ssr),
140
- id: dom.getTriggerId(state.context, value),
141
- tabIndex: triggerState.selected && composite ? 0 : -1,
142
- onFocus() {
143
- send({ type: "TAB_FOCUS", value })
144
- },
145
- onBlur(event) {
146
- const target = event.relatedTarget as HTMLElement | null
147
- if (target?.getAttribute("role") !== "tab") {
148
- send({ type: "TAB_BLUR" })
149
- }
150
- },
151
- onClick(event) {
152
- if (event.defaultPrevented) return
153
- if (disabled) return
154
- if (isSafari()) {
155
- event.currentTarget.focus()
156
- }
157
- send({ type: "TAB_CLICK", value })
158
- },
159
- })
160
- },
161
-
162
- getContentProps(props) {
163
- const { value } = props
164
- const selected = state.context.value === value
165
- return normalize.element({
166
- ...parts.content.attrs,
167
- dir: state.context.dir,
168
- id: dom.getContentId(state.context, value),
169
- tabIndex: composite ? 0 : -1,
170
- "aria-labelledby": dom.getTriggerId(state.context, value),
171
- role: "tabpanel",
172
- "data-ownedby": dom.getListId(state.context),
173
- "data-selected": dataAttr(selected),
174
- "data-orientation": state.context.orientation,
175
- hidden: !selected,
176
- })
177
- },
178
-
179
- getIndicatorProps() {
180
- return normalize.element({
181
- id: dom.getIndicatorId(state.context),
182
- ...parts.indicator.attrs,
183
- dir: state.context.dir,
184
- "data-orientation": state.context.orientation,
185
- style: {
186
- "--transition-property": "left, right, top, bottom, width, height",
187
- "--left": indicator.rect?.left,
188
- "--top": indicator.rect?.top,
189
- "--width": indicator.rect?.width,
190
- "--height": indicator.rect?.height,
191
- position: "absolute",
192
- willChange: "var(--transition-property)",
193
- transitionProperty: "var(--transition-property)",
194
- transitionDuration: indicator.transition ? "var(--transition-duration, 150ms)" : "0ms",
195
- transitionTimingFunction: "var(--transition-timing-function)",
196
- [isHorizontal ? "left" : "top"]: isHorizontal ? "var(--left)" : "var(--top)",
197
- },
198
- })
199
- },
200
- }
201
- }
package/src/tabs.dom.ts DELETED
@@ -1,56 +0,0 @@
1
- import { createScope, itemById, nextById, prevById, queryAll } from "@zag-js/dom-query"
2
- import { first, last } from "@zag-js/utils"
3
- import type { MachineContext as Ctx } from "./tabs.types"
4
-
5
- export const dom = createScope({
6
- getRootId: (ctx: Ctx) => ctx.ids?.root ?? `tabs:${ctx.id}`,
7
- getListId: (ctx: Ctx) => ctx.ids?.list ?? `tabs:${ctx.id}:list`,
8
- getContentId: (ctx: Ctx, id: string) => ctx.ids?.content ?? `tabs:${ctx.id}:content-${id}`,
9
- getTriggerId: (ctx: Ctx, id: string) => ctx.ids?.trigger ?? `tabs:${ctx.id}:trigger-${id}`,
10
- getIndicatorId: (ctx: Ctx) => ctx.ids?.indicator ?? `tabs:${ctx.id}:indicator`,
11
-
12
- getListEl: (ctx: Ctx) => dom.getById(ctx, dom.getListId(ctx)),
13
- getContentEl: (ctx: Ctx, id: string) => dom.getById(ctx, dom.getContentId(ctx, id)),
14
- getTriggerEl: (ctx: Ctx, id: string) => dom.getById(ctx, dom.getTriggerId(ctx, id)),
15
- getIndicatorEl: (ctx: Ctx) => dom.getById(ctx, dom.getIndicatorId(ctx)),
16
-
17
- getElements: (ctx: Ctx) => {
18
- const ownerId = CSS.escape(dom.getListId(ctx))
19
- const selector = `[role=tab][data-ownedby='${ownerId}']:not([disabled])`
20
- return queryAll(dom.getListEl(ctx), selector)
21
- },
22
-
23
- getFirstTriggerEl: (ctx: Ctx) => first(dom.getElements(ctx)),
24
- getLastTriggerEl: (ctx: Ctx) => last(dom.getElements(ctx)),
25
- getNextTriggerEl: (ctx: Ctx, id: string) => nextById(dom.getElements(ctx), dom.getTriggerId(ctx, id), ctx.loopFocus),
26
- getPrevTriggerEl: (ctx: Ctx, id: string) => prevById(dom.getElements(ctx), dom.getTriggerId(ctx, id), ctx.loopFocus),
27
- getSelectedContentEl: (ctx: Ctx) => {
28
- if (!ctx.value) return
29
- return dom.getContentEl(ctx, ctx.value)
30
- },
31
- getSelectedTriggerEl: (ctx: Ctx) => {
32
- if (!ctx.value) return
33
- return dom.getTriggerEl(ctx, ctx.value)
34
- },
35
-
36
- getOffsetRect: (el: HTMLElement | undefined) => {
37
- return {
38
- left: el?.offsetLeft ?? 0,
39
- top: el?.offsetTop ?? 0,
40
- width: el?.offsetWidth ?? 0,
41
- height: el?.offsetHeight ?? 0,
42
- }
43
- },
44
-
45
- getRectById: (ctx: Ctx, id: string) => {
46
- const tab = itemById(dom.getElements(ctx), dom.getTriggerId(ctx, id))
47
- return dom.resolveRect(dom.getOffsetRect(tab))
48
- },
49
-
50
- resolveRect: (rect: Record<"width" | "height" | "left" | "top", number>) => ({
51
- width: `${rect.width}px`,
52
- height: `${rect.height}px`,
53
- left: `${rect.left}px`,
54
- top: `${rect.top}px`,
55
- }),
56
- })
@@ -1,279 +0,0 @@
1
- import { createMachine, guards } from "@zag-js/core"
2
- import { clickIfLink } from "@zag-js/dom-event"
3
- import { nextTick, raf, getFocusables } from "@zag-js/dom-query"
4
- import { trackElementRect } from "@zag-js/element-rect"
5
- import { compact, isEqual } from "@zag-js/utils"
6
- import { dom } from "./tabs.dom"
7
- import type { MachineContext, MachineState, UserDefinedContext } from "./tabs.types"
8
-
9
- const { not } = guards
10
-
11
- export function machine(userContext: UserDefinedContext) {
12
- const ctx = compact(userContext)
13
- return createMachine<MachineContext, MachineState>(
14
- {
15
- initial: "idle",
16
-
17
- context: {
18
- dir: "ltr",
19
- orientation: "horizontal",
20
- activationMode: "automatic",
21
- value: null,
22
- loopFocus: true,
23
- composite: true,
24
- ...ctx,
25
- focusedValue: ctx.value ?? null,
26
- ssr: true,
27
- indicatorState: {
28
- rendered: false,
29
- transition: false,
30
- rect: { left: "0px", top: "0px", width: "0px", height: "0px" },
31
- },
32
- },
33
-
34
- watch: {
35
- value: ["allowIndicatorTransition", "syncIndicatorRect", "syncTabIndex", "clickIfLink"],
36
- dir: ["syncIndicatorRect"],
37
- orientation: ["syncIndicatorRect"],
38
- },
39
-
40
- on: {
41
- SET_VALUE: {
42
- actions: "setValue",
43
- },
44
- CLEAR_VALUE: {
45
- actions: "clearValue",
46
- },
47
- SET_INDICATOR_RECT: {
48
- actions: "setIndicatorRect",
49
- },
50
- SYNC_TAB_INDEX: {
51
- actions: "syncTabIndex",
52
- },
53
- },
54
-
55
- created: ["syncFocusedValue"],
56
-
57
- entry: ["checkRenderedElements", "syncIndicatorRect", "syncTabIndex", "syncSsr"],
58
-
59
- exit: ["cleanupObserver"],
60
-
61
- states: {
62
- idle: {
63
- on: {
64
- TAB_FOCUS: {
65
- target: "focused",
66
- actions: "setFocusedValue",
67
- },
68
- TAB_CLICK: {
69
- target: "focused",
70
- actions: ["setFocusedValue", "setValue"],
71
- },
72
- },
73
- },
74
- focused: {
75
- on: {
76
- TAB_CLICK: {
77
- target: "focused",
78
- actions: ["setFocusedValue", "setValue"],
79
- },
80
- ARROW_PREV: [
81
- {
82
- guard: "selectOnFocus",
83
- actions: ["focusPrevTab", "selectFocusedTab"],
84
- },
85
- {
86
- actions: "focusPrevTab",
87
- },
88
- ],
89
- ARROW_NEXT: [
90
- {
91
- guard: "selectOnFocus",
92
- actions: ["focusNextTab", "selectFocusedTab"],
93
- },
94
- {
95
- actions: "focusNextTab",
96
- },
97
- ],
98
- HOME: [
99
- {
100
- guard: "selectOnFocus",
101
- actions: ["focusFirstTab", "selectFocusedTab"],
102
- },
103
- {
104
- actions: "focusFirstTab",
105
- },
106
- ],
107
- END: [
108
- {
109
- guard: "selectOnFocus",
110
- actions: ["focusLastTab", "selectFocusedTab"],
111
- },
112
- {
113
- actions: "focusLastTab",
114
- },
115
- ],
116
- ENTER: {
117
- guard: not("selectOnFocus"),
118
- actions: "selectFocusedTab",
119
- },
120
- TAB_FOCUS: {
121
- actions: ["setFocusedValue"],
122
- },
123
- TAB_BLUR: {
124
- target: "idle",
125
- actions: "clearFocusedValue",
126
- },
127
- },
128
- },
129
- },
130
- },
131
- {
132
- guards: {
133
- selectOnFocus: (ctx) => ctx.activationMode === "automatic",
134
- },
135
-
136
- actions: {
137
- syncFocusedValue(ctx) {
138
- if (ctx.value != null && ctx.focusedValue == null) {
139
- ctx.focusedValue = ctx.value
140
- }
141
- },
142
- selectFocusedTab(ctx) {
143
- raf(() => {
144
- set.value(ctx, ctx.focusedValue)
145
- })
146
- },
147
- setFocusedValue(ctx, evt) {
148
- if (evt.value == null) return
149
- set.focusedValue(ctx, evt.value)
150
- },
151
- clearFocusedValue(ctx) {
152
- set.focusedValue(ctx, null)
153
- },
154
- setValue(ctx, evt) {
155
- set.value(ctx, evt.value)
156
- },
157
- clearValue(ctx) {
158
- set.value(ctx, null)
159
- },
160
- focusFirstTab(ctx) {
161
- raf(() => {
162
- dom.getFirstTriggerEl(ctx)?.focus()
163
- })
164
- },
165
- focusLastTab(ctx) {
166
- raf(() => {
167
- dom.getLastTriggerEl(ctx)?.focus()
168
- })
169
- },
170
- focusNextTab(ctx) {
171
- if (!ctx.focusedValue) return
172
- const triggerEl = dom.getNextTriggerEl(ctx, ctx.focusedValue)
173
- raf(() => {
174
- if (ctx.composite) {
175
- triggerEl?.focus()
176
- } else if (triggerEl?.dataset.value != null) {
177
- set.focusedValue(ctx, triggerEl.dataset.value)
178
- }
179
- })
180
- },
181
- focusPrevTab(ctx) {
182
- if (!ctx.focusedValue) return
183
- const triggerEl = dom.getPrevTriggerEl(ctx, ctx.focusedValue)
184
- raf(() => {
185
- if (ctx.composite) {
186
- triggerEl?.focus()
187
- } else if (triggerEl?.dataset.value != null) {
188
- set.focusedValue(ctx, triggerEl.dataset.value)
189
- }
190
- })
191
- },
192
- checkRenderedElements(ctx) {
193
- ctx.indicatorState.rendered = !!dom.getIndicatorEl(ctx)
194
- },
195
- syncTabIndex(ctx) {
196
- raf(() => {
197
- const contentEl = dom.getSelectedContentEl(ctx)
198
- if (!contentEl) return
199
- const focusables = getFocusables(contentEl)
200
- if (focusables.length > 0) {
201
- contentEl.removeAttribute("tabindex")
202
- } else {
203
- contentEl.setAttribute("tabindex", "0")
204
- }
205
- })
206
- },
207
- cleanupObserver(ctx) {
208
- ctx.indicatorCleanup?.()
209
- },
210
- allowIndicatorTransition(ctx) {
211
- ctx.indicatorState.transition = true
212
- },
213
- setIndicatorRect(ctx, evt) {
214
- const value = evt.id ?? ctx.value
215
- if (!ctx.indicatorState.rendered || !value) return
216
-
217
- const triggerEl = dom.getTriggerEl(ctx, value)
218
- if (!triggerEl) return
219
-
220
- ctx.indicatorState.rect = dom.getRectById(ctx, value)
221
- nextTick(() => {
222
- ctx.indicatorState.transition = false
223
- })
224
- },
225
- syncSsr(ctx) {
226
- ctx.ssr = false
227
- },
228
- syncIndicatorRect(ctx) {
229
- ctx.indicatorCleanup?.()
230
-
231
- const value = ctx.value
232
- if (!ctx.indicatorState.rendered || !value) return
233
-
234
- const triggerEl = dom.getSelectedTriggerEl(ctx)
235
- if (!triggerEl) return
236
-
237
- ctx.indicatorCleanup = trackElementRect(triggerEl, {
238
- getRect(el) {
239
- return dom.getOffsetRect(el)
240
- },
241
- onChange(rect) {
242
- ctx.indicatorState.rect = dom.resolveRect(rect)
243
- nextTick(() => {
244
- ctx.indicatorState.transition = false
245
- })
246
- },
247
- })
248
- },
249
- clickIfLink(ctx) {
250
- clickIfLink(dom.getSelectedTriggerEl(ctx))
251
- },
252
- },
253
- },
254
- )
255
- }
256
-
257
- const invoke = {
258
- change: (ctx: MachineContext) => {
259
- if (ctx.value == null) return
260
- ctx.onValueChange?.({ value: ctx.value })
261
- },
262
- focusChange: (ctx: MachineContext) => {
263
- if (ctx.focusedValue == null) return
264
- ctx.onFocusChange?.({ focusedValue: ctx.focusedValue })
265
- },
266
- }
267
-
268
- const set = {
269
- value: (ctx: MachineContext, value: string | null) => {
270
- if (isEqual(value, ctx.value)) return
271
- ctx.value = value
272
- invoke.change(ctx)
273
- },
274
- focusedValue: (ctx: MachineContext, value: string | null) => {
275
- if (isEqual(value, ctx.focusedValue)) return
276
- ctx.focusedValue = value
277
- invoke.focusChange(ctx)
278
- },
279
- }
package/src/tabs.props.ts DELETED
@@ -1,26 +0,0 @@
1
- import { createProps } from "@zag-js/types"
2
- import { createSplitProps } from "@zag-js/utils"
3
- import type { ContentProps, TriggerProps, UserDefinedContext } from "./tabs.types"
4
-
5
- export const props = createProps<UserDefinedContext>()([
6
- "activationMode",
7
- "composite",
8
- "dir",
9
- "getRootNode",
10
- "id",
11
- "ids",
12
- "loopFocus",
13
- "onFocusChange",
14
- "onValueChange",
15
- "orientation",
16
- "translations",
17
- "value",
18
- ])
19
-
20
- export const splitProps = createSplitProps<Partial<UserDefinedContext>>(props)
21
-
22
- export const triggerProps = createProps<TriggerProps>()(["disabled", "value"])
23
- export const splitTriggerProps = createSplitProps<TriggerProps>(triggerProps)
24
-
25
- export const contentProps = createProps<ContentProps>()(["value"])
26
- export const splitContentProps = createSplitProps<ContentProps>(contentProps)