@zag-js/tabs 0.0.0-20220813122512

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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021 Chakra UI
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,19 @@
1
+ # @zag-js/tabs
2
+
3
+ Core logic for the tabs widget implemented as a state machine
4
+
5
+ ## Installation
6
+
7
+ ```sh
8
+ yarn add @zag-js/tabs
9
+ # or
10
+ npm i @zag-js/tabs
11
+ ```
12
+
13
+ ## Contribution
14
+
15
+ Yes please! See the [contributing guidelines](https://github.com/chakra-ui/zag/blob/main/CONTRIBUTING.md) for details.
16
+
17
+ ## Licence
18
+
19
+ This project is licensed under the terms of the [MIT license](https://github.com/chakra-ui/zag/blob/main/LICENSE).
@@ -0,0 +1,115 @@
1
+ import { RequiredBy, DirectionProperty, CommonProperties, Context, PropTypes, NormalizeProps } from '@zag-js/types';
2
+ import * as _zag_js_core from '@zag-js/core';
3
+ import { StateMachine } from '@zag-js/core';
4
+
5
+ declare type IntlMessages = {
6
+ tablistLabel?: string;
7
+ deleteLabel?(value: string): string;
8
+ };
9
+ declare type ElementIds = Partial<{
10
+ root: string;
11
+ trigger: string;
12
+ triggerGroup: string;
13
+ contentGroup: string;
14
+ content: string;
15
+ }>;
16
+ declare type PublicContext = DirectionProperty & CommonProperties & {
17
+ /**
18
+ * The ids of the elements in the tabs. Useful for composition.
19
+ */
20
+ ids?: ElementIds;
21
+ /**
22
+ * Specifies the localized strings that identifies the accessibility elements and their states
23
+ */
24
+ messages: IntlMessages;
25
+ /**
26
+ * Whether the keyboard navigation will loop from last tab to first, and vice versa.
27
+ * @default true
28
+ */
29
+ loop: boolean;
30
+ /**
31
+ * Whether the indicator is rendered.
32
+ */
33
+ isIndicatorRendered: boolean;
34
+ /**
35
+ * The selected tab id
36
+ */
37
+ value: string | null;
38
+ /**
39
+ * The orientation of the tabs. Can be `horizontal` or `vertical`
40
+ * - `horizontal`: only left and right arrow key navigation will work.
41
+ * - `vertical`: only up and down arrow key navigation will work.
42
+ *
43
+ * @default "horizontal"
44
+ */
45
+ orientation?: "horizontal" | "vertical";
46
+ /**
47
+ * The activation mode of the tabs. Can be `manual` or `automatic`
48
+ * - `manual`: Tabs are activated when clicked or press `enter` key.
49
+ * - `automatic`: Tabs are activated when receiving focus
50
+ * @default "automatic"
51
+ */
52
+ activationMode?: "manual" | "automatic";
53
+ /**
54
+ * Callback to be called when the selected/active tab changes
55
+ */
56
+ onChange?: (details: {
57
+ value: string | null;
58
+ }) => void;
59
+ /**
60
+ * Callback to be called when the focused tab changes
61
+ */
62
+ onFocus?: (details: {
63
+ value: string | null;
64
+ }) => void;
65
+ /**
66
+ * Callback to be called when a tab's close button is clicked
67
+ */
68
+ onDelete?: (details: {
69
+ value: string;
70
+ }) => void;
71
+ };
72
+ declare type UserDefinedContext = RequiredBy<PublicContext, "id">;
73
+ declare type ComputedContext = Readonly<{
74
+ /**
75
+ * @computed
76
+ * Whether the tab is in the horizontal orientation
77
+ */
78
+ isHorizontal: boolean;
79
+ /**
80
+ * @computed
81
+ * Whether the tab is in the vertical orientation
82
+ */
83
+ isVertical: boolean;
84
+ }>;
85
+ declare type PrivateContext = Context<{}>;
86
+ declare type MachineContext = PublicContext & ComputedContext & PrivateContext;
87
+ declare type MachineState = {
88
+ value: "unknown" | "idle" | "focused";
89
+ };
90
+ declare type State = StateMachine.State<MachineContext, MachineState>;
91
+ declare type Send = StateMachine.Send<StateMachine.AnyEventObject>;
92
+ declare type TabProps = {
93
+ value: string;
94
+ disabled?: boolean;
95
+ };
96
+
97
+ declare function connect<T extends PropTypes>(state: State, send: Send, normalize: NormalizeProps<T>): {
98
+ value: string | null;
99
+ focusedValue: string | null;
100
+ previousValues: string[];
101
+ setValue(value: string): void;
102
+ rootProps: T["element"];
103
+ triggerGroupProps: T["element"];
104
+ getTriggerProps(props: TabProps): T["button"];
105
+ contentGroupProps: T["element"];
106
+ getContentProps({ value }: {
107
+ value: string;
108
+ }): T["element"];
109
+ getDeleteButtonProps({ value, disabled }: TabProps): T["button"];
110
+ indicatorProps: T["element"];
111
+ };
112
+
113
+ declare function machine(ctx: UserDefinedContext): _zag_js_core.Machine<MachineContext, MachineState, _zag_js_core.StateMachine.AnyEventObject>;
114
+
115
+ export { UserDefinedContext as Context, connect, machine };
package/dist/index.js ADDED
@@ -0,0 +1,586 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var src_exports = {};
22
+ __export(src_exports, {
23
+ connect: () => connect,
24
+ machine: () => machine
25
+ });
26
+ module.exports = __toCommonJS(src_exports);
27
+
28
+ // ../../utilities/dom/dist/index.mjs
29
+ var dataAttr = (guard) => {
30
+ return guard ? "" : void 0;
31
+ };
32
+ var isDom = () => typeof window !== "undefined";
33
+ function getPlatform() {
34
+ const agent = navigator.userAgentData;
35
+ return (agent == null ? void 0 : agent.platform) ?? navigator.platform;
36
+ }
37
+ var pt = (v) => isDom() && v.test(getPlatform());
38
+ var vn = (v) => isDom() && v.test(navigator.vendor);
39
+ var isSafari = () => isApple() && vn(/apple/i);
40
+ var isApple = () => pt(/mac|iphone|ipad|ipod/i);
41
+ function isDocument(el) {
42
+ return el.nodeType === Node.DOCUMENT_NODE;
43
+ }
44
+ function isWindow(value) {
45
+ return (value == null ? void 0 : value.toString()) === "[object Window]";
46
+ }
47
+ function isFrame(element) {
48
+ return element.localName === "iframe";
49
+ }
50
+ function getDocument(el) {
51
+ if (isWindow(el))
52
+ return el.document;
53
+ if (isDocument(el))
54
+ return el;
55
+ return (el == null ? void 0 : el.ownerDocument) ?? document;
56
+ }
57
+ function defineDomHelpers(helpers) {
58
+ const dom2 = {
59
+ getRootNode: (ctx) => {
60
+ var _a;
61
+ return ((_a = ctx.getRootNode) == null ? void 0 : _a.call(ctx)) ?? document;
62
+ },
63
+ getDoc: (ctx) => getDocument(dom2.getRootNode(ctx)),
64
+ getWin: (ctx) => dom2.getDoc(ctx).defaultView ?? window,
65
+ getActiveElement: (ctx) => dom2.getDoc(ctx).activeElement,
66
+ getById: (ctx, id) => dom2.getRootNode(ctx).getElementById(id)
67
+ };
68
+ return {
69
+ ...dom2,
70
+ ...helpers
71
+ };
72
+ }
73
+ function isHTMLElement(v) {
74
+ return typeof v === "object" && (v == null ? void 0 : v.nodeType) === Node.ELEMENT_NODE && typeof (v == null ? void 0 : v.nodeName) === "string";
75
+ }
76
+ function isVisible(el) {
77
+ if (!isHTMLElement(el))
78
+ return false;
79
+ return el.offsetWidth > 0 || el.offsetHeight > 0 || el.getClientRects().length > 0;
80
+ }
81
+ var focusableSelector = "input:not([type='hidden']):not([disabled]), select:not([disabled]), textarea:not([disabled]), a[href], button:not([disabled]), [tabindex], iframe, object, embed, area[href], audio[controls], video[controls], [contenteditable]:not([contenteditable='false']), details > summary:first-of-type";
82
+ var getFocusables = (container, includeContainer = false) => {
83
+ if (!container)
84
+ return [];
85
+ const elements = Array.from(container.querySelectorAll(focusableSelector));
86
+ const include = includeContainer == true || includeContainer == "if-empty" && elements.length === 0;
87
+ if (include && isHTMLElement(container) && isFocusable(container)) {
88
+ elements.unshift(container);
89
+ }
90
+ const focusableElements = elements.filter(isFocusable);
91
+ focusableElements.forEach((element, i) => {
92
+ if (isFrame(element) && element.contentDocument) {
93
+ const frameBody = element.contentDocument.body;
94
+ focusableElements.splice(i, 1, ...getFocusables(frameBody));
95
+ }
96
+ });
97
+ return focusableElements;
98
+ };
99
+ function isFocusable(element) {
100
+ if (!element)
101
+ return false;
102
+ return element.matches(focusableSelector) && isVisible(element);
103
+ }
104
+ var rtlKeyMap = {
105
+ ArrowLeft: "ArrowRight",
106
+ ArrowRight: "ArrowLeft",
107
+ Home: "End",
108
+ End: "Home"
109
+ };
110
+ var sameKeyMap = {
111
+ Up: "ArrowUp",
112
+ Down: "ArrowDown",
113
+ Esc: "Escape",
114
+ " ": "Space",
115
+ ",": "Comma",
116
+ Left: "ArrowLeft",
117
+ Right: "ArrowRight"
118
+ };
119
+ function getEventKey(event, options = {}) {
120
+ const { dir = "ltr", orientation = "horizontal" } = options;
121
+ let { key } = event;
122
+ key = sameKeyMap[key] ?? key;
123
+ const isRtl = dir === "rtl" && orientation === "horizontal";
124
+ if (isRtl && key in rtlKeyMap) {
125
+ key = rtlKeyMap[key];
126
+ }
127
+ return key;
128
+ }
129
+ function nextTick(fn) {
130
+ const set = /* @__PURE__ */ new Set();
131
+ function raf2(fn2) {
132
+ const id = globalThis.requestAnimationFrame(fn2);
133
+ set.add(() => globalThis.cancelAnimationFrame(id));
134
+ }
135
+ raf2(() => raf2(fn));
136
+ return function cleanup() {
137
+ set.forEach(function(fn2) {
138
+ fn2();
139
+ });
140
+ };
141
+ }
142
+ function raf(fn) {
143
+ const id = globalThis.requestAnimationFrame(fn);
144
+ return function cleanup() {
145
+ globalThis.cancelAnimationFrame(id);
146
+ };
147
+ }
148
+ function queryAll(root, selector) {
149
+ return Array.from((root == null ? void 0 : root.querySelectorAll(selector)) ?? []);
150
+ }
151
+ function itemById(v, id) {
152
+ return v.find((node) => node.id === id);
153
+ }
154
+ function indexOfId(v, id) {
155
+ const item = itemById(v, id);
156
+ return item ? v.indexOf(item) : -1;
157
+ }
158
+ function nextById(v, id, loop = true) {
159
+ let idx = indexOfId(v, id);
160
+ idx = loop ? (idx + 1) % v.length : Math.min(idx + 1, v.length - 1);
161
+ return v[idx];
162
+ }
163
+ function prevById(v, id, loop = true) {
164
+ let idx = indexOfId(v, id);
165
+ if (idx === -1)
166
+ return loop ? v[v.length - 1] : null;
167
+ idx = loop ? (idx - 1 + v.length) % v.length : Math.max(0, idx - 1);
168
+ return v[idx];
169
+ }
170
+
171
+ // ../../utilities/core/dist/index.mjs
172
+ var first = (v) => v[0];
173
+ var last = (v) => v[v.length - 1];
174
+
175
+ // src/tabs.dom.ts
176
+ var dom = defineDomHelpers({
177
+ getRootId: (ctx) => {
178
+ var _a;
179
+ return ((_a = ctx.ids) == null ? void 0 : _a.root) ?? `tabs:${ctx.id}`;
180
+ },
181
+ getTriggerGroupId: (ctx) => {
182
+ var _a;
183
+ return ((_a = ctx.ids) == null ? void 0 : _a.triggerGroup) ?? `tabs:${ctx.id}:trigger-group`;
184
+ },
185
+ getContentId: (ctx, id) => {
186
+ var _a;
187
+ return ((_a = ctx.ids) == null ? void 0 : _a.content) ?? `tabs:${ctx.id}:content-${id}`;
188
+ },
189
+ getContentGroupId: (ctx) => {
190
+ var _a;
191
+ return ((_a = ctx.ids) == null ? void 0 : _a.contentGroup) ?? `tabs:${ctx.id}:content-group`;
192
+ },
193
+ getTriggerId: (ctx, id) => {
194
+ var _a;
195
+ return ((_a = ctx.ids) == null ? void 0 : _a.trigger) ?? `tabs:${ctx.id}:trigger-${id}`;
196
+ },
197
+ getIndicatorId: (ctx) => `tabs:${ctx.id}:indicator`,
198
+ getTriggerGroupEl: (ctx) => dom.getById(ctx, dom.getTriggerGroupId(ctx)),
199
+ getContentEl: (ctx, id) => dom.getById(ctx, dom.getContentId(ctx, id)),
200
+ getTriggerEl: (ctx, id) => dom.getById(ctx, dom.getTriggerId(ctx, id)),
201
+ getIndicatorEl: (ctx) => dom.getById(ctx, dom.getIndicatorId(ctx)),
202
+ getElements: (ctx) => {
203
+ const ownerId = CSS.escape(dom.getTriggerGroupId(ctx));
204
+ const selector = `[role=tab][data-ownedby='${ownerId}']:not([disabled])`;
205
+ return queryAll(dom.getTriggerGroupEl(ctx), selector);
206
+ },
207
+ getFirstEl: (ctx) => first(dom.getElements(ctx)),
208
+ getLastEl: (ctx) => last(dom.getElements(ctx)),
209
+ getNextEl: (ctx, id) => nextById(dom.getElements(ctx), dom.getTriggerId(ctx, id), ctx.loop),
210
+ getPrevEl: (ctx, id) => prevById(dom.getElements(ctx), dom.getTriggerId(ctx, id), ctx.loop),
211
+ getActiveContentEl: (ctx) => {
212
+ if (!ctx.value)
213
+ return;
214
+ const id = dom.getContentId(ctx, ctx.value);
215
+ return dom.getById(ctx, id);
216
+ },
217
+ getRectById: (ctx, id) => {
218
+ const empty = {
219
+ offsetLeft: 0,
220
+ offsetTop: 0,
221
+ offsetWidth: 0,
222
+ offsetHeight: 0
223
+ };
224
+ const tab = itemById(dom.getElements(ctx), dom.getTriggerId(ctx, id)) ?? empty;
225
+ if (ctx.isVertical) {
226
+ return {
227
+ top: `${tab.offsetTop}px`,
228
+ height: `${tab.offsetHeight}px`
229
+ };
230
+ }
231
+ return {
232
+ left: `${tab.offsetLeft}px`,
233
+ width: `${tab.offsetWidth}px`
234
+ };
235
+ }
236
+ });
237
+
238
+ // src/tabs.connect.ts
239
+ function connect(state, send, normalize) {
240
+ const messages = state.context.messages;
241
+ const isFocused = state.matches("focused");
242
+ return {
243
+ value: state.context.value,
244
+ focusedValue: state.context.focusedValue,
245
+ previousValues: Array.from(state.context.previousValues),
246
+ setValue(value) {
247
+ send({ type: "SET_VALUE", value });
248
+ },
249
+ rootProps: normalize.element({
250
+ "data-part": "root",
251
+ id: dom.getRootId(state.context),
252
+ "data-orientation": state.context.orientation,
253
+ "data-focus": dataAttr(isFocused),
254
+ dir: state.context.dir
255
+ }),
256
+ triggerGroupProps: normalize.element({
257
+ "data-part": "trigger-group",
258
+ id: dom.getTriggerGroupId(state.context),
259
+ role: "tablist",
260
+ "data-focus": dataAttr(isFocused),
261
+ "aria-orientation": state.context.orientation,
262
+ "data-orientation": state.context.orientation,
263
+ "aria-label": messages.tablistLabel,
264
+ onKeyDown(event) {
265
+ const keyMap = {
266
+ ArrowDown() {
267
+ send("ARROW_DOWN");
268
+ },
269
+ ArrowUp() {
270
+ send("ARROW_UP");
271
+ },
272
+ ArrowLeft() {
273
+ send("ARROW_LEFT");
274
+ },
275
+ ArrowRight() {
276
+ send("ARROW_RIGHT");
277
+ },
278
+ Home() {
279
+ send("HOME");
280
+ },
281
+ End() {
282
+ send("END");
283
+ },
284
+ Enter() {
285
+ send({ type: "ENTER", value: state.context.focusedValue });
286
+ }
287
+ };
288
+ let key = getEventKey(event, state.context);
289
+ const exec = keyMap[key];
290
+ if (exec) {
291
+ event.preventDefault();
292
+ exec(event);
293
+ }
294
+ }
295
+ }),
296
+ getTriggerProps(props) {
297
+ const { value, disabled } = props;
298
+ const selected = state.context.value === value;
299
+ return normalize.button({
300
+ "data-part": "trigger",
301
+ role: "tab",
302
+ type: "button",
303
+ disabled,
304
+ "data-orientation": state.context.orientation,
305
+ "data-disabled": dataAttr(disabled),
306
+ "aria-disabled": disabled,
307
+ "data-value": value,
308
+ "aria-selected": selected,
309
+ "data-selected": dataAttr(selected),
310
+ "aria-controls": dom.getContentId(state.context, value),
311
+ "data-ownedby": dom.getTriggerGroupId(state.context),
312
+ id: dom.getTriggerId(state.context, value),
313
+ tabIndex: selected ? 0 : -1,
314
+ onFocus() {
315
+ send({ type: "TAB_FOCUS", value });
316
+ },
317
+ onBlur(event) {
318
+ const target = event.relatedTarget;
319
+ if ((target == null ? void 0 : target.getAttribute("role")) !== "tab") {
320
+ send({ type: "TAB_BLUR" });
321
+ }
322
+ },
323
+ onClick(event) {
324
+ if (disabled)
325
+ return;
326
+ if (isSafari()) {
327
+ event.currentTarget.focus();
328
+ }
329
+ send({ type: "TAB_CLICK", value });
330
+ }
331
+ });
332
+ },
333
+ contentGroupProps: normalize.element({
334
+ "data-part": "content-group",
335
+ id: dom.getContentGroupId(state.context),
336
+ "data-orientation": state.context.orientation
337
+ }),
338
+ getContentProps({ value }) {
339
+ const selected = state.context.value === value;
340
+ return normalize.element({
341
+ "data-part": "content",
342
+ id: dom.getContentId(state.context, value),
343
+ tabIndex: 0,
344
+ "aria-labelledby": dom.getTriggerId(state.context, value),
345
+ role: "tabpanel",
346
+ "data-ownedby": dom.getTriggerGroupId(state.context),
347
+ hidden: !selected
348
+ });
349
+ },
350
+ getDeleteButtonProps({ value, disabled }) {
351
+ var _a;
352
+ return normalize.button({
353
+ "data-part": "delete-button",
354
+ type: "button",
355
+ tabIndex: -1,
356
+ "aria-label": (_a = messages.deleteLabel) == null ? void 0 : _a.call(messages, value),
357
+ disabled,
358
+ onClick() {
359
+ send({ type: "DELETE", value });
360
+ }
361
+ });
362
+ },
363
+ indicatorProps: normalize.element({
364
+ id: dom.getIndicatorId(state.context),
365
+ "data-part": "indicator",
366
+ "data-orientation": state.context.orientation,
367
+ style: {
368
+ "--transition-duration": "200ms",
369
+ "--transition-property": "left, right, top, bottom, width, height",
370
+ position: "absolute",
371
+ willChange: "var(--transition-property)",
372
+ transitionProperty: "var(--transition-property)",
373
+ transitionDuration: state.context.hasMeasuredRect ? "var(--transition-duration)" : "0ms",
374
+ transitionTimingFunction: "var(--transition-timing-function)",
375
+ ...state.context.indicatorRect
376
+ }
377
+ })
378
+ };
379
+ }
380
+
381
+ // src/tabs.machine.ts
382
+ var import_core = require("@zag-js/core");
383
+ var { not } = import_core.guards;
384
+ function machine(ctx) {
385
+ return (0, import_core.createMachine)(
386
+ {
387
+ initial: "unknown",
388
+ context: {
389
+ dir: "ltr",
390
+ orientation: "horizontal",
391
+ activationMode: "automatic",
392
+ value: null,
393
+ focusedValue: null,
394
+ previousValues: [],
395
+ indicatorRect: { left: "0px", top: "0px", width: "0px", height: "0px" },
396
+ hasMeasuredRect: false,
397
+ isIndicatorRendered: false,
398
+ loop: true,
399
+ messages: {},
400
+ ...ctx
401
+ },
402
+ computed: {
403
+ isHorizontal: (ctx2) => ctx2.orientation === "horizontal",
404
+ isVertical: (ctx2) => ctx2.orientation === "vertical"
405
+ },
406
+ created: ["setPrevSelectedTabs"],
407
+ watch: {
408
+ focusedValue: "invokeOnFocus",
409
+ value: ["invokeOnChange", "setPrevSelectedTabs", "setIndicatorRect", "setContentTabIndex"],
410
+ dir: ["clearMeasured", "setIndicatorRect"]
411
+ },
412
+ on: {
413
+ SET_VALUE: {
414
+ actions: "setValue"
415
+ },
416
+ DELETE: {
417
+ actions: "deleteValue"
418
+ }
419
+ },
420
+ states: {
421
+ unknown: {
422
+ on: {
423
+ SETUP: {
424
+ target: "idle",
425
+ actions: ["checkRenderedElements", "setIndicatorRect", "setContentTabIndex"]
426
+ }
427
+ }
428
+ },
429
+ idle: {
430
+ on: {
431
+ TAB_FOCUS: {
432
+ guard: "selectOnFocus",
433
+ target: "focused",
434
+ actions: ["setFocusedValue", "setValue"]
435
+ },
436
+ TAB_CLICK: {
437
+ target: "focused",
438
+ actions: ["setFocusedValue", "setValue"]
439
+ }
440
+ }
441
+ },
442
+ focused: {
443
+ on: {
444
+ TAB_CLICK: {
445
+ target: "focused",
446
+ actions: ["setFocusedValue", "setValue"]
447
+ },
448
+ ARROW_LEFT: {
449
+ guard: "isHorizontal",
450
+ actions: "focusPrevTab"
451
+ },
452
+ ARROW_RIGHT: {
453
+ guard: "isHorizontal",
454
+ actions: "focusNextTab"
455
+ },
456
+ ARROW_UP: {
457
+ guard: "isVertical",
458
+ actions: "focusPrevTab"
459
+ },
460
+ ARROW_DOWN: {
461
+ guard: "isVertical",
462
+ actions: "focusNextTab"
463
+ },
464
+ HOME: {
465
+ actions: "focusFirstTab"
466
+ },
467
+ END: {
468
+ actions: "focusLastTab"
469
+ },
470
+ ENTER: {
471
+ guard: not("selectOnFocus"),
472
+ actions: "setValue"
473
+ },
474
+ TAB_FOCUS: [
475
+ {
476
+ guard: "selectOnFocus",
477
+ actions: ["setFocusedValue", "setValue"]
478
+ },
479
+ { actions: "setFocusedValue" }
480
+ ],
481
+ TAB_BLUR: {
482
+ target: "idle",
483
+ actions: "clearFocusedValue"
484
+ }
485
+ }
486
+ }
487
+ }
488
+ },
489
+ {
490
+ guards: {
491
+ isVertical: (ctx2) => ctx2.isVertical,
492
+ isHorizontal: (ctx2) => ctx2.isHorizontal,
493
+ selectOnFocus: (ctx2) => ctx2.activationMode === "automatic"
494
+ },
495
+ actions: {
496
+ setFocusedValue(ctx2, evt) {
497
+ ctx2.focusedValue = evt.value;
498
+ },
499
+ clearFocusedValue(ctx2) {
500
+ ctx2.focusedValue = null;
501
+ },
502
+ setValue(ctx2, evt) {
503
+ ctx2.value = evt.value;
504
+ },
505
+ invokeOnDelete(ctx2, evt) {
506
+ var _a;
507
+ (_a = ctx2.onDelete) == null ? void 0 : _a.call(ctx2, { value: evt.value });
508
+ },
509
+ focusFirstTab(ctx2) {
510
+ raf(() => {
511
+ var _a;
512
+ return (_a = dom.getFirstEl(ctx2)) == null ? void 0 : _a.focus();
513
+ });
514
+ },
515
+ focusLastTab(ctx2) {
516
+ raf(() => {
517
+ var _a;
518
+ return (_a = dom.getLastEl(ctx2)) == null ? void 0 : _a.focus();
519
+ });
520
+ },
521
+ focusNextTab(ctx2) {
522
+ if (!ctx2.focusedValue)
523
+ return;
524
+ const next = dom.getNextEl(ctx2, ctx2.focusedValue);
525
+ raf(() => next == null ? void 0 : next.focus());
526
+ },
527
+ focusPrevTab(ctx2) {
528
+ if (!ctx2.focusedValue)
529
+ return;
530
+ const prev = dom.getPrevEl(ctx2, ctx2.focusedValue);
531
+ raf(() => prev == null ? void 0 : prev.focus());
532
+ },
533
+ setIndicatorRect(ctx2) {
534
+ nextTick(() => {
535
+ if (!ctx2.isIndicatorRendered || !ctx2.value)
536
+ return;
537
+ ctx2.indicatorRect = dom.getRectById(ctx2, ctx2.value);
538
+ if (ctx2.hasMeasuredRect)
539
+ return;
540
+ nextTick(() => {
541
+ ctx2.hasMeasuredRect = true;
542
+ });
543
+ });
544
+ },
545
+ checkRenderedElements(ctx2) {
546
+ ctx2.isIndicatorRendered = !!dom.getIndicatorEl(ctx2);
547
+ },
548
+ clearMeasured(ctx2) {
549
+ ctx2.hasMeasuredRect = false;
550
+ },
551
+ invokeOnChange(ctx2) {
552
+ var _a;
553
+ (_a = ctx2.onChange) == null ? void 0 : _a.call(ctx2, { value: ctx2.value });
554
+ },
555
+ invokeOnFocus(ctx2) {
556
+ var _a;
557
+ (_a = ctx2.onFocus) == null ? void 0 : _a.call(ctx2, { value: ctx2.focusedValue });
558
+ },
559
+ setPrevSelectedTabs(ctx2) {
560
+ if (ctx2.value != null) {
561
+ const newSelected = Array.from(ctx2.previousValues).concat(ctx2.value);
562
+ ctx2.previousValues = Array.from(new Set(newSelected));
563
+ }
564
+ },
565
+ setContentTabIndex(ctx2) {
566
+ raf(() => {
567
+ const panel = dom.getActiveContentEl(ctx2);
568
+ if (!panel)
569
+ return;
570
+ const focusables = getFocusables(panel);
571
+ if (focusables.length > 0) {
572
+ panel.removeAttribute("tabindex");
573
+ } else {
574
+ panel.setAttribute("tabindex", "0");
575
+ }
576
+ });
577
+ }
578
+ }
579
+ }
580
+ );
581
+ }
582
+ // Annotate the CommonJS export names for ESM import in node:
583
+ 0 && (module.exports = {
584
+ connect,
585
+ machine
586
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,558 @@
1
+ // ../../utilities/dom/dist/index.mjs
2
+ var dataAttr = (guard) => {
3
+ return guard ? "" : void 0;
4
+ };
5
+ var isDom = () => typeof window !== "undefined";
6
+ function getPlatform() {
7
+ const agent = navigator.userAgentData;
8
+ return (agent == null ? void 0 : agent.platform) ?? navigator.platform;
9
+ }
10
+ var pt = (v) => isDom() && v.test(getPlatform());
11
+ var vn = (v) => isDom() && v.test(navigator.vendor);
12
+ var isSafari = () => isApple() && vn(/apple/i);
13
+ var isApple = () => pt(/mac|iphone|ipad|ipod/i);
14
+ function isDocument(el) {
15
+ return el.nodeType === Node.DOCUMENT_NODE;
16
+ }
17
+ function isWindow(value) {
18
+ return (value == null ? void 0 : value.toString()) === "[object Window]";
19
+ }
20
+ function isFrame(element) {
21
+ return element.localName === "iframe";
22
+ }
23
+ function getDocument(el) {
24
+ if (isWindow(el))
25
+ return el.document;
26
+ if (isDocument(el))
27
+ return el;
28
+ return (el == null ? void 0 : el.ownerDocument) ?? document;
29
+ }
30
+ function defineDomHelpers(helpers) {
31
+ const dom2 = {
32
+ getRootNode: (ctx) => {
33
+ var _a;
34
+ return ((_a = ctx.getRootNode) == null ? void 0 : _a.call(ctx)) ?? document;
35
+ },
36
+ getDoc: (ctx) => getDocument(dom2.getRootNode(ctx)),
37
+ getWin: (ctx) => dom2.getDoc(ctx).defaultView ?? window,
38
+ getActiveElement: (ctx) => dom2.getDoc(ctx).activeElement,
39
+ getById: (ctx, id) => dom2.getRootNode(ctx).getElementById(id)
40
+ };
41
+ return {
42
+ ...dom2,
43
+ ...helpers
44
+ };
45
+ }
46
+ function isHTMLElement(v) {
47
+ return typeof v === "object" && (v == null ? void 0 : v.nodeType) === Node.ELEMENT_NODE && typeof (v == null ? void 0 : v.nodeName) === "string";
48
+ }
49
+ function isVisible(el) {
50
+ if (!isHTMLElement(el))
51
+ return false;
52
+ return el.offsetWidth > 0 || el.offsetHeight > 0 || el.getClientRects().length > 0;
53
+ }
54
+ var focusableSelector = "input:not([type='hidden']):not([disabled]), select:not([disabled]), textarea:not([disabled]), a[href], button:not([disabled]), [tabindex], iframe, object, embed, area[href], audio[controls], video[controls], [contenteditable]:not([contenteditable='false']), details > summary:first-of-type";
55
+ var getFocusables = (container, includeContainer = false) => {
56
+ if (!container)
57
+ return [];
58
+ const elements = Array.from(container.querySelectorAll(focusableSelector));
59
+ const include = includeContainer == true || includeContainer == "if-empty" && elements.length === 0;
60
+ if (include && isHTMLElement(container) && isFocusable(container)) {
61
+ elements.unshift(container);
62
+ }
63
+ const focusableElements = elements.filter(isFocusable);
64
+ focusableElements.forEach((element, i) => {
65
+ if (isFrame(element) && element.contentDocument) {
66
+ const frameBody = element.contentDocument.body;
67
+ focusableElements.splice(i, 1, ...getFocusables(frameBody));
68
+ }
69
+ });
70
+ return focusableElements;
71
+ };
72
+ function isFocusable(element) {
73
+ if (!element)
74
+ return false;
75
+ return element.matches(focusableSelector) && isVisible(element);
76
+ }
77
+ var rtlKeyMap = {
78
+ ArrowLeft: "ArrowRight",
79
+ ArrowRight: "ArrowLeft",
80
+ Home: "End",
81
+ End: "Home"
82
+ };
83
+ var sameKeyMap = {
84
+ Up: "ArrowUp",
85
+ Down: "ArrowDown",
86
+ Esc: "Escape",
87
+ " ": "Space",
88
+ ",": "Comma",
89
+ Left: "ArrowLeft",
90
+ Right: "ArrowRight"
91
+ };
92
+ function getEventKey(event, options = {}) {
93
+ const { dir = "ltr", orientation = "horizontal" } = options;
94
+ let { key } = event;
95
+ key = sameKeyMap[key] ?? key;
96
+ const isRtl = dir === "rtl" && orientation === "horizontal";
97
+ if (isRtl && key in rtlKeyMap) {
98
+ key = rtlKeyMap[key];
99
+ }
100
+ return key;
101
+ }
102
+ function nextTick(fn) {
103
+ const set = /* @__PURE__ */ new Set();
104
+ function raf2(fn2) {
105
+ const id = globalThis.requestAnimationFrame(fn2);
106
+ set.add(() => globalThis.cancelAnimationFrame(id));
107
+ }
108
+ raf2(() => raf2(fn));
109
+ return function cleanup() {
110
+ set.forEach(function(fn2) {
111
+ fn2();
112
+ });
113
+ };
114
+ }
115
+ function raf(fn) {
116
+ const id = globalThis.requestAnimationFrame(fn);
117
+ return function cleanup() {
118
+ globalThis.cancelAnimationFrame(id);
119
+ };
120
+ }
121
+ function queryAll(root, selector) {
122
+ return Array.from((root == null ? void 0 : root.querySelectorAll(selector)) ?? []);
123
+ }
124
+ function itemById(v, id) {
125
+ return v.find((node) => node.id === id);
126
+ }
127
+ function indexOfId(v, id) {
128
+ const item = itemById(v, id);
129
+ return item ? v.indexOf(item) : -1;
130
+ }
131
+ function nextById(v, id, loop = true) {
132
+ let idx = indexOfId(v, id);
133
+ idx = loop ? (idx + 1) % v.length : Math.min(idx + 1, v.length - 1);
134
+ return v[idx];
135
+ }
136
+ function prevById(v, id, loop = true) {
137
+ let idx = indexOfId(v, id);
138
+ if (idx === -1)
139
+ return loop ? v[v.length - 1] : null;
140
+ idx = loop ? (idx - 1 + v.length) % v.length : Math.max(0, idx - 1);
141
+ return v[idx];
142
+ }
143
+
144
+ // ../../utilities/core/dist/index.mjs
145
+ var first = (v) => v[0];
146
+ var last = (v) => v[v.length - 1];
147
+
148
+ // src/tabs.dom.ts
149
+ var dom = defineDomHelpers({
150
+ getRootId: (ctx) => {
151
+ var _a;
152
+ return ((_a = ctx.ids) == null ? void 0 : _a.root) ?? `tabs:${ctx.id}`;
153
+ },
154
+ getTriggerGroupId: (ctx) => {
155
+ var _a;
156
+ return ((_a = ctx.ids) == null ? void 0 : _a.triggerGroup) ?? `tabs:${ctx.id}:trigger-group`;
157
+ },
158
+ getContentId: (ctx, id) => {
159
+ var _a;
160
+ return ((_a = ctx.ids) == null ? void 0 : _a.content) ?? `tabs:${ctx.id}:content-${id}`;
161
+ },
162
+ getContentGroupId: (ctx) => {
163
+ var _a;
164
+ return ((_a = ctx.ids) == null ? void 0 : _a.contentGroup) ?? `tabs:${ctx.id}:content-group`;
165
+ },
166
+ getTriggerId: (ctx, id) => {
167
+ var _a;
168
+ return ((_a = ctx.ids) == null ? void 0 : _a.trigger) ?? `tabs:${ctx.id}:trigger-${id}`;
169
+ },
170
+ getIndicatorId: (ctx) => `tabs:${ctx.id}:indicator`,
171
+ getTriggerGroupEl: (ctx) => dom.getById(ctx, dom.getTriggerGroupId(ctx)),
172
+ getContentEl: (ctx, id) => dom.getById(ctx, dom.getContentId(ctx, id)),
173
+ getTriggerEl: (ctx, id) => dom.getById(ctx, dom.getTriggerId(ctx, id)),
174
+ getIndicatorEl: (ctx) => dom.getById(ctx, dom.getIndicatorId(ctx)),
175
+ getElements: (ctx) => {
176
+ const ownerId = CSS.escape(dom.getTriggerGroupId(ctx));
177
+ const selector = `[role=tab][data-ownedby='${ownerId}']:not([disabled])`;
178
+ return queryAll(dom.getTriggerGroupEl(ctx), selector);
179
+ },
180
+ getFirstEl: (ctx) => first(dom.getElements(ctx)),
181
+ getLastEl: (ctx) => last(dom.getElements(ctx)),
182
+ getNextEl: (ctx, id) => nextById(dom.getElements(ctx), dom.getTriggerId(ctx, id), ctx.loop),
183
+ getPrevEl: (ctx, id) => prevById(dom.getElements(ctx), dom.getTriggerId(ctx, id), ctx.loop),
184
+ getActiveContentEl: (ctx) => {
185
+ if (!ctx.value)
186
+ return;
187
+ const id = dom.getContentId(ctx, ctx.value);
188
+ return dom.getById(ctx, id);
189
+ },
190
+ getRectById: (ctx, id) => {
191
+ const empty = {
192
+ offsetLeft: 0,
193
+ offsetTop: 0,
194
+ offsetWidth: 0,
195
+ offsetHeight: 0
196
+ };
197
+ const tab = itemById(dom.getElements(ctx), dom.getTriggerId(ctx, id)) ?? empty;
198
+ if (ctx.isVertical) {
199
+ return {
200
+ top: `${tab.offsetTop}px`,
201
+ height: `${tab.offsetHeight}px`
202
+ };
203
+ }
204
+ return {
205
+ left: `${tab.offsetLeft}px`,
206
+ width: `${tab.offsetWidth}px`
207
+ };
208
+ }
209
+ });
210
+
211
+ // src/tabs.connect.ts
212
+ function connect(state, send, normalize) {
213
+ const messages = state.context.messages;
214
+ const isFocused = state.matches("focused");
215
+ return {
216
+ value: state.context.value,
217
+ focusedValue: state.context.focusedValue,
218
+ previousValues: Array.from(state.context.previousValues),
219
+ setValue(value) {
220
+ send({ type: "SET_VALUE", value });
221
+ },
222
+ rootProps: normalize.element({
223
+ "data-part": "root",
224
+ id: dom.getRootId(state.context),
225
+ "data-orientation": state.context.orientation,
226
+ "data-focus": dataAttr(isFocused),
227
+ dir: state.context.dir
228
+ }),
229
+ triggerGroupProps: normalize.element({
230
+ "data-part": "trigger-group",
231
+ id: dom.getTriggerGroupId(state.context),
232
+ role: "tablist",
233
+ "data-focus": dataAttr(isFocused),
234
+ "aria-orientation": state.context.orientation,
235
+ "data-orientation": state.context.orientation,
236
+ "aria-label": messages.tablistLabel,
237
+ onKeyDown(event) {
238
+ const keyMap = {
239
+ ArrowDown() {
240
+ send("ARROW_DOWN");
241
+ },
242
+ ArrowUp() {
243
+ send("ARROW_UP");
244
+ },
245
+ ArrowLeft() {
246
+ send("ARROW_LEFT");
247
+ },
248
+ ArrowRight() {
249
+ send("ARROW_RIGHT");
250
+ },
251
+ Home() {
252
+ send("HOME");
253
+ },
254
+ End() {
255
+ send("END");
256
+ },
257
+ Enter() {
258
+ send({ type: "ENTER", value: state.context.focusedValue });
259
+ }
260
+ };
261
+ let key = getEventKey(event, state.context);
262
+ const exec = keyMap[key];
263
+ if (exec) {
264
+ event.preventDefault();
265
+ exec(event);
266
+ }
267
+ }
268
+ }),
269
+ getTriggerProps(props) {
270
+ const { value, disabled } = props;
271
+ const selected = state.context.value === value;
272
+ return normalize.button({
273
+ "data-part": "trigger",
274
+ role: "tab",
275
+ type: "button",
276
+ disabled,
277
+ "data-orientation": state.context.orientation,
278
+ "data-disabled": dataAttr(disabled),
279
+ "aria-disabled": disabled,
280
+ "data-value": value,
281
+ "aria-selected": selected,
282
+ "data-selected": dataAttr(selected),
283
+ "aria-controls": dom.getContentId(state.context, value),
284
+ "data-ownedby": dom.getTriggerGroupId(state.context),
285
+ id: dom.getTriggerId(state.context, value),
286
+ tabIndex: selected ? 0 : -1,
287
+ onFocus() {
288
+ send({ type: "TAB_FOCUS", value });
289
+ },
290
+ onBlur(event) {
291
+ const target = event.relatedTarget;
292
+ if ((target == null ? void 0 : target.getAttribute("role")) !== "tab") {
293
+ send({ type: "TAB_BLUR" });
294
+ }
295
+ },
296
+ onClick(event) {
297
+ if (disabled)
298
+ return;
299
+ if (isSafari()) {
300
+ event.currentTarget.focus();
301
+ }
302
+ send({ type: "TAB_CLICK", value });
303
+ }
304
+ });
305
+ },
306
+ contentGroupProps: normalize.element({
307
+ "data-part": "content-group",
308
+ id: dom.getContentGroupId(state.context),
309
+ "data-orientation": state.context.orientation
310
+ }),
311
+ getContentProps({ value }) {
312
+ const selected = state.context.value === value;
313
+ return normalize.element({
314
+ "data-part": "content",
315
+ id: dom.getContentId(state.context, value),
316
+ tabIndex: 0,
317
+ "aria-labelledby": dom.getTriggerId(state.context, value),
318
+ role: "tabpanel",
319
+ "data-ownedby": dom.getTriggerGroupId(state.context),
320
+ hidden: !selected
321
+ });
322
+ },
323
+ getDeleteButtonProps({ value, disabled }) {
324
+ var _a;
325
+ return normalize.button({
326
+ "data-part": "delete-button",
327
+ type: "button",
328
+ tabIndex: -1,
329
+ "aria-label": (_a = messages.deleteLabel) == null ? void 0 : _a.call(messages, value),
330
+ disabled,
331
+ onClick() {
332
+ send({ type: "DELETE", value });
333
+ }
334
+ });
335
+ },
336
+ indicatorProps: normalize.element({
337
+ id: dom.getIndicatorId(state.context),
338
+ "data-part": "indicator",
339
+ "data-orientation": state.context.orientation,
340
+ style: {
341
+ "--transition-duration": "200ms",
342
+ "--transition-property": "left, right, top, bottom, width, height",
343
+ position: "absolute",
344
+ willChange: "var(--transition-property)",
345
+ transitionProperty: "var(--transition-property)",
346
+ transitionDuration: state.context.hasMeasuredRect ? "var(--transition-duration)" : "0ms",
347
+ transitionTimingFunction: "var(--transition-timing-function)",
348
+ ...state.context.indicatorRect
349
+ }
350
+ })
351
+ };
352
+ }
353
+
354
+ // src/tabs.machine.ts
355
+ import { createMachine, guards } from "@zag-js/core";
356
+ var { not } = guards;
357
+ function machine(ctx) {
358
+ return createMachine(
359
+ {
360
+ initial: "unknown",
361
+ context: {
362
+ dir: "ltr",
363
+ orientation: "horizontal",
364
+ activationMode: "automatic",
365
+ value: null,
366
+ focusedValue: null,
367
+ previousValues: [],
368
+ indicatorRect: { left: "0px", top: "0px", width: "0px", height: "0px" },
369
+ hasMeasuredRect: false,
370
+ isIndicatorRendered: false,
371
+ loop: true,
372
+ messages: {},
373
+ ...ctx
374
+ },
375
+ computed: {
376
+ isHorizontal: (ctx2) => ctx2.orientation === "horizontal",
377
+ isVertical: (ctx2) => ctx2.orientation === "vertical"
378
+ },
379
+ created: ["setPrevSelectedTabs"],
380
+ watch: {
381
+ focusedValue: "invokeOnFocus",
382
+ value: ["invokeOnChange", "setPrevSelectedTabs", "setIndicatorRect", "setContentTabIndex"],
383
+ dir: ["clearMeasured", "setIndicatorRect"]
384
+ },
385
+ on: {
386
+ SET_VALUE: {
387
+ actions: "setValue"
388
+ },
389
+ DELETE: {
390
+ actions: "deleteValue"
391
+ }
392
+ },
393
+ states: {
394
+ unknown: {
395
+ on: {
396
+ SETUP: {
397
+ target: "idle",
398
+ actions: ["checkRenderedElements", "setIndicatorRect", "setContentTabIndex"]
399
+ }
400
+ }
401
+ },
402
+ idle: {
403
+ on: {
404
+ TAB_FOCUS: {
405
+ guard: "selectOnFocus",
406
+ target: "focused",
407
+ actions: ["setFocusedValue", "setValue"]
408
+ },
409
+ TAB_CLICK: {
410
+ target: "focused",
411
+ actions: ["setFocusedValue", "setValue"]
412
+ }
413
+ }
414
+ },
415
+ focused: {
416
+ on: {
417
+ TAB_CLICK: {
418
+ target: "focused",
419
+ actions: ["setFocusedValue", "setValue"]
420
+ },
421
+ ARROW_LEFT: {
422
+ guard: "isHorizontal",
423
+ actions: "focusPrevTab"
424
+ },
425
+ ARROW_RIGHT: {
426
+ guard: "isHorizontal",
427
+ actions: "focusNextTab"
428
+ },
429
+ ARROW_UP: {
430
+ guard: "isVertical",
431
+ actions: "focusPrevTab"
432
+ },
433
+ ARROW_DOWN: {
434
+ guard: "isVertical",
435
+ actions: "focusNextTab"
436
+ },
437
+ HOME: {
438
+ actions: "focusFirstTab"
439
+ },
440
+ END: {
441
+ actions: "focusLastTab"
442
+ },
443
+ ENTER: {
444
+ guard: not("selectOnFocus"),
445
+ actions: "setValue"
446
+ },
447
+ TAB_FOCUS: [
448
+ {
449
+ guard: "selectOnFocus",
450
+ actions: ["setFocusedValue", "setValue"]
451
+ },
452
+ { actions: "setFocusedValue" }
453
+ ],
454
+ TAB_BLUR: {
455
+ target: "idle",
456
+ actions: "clearFocusedValue"
457
+ }
458
+ }
459
+ }
460
+ }
461
+ },
462
+ {
463
+ guards: {
464
+ isVertical: (ctx2) => ctx2.isVertical,
465
+ isHorizontal: (ctx2) => ctx2.isHorizontal,
466
+ selectOnFocus: (ctx2) => ctx2.activationMode === "automatic"
467
+ },
468
+ actions: {
469
+ setFocusedValue(ctx2, evt) {
470
+ ctx2.focusedValue = evt.value;
471
+ },
472
+ clearFocusedValue(ctx2) {
473
+ ctx2.focusedValue = null;
474
+ },
475
+ setValue(ctx2, evt) {
476
+ ctx2.value = evt.value;
477
+ },
478
+ invokeOnDelete(ctx2, evt) {
479
+ var _a;
480
+ (_a = ctx2.onDelete) == null ? void 0 : _a.call(ctx2, { value: evt.value });
481
+ },
482
+ focusFirstTab(ctx2) {
483
+ raf(() => {
484
+ var _a;
485
+ return (_a = dom.getFirstEl(ctx2)) == null ? void 0 : _a.focus();
486
+ });
487
+ },
488
+ focusLastTab(ctx2) {
489
+ raf(() => {
490
+ var _a;
491
+ return (_a = dom.getLastEl(ctx2)) == null ? void 0 : _a.focus();
492
+ });
493
+ },
494
+ focusNextTab(ctx2) {
495
+ if (!ctx2.focusedValue)
496
+ return;
497
+ const next = dom.getNextEl(ctx2, ctx2.focusedValue);
498
+ raf(() => next == null ? void 0 : next.focus());
499
+ },
500
+ focusPrevTab(ctx2) {
501
+ if (!ctx2.focusedValue)
502
+ return;
503
+ const prev = dom.getPrevEl(ctx2, ctx2.focusedValue);
504
+ raf(() => prev == null ? void 0 : prev.focus());
505
+ },
506
+ setIndicatorRect(ctx2) {
507
+ nextTick(() => {
508
+ if (!ctx2.isIndicatorRendered || !ctx2.value)
509
+ return;
510
+ ctx2.indicatorRect = dom.getRectById(ctx2, ctx2.value);
511
+ if (ctx2.hasMeasuredRect)
512
+ return;
513
+ nextTick(() => {
514
+ ctx2.hasMeasuredRect = true;
515
+ });
516
+ });
517
+ },
518
+ checkRenderedElements(ctx2) {
519
+ ctx2.isIndicatorRendered = !!dom.getIndicatorEl(ctx2);
520
+ },
521
+ clearMeasured(ctx2) {
522
+ ctx2.hasMeasuredRect = false;
523
+ },
524
+ invokeOnChange(ctx2) {
525
+ var _a;
526
+ (_a = ctx2.onChange) == null ? void 0 : _a.call(ctx2, { value: ctx2.value });
527
+ },
528
+ invokeOnFocus(ctx2) {
529
+ var _a;
530
+ (_a = ctx2.onFocus) == null ? void 0 : _a.call(ctx2, { value: ctx2.focusedValue });
531
+ },
532
+ setPrevSelectedTabs(ctx2) {
533
+ if (ctx2.value != null) {
534
+ const newSelected = Array.from(ctx2.previousValues).concat(ctx2.value);
535
+ ctx2.previousValues = Array.from(new Set(newSelected));
536
+ }
537
+ },
538
+ setContentTabIndex(ctx2) {
539
+ raf(() => {
540
+ const panel = dom.getActiveContentEl(ctx2);
541
+ if (!panel)
542
+ return;
543
+ const focusables = getFocusables(panel);
544
+ if (focusables.length > 0) {
545
+ panel.removeAttribute("tabindex");
546
+ } else {
547
+ panel.setAttribute("tabindex", "0");
548
+ }
549
+ });
550
+ }
551
+ }
552
+ }
553
+ );
554
+ }
555
+ export {
556
+ connect,
557
+ machine
558
+ };
package/package.json ADDED
@@ -0,0 +1,49 @@
1
+ {
2
+ "name": "@zag-js/tabs",
3
+ "version": "0.0.0-20220813122512",
4
+ "description": "Core logic for the tabs widget implemented as a state machine",
5
+ "keywords": [
6
+ "js",
7
+ "machine",
8
+ "xstate",
9
+ "statechart",
10
+ "component",
11
+ "chakra-ui",
12
+ "tabs"
13
+ ],
14
+ "author": "Segun Adebayo <sage@adebayosegun.com>",
15
+ "homepage": "https://github.com/chakra-ui/zag#readme",
16
+ "license": "MIT",
17
+ "main": "dist/index.js",
18
+ "types": "dist/index.d.ts",
19
+ "module": "dist/index.mjs",
20
+ "repository": "https://github.com/chakra-ui/zag/tree/main/packages/tabs",
21
+ "sideEffects": false,
22
+ "files": [
23
+ "dist/**/*"
24
+ ],
25
+ "publishConfig": {
26
+ "access": "public"
27
+ },
28
+ "bugs": {
29
+ "url": "https://github.com/chakra-ui/zag/issues"
30
+ },
31
+ "dependencies": {
32
+ "@zag-js/core": "0.1.9",
33
+ "@zag-js/types": "0.0.0-20220813122512"
34
+ },
35
+ "devDependencies": {
36
+ "@zag-js/dom-utils": "0.0.0-20220813122512",
37
+ "@zag-js/utils": "0.1.3"
38
+ },
39
+ "scripts": {
40
+ "build-fast": "tsup src/index.ts --format=esm,cjs",
41
+ "start": "pnpm build --watch",
42
+ "build": "tsup src/index.ts --format=esm,cjs --dts",
43
+ "test": "jest --config ../../../jest.config.js --rootDir . --passWithNoTests",
44
+ "lint": "eslint src --ext .ts,.tsx",
45
+ "test-ci": "pnpm test --ci --runInBand",
46
+ "test-watch": "pnpm test --watch -u",
47
+ "typecheck": "tsc --noEmit"
48
+ }
49
+ }