@zag-js/tabs 1.34.1 → 1.35.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.
package/dist/index.mjs CHANGED
@@ -1,506 +1,10 @@
1
- import { createAnatomy } from '@zag-js/anatomy';
2
- import { isAnchorElement, resizeObserverBorderBox, raf, getFocusables, clickIfLink, queryAll, itemById, prevById, nextById, dataAttr, isOpeningInNewTab, isSafari, isComposingEvent, contains, getEventTarget, getEventKey } from '@zag-js/dom-query';
3
- import { callAll, createSplitProps, isEqual, last, first, toPx } from '@zag-js/utils';
4
- import { setup } from '@zag-js/core';
5
- import { createProps } from '@zag-js/types';
6
-
7
- // src/tabs.anatomy.ts
8
- var anatomy = createAnatomy("tabs").parts("root", "list", "trigger", "content", "indicator");
9
- var parts = anatomy.build();
10
- var getRootId = (ctx) => ctx.ids?.root ?? `tabs:${ctx.id}`;
11
- var getListId = (ctx) => ctx.ids?.list ?? `tabs:${ctx.id}:list`;
12
- var getContentId = (ctx, value) => ctx.ids?.content?.(value) ?? `tabs:${ctx.id}:content-${value}`;
13
- var getTriggerId = (ctx, value) => ctx.ids?.trigger?.(value) ?? `tabs:${ctx.id}:trigger-${value}`;
14
- var getIndicatorId = (ctx) => ctx.ids?.indicator ?? `tabs:${ctx.id}:indicator`;
15
- var getListEl = (ctx) => ctx.getById(getListId(ctx));
16
- var getContentEl = (ctx, value) => ctx.getById(getContentId(ctx, value));
17
- var getTriggerEl = (ctx, value) => value != null ? ctx.getById(getTriggerId(ctx, value)) : null;
18
- var getIndicatorEl = (ctx) => ctx.getById(getIndicatorId(ctx));
19
- var getElements = (ctx) => {
20
- const ownerId = CSS.escape(getListId(ctx));
21
- const selector = `[role=tab][data-ownedby='${ownerId}']:not([disabled])`;
22
- return queryAll(getListEl(ctx), selector);
1
+ // src/index.ts
2
+ import { anatomy } from "./tabs.anatomy.mjs";
3
+ import { connect } from "./tabs.connect.mjs";
4
+ import { machine } from "./tabs.machine.mjs";
5
+ export * from "./tabs.props.mjs";
6
+ export {
7
+ anatomy,
8
+ connect,
9
+ machine
23
10
  };
24
- var getFirstTriggerEl = (ctx) => first(getElements(ctx));
25
- var getLastTriggerEl = (ctx) => last(getElements(ctx));
26
- var getNextTriggerEl = (ctx, opts) => nextById(getElements(ctx), getTriggerId(ctx, opts.value), opts.loopFocus);
27
- var getPrevTriggerEl = (ctx, opts) => prevById(getElements(ctx), getTriggerId(ctx, opts.value), opts.loopFocus);
28
- var getOffsetRect = (el) => ({
29
- x: el?.offsetLeft ?? 0,
30
- y: el?.offsetTop ?? 0,
31
- width: el?.offsetWidth ?? 0,
32
- height: el?.offsetHeight ?? 0
33
- });
34
- var getRectByValue = (ctx, value) => {
35
- const tab = itemById(getElements(ctx), getTriggerId(ctx, value));
36
- return getOffsetRect(tab);
37
- };
38
-
39
- // src/tabs.connect.ts
40
- function connect(service, normalize) {
41
- const { state, send, context, prop, scope } = service;
42
- const translations = prop("translations");
43
- const focused = state.matches("focused");
44
- const isVertical = prop("orientation") === "vertical";
45
- const isHorizontal = prop("orientation") === "horizontal";
46
- const composite = prop("composite");
47
- function getTriggerState(props2) {
48
- return {
49
- selected: context.get("value") === props2.value,
50
- focused: context.get("focusedValue") === props2.value,
51
- disabled: !!props2.disabled
52
- };
53
- }
54
- return {
55
- value: context.get("value"),
56
- focusedValue: context.get("focusedValue"),
57
- setValue(value) {
58
- send({ type: "SET_VALUE", value });
59
- },
60
- clearValue() {
61
- send({ type: "CLEAR_VALUE" });
62
- },
63
- setIndicatorRect(value) {
64
- const id = getTriggerId(scope, value);
65
- send({ type: "SET_INDICATOR_RECT", id });
66
- },
67
- syncTabIndex() {
68
- send({ type: "SYNC_TAB_INDEX" });
69
- },
70
- selectNext(fromValue) {
71
- send({ type: "TAB_FOCUS", value: fromValue, src: "selectNext" });
72
- send({ type: "ARROW_NEXT", src: "selectNext" });
73
- },
74
- selectPrev(fromValue) {
75
- send({ type: "TAB_FOCUS", value: fromValue, src: "selectPrev" });
76
- send({ type: "ARROW_PREV", src: "selectPrev" });
77
- },
78
- focus() {
79
- const value = context.get("value");
80
- if (!value) return;
81
- getTriggerEl(scope, value)?.focus();
82
- },
83
- getRootProps() {
84
- return normalize.element({
85
- ...parts.root.attrs,
86
- id: getRootId(scope),
87
- "data-orientation": prop("orientation"),
88
- "data-focus": dataAttr(focused),
89
- dir: prop("dir")
90
- });
91
- },
92
- getListProps() {
93
- return normalize.element({
94
- ...parts.list.attrs,
95
- id: getListId(scope),
96
- role: "tablist",
97
- dir: prop("dir"),
98
- "data-focus": dataAttr(focused),
99
- "aria-orientation": prop("orientation"),
100
- "data-orientation": prop("orientation"),
101
- "aria-label": translations?.listLabel,
102
- onKeyDown(event) {
103
- if (event.defaultPrevented) return;
104
- if (isComposingEvent(event)) return;
105
- if (!contains(event.currentTarget, getEventTarget(event))) return;
106
- const keyMap = {
107
- ArrowDown() {
108
- if (isHorizontal) return;
109
- send({ type: "ARROW_NEXT", key: "ArrowDown" });
110
- },
111
- ArrowUp() {
112
- if (isHorizontal) return;
113
- send({ type: "ARROW_PREV", key: "ArrowUp" });
114
- },
115
- ArrowLeft() {
116
- if (isVertical) return;
117
- send({ type: "ARROW_PREV", key: "ArrowLeft" });
118
- },
119
- ArrowRight() {
120
- if (isVertical) return;
121
- send({ type: "ARROW_NEXT", key: "ArrowRight" });
122
- },
123
- Home() {
124
- send({ type: "HOME" });
125
- },
126
- End() {
127
- send({ type: "END" });
128
- }
129
- };
130
- let key = getEventKey(event, {
131
- dir: prop("dir"),
132
- orientation: prop("orientation")
133
- });
134
- const exec = keyMap[key];
135
- if (exec) {
136
- event.preventDefault();
137
- exec(event);
138
- return;
139
- }
140
- }
141
- });
142
- },
143
- getTriggerState,
144
- getTriggerProps(props2) {
145
- const { value, disabled } = props2;
146
- const triggerState = getTriggerState(props2);
147
- return normalize.button({
148
- ...parts.trigger.attrs,
149
- role: "tab",
150
- type: "button",
151
- disabled,
152
- dir: prop("dir"),
153
- "data-orientation": prop("orientation"),
154
- "data-disabled": dataAttr(disabled),
155
- "aria-disabled": disabled,
156
- "data-value": value,
157
- "aria-selected": triggerState.selected,
158
- "data-selected": dataAttr(triggerState.selected),
159
- "data-focus": dataAttr(triggerState.focused),
160
- "aria-controls": triggerState.selected ? getContentId(scope, value) : void 0,
161
- "data-ownedby": getListId(scope),
162
- "data-ssr": dataAttr(context.get("ssr")),
163
- id: getTriggerId(scope, value),
164
- tabIndex: triggerState.selected && composite ? 0 : -1,
165
- onFocus() {
166
- send({ type: "TAB_FOCUS", value });
167
- },
168
- onBlur(event) {
169
- const target = event.relatedTarget;
170
- if (target?.getAttribute("role") !== "tab") {
171
- send({ type: "TAB_BLUR" });
172
- }
173
- },
174
- onClick(event) {
175
- if (event.defaultPrevented) return;
176
- if (isOpeningInNewTab(event)) return;
177
- if (disabled) return;
178
- if (isSafari()) {
179
- event.currentTarget.focus();
180
- }
181
- send({ type: "TAB_CLICK", value });
182
- }
183
- });
184
- },
185
- getContentProps(props2) {
186
- const { value } = props2;
187
- const selected = context.get("value") === value;
188
- return normalize.element({
189
- ...parts.content.attrs,
190
- dir: prop("dir"),
191
- id: getContentId(scope, value),
192
- tabIndex: composite ? 0 : -1,
193
- "aria-labelledby": getTriggerId(scope, value),
194
- role: "tabpanel",
195
- "data-ownedby": getListId(scope),
196
- "data-selected": dataAttr(selected),
197
- "data-orientation": prop("orientation"),
198
- hidden: !selected
199
- });
200
- },
201
- getIndicatorProps() {
202
- const rect = context.get("indicatorRect");
203
- const rectIsEmpty = rect == null || rect.width === 0 && rect.height === 0 && rect.x === 0 && rect.y === 0;
204
- return normalize.element({
205
- id: getIndicatorId(scope),
206
- ...parts.indicator.attrs,
207
- dir: prop("dir"),
208
- "data-orientation": prop("orientation"),
209
- hidden: rectIsEmpty,
210
- style: {
211
- "--transition-property": "left, right, top, bottom, width, height",
212
- "--left": toPx(rect?.x),
213
- "--top": toPx(rect?.y),
214
- "--width": toPx(rect?.width),
215
- "--height": toPx(rect?.height),
216
- position: "absolute",
217
- willChange: "var(--transition-property)",
218
- transitionProperty: "var(--transition-property)",
219
- transitionDuration: "var(--transition-duration, 150ms)",
220
- transitionTimingFunction: "var(--transition-timing-function)",
221
- [isHorizontal ? "left" : "top"]: isHorizontal ? "var(--left)" : "var(--top)"
222
- }
223
- });
224
- }
225
- };
226
- }
227
- var { createMachine } = setup();
228
- var machine = createMachine({
229
- props({ props: props2 }) {
230
- return {
231
- dir: "ltr",
232
- orientation: "horizontal",
233
- activationMode: "automatic",
234
- loopFocus: true,
235
- composite: true,
236
- navigate(details) {
237
- clickIfLink(details.node);
238
- },
239
- defaultValue: null,
240
- ...props2
241
- };
242
- },
243
- initialState() {
244
- return "idle";
245
- },
246
- context({ prop, bindable }) {
247
- return {
248
- value: bindable(() => ({
249
- defaultValue: prop("defaultValue"),
250
- value: prop("value"),
251
- onChange(value) {
252
- prop("onValueChange")?.({ value });
253
- }
254
- })),
255
- focusedValue: bindable(() => ({
256
- defaultValue: prop("value") || prop("defaultValue"),
257
- sync: true,
258
- onChange(value) {
259
- prop("onFocusChange")?.({ focusedValue: value });
260
- }
261
- })),
262
- ssr: bindable(() => ({ defaultValue: true })),
263
- indicatorRect: bindable(() => ({
264
- defaultValue: null
265
- }))
266
- };
267
- },
268
- watch({ context, prop, track, action }) {
269
- track([() => context.get("value")], () => {
270
- action(["syncIndicatorRect", "syncTabIndex", "navigateIfNeeded"]);
271
- });
272
- track([() => prop("dir"), () => prop("orientation")], () => {
273
- action(["syncIndicatorRect"]);
274
- });
275
- },
276
- on: {
277
- SET_VALUE: {
278
- actions: ["setValue"]
279
- },
280
- CLEAR_VALUE: {
281
- actions: ["clearValue"]
282
- },
283
- SET_INDICATOR_RECT: {
284
- actions: ["setIndicatorRect"]
285
- },
286
- SYNC_TAB_INDEX: {
287
- actions: ["syncTabIndex"]
288
- }
289
- },
290
- entry: ["syncIndicatorRect", "syncTabIndex", "syncSsr"],
291
- exit: ["cleanupObserver"],
292
- states: {
293
- idle: {
294
- on: {
295
- TAB_FOCUS: {
296
- target: "focused",
297
- actions: ["setFocusedValue"]
298
- },
299
- TAB_CLICK: {
300
- target: "focused",
301
- actions: ["setFocusedValue", "setValue"]
302
- }
303
- }
304
- },
305
- focused: {
306
- on: {
307
- TAB_CLICK: {
308
- actions: ["setFocusedValue", "setValue"]
309
- },
310
- ARROW_PREV: [
311
- {
312
- guard: "selectOnFocus",
313
- actions: ["focusPrevTab", "selectFocusedTab"]
314
- },
315
- {
316
- actions: ["focusPrevTab"]
317
- }
318
- ],
319
- ARROW_NEXT: [
320
- {
321
- guard: "selectOnFocus",
322
- actions: ["focusNextTab", "selectFocusedTab"]
323
- },
324
- {
325
- actions: ["focusNextTab"]
326
- }
327
- ],
328
- HOME: [
329
- {
330
- guard: "selectOnFocus",
331
- actions: ["focusFirstTab", "selectFocusedTab"]
332
- },
333
- {
334
- actions: ["focusFirstTab"]
335
- }
336
- ],
337
- END: [
338
- {
339
- guard: "selectOnFocus",
340
- actions: ["focusLastTab", "selectFocusedTab"]
341
- },
342
- {
343
- actions: ["focusLastTab"]
344
- }
345
- ],
346
- TAB_FOCUS: {
347
- actions: ["setFocusedValue"]
348
- },
349
- TAB_BLUR: {
350
- target: "idle",
351
- actions: ["clearFocusedValue"]
352
- }
353
- }
354
- }
355
- },
356
- implementations: {
357
- guards: {
358
- selectOnFocus: ({ prop }) => prop("activationMode") === "automatic"
359
- },
360
- actions: {
361
- selectFocusedTab({ context, prop }) {
362
- raf(() => {
363
- const focusedValue = context.get("focusedValue");
364
- if (!focusedValue) return;
365
- const nullable = prop("deselectable") && context.get("value") === focusedValue;
366
- const value = nullable ? null : focusedValue;
367
- context.set("value", value);
368
- });
369
- },
370
- setFocusedValue({ context, event, flush }) {
371
- if (event.value == null) return;
372
- flush(() => {
373
- context.set("focusedValue", event.value);
374
- });
375
- },
376
- clearFocusedValue({ context }) {
377
- context.set("focusedValue", null);
378
- },
379
- setValue({ context, event, prop }) {
380
- const nullable = prop("deselectable") && context.get("value") === context.get("focusedValue");
381
- context.set("value", nullable ? null : event.value);
382
- },
383
- clearValue({ context }) {
384
- context.set("value", null);
385
- },
386
- focusFirstTab({ scope }) {
387
- raf(() => {
388
- getFirstTriggerEl(scope)?.focus();
389
- });
390
- },
391
- focusLastTab({ scope }) {
392
- raf(() => {
393
- getLastTriggerEl(scope)?.focus();
394
- });
395
- },
396
- focusNextTab({ context, prop, scope, event }) {
397
- const focusedValue = event.value ?? context.get("focusedValue");
398
- if (!focusedValue) return;
399
- const triggerEl = getNextTriggerEl(scope, {
400
- value: focusedValue,
401
- loopFocus: prop("loopFocus")
402
- });
403
- raf(() => {
404
- if (prop("composite")) {
405
- triggerEl?.focus();
406
- } else if (triggerEl?.dataset.value != null) {
407
- context.set("focusedValue", triggerEl.dataset.value);
408
- }
409
- });
410
- },
411
- focusPrevTab({ context, prop, scope, event }) {
412
- const focusedValue = event.value ?? context.get("focusedValue");
413
- if (!focusedValue) return;
414
- const triggerEl = getPrevTriggerEl(scope, {
415
- value: focusedValue,
416
- loopFocus: prop("loopFocus")
417
- });
418
- raf(() => {
419
- if (prop("composite")) {
420
- triggerEl?.focus();
421
- } else if (triggerEl?.dataset.value != null) {
422
- context.set("focusedValue", triggerEl.dataset.value);
423
- }
424
- });
425
- },
426
- syncTabIndex({ context, scope }) {
427
- raf(() => {
428
- const value = context.get("value");
429
- if (!value) return;
430
- const contentEl = getContentEl(scope, value);
431
- if (!contentEl) return;
432
- const focusables = getFocusables(contentEl);
433
- if (focusables.length > 0) {
434
- contentEl.removeAttribute("tabindex");
435
- } else {
436
- contentEl.setAttribute("tabindex", "0");
437
- }
438
- });
439
- },
440
- cleanupObserver({ refs }) {
441
- const cleanup = refs.get("indicatorCleanup");
442
- if (cleanup) cleanup();
443
- },
444
- setIndicatorRect({ context, event, scope }) {
445
- const value = event.id ?? context.get("value");
446
- const indicatorEl = getIndicatorEl(scope);
447
- if (!indicatorEl) return;
448
- if (!value) return;
449
- const triggerEl = getTriggerEl(scope, value);
450
- if (!triggerEl) return;
451
- context.set("indicatorRect", getRectByValue(scope, value));
452
- },
453
- syncSsr({ context }) {
454
- context.set("ssr", false);
455
- },
456
- syncIndicatorRect({ context, refs, scope }) {
457
- const cleanup = refs.get("indicatorCleanup");
458
- if (cleanup) cleanup();
459
- const indicatorEl = getIndicatorEl(scope);
460
- if (!indicatorEl) return;
461
- const exec = () => {
462
- const triggerEl = getTriggerEl(scope, context.get("value"));
463
- if (!triggerEl) return;
464
- const rect = getOffsetRect(triggerEl);
465
- context.set("indicatorRect", (prev) => isEqual(prev, rect) ? prev : rect);
466
- };
467
- exec();
468
- const triggerEls = getElements(scope);
469
- const indicatorCleanup = callAll(...triggerEls.map((el) => resizeObserverBorderBox.observe(el, exec)));
470
- refs.set("indicatorCleanup", indicatorCleanup);
471
- },
472
- navigateIfNeeded({ context, prop, scope }) {
473
- const value = context.get("value");
474
- if (!value) return;
475
- const triggerEl = getTriggerEl(scope, value);
476
- if (isAnchorElement(triggerEl)) {
477
- prop("navigate")?.({ value, node: triggerEl, href: triggerEl.href });
478
- }
479
- }
480
- }
481
- }
482
- });
483
- var props = createProps()([
484
- "activationMode",
485
- "composite",
486
- "deselectable",
487
- "dir",
488
- "getRootNode",
489
- "id",
490
- "ids",
491
- "loopFocus",
492
- "navigate",
493
- "onFocusChange",
494
- "onValueChange",
495
- "orientation",
496
- "translations",
497
- "value",
498
- "defaultValue"
499
- ]);
500
- var splitProps = createSplitProps(props);
501
- var triggerProps = createProps()(["disabled", "value"]);
502
- var splitTriggerProps = createSplitProps(triggerProps);
503
- var contentProps = createProps()(["value"]);
504
- var splitContentProps = createSplitProps(contentProps);
505
-
506
- export { anatomy, connect, contentProps, machine, props, splitContentProps, splitProps, splitTriggerProps, triggerProps };
@@ -0,0 +1,6 @@
1
+ import * as _zag_js_anatomy from '@zag-js/anatomy';
2
+
3
+ declare const anatomy: _zag_js_anatomy.AnatomyInstance<"root" | "list" | "trigger" | "content" | "indicator">;
4
+ declare const parts: Record<"root" | "list" | "trigger" | "content" | "indicator", _zag_js_anatomy.AnatomyPart>;
5
+
6
+ export { anatomy, parts };
@@ -0,0 +1,6 @@
1
+ import * as _zag_js_anatomy from '@zag-js/anatomy';
2
+
3
+ declare const anatomy: _zag_js_anatomy.AnatomyInstance<"root" | "list" | "trigger" | "content" | "indicator">;
4
+ declare const parts: Record<"root" | "list" | "trigger" | "content" | "indicator", _zag_js_anatomy.AnatomyPart>;
5
+
6
+ export { anatomy, parts };
@@ -0,0 +1,34 @@
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/tabs.anatomy.ts
21
+ var tabs_anatomy_exports = {};
22
+ __export(tabs_anatomy_exports, {
23
+ anatomy: () => anatomy,
24
+ parts: () => parts
25
+ });
26
+ module.exports = __toCommonJS(tabs_anatomy_exports);
27
+ var import_anatomy = require("@zag-js/anatomy");
28
+ var anatomy = (0, import_anatomy.createAnatomy)("tabs").parts("root", "list", "trigger", "content", "indicator");
29
+ var parts = anatomy.build();
30
+ // Annotate the CommonJS export names for ESM import in node:
31
+ 0 && (module.exports = {
32
+ anatomy,
33
+ parts
34
+ });
@@ -0,0 +1,8 @@
1
+ // src/tabs.anatomy.ts
2
+ import { createAnatomy } from "@zag-js/anatomy";
3
+ var anatomy = createAnatomy("tabs").parts("root", "list", "trigger", "content", "indicator");
4
+ var parts = anatomy.build();
5
+ export {
6
+ anatomy,
7
+ parts
8
+ };
@@ -0,0 +1,7 @@
1
+ import { Service } from '@zag-js/core';
2
+ import { PropTypes, NormalizeProps } from '@zag-js/types';
3
+ import { TabsSchema, TabsApi } from './tabs.types.mjs';
4
+
5
+ declare function connect<T extends PropTypes>(service: Service<TabsSchema>, normalize: NormalizeProps<T>): TabsApi<T>;
6
+
7
+ export { connect };
@@ -0,0 +1,7 @@
1
+ import { Service } from '@zag-js/core';
2
+ import { PropTypes, NormalizeProps } from '@zag-js/types';
3
+ import { TabsSchema, TabsApi } from './tabs.types.js';
4
+
5
+ declare function connect<T extends PropTypes>(service: Service<TabsSchema>, normalize: NormalizeProps<T>): TabsApi<T>;
6
+
7
+ export { connect };