@zag-js/tabs 1.34.1 → 1.35.1

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