@zag-js/tabs 1.47.0 → 1.48.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,302 +1,262 @@
1
- // src/tabs.machine.ts
2
- import { setup } from "@zag-js/core";
1
+ import { getContentEl, getElements, getFirstTriggerEl, getIndicatorEl, getLastTriggerEl, getListEl, getNextTriggerEl, getOffsetRect, getPrevTriggerEl, getRectByValue, getTriggerEl } from "./tabs.dom.mjs";
3
2
  import { AnimationFrame, getFocusables, isAnchorElement, raf, resizeObserverBorderBox } from "@zag-js/dom-query";
4
3
  import { callAll, isEqual } from "@zag-js/utils";
5
- import * as dom from "./tabs.dom.mjs";
6
- var { createMachine } = setup();
7
- var machine = createMachine({
8
- props({ props }) {
9
- return {
10
- dir: "ltr",
11
- orientation: "horizontal",
12
- activationMode: "automatic",
13
- loopFocus: true,
14
- composite: true,
15
- defaultValue: null,
16
- ...props
17
- };
18
- },
19
- initialState() {
20
- return "idle";
21
- },
22
- context({ prop, bindable }) {
23
- return {
24
- value: bindable(() => ({
25
- defaultValue: prop("defaultValue"),
26
- value: prop("value"),
27
- onChange(value) {
28
- prop("onValueChange")?.({ value });
29
- }
30
- })),
31
- focusedValue: bindable(() => ({
32
- defaultValue: prop("value") || prop("defaultValue"),
33
- sync: true,
34
- onChange(value) {
35
- prop("onFocusChange")?.({ focusedValue: value });
36
- }
37
- })),
38
- ssr: bindable(() => ({ defaultValue: true })),
39
- indicatorRect: bindable(() => ({
40
- defaultValue: null
41
- })),
42
- animateIndicator: bindable(() => ({
43
- defaultValue: false
44
- }))
45
- };
46
- },
47
- refs() {
48
- return {
49
- indicatorCleanup: null,
50
- prevValue: null,
51
- focusFrame: AnimationFrame.create()
52
- };
53
- },
54
- watch({ context, prop, track, action }) {
55
- track([() => context.get("value")], () => {
56
- action(["syncIndicatorAnimation", "syncIndicatorRect", "syncTabIndex"]);
57
- });
58
- track([() => prop("dir"), () => prop("orientation")], () => {
59
- action(["syncIndicatorRect"]);
60
- });
61
- },
62
- on: {
63
- SET_VALUE: {
64
- actions: ["setValue"]
65
- },
66
- CLEAR_VALUE: {
67
- actions: ["clearValue"]
68
- },
69
- SET_INDICATOR_RECT: {
70
- actions: ["setIndicatorRect"]
71
- },
72
- SYNC_TAB_INDEX: {
73
- actions: ["syncTabIndex"]
74
- },
75
- INDICATOR_TRANSITION_END: {
76
- actions: ["clearIndicatorAnimation"]
77
- }
78
- },
79
- entry: ["syncPrevValue", "syncIndicatorRect", "syncTabIndex", "syncSsr"],
80
- exit: ["cleanupObserver", "cancelFocusFrame"],
81
- states: {
82
- idle: {
83
- on: {
84
- TAB_FOCUS: {
85
- target: "focused",
86
- actions: ["setFocusedValue"]
87
- },
88
- TAB_CLICK: {
89
- target: "focused",
90
- actions: ["setFocusedValue", "setValue", "requestNavigation"]
91
- }
92
- }
93
- },
94
- focused: {
95
- exit: ["cancelFocusFrame"],
96
- on: {
97
- TAB_CLICK: {
98
- actions: ["setFocusedValue", "setValue", "requestNavigation"]
99
- },
100
- ARROW_PREV: [
101
- {
102
- guard: "selectOnFocus",
103
- actions: ["focusPrevTab", "selectFocusedTab"]
104
- },
105
- {
106
- actions: ["focusPrevTab"]
107
- }
108
- ],
109
- ARROW_NEXT: [
110
- {
111
- guard: "selectOnFocus",
112
- actions: ["focusNextTab", "selectFocusedTab"]
113
- },
114
- {
115
- actions: ["focusNextTab"]
116
- }
117
- ],
118
- HOME: [
119
- {
120
- guard: "selectOnFocus",
121
- actions: ["focusFirstTab", "selectFocusedTab"]
122
- },
123
- {
124
- actions: ["focusFirstTab"]
125
- }
126
- ],
127
- END: [
128
- {
129
- guard: "selectOnFocus",
130
- actions: ["focusLastTab", "selectFocusedTab"]
131
- },
132
- {
133
- actions: ["focusLastTab"]
134
- }
135
- ],
136
- TAB_FOCUS: {
137
- actions: ["setFocusedValue"]
138
- },
139
- TAB_BLUR: {
140
- target: "idle",
141
- actions: ["clearFocusedValue"]
142
- }
143
- }
144
- }
145
- },
146
- implementations: {
147
- guards: {
148
- selectOnFocus: ({ prop }) => prop("activationMode") === "automatic"
149
- },
150
- actions: {
151
- selectFocusedTab({ context, prop, event, scope }) {
152
- raf(() => {
153
- const focusedValue = context.get("focusedValue");
154
- if (!focusedValue) return;
155
- const nullable = prop("deselectable") && context.get("value") === focusedValue;
156
- const value = nullable ? null : focusedValue;
157
- if (context.get("value") === value) return;
158
- if (event.src === "keyboard") {
159
- const triggerEl = dom.getTriggerEl(scope, focusedValue);
160
- if (isAnchorElement(triggerEl) && prop("navigate") !== null) {
161
- triggerEl.click();
162
- return;
163
- }
164
- }
165
- context.set("value", value);
166
- });
167
- },
168
- setFocusedValue({ context, event, flush }) {
169
- if (event.value == null) return;
170
- flush(() => {
171
- context.set("focusedValue", event.value);
172
- });
173
- },
174
- clearFocusedValue({ context }) {
175
- context.set("focusedValue", null);
176
- },
177
- setValue({ context, event, prop }) {
178
- const nullable = prop("deselectable") && context.get("value") === context.get("focusedValue");
179
- context.set("value", nullable ? null : event.value);
180
- },
181
- clearValue({ context }) {
182
- context.set("value", null);
183
- },
184
- focusFirstTab({ scope, refs }) {
185
- refs.get("focusFrame").request(() => {
186
- dom.getFirstTriggerEl(scope)?.focus();
187
- });
188
- },
189
- focusLastTab({ scope, refs }) {
190
- refs.get("focusFrame").request(() => {
191
- dom.getLastTriggerEl(scope)?.focus();
192
- });
193
- },
194
- focusNextTab({ context, prop, scope, event, refs }) {
195
- const focusedValue = event.value ?? context.get("focusedValue");
196
- if (!focusedValue) return;
197
- const triggerEl = dom.getNextTriggerEl(scope, {
198
- value: focusedValue,
199
- loopFocus: prop("loopFocus")
200
- });
201
- refs.get("focusFrame").request(() => {
202
- if (prop("composite")) {
203
- triggerEl?.focus();
204
- } else if (triggerEl?.dataset.value != null) {
205
- context.set("focusedValue", triggerEl.dataset.value);
206
- }
207
- });
208
- },
209
- focusPrevTab({ context, prop, scope, event, refs }) {
210
- const focusedValue = event.value ?? context.get("focusedValue");
211
- if (!focusedValue) return;
212
- const triggerEl = dom.getPrevTriggerEl(scope, {
213
- value: focusedValue,
214
- loopFocus: prop("loopFocus")
215
- });
216
- refs.get("focusFrame").request(() => {
217
- if (prop("composite")) {
218
- triggerEl?.focus();
219
- } else if (triggerEl?.dataset.value != null) {
220
- context.set("focusedValue", triggerEl.dataset.value);
221
- }
222
- });
223
- },
224
- syncTabIndex({ context, scope }) {
225
- raf(() => {
226
- const value = context.get("value");
227
- if (!value) return;
228
- const contentEl = dom.getContentEl(scope, value);
229
- if (!contentEl) return;
230
- const focusables = getFocusables(contentEl);
231
- if (focusables.length > 0) {
232
- contentEl.removeAttribute("tabindex");
233
- } else {
234
- contentEl.setAttribute("tabindex", "0");
235
- }
236
- });
237
- },
238
- cancelFocusFrame({ refs }) {
239
- refs.get("focusFrame").cancel();
240
- },
241
- cleanupObserver({ refs }) {
242
- const cleanup = refs.get("indicatorCleanup");
243
- if (cleanup) cleanup();
244
- },
245
- setIndicatorRect({ context, event, scope }) {
246
- const value = event.id ?? context.get("value");
247
- const indicatorEl = dom.getIndicatorEl(scope);
248
- if (!indicatorEl) return;
249
- if (!value) return;
250
- const triggerEl = dom.getTriggerEl(scope, value);
251
- if (!triggerEl) return;
252
- context.set("indicatorRect", dom.getRectByValue(scope, value));
253
- },
254
- syncSsr({ context }) {
255
- context.set("ssr", false);
256
- },
257
- syncPrevValue({ context, refs }) {
258
- refs.set("prevValue", context.get("value"));
259
- },
260
- syncIndicatorAnimation({ context, refs }) {
261
- const prevValue = refs.get("prevValue");
262
- const nextValue = context.get("value");
263
- const animate = prevValue != null && nextValue != null && prevValue !== nextValue;
264
- context.set("animateIndicator", animate);
265
- refs.set("prevValue", nextValue);
266
- },
267
- clearIndicatorAnimation({ context }) {
268
- context.set("animateIndicator", false);
269
- },
270
- syncIndicatorRect({ context, refs, scope }) {
271
- const cleanup = refs.get("indicatorCleanup");
272
- if (cleanup) cleanup();
273
- const indicatorEl = dom.getIndicatorEl(scope);
274
- if (!indicatorEl) return;
275
- const exec = () => {
276
- const triggerEl = dom.getTriggerEl(scope, context.get("value"));
277
- if (!triggerEl) return;
278
- const rect = dom.getOffsetRect(triggerEl);
279
- context.set("indicatorRect", (prev) => isEqual(prev, rect) ? prev : rect);
280
- };
281
- exec();
282
- const triggerEls = dom.getElements(scope);
283
- const listEl = dom.getListEl(scope);
284
- const indicatorCleanup = callAll(
285
- ...triggerEls.map((el) => resizeObserverBorderBox.observe(el, exec)),
286
- ...listEl ? [resizeObserverBorderBox.observe(listEl, exec)] : []
287
- );
288
- refs.set("indicatorCleanup", indicatorCleanup);
289
- },
290
- requestNavigation({ event, prop, scope }) {
291
- const navigate = prop("navigate");
292
- if (!navigate || event.value == null) return;
293
- const node = dom.getTriggerEl(scope, event.value);
294
- if (!isAnchorElement(node)) return;
295
- navigate({ value: event.value, node, href: node.href });
296
- }
297
- }
298
- }
4
+ import { setup } from "@zag-js/core";
5
+ // src/tabs.machine.ts
6
+ const { createMachine } = setup();
7
+ const machine = createMachine({
8
+ props({ props }) {
9
+ return {
10
+ dir: "ltr",
11
+ orientation: "horizontal",
12
+ activationMode: "automatic",
13
+ loopFocus: true,
14
+ composite: true,
15
+ defaultValue: null,
16
+ ...props
17
+ };
18
+ },
19
+ initialState() {
20
+ return "idle";
21
+ },
22
+ context({ prop, bindable }) {
23
+ return {
24
+ value: bindable(() => ({
25
+ defaultValue: prop("defaultValue"),
26
+ value: prop("value"),
27
+ onChange(value) {
28
+ prop("onValueChange")?.({ value });
29
+ }
30
+ })),
31
+ focusedValue: bindable(() => ({
32
+ defaultValue: prop("value") || prop("defaultValue"),
33
+ sync: true,
34
+ onChange(value) {
35
+ prop("onFocusChange")?.({ focusedValue: value });
36
+ }
37
+ })),
38
+ ssr: bindable(() => ({ defaultValue: true })),
39
+ indicatorRect: bindable(() => ({ defaultValue: null })),
40
+ animateIndicator: bindable(() => ({ defaultValue: false }))
41
+ };
42
+ },
43
+ refs() {
44
+ return {
45
+ indicatorCleanup: null,
46
+ prevValue: null,
47
+ focusFrame: AnimationFrame.create()
48
+ };
49
+ },
50
+ watch({ context, prop, track, action }) {
51
+ track([() => context.get("value")], () => {
52
+ action([
53
+ "syncIndicatorAnimation",
54
+ "syncIndicatorRect",
55
+ "syncTabIndex"
56
+ ]);
57
+ });
58
+ track([() => prop("dir"), () => prop("orientation")], () => {
59
+ action(["syncIndicatorRect"]);
60
+ });
61
+ },
62
+ on: {
63
+ SET_VALUE: { actions: ["setValue"] },
64
+ CLEAR_VALUE: { actions: ["clearValue"] },
65
+ SET_INDICATOR_RECT: { actions: ["setIndicatorRect"] },
66
+ SYNC_TAB_INDEX: { actions: ["syncTabIndex"] },
67
+ INDICATOR_TRANSITION_END: { actions: ["clearIndicatorAnimation"] }
68
+ },
69
+ entry: [
70
+ "syncPrevValue",
71
+ "syncIndicatorRect",
72
+ "syncTabIndex",
73
+ "syncSsr"
74
+ ],
75
+ exit: ["cleanupObserver", "cancelFocusFrame"],
76
+ states: {
77
+ idle: { on: {
78
+ TAB_FOCUS: {
79
+ target: "focused",
80
+ actions: ["setFocusedValue"]
81
+ },
82
+ TAB_CLICK: {
83
+ target: "focused",
84
+ actions: [
85
+ "setFocusedValue",
86
+ "setValue",
87
+ "requestNavigation"
88
+ ]
89
+ }
90
+ } },
91
+ focused: {
92
+ exit: ["cancelFocusFrame"],
93
+ on: {
94
+ TAB_CLICK: { actions: [
95
+ "setFocusedValue",
96
+ "setValue",
97
+ "requestNavigation"
98
+ ] },
99
+ ARROW_PREV: [{
100
+ guard: "selectOnFocus",
101
+ actions: ["focusPrevTab", "selectFocusedTab"]
102
+ }, { actions: ["focusPrevTab"] }],
103
+ ARROW_NEXT: [{
104
+ guard: "selectOnFocus",
105
+ actions: ["focusNextTab", "selectFocusedTab"]
106
+ }, { actions: ["focusNextTab"] }],
107
+ HOME: [{
108
+ guard: "selectOnFocus",
109
+ actions: ["focusFirstTab", "selectFocusedTab"]
110
+ }, { actions: ["focusFirstTab"] }],
111
+ END: [{
112
+ guard: "selectOnFocus",
113
+ actions: ["focusLastTab", "selectFocusedTab"]
114
+ }, { actions: ["focusLastTab"] }],
115
+ TAB_FOCUS: { actions: ["setFocusedValue"] },
116
+ TAB_BLUR: {
117
+ target: "idle",
118
+ actions: ["clearFocusedValue"]
119
+ }
120
+ }
121
+ }
122
+ },
123
+ implementations: {
124
+ guards: { selectOnFocus: ({ prop }) => prop("activationMode") === "automatic" },
125
+ actions: {
126
+ selectFocusedTab({ context, prop, event, scope }) {
127
+ raf(() => {
128
+ const focusedValue = context.get("focusedValue");
129
+ if (!focusedValue) return;
130
+ const value = prop("deselectable") && context.get("value") === focusedValue ? null : focusedValue;
131
+ if (context.get("value") === value) return;
132
+ if (event.src === "keyboard") {
133
+ const triggerEl = getTriggerEl(scope, focusedValue);
134
+ if (isAnchorElement(triggerEl) && prop("navigate") !== null) {
135
+ triggerEl.click();
136
+ return;
137
+ }
138
+ }
139
+ context.set("value", value);
140
+ });
141
+ },
142
+ setFocusedValue({ context, event, flush }) {
143
+ if (event.value == null) return;
144
+ flush(() => {
145
+ context.set("focusedValue", event.value);
146
+ });
147
+ },
148
+ clearFocusedValue({ context }) {
149
+ context.set("focusedValue", null);
150
+ },
151
+ setValue({ context, event, prop }) {
152
+ const nullable = prop("deselectable") && context.get("value") === context.get("focusedValue");
153
+ context.set("value", nullable ? null : event.value);
154
+ },
155
+ clearValue({ context }) {
156
+ context.set("value", null);
157
+ },
158
+ focusFirstTab({ scope, refs }) {
159
+ refs.get("focusFrame").request(() => {
160
+ getFirstTriggerEl(scope)?.focus();
161
+ });
162
+ },
163
+ focusLastTab({ scope, refs }) {
164
+ refs.get("focusFrame").request(() => {
165
+ getLastTriggerEl(scope)?.focus();
166
+ });
167
+ },
168
+ focusNextTab({ context, prop, scope, event, refs }) {
169
+ const focusedValue = event.value ?? context.get("focusedValue");
170
+ if (!focusedValue) return;
171
+ const triggerEl = getNextTriggerEl(scope, {
172
+ value: focusedValue,
173
+ loopFocus: prop("loopFocus")
174
+ });
175
+ refs.get("focusFrame").request(() => {
176
+ if (prop("composite")) triggerEl?.focus();
177
+ else if (triggerEl?.dataset.value != null) context.set("focusedValue", triggerEl.dataset.value);
178
+ });
179
+ },
180
+ focusPrevTab({ context, prop, scope, event, refs }) {
181
+ const focusedValue = event.value ?? context.get("focusedValue");
182
+ if (!focusedValue) return;
183
+ const triggerEl = getPrevTriggerEl(scope, {
184
+ value: focusedValue,
185
+ loopFocus: prop("loopFocus")
186
+ });
187
+ refs.get("focusFrame").request(() => {
188
+ if (prop("composite")) triggerEl?.focus();
189
+ else if (triggerEl?.dataset.value != null) context.set("focusedValue", triggerEl.dataset.value);
190
+ });
191
+ },
192
+ syncTabIndex({ context, scope }) {
193
+ raf(() => {
194
+ const value = context.get("value");
195
+ if (!value) return;
196
+ const contentEl = getContentEl(scope, value);
197
+ if (!contentEl) return;
198
+ if (getFocusables(contentEl).length > 0) contentEl.removeAttribute("tabindex");
199
+ else contentEl.setAttribute("tabindex", "0");
200
+ });
201
+ },
202
+ cancelFocusFrame({ refs }) {
203
+ refs.get("focusFrame").cancel();
204
+ },
205
+ cleanupObserver({ refs }) {
206
+ const cleanup = refs.get("indicatorCleanup");
207
+ if (cleanup) cleanup();
208
+ },
209
+ setIndicatorRect({ context, event, scope }) {
210
+ const value = event.id ?? context.get("value");
211
+ if (!getIndicatorEl(scope)) return;
212
+ if (!value) return;
213
+ if (!getTriggerEl(scope, value)) return;
214
+ context.set("indicatorRect", getRectByValue(scope, value));
215
+ },
216
+ syncSsr({ context }) {
217
+ context.set("ssr", false);
218
+ },
219
+ syncPrevValue({ context, refs }) {
220
+ refs.set("prevValue", context.get("value"));
221
+ },
222
+ syncIndicatorAnimation({ context, refs }) {
223
+ const prevValue = refs.get("prevValue");
224
+ const nextValue = context.get("value");
225
+ const animate = prevValue != null && nextValue != null && prevValue !== nextValue;
226
+ context.set("animateIndicator", animate);
227
+ refs.set("prevValue", nextValue);
228
+ },
229
+ clearIndicatorAnimation({ context }) {
230
+ context.set("animateIndicator", false);
231
+ },
232
+ syncIndicatorRect({ context, refs, scope }) {
233
+ const cleanup = refs.get("indicatorCleanup");
234
+ if (cleanup) cleanup();
235
+ if (!getIndicatorEl(scope)) return;
236
+ const exec = () => {
237
+ const triggerEl = getTriggerEl(scope, context.get("value"));
238
+ if (!triggerEl) return;
239
+ const rect = getOffsetRect(triggerEl);
240
+ context.set("indicatorRect", (prev) => isEqual(prev, rect) ? prev : rect);
241
+ };
242
+ exec();
243
+ const triggerEls = getElements(scope);
244
+ const listEl = getListEl(scope);
245
+ const indicatorCleanup = callAll(...triggerEls.map((el) => resizeObserverBorderBox.observe(el, exec)), ...listEl ? [resizeObserverBorderBox.observe(listEl, exec)] : []);
246
+ refs.set("indicatorCleanup", indicatorCleanup);
247
+ },
248
+ requestNavigation({ event, prop, scope }) {
249
+ const navigate = prop("navigate");
250
+ if (!navigate || event.value == null) return;
251
+ const node = getTriggerEl(scope, event.value);
252
+ if (!isAnchorElement(node)) return;
253
+ navigate({
254
+ value: event.value,
255
+ node,
256
+ href: node.href
257
+ });
258
+ }
259
+ }
260
+ }
299
261
  });
300
- export {
301
- machine
302
- };
262
+ export { machine };
@@ -1,13 +1,7 @@
1
- import { TabsProps, ContentProps, TriggerProps } from './tabs.types.mjs';
2
- import '@zag-js/core';
3
- import '@zag-js/dom-query';
4
- import '@zag-js/types';
5
-
6
- declare const props: (keyof TabsProps)[];
7
- declare const splitProps: <Props extends Partial<TabsProps>>(props: Props) => [Partial<TabsProps>, Omit<Props, keyof TabsProps>];
8
- declare const triggerProps: (keyof TriggerProps)[];
9
- declare const splitTriggerProps: <Props extends TriggerProps>(props: Props) => [TriggerProps, Omit<Props, keyof TriggerProps>];
10
- declare const contentProps: "value"[];
11
- declare const splitContentProps: <Props extends ContentProps>(props: Props) => [ContentProps, Omit<Props, "value">];
12
-
13
- export { contentProps, props, splitContentProps, splitProps, splitTriggerProps, triggerProps };
1
+ import { ContentProps, TabsProps, TriggerProps } from "./tabs.types.mjs";
2
+ export declare const props: (keyof TabsProps)[];
3
+ export declare const splitProps: <Props extends Partial<TabsProps>>(props: Props) => [Partial<TabsProps>, Omit<Props, keyof TabsProps>];
4
+ export declare const triggerProps: (keyof TriggerProps)[];
5
+ export declare const splitTriggerProps: <Props extends TriggerProps>(props: Props) => [TriggerProps, Omit<Props, keyof TriggerProps>];
6
+ export declare const contentProps: "value"[];
7
+ export declare const splitContentProps: <Props extends ContentProps>(props: Props) => [ContentProps, Omit<Props, "value">];
@@ -1,13 +1,7 @@
1
- import { TabsProps, ContentProps, TriggerProps } from './tabs.types.js';
2
- import '@zag-js/core';
3
- import '@zag-js/dom-query';
4
- import '@zag-js/types';
5
-
6
- declare const props: (keyof TabsProps)[];
7
- declare const splitProps: <Props extends Partial<TabsProps>>(props: Props) => [Partial<TabsProps>, Omit<Props, keyof TabsProps>];
8
- declare const triggerProps: (keyof TriggerProps)[];
9
- declare const splitTriggerProps: <Props extends TriggerProps>(props: Props) => [TriggerProps, Omit<Props, keyof TriggerProps>];
10
- declare const contentProps: "value"[];
11
- declare const splitContentProps: <Props extends ContentProps>(props: Props) => [ContentProps, Omit<Props, "value">];
12
-
13
- export { contentProps, props, splitContentProps, splitProps, splitTriggerProps, triggerProps };
1
+ import { ContentProps, TabsProps, TriggerProps } from "./tabs.types.js";
2
+ export declare const props: (keyof TabsProps)[];
3
+ export declare const splitProps: <Props extends Partial<TabsProps>>(props: Props) => [Partial<TabsProps>, Omit<Props, keyof TabsProps>];
4
+ export declare const triggerProps: (keyof TriggerProps)[];
5
+ export declare const splitTriggerProps: <Props extends TriggerProps>(props: Props) => [TriggerProps, Omit<Props, keyof TriggerProps>];
6
+ export declare const contentProps: "value"[];
7
+ export declare const splitContentProps: <Props extends ContentProps>(props: Props) => [ContentProps, Omit<Props, "value">];