@zag-js/tabs 1.46.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,332 +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
- };
86
- },
87
- watch({ context, prop, track, action }) {
88
- track([() => context.get("value")], () => {
89
- action(["syncIndicatorAnimation", "syncIndicatorRect", "syncTabIndex"]);
90
- });
91
- track([() => prop("dir"), () => prop("orientation")], () => {
92
- action(["syncIndicatorRect"]);
93
- });
94
- },
95
- on: {
96
- SET_VALUE: {
97
- actions: ["setValue"]
98
- },
99
- CLEAR_VALUE: {
100
- actions: ["clearValue"]
101
- },
102
- SET_INDICATOR_RECT: {
103
- actions: ["setIndicatorRect"]
104
- },
105
- SYNC_TAB_INDEX: {
106
- actions: ["syncTabIndex"]
107
- },
108
- INDICATOR_TRANSITION_END: {
109
- actions: ["clearIndicatorAnimation"]
110
- }
111
- },
112
- entry: ["syncPrevValue", "syncIndicatorRect", "syncTabIndex", "syncSsr"],
113
- exit: ["cleanupObserver"],
114
- states: {
115
- idle: {
116
- on: {
117
- TAB_FOCUS: {
118
- target: "focused",
119
- actions: ["setFocusedValue"]
120
- },
121
- TAB_CLICK: {
122
- target: "focused",
123
- actions: ["setFocusedValue", "setValue", "requestNavigation"]
124
- }
125
- }
126
- },
127
- focused: {
128
- on: {
129
- TAB_CLICK: {
130
- actions: ["setFocusedValue", "setValue", "requestNavigation"]
131
- },
132
- ARROW_PREV: [
133
- {
134
- guard: "selectOnFocus",
135
- actions: ["focusPrevTab", "selectFocusedTab"]
136
- },
137
- {
138
- actions: ["focusPrevTab"]
139
- }
140
- ],
141
- ARROW_NEXT: [
142
- {
143
- guard: "selectOnFocus",
144
- actions: ["focusNextTab", "selectFocusedTab"]
145
- },
146
- {
147
- actions: ["focusNextTab"]
148
- }
149
- ],
150
- HOME: [
151
- {
152
- guard: "selectOnFocus",
153
- actions: ["focusFirstTab", "selectFocusedTab"]
154
- },
155
- {
156
- actions: ["focusFirstTab"]
157
- }
158
- ],
159
- END: [
160
- {
161
- guard: "selectOnFocus",
162
- actions: ["focusLastTab", "selectFocusedTab"]
163
- },
164
- {
165
- actions: ["focusLastTab"]
166
- }
167
- ],
168
- TAB_FOCUS: {
169
- actions: ["setFocusedValue"]
170
- },
171
- TAB_BLUR: {
172
- target: "idle",
173
- actions: ["clearFocusedValue"]
174
- }
175
- }
176
- }
177
- },
178
- implementations: {
179
- guards: {
180
- selectOnFocus: ({ prop }) => prop("activationMode") === "automatic"
181
- },
182
- actions: {
183
- selectFocusedTab({ context, prop, event, scope }) {
184
- (0, import_dom_query.raf)(() => {
185
- const focusedValue = context.get("focusedValue");
186
- if (!focusedValue) return;
187
- const nullable = prop("deselectable") && context.get("value") === focusedValue;
188
- const value = nullable ? null : focusedValue;
189
- if (context.get("value") === value) return;
190
- if (event.src === "keyboard") {
191
- const triggerEl = dom.getTriggerEl(scope, focusedValue);
192
- if ((0, import_dom_query.isAnchorElement)(triggerEl) && prop("navigate") !== null) {
193
- triggerEl.click();
194
- return;
195
- }
196
- }
197
- context.set("value", value);
198
- });
199
- },
200
- setFocusedValue({ context, event, flush }) {
201
- if (event.value == null) return;
202
- flush(() => {
203
- context.set("focusedValue", event.value);
204
- });
205
- },
206
- clearFocusedValue({ context }) {
207
- context.set("focusedValue", null);
208
- },
209
- setValue({ context, event, prop }) {
210
- const nullable = prop("deselectable") && context.get("value") === context.get("focusedValue");
211
- context.set("value", nullable ? null : event.value);
212
- },
213
- clearValue({ context }) {
214
- context.set("value", null);
215
- },
216
- focusFirstTab({ scope }) {
217
- (0, import_dom_query.raf)(() => {
218
- dom.getFirstTriggerEl(scope)?.focus();
219
- });
220
- },
221
- focusLastTab({ scope }) {
222
- (0, import_dom_query.raf)(() => {
223
- dom.getLastTriggerEl(scope)?.focus();
224
- });
225
- },
226
- focusNextTab({ context, prop, scope, event }) {
227
- const focusedValue = event.value ?? context.get("focusedValue");
228
- if (!focusedValue) return;
229
- const triggerEl = dom.getNextTriggerEl(scope, {
230
- value: focusedValue,
231
- loopFocus: prop("loopFocus")
232
- });
233
- (0, import_dom_query.raf)(() => {
234
- if (prop("composite")) {
235
- triggerEl?.focus();
236
- } else if (triggerEl?.dataset.value != null) {
237
- context.set("focusedValue", triggerEl.dataset.value);
238
- }
239
- });
240
- },
241
- focusPrevTab({ context, prop, scope, event }) {
242
- const focusedValue = event.value ?? context.get("focusedValue");
243
- if (!focusedValue) return;
244
- const triggerEl = dom.getPrevTriggerEl(scope, {
245
- value: focusedValue,
246
- loopFocus: prop("loopFocus")
247
- });
248
- (0, import_dom_query.raf)(() => {
249
- if (prop("composite")) {
250
- triggerEl?.focus();
251
- } else if (triggerEl?.dataset.value != null) {
252
- context.set("focusedValue", triggerEl.dataset.value);
253
- }
254
- });
255
- },
256
- syncTabIndex({ context, scope }) {
257
- (0, import_dom_query.raf)(() => {
258
- const value = context.get("value");
259
- if (!value) return;
260
- const contentEl = dom.getContentEl(scope, value);
261
- if (!contentEl) return;
262
- const focusables = (0, import_dom_query.getFocusables)(contentEl);
263
- if (focusables.length > 0) {
264
- contentEl.removeAttribute("tabindex");
265
- } else {
266
- contentEl.setAttribute("tabindex", "0");
267
- }
268
- });
269
- },
270
- cleanupObserver({ refs }) {
271
- const cleanup = refs.get("indicatorCleanup");
272
- if (cleanup) cleanup();
273
- },
274
- setIndicatorRect({ context, event, scope }) {
275
- const value = event.id ?? context.get("value");
276
- const indicatorEl = dom.getIndicatorEl(scope);
277
- if (!indicatorEl) return;
278
- if (!value) return;
279
- const triggerEl = dom.getTriggerEl(scope, value);
280
- if (!triggerEl) return;
281
- context.set("indicatorRect", dom.getRectByValue(scope, value));
282
- },
283
- syncSsr({ context }) {
284
- context.set("ssr", false);
285
- },
286
- syncPrevValue({ context, refs }) {
287
- refs.set("prevValue", context.get("value"));
288
- },
289
- syncIndicatorAnimation({ context, refs }) {
290
- const prevValue = refs.get("prevValue");
291
- const nextValue = context.get("value");
292
- const animate = prevValue != null && nextValue != null && prevValue !== nextValue;
293
- context.set("animateIndicator", animate);
294
- refs.set("prevValue", nextValue);
295
- },
296
- clearIndicatorAnimation({ context }) {
297
- context.set("animateIndicator", false);
298
- },
299
- syncIndicatorRect({ context, refs, scope }) {
300
- const cleanup = refs.get("indicatorCleanup");
301
- if (cleanup) cleanup();
302
- const indicatorEl = dom.getIndicatorEl(scope);
303
- if (!indicatorEl) return;
304
- const exec = () => {
305
- const triggerEl = dom.getTriggerEl(scope, context.get("value"));
306
- if (!triggerEl) return;
307
- const rect = dom.getOffsetRect(triggerEl);
308
- context.set("indicatorRect", (prev) => (0, import_utils.isEqual)(prev, rect) ? prev : rect);
309
- };
310
- exec();
311
- const triggerEls = dom.getElements(scope);
312
- const listEl = dom.getListEl(scope);
313
- const indicatorCleanup = (0, import_utils.callAll)(
314
- ...triggerEls.map((el) => import_dom_query.resizeObserverBorderBox.observe(el, exec)),
315
- ...listEl ? [import_dom_query.resizeObserverBorderBox.observe(listEl, exec)] : []
316
- );
317
- refs.set("indicatorCleanup", indicatorCleanup);
318
- },
319
- requestNavigation({ event, prop, scope }) {
320
- const navigate = prop("navigate");
321
- if (!navigate || event.value == null) return;
322
- const node = dom.getTriggerEl(scope, event.value);
323
- if (!(0, import_dom_query.isAnchorElement)(node)) return;
324
- navigate({ value: event.value, node, href: node.href });
325
- }
326
- }
327
- }
1
+ Object.defineProperties(exports, {
2
+ __esModule: { value: true },
3
+ [Symbol.toStringTag]: { value: "Module" }
328
4
  });
329
- // Annotate the CommonJS export names for ESM import in node:
330
- 0 && (module.exports = {
331
- 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
+ }
332
264
  });
265
+ exports.machine = machine;