@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,337 +1,265 @@
1
- "use strict";
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __export = (target, all) => {
9
- for (var name in all)
10
- __defProp(target, name, { get: all[name], enumerable: true });
11
- };
12
- var __copyProps = (to, from, except, desc) => {
13
- if (from && typeof from === "object" || typeof from === "function") {
14
- for (let key of __getOwnPropNames(from))
15
- if (!__hasOwnProp.call(to, key) && key !== except)
16
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
- }
18
- return to;
19
- };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
-
30
- // src/tabs.machine.ts
31
- var tabs_machine_exports = {};
32
- __export(tabs_machine_exports, {
33
- machine: () => machine
34
- });
35
- module.exports = __toCommonJS(tabs_machine_exports);
36
- var import_core = require("@zag-js/core");
37
- var import_dom_query = require("@zag-js/dom-query");
38
- var import_utils = require("@zag-js/utils");
39
- var dom = __toESM(require("./tabs.dom.js"));
40
- var { createMachine } = (0, import_core.setup)();
41
- var machine = createMachine({
42
- props({ props }) {
43
- return {
44
- dir: "ltr",
45
- orientation: "horizontal",
46
- activationMode: "automatic",
47
- loopFocus: true,
48
- composite: true,
49
- defaultValue: null,
50
- ...props
51
- };
52
- },
53
- initialState() {
54
- return "idle";
55
- },
56
- context({ prop, bindable }) {
57
- return {
58
- value: bindable(() => ({
59
- defaultValue: prop("defaultValue"),
60
- value: prop("value"),
61
- onChange(value) {
62
- prop("onValueChange")?.({ value });
63
- }
64
- })),
65
- focusedValue: bindable(() => ({
66
- defaultValue: prop("value") || prop("defaultValue"),
67
- sync: true,
68
- onChange(value) {
69
- prop("onFocusChange")?.({ focusedValue: value });
70
- }
71
- })),
72
- ssr: bindable(() => ({ defaultValue: true })),
73
- indicatorRect: bindable(() => ({
74
- defaultValue: null
75
- })),
76
- animateIndicator: bindable(() => ({
77
- defaultValue: false
78
- }))
79
- };
80
- },
81
- refs() {
82
- return {
83
- indicatorCleanup: null,
84
- prevValue: null,
85
- focusFrame: import_dom_query.AnimationFrame.create()
86
- };
87
- },
88
- watch({ context, prop, track, action }) {
89
- track([() => context.get("value")], () => {
90
- action(["syncIndicatorAnimation", "syncIndicatorRect", "syncTabIndex"]);
91
- });
92
- track([() => prop("dir"), () => prop("orientation")], () => {
93
- action(["syncIndicatorRect"]);
94
- });
95
- },
96
- on: {
97
- SET_VALUE: {
98
- actions: ["setValue"]
99
- },
100
- CLEAR_VALUE: {
101
- actions: ["clearValue"]
102
- },
103
- SET_INDICATOR_RECT: {
104
- actions: ["setIndicatorRect"]
105
- },
106
- SYNC_TAB_INDEX: {
107
- actions: ["syncTabIndex"]
108
- },
109
- INDICATOR_TRANSITION_END: {
110
- actions: ["clearIndicatorAnimation"]
111
- }
112
- },
113
- entry: ["syncPrevValue", "syncIndicatorRect", "syncTabIndex", "syncSsr"],
114
- exit: ["cleanupObserver", "cancelFocusFrame"],
115
- states: {
116
- idle: {
117
- on: {
118
- TAB_FOCUS: {
119
- target: "focused",
120
- actions: ["setFocusedValue"]
121
- },
122
- TAB_CLICK: {
123
- target: "focused",
124
- actions: ["setFocusedValue", "setValue", "requestNavigation"]
125
- }
126
- }
127
- },
128
- focused: {
129
- exit: ["cancelFocusFrame"],
130
- on: {
131
- TAB_CLICK: {
132
- actions: ["setFocusedValue", "setValue", "requestNavigation"]
133
- },
134
- ARROW_PREV: [
135
- {
136
- guard: "selectOnFocus",
137
- actions: ["focusPrevTab", "selectFocusedTab"]
138
- },
139
- {
140
- actions: ["focusPrevTab"]
141
- }
142
- ],
143
- ARROW_NEXT: [
144
- {
145
- guard: "selectOnFocus",
146
- actions: ["focusNextTab", "selectFocusedTab"]
147
- },
148
- {
149
- actions: ["focusNextTab"]
150
- }
151
- ],
152
- HOME: [
153
- {
154
- guard: "selectOnFocus",
155
- actions: ["focusFirstTab", "selectFocusedTab"]
156
- },
157
- {
158
- actions: ["focusFirstTab"]
159
- }
160
- ],
161
- END: [
162
- {
163
- guard: "selectOnFocus",
164
- actions: ["focusLastTab", "selectFocusedTab"]
165
- },
166
- {
167
- actions: ["focusLastTab"]
168
- }
169
- ],
170
- TAB_FOCUS: {
171
- actions: ["setFocusedValue"]
172
- },
173
- TAB_BLUR: {
174
- target: "idle",
175
- actions: ["clearFocusedValue"]
176
- }
177
- }
178
- }
179
- },
180
- implementations: {
181
- guards: {
182
- selectOnFocus: ({ prop }) => prop("activationMode") === "automatic"
183
- },
184
- actions: {
185
- selectFocusedTab({ context, prop, event, scope }) {
186
- (0, import_dom_query.raf)(() => {
187
- const focusedValue = context.get("focusedValue");
188
- if (!focusedValue) return;
189
- const nullable = prop("deselectable") && context.get("value") === focusedValue;
190
- const value = nullable ? null : focusedValue;
191
- if (context.get("value") === value) return;
192
- if (event.src === "keyboard") {
193
- const triggerEl = dom.getTriggerEl(scope, focusedValue);
194
- if ((0, import_dom_query.isAnchorElement)(triggerEl) && prop("navigate") !== null) {
195
- triggerEl.click();
196
- return;
197
- }
198
- }
199
- context.set("value", value);
200
- });
201
- },
202
- setFocusedValue({ context, event, flush }) {
203
- if (event.value == null) return;
204
- flush(() => {
205
- context.set("focusedValue", event.value);
206
- });
207
- },
208
- clearFocusedValue({ context }) {
209
- context.set("focusedValue", null);
210
- },
211
- setValue({ context, event, prop }) {
212
- const nullable = prop("deselectable") && context.get("value") === context.get("focusedValue");
213
- context.set("value", nullable ? null : event.value);
214
- },
215
- clearValue({ context }) {
216
- context.set("value", null);
217
- },
218
- focusFirstTab({ scope, refs }) {
219
- refs.get("focusFrame").request(() => {
220
- dom.getFirstTriggerEl(scope)?.focus();
221
- });
222
- },
223
- focusLastTab({ scope, refs }) {
224
- refs.get("focusFrame").request(() => {
225
- dom.getLastTriggerEl(scope)?.focus();
226
- });
227
- },
228
- focusNextTab({ context, prop, scope, event, refs }) {
229
- const focusedValue = event.value ?? context.get("focusedValue");
230
- if (!focusedValue) return;
231
- const triggerEl = dom.getNextTriggerEl(scope, {
232
- value: focusedValue,
233
- loopFocus: prop("loopFocus")
234
- });
235
- refs.get("focusFrame").request(() => {
236
- if (prop("composite")) {
237
- triggerEl?.focus();
238
- } else if (triggerEl?.dataset.value != null) {
239
- context.set("focusedValue", triggerEl.dataset.value);
240
- }
241
- });
242
- },
243
- focusPrevTab({ context, prop, scope, event, refs }) {
244
- const focusedValue = event.value ?? context.get("focusedValue");
245
- if (!focusedValue) return;
246
- const triggerEl = dom.getPrevTriggerEl(scope, {
247
- value: focusedValue,
248
- loopFocus: prop("loopFocus")
249
- });
250
- refs.get("focusFrame").request(() => {
251
- if (prop("composite")) {
252
- triggerEl?.focus();
253
- } else if (triggerEl?.dataset.value != null) {
254
- context.set("focusedValue", triggerEl.dataset.value);
255
- }
256
- });
257
- },
258
- syncTabIndex({ context, scope }) {
259
- (0, import_dom_query.raf)(() => {
260
- const value = context.get("value");
261
- if (!value) return;
262
- const contentEl = dom.getContentEl(scope, value);
263
- if (!contentEl) return;
264
- const focusables = (0, import_dom_query.getFocusables)(contentEl);
265
- if (focusables.length > 0) {
266
- contentEl.removeAttribute("tabindex");
267
- } else {
268
- contentEl.setAttribute("tabindex", "0");
269
- }
270
- });
271
- },
272
- cancelFocusFrame({ refs }) {
273
- refs.get("focusFrame").cancel();
274
- },
275
- cleanupObserver({ refs }) {
276
- const cleanup = refs.get("indicatorCleanup");
277
- if (cleanup) cleanup();
278
- },
279
- setIndicatorRect({ context, event, scope }) {
280
- const value = event.id ?? context.get("value");
281
- const indicatorEl = dom.getIndicatorEl(scope);
282
- if (!indicatorEl) return;
283
- if (!value) return;
284
- const triggerEl = dom.getTriggerEl(scope, value);
285
- if (!triggerEl) return;
286
- context.set("indicatorRect", dom.getRectByValue(scope, value));
287
- },
288
- syncSsr({ context }) {
289
- context.set("ssr", false);
290
- },
291
- syncPrevValue({ context, refs }) {
292
- refs.set("prevValue", context.get("value"));
293
- },
294
- syncIndicatorAnimation({ context, refs }) {
295
- const prevValue = refs.get("prevValue");
296
- const nextValue = context.get("value");
297
- const animate = prevValue != null && nextValue != null && prevValue !== nextValue;
298
- context.set("animateIndicator", animate);
299
- refs.set("prevValue", nextValue);
300
- },
301
- clearIndicatorAnimation({ context }) {
302
- context.set("animateIndicator", false);
303
- },
304
- syncIndicatorRect({ context, refs, scope }) {
305
- const cleanup = refs.get("indicatorCleanup");
306
- if (cleanup) cleanup();
307
- const indicatorEl = dom.getIndicatorEl(scope);
308
- if (!indicatorEl) return;
309
- const exec = () => {
310
- const triggerEl = dom.getTriggerEl(scope, context.get("value"));
311
- if (!triggerEl) return;
312
- const rect = dom.getOffsetRect(triggerEl);
313
- context.set("indicatorRect", (prev) => (0, import_utils.isEqual)(prev, rect) ? prev : rect);
314
- };
315
- exec();
316
- const triggerEls = dom.getElements(scope);
317
- const listEl = dom.getListEl(scope);
318
- const indicatorCleanup = (0, import_utils.callAll)(
319
- ...triggerEls.map((el) => import_dom_query.resizeObserverBorderBox.observe(el, exec)),
320
- ...listEl ? [import_dom_query.resizeObserverBorderBox.observe(listEl, exec)] : []
321
- );
322
- refs.set("indicatorCleanup", indicatorCleanup);
323
- },
324
- requestNavigation({ event, prop, scope }) {
325
- const navigate = prop("navigate");
326
- if (!navigate || event.value == null) return;
327
- const node = dom.getTriggerEl(scope, event.value);
328
- if (!(0, import_dom_query.isAnchorElement)(node)) return;
329
- navigate({ value: event.value, node, href: node.href });
330
- }
331
- }
332
- }
1
+ Object.defineProperties(exports, {
2
+ __esModule: { value: true },
3
+ [Symbol.toStringTag]: { value: "Module" }
333
4
  });
334
- // Annotate the CommonJS export names for ESM import in node:
335
- 0 && (module.exports = {
336
- machine
5
+ const require_tabs_dom = require("./tabs.dom.js");
6
+ let _zag_js_dom_query = require("@zag-js/dom-query");
7
+ let _zag_js_utils = require("@zag-js/utils");
8
+ // src/tabs.machine.ts
9
+ const { createMachine } = (0, require("@zag-js/core").setup)();
10
+ const machine = createMachine({
11
+ props({ props }) {
12
+ return {
13
+ dir: "ltr",
14
+ orientation: "horizontal",
15
+ activationMode: "automatic",
16
+ loopFocus: true,
17
+ composite: true,
18
+ defaultValue: null,
19
+ ...props
20
+ };
21
+ },
22
+ initialState() {
23
+ return "idle";
24
+ },
25
+ context({ prop, bindable }) {
26
+ return {
27
+ value: bindable(() => ({
28
+ defaultValue: prop("defaultValue"),
29
+ value: prop("value"),
30
+ onChange(value) {
31
+ prop("onValueChange")?.({ value });
32
+ }
33
+ })),
34
+ focusedValue: bindable(() => ({
35
+ defaultValue: prop("value") || prop("defaultValue"),
36
+ sync: true,
37
+ onChange(value) {
38
+ prop("onFocusChange")?.({ focusedValue: value });
39
+ }
40
+ })),
41
+ ssr: bindable(() => ({ defaultValue: true })),
42
+ indicatorRect: bindable(() => ({ defaultValue: null })),
43
+ animateIndicator: bindable(() => ({ defaultValue: false }))
44
+ };
45
+ },
46
+ refs() {
47
+ return {
48
+ indicatorCleanup: null,
49
+ prevValue: null,
50
+ focusFrame: _zag_js_dom_query.AnimationFrame.create()
51
+ };
52
+ },
53
+ watch({ context, prop, track, action }) {
54
+ track([() => context.get("value")], () => {
55
+ action([
56
+ "syncIndicatorAnimation",
57
+ "syncIndicatorRect",
58
+ "syncTabIndex"
59
+ ]);
60
+ });
61
+ track([() => prop("dir"), () => prop("orientation")], () => {
62
+ action(["syncIndicatorRect"]);
63
+ });
64
+ },
65
+ on: {
66
+ SET_VALUE: { actions: ["setValue"] },
67
+ CLEAR_VALUE: { actions: ["clearValue"] },
68
+ SET_INDICATOR_RECT: { actions: ["setIndicatorRect"] },
69
+ SYNC_TAB_INDEX: { actions: ["syncTabIndex"] },
70
+ INDICATOR_TRANSITION_END: { actions: ["clearIndicatorAnimation"] }
71
+ },
72
+ entry: [
73
+ "syncPrevValue",
74
+ "syncIndicatorRect",
75
+ "syncTabIndex",
76
+ "syncSsr"
77
+ ],
78
+ exit: ["cleanupObserver", "cancelFocusFrame"],
79
+ states: {
80
+ idle: { on: {
81
+ TAB_FOCUS: {
82
+ target: "focused",
83
+ actions: ["setFocusedValue"]
84
+ },
85
+ TAB_CLICK: {
86
+ target: "focused",
87
+ actions: [
88
+ "setFocusedValue",
89
+ "setValue",
90
+ "requestNavigation"
91
+ ]
92
+ }
93
+ } },
94
+ focused: {
95
+ exit: ["cancelFocusFrame"],
96
+ on: {
97
+ TAB_CLICK: { actions: [
98
+ "setFocusedValue",
99
+ "setValue",
100
+ "requestNavigation"
101
+ ] },
102
+ ARROW_PREV: [{
103
+ guard: "selectOnFocus",
104
+ actions: ["focusPrevTab", "selectFocusedTab"]
105
+ }, { actions: ["focusPrevTab"] }],
106
+ ARROW_NEXT: [{
107
+ guard: "selectOnFocus",
108
+ actions: ["focusNextTab", "selectFocusedTab"]
109
+ }, { actions: ["focusNextTab"] }],
110
+ HOME: [{
111
+ guard: "selectOnFocus",
112
+ actions: ["focusFirstTab", "selectFocusedTab"]
113
+ }, { actions: ["focusFirstTab"] }],
114
+ END: [{
115
+ guard: "selectOnFocus",
116
+ actions: ["focusLastTab", "selectFocusedTab"]
117
+ }, { actions: ["focusLastTab"] }],
118
+ TAB_FOCUS: { actions: ["setFocusedValue"] },
119
+ TAB_BLUR: {
120
+ target: "idle",
121
+ actions: ["clearFocusedValue"]
122
+ }
123
+ }
124
+ }
125
+ },
126
+ implementations: {
127
+ guards: { selectOnFocus: ({ prop }) => prop("activationMode") === "automatic" },
128
+ actions: {
129
+ selectFocusedTab({ context, prop, event, scope }) {
130
+ (0, _zag_js_dom_query.raf)(() => {
131
+ const focusedValue = context.get("focusedValue");
132
+ if (!focusedValue) return;
133
+ const value = prop("deselectable") && context.get("value") === focusedValue ? null : focusedValue;
134
+ if (context.get("value") === value) return;
135
+ if (event.src === "keyboard") {
136
+ const triggerEl = require_tabs_dom.getTriggerEl(scope, focusedValue);
137
+ if ((0, _zag_js_dom_query.isAnchorElement)(triggerEl) && prop("navigate") !== null) {
138
+ triggerEl.click();
139
+ return;
140
+ }
141
+ }
142
+ context.set("value", value);
143
+ });
144
+ },
145
+ setFocusedValue({ context, event, flush }) {
146
+ if (event.value == null) return;
147
+ flush(() => {
148
+ context.set("focusedValue", event.value);
149
+ });
150
+ },
151
+ clearFocusedValue({ context }) {
152
+ context.set("focusedValue", null);
153
+ },
154
+ setValue({ context, event, prop }) {
155
+ const nullable = prop("deselectable") && context.get("value") === context.get("focusedValue");
156
+ context.set("value", nullable ? null : event.value);
157
+ },
158
+ clearValue({ context }) {
159
+ context.set("value", null);
160
+ },
161
+ focusFirstTab({ scope, refs }) {
162
+ refs.get("focusFrame").request(() => {
163
+ require_tabs_dom.getFirstTriggerEl(scope)?.focus();
164
+ });
165
+ },
166
+ focusLastTab({ scope, refs }) {
167
+ refs.get("focusFrame").request(() => {
168
+ require_tabs_dom.getLastTriggerEl(scope)?.focus();
169
+ });
170
+ },
171
+ focusNextTab({ context, prop, scope, event, refs }) {
172
+ const focusedValue = event.value ?? context.get("focusedValue");
173
+ if (!focusedValue) return;
174
+ const triggerEl = require_tabs_dom.getNextTriggerEl(scope, {
175
+ value: focusedValue,
176
+ loopFocus: prop("loopFocus")
177
+ });
178
+ refs.get("focusFrame").request(() => {
179
+ if (prop("composite")) triggerEl?.focus();
180
+ else if (triggerEl?.dataset.value != null) context.set("focusedValue", triggerEl.dataset.value);
181
+ });
182
+ },
183
+ focusPrevTab({ context, prop, scope, event, refs }) {
184
+ const focusedValue = event.value ?? context.get("focusedValue");
185
+ if (!focusedValue) return;
186
+ const triggerEl = require_tabs_dom.getPrevTriggerEl(scope, {
187
+ value: focusedValue,
188
+ loopFocus: prop("loopFocus")
189
+ });
190
+ refs.get("focusFrame").request(() => {
191
+ if (prop("composite")) triggerEl?.focus();
192
+ else if (triggerEl?.dataset.value != null) context.set("focusedValue", triggerEl.dataset.value);
193
+ });
194
+ },
195
+ syncTabIndex({ context, scope }) {
196
+ (0, _zag_js_dom_query.raf)(() => {
197
+ const value = context.get("value");
198
+ if (!value) return;
199
+ const contentEl = require_tabs_dom.getContentEl(scope, value);
200
+ if (!contentEl) return;
201
+ if ((0, _zag_js_dom_query.getFocusables)(contentEl).length > 0) contentEl.removeAttribute("tabindex");
202
+ else contentEl.setAttribute("tabindex", "0");
203
+ });
204
+ },
205
+ cancelFocusFrame({ refs }) {
206
+ refs.get("focusFrame").cancel();
207
+ },
208
+ cleanupObserver({ refs }) {
209
+ const cleanup = refs.get("indicatorCleanup");
210
+ if (cleanup) cleanup();
211
+ },
212
+ setIndicatorRect({ context, event, scope }) {
213
+ const value = event.id ?? context.get("value");
214
+ if (!require_tabs_dom.getIndicatorEl(scope)) return;
215
+ if (!value) return;
216
+ if (!require_tabs_dom.getTriggerEl(scope, value)) return;
217
+ context.set("indicatorRect", require_tabs_dom.getRectByValue(scope, value));
218
+ },
219
+ syncSsr({ context }) {
220
+ context.set("ssr", false);
221
+ },
222
+ syncPrevValue({ context, refs }) {
223
+ refs.set("prevValue", context.get("value"));
224
+ },
225
+ syncIndicatorAnimation({ context, refs }) {
226
+ const prevValue = refs.get("prevValue");
227
+ const nextValue = context.get("value");
228
+ const animate = prevValue != null && nextValue != null && prevValue !== nextValue;
229
+ context.set("animateIndicator", animate);
230
+ refs.set("prevValue", nextValue);
231
+ },
232
+ clearIndicatorAnimation({ context }) {
233
+ context.set("animateIndicator", false);
234
+ },
235
+ syncIndicatorRect({ context, refs, scope }) {
236
+ const cleanup = refs.get("indicatorCleanup");
237
+ if (cleanup) cleanup();
238
+ if (!require_tabs_dom.getIndicatorEl(scope)) return;
239
+ const exec = () => {
240
+ const triggerEl = require_tabs_dom.getTriggerEl(scope, context.get("value"));
241
+ if (!triggerEl) return;
242
+ const rect = require_tabs_dom.getOffsetRect(triggerEl);
243
+ context.set("indicatorRect", (prev) => (0, _zag_js_utils.isEqual)(prev, rect) ? prev : rect);
244
+ };
245
+ exec();
246
+ const triggerEls = require_tabs_dom.getElements(scope);
247
+ const listEl = require_tabs_dom.getListEl(scope);
248
+ const indicatorCleanup = (0, _zag_js_utils.callAll)(...triggerEls.map((el) => _zag_js_dom_query.resizeObserverBorderBox.observe(el, exec)), ...listEl ? [_zag_js_dom_query.resizeObserverBorderBox.observe(listEl, exec)] : []);
249
+ refs.set("indicatorCleanup", indicatorCleanup);
250
+ },
251
+ requestNavigation({ event, prop, scope }) {
252
+ const navigate = prop("navigate");
253
+ if (!navigate || event.value == null) return;
254
+ const node = require_tabs_dom.getTriggerEl(scope, event.value);
255
+ if (!(0, _zag_js_dom_query.isAnchorElement)(node)) return;
256
+ navigate({
257
+ value: event.value,
258
+ node,
259
+ href: node.href
260
+ });
261
+ }
262
+ }
263
+ }
337
264
  });
265
+ exports.machine = machine;