@zag-js/tabs 0.1.10 → 0.1.13

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
@@ -2,21 +2,7 @@
2
2
  var __defProp = Object.defineProperty;
3
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __getOwnPropSymbols = Object.getOwnPropertySymbols;
6
5
  var __hasOwnProp = Object.prototype.hasOwnProperty;
7
- var __propIsEnum = Object.prototype.propertyIsEnumerable;
8
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
9
- var __spreadValues = (a, b) => {
10
- for (var prop in b || (b = {}))
11
- if (__hasOwnProp.call(b, prop))
12
- __defNormalProp(a, prop, b[prop]);
13
- if (__getOwnPropSymbols)
14
- for (var prop of __getOwnPropSymbols(b)) {
15
- if (__propIsEnum.call(b, prop))
16
- __defNormalProp(a, prop, b[prop]);
17
- }
18
- return a;
19
- };
20
6
  var __export = (target, all) => {
21
7
  for (var name in all)
22
8
  __defProp(target, name, { get: all[name], enumerable: true });
@@ -40,30 +26,13 @@ __export(src_exports, {
40
26
  module.exports = __toCommonJS(src_exports);
41
27
 
42
28
  // ../../utilities/dom/dist/index.mjs
43
- var __defProp2 = Object.defineProperty;
44
- var __getOwnPropSymbols2 = Object.getOwnPropertySymbols;
45
- var __hasOwnProp2 = Object.prototype.hasOwnProperty;
46
- var __propIsEnum2 = Object.prototype.propertyIsEnumerable;
47
- var __defNormalProp2 = (obj, key, value) => key in obj ? __defProp2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
48
- var __spreadValues2 = (a, b) => {
49
- for (var prop in b || (b = {}))
50
- if (__hasOwnProp2.call(b, prop))
51
- __defNormalProp2(a, prop, b[prop]);
52
- if (__getOwnPropSymbols2)
53
- for (var prop of __getOwnPropSymbols2(b)) {
54
- if (__propIsEnum2.call(b, prop))
55
- __defNormalProp2(a, prop, b[prop]);
56
- }
57
- return a;
58
- };
59
29
  var dataAttr = (guard) => {
60
30
  return guard ? "" : void 0;
61
31
  };
62
32
  var isDom = () => typeof window !== "undefined";
63
33
  function getPlatform() {
64
- var _a;
65
34
  const agent = navigator.userAgentData;
66
- return (_a = agent == null ? void 0 : agent.platform) != null ? _a : navigator.platform;
35
+ return (agent == null ? void 0 : agent.platform) ?? navigator.platform;
67
36
  }
68
37
  var pt = (v) => isDom() && v.test(getPlatform());
69
38
  var vn = (v) => isDom() && v.test(navigator.vendor);
@@ -79,28 +48,27 @@ function isFrame(element) {
79
48
  return element.localName === "iframe";
80
49
  }
81
50
  function getDocument(el) {
82
- var _a;
83
51
  if (isWindow(el))
84
52
  return el.document;
85
53
  if (isDocument(el))
86
54
  return el;
87
- return (_a = el == null ? void 0 : el.ownerDocument) != null ? _a : document;
55
+ return (el == null ? void 0 : el.ownerDocument) ?? document;
88
56
  }
89
57
  function defineDomHelpers(helpers) {
90
58
  const dom2 = {
91
59
  getRootNode: (ctx) => {
92
- var _a, _b;
93
- return (_b = (_a = ctx.getRootNode) == null ? void 0 : _a.call(ctx)) != null ? _b : document;
94
- },
95
- getDoc: (ctx) => getDocument(dom2.getRootNode(ctx)),
96
- getWin: (ctx) => {
97
60
  var _a;
98
- return (_a = dom2.getDoc(ctx).defaultView) != null ? _a : window;
61
+ return ((_a = ctx.getRootNode) == null ? void 0 : _a.call(ctx)) ?? document;
99
62
  },
63
+ getDoc: (ctx) => getDocument(dom2.getRootNode(ctx)),
64
+ getWin: (ctx) => dom2.getDoc(ctx).defaultView ?? window,
100
65
  getActiveElement: (ctx) => dom2.getDoc(ctx).activeElement,
101
66
  getById: (ctx, id) => dom2.getRootNode(ctx).getElementById(id)
102
67
  };
103
- return __spreadValues2(__spreadValues2({}, dom2), helpers);
68
+ return {
69
+ ...dom2,
70
+ ...helpers
71
+ };
104
72
  }
105
73
  function isHTMLElement(v) {
106
74
  return typeof v === "object" && (v == null ? void 0 : v.nodeType) === Node.ELEMENT_NODE && typeof (v == null ? void 0 : v.nodeName) === "string";
@@ -135,9 +103,7 @@ function isFocusable(element) {
135
103
  }
136
104
  var rtlKeyMap = {
137
105
  ArrowLeft: "ArrowRight",
138
- ArrowRight: "ArrowLeft",
139
- Home: "End",
140
- End: "Home"
106
+ ArrowRight: "ArrowLeft"
141
107
  };
142
108
  var sameKeyMap = {
143
109
  Up: "ArrowUp",
@@ -149,10 +115,9 @@ var sameKeyMap = {
149
115
  Right: "ArrowRight"
150
116
  };
151
117
  function getEventKey(event, options = {}) {
152
- var _a;
153
118
  const { dir = "ltr", orientation = "horizontal" } = options;
154
119
  let { key } = event;
155
- key = (_a = sameKeyMap[key]) != null ? _a : key;
120
+ key = sameKeyMap[key] ?? key;
156
121
  const isRtl = dir === "rtl" && orientation === "horizontal";
157
122
  if (isRtl && key in rtlKeyMap) {
158
123
  key = rtlKeyMap[key];
@@ -179,8 +144,7 @@ function raf(fn) {
179
144
  };
180
145
  }
181
146
  function queryAll(root, selector) {
182
- var _a;
183
- return Array.from((_a = root == null ? void 0 : root.querySelectorAll(selector)) != null ? _a : []);
147
+ return Array.from((root == null ? void 0 : root.querySelectorAll(selector)) ?? []);
184
148
  }
185
149
  function itemById(v, id) {
186
150
  return v.find((node) => node.id === id);
@@ -209,24 +173,24 @@ var last = (v) => v[v.length - 1];
209
173
  // src/tabs.dom.ts
210
174
  var dom = defineDomHelpers({
211
175
  getRootId: (ctx) => {
212
- var _a, _b;
213
- return (_b = (_a = ctx.ids) == null ? void 0 : _a.root) != null ? _b : `tabs:${ctx.id}`;
176
+ var _a;
177
+ return ((_a = ctx.ids) == null ? void 0 : _a.root) ?? `tabs:${ctx.id}`;
214
178
  },
215
179
  getTriggerGroupId: (ctx) => {
216
- var _a, _b;
217
- return (_b = (_a = ctx.ids) == null ? void 0 : _a.triggerGroup) != null ? _b : `tabs:${ctx.id}:trigger-group`;
180
+ var _a;
181
+ return ((_a = ctx.ids) == null ? void 0 : _a.triggerGroup) ?? `tabs:${ctx.id}:trigger-group`;
218
182
  },
219
183
  getContentId: (ctx, id) => {
220
- var _a, _b;
221
- return (_b = (_a = ctx.ids) == null ? void 0 : _a.content) != null ? _b : `tabs:${ctx.id}:content-${id}`;
184
+ var _a;
185
+ return ((_a = ctx.ids) == null ? void 0 : _a.content) ?? `tabs:${ctx.id}:content-${id}`;
222
186
  },
223
187
  getContentGroupId: (ctx) => {
224
- var _a, _b;
225
- return (_b = (_a = ctx.ids) == null ? void 0 : _a.contentGroup) != null ? _b : `tabs:${ctx.id}:content-group`;
188
+ var _a;
189
+ return ((_a = ctx.ids) == null ? void 0 : _a.contentGroup) ?? `tabs:${ctx.id}:content-group`;
226
190
  },
227
191
  getTriggerId: (ctx, id) => {
228
- var _a, _b;
229
- return (_b = (_a = ctx.ids) == null ? void 0 : _a.trigger) != null ? _b : `tabs:${ctx.id}:trigger-${id}`;
192
+ var _a;
193
+ return ((_a = ctx.ids) == null ? void 0 : _a.trigger) ?? `tabs:${ctx.id}:trigger-${id}`;
230
194
  },
231
195
  getIndicatorId: (ctx) => `tabs:${ctx.id}:indicator`,
232
196
  getTriggerGroupEl: (ctx) => dom.getById(ctx, dom.getTriggerGroupId(ctx)),
@@ -249,14 +213,13 @@ var dom = defineDomHelpers({
249
213
  return dom.getById(ctx, id);
250
214
  },
251
215
  getRectById: (ctx, id) => {
252
- var _a;
253
216
  const empty = {
254
217
  offsetLeft: 0,
255
218
  offsetTop: 0,
256
219
  offsetWidth: 0,
257
220
  offsetHeight: 0
258
221
  };
259
- const tab = (_a = itemById(dom.getElements(ctx), dom.getTriggerId(ctx, id))) != null ? _a : empty;
222
+ const tab = itemById(dom.getElements(ctx), dom.getTriggerId(ctx, id)) ?? empty;
260
223
  if (ctx.isVertical) {
261
224
  return {
262
225
  top: `${tab.offsetTop}px`,
@@ -399,15 +362,16 @@ function connect(state, send, normalize) {
399
362
  id: dom.getIndicatorId(state.context),
400
363
  "data-part": "indicator",
401
364
  "data-orientation": state.context.orientation,
402
- style: __spreadValues({
365
+ style: {
403
366
  "--transition-duration": "200ms",
404
367
  "--transition-property": "left, right, top, bottom, width, height",
405
368
  position: "absolute",
406
369
  willChange: "var(--transition-property)",
407
370
  transitionProperty: "var(--transition-property)",
408
371
  transitionDuration: state.context.hasMeasuredRect ? "var(--transition-duration)" : "0ms",
409
- transitionTimingFunction: "var(--transition-timing-function)"
410
- }, state.context.indicatorRect)
372
+ transitionTimingFunction: "var(--transition-timing-function)",
373
+ ...state.context.indicatorRect
374
+ }
411
375
  })
412
376
  };
413
377
  }
@@ -416,196 +380,205 @@ function connect(state, send, normalize) {
416
380
  var import_core = require("@zag-js/core");
417
381
  var { not } = import_core.guards;
418
382
  function machine(ctx) {
419
- return (0, import_core.createMachine)({
420
- initial: "unknown",
421
- context: __spreadValues({
422
- dir: "ltr",
423
- orientation: "horizontal",
424
- activationMode: "automatic",
425
- value: null,
426
- focusedValue: null,
427
- previousValues: [],
428
- indicatorRect: { left: "0px", top: "0px", width: "0px", height: "0px" },
429
- hasMeasuredRect: false,
430
- isIndicatorRendered: false,
431
- loop: true,
432
- messages: {}
433
- }, ctx),
434
- computed: {
435
- isHorizontal: (ctx2) => ctx2.orientation === "horizontal",
436
- isVertical: (ctx2) => ctx2.orientation === "vertical"
437
- },
438
- created: ["setPrevSelectedTabs"],
439
- watch: {
440
- focusedValue: "invokeOnFocus",
441
- value: ["invokeOnChange", "setPrevSelectedTabs", "setIndicatorRect", "setContentTabIndex"],
442
- dir: ["clearMeasured", "setIndicatorRect"]
443
- },
444
- on: {
445
- SET_VALUE: {
446
- actions: "setValue"
383
+ return (0, import_core.createMachine)(
384
+ {
385
+ initial: "unknown",
386
+ context: {
387
+ dir: "ltr",
388
+ orientation: "horizontal",
389
+ activationMode: "automatic",
390
+ value: null,
391
+ focusedValue: null,
392
+ previousValues: [],
393
+ indicatorRect: { left: "0px", top: "0px", width: "0px", height: "0px" },
394
+ hasMeasuredRect: false,
395
+ isIndicatorRendered: false,
396
+ loop: true,
397
+ messages: {},
398
+ ...ctx
447
399
  },
448
- DELETE: {
449
- actions: "deleteValue"
450
- }
451
- },
452
- states: {
453
- unknown: {
454
- on: {
455
- SETUP: {
456
- target: "idle",
457
- actions: ["checkRenderedElements", "setIndicatorRect", "setContentTabIndex"]
458
- }
459
- }
400
+ computed: {
401
+ isHorizontal: (ctx2) => ctx2.orientation === "horizontal",
402
+ isVertical: (ctx2) => ctx2.orientation === "vertical"
460
403
  },
461
- idle: {
462
- on: {
463
- TAB_FOCUS: {
464
- guard: "selectOnFocus",
465
- target: "focused",
466
- actions: ["setFocusedValue", "setValue"]
467
- },
468
- TAB_CLICK: {
469
- target: "focused",
470
- actions: ["setFocusedValue", "setValue"]
471
- }
404
+ created: ["setPrevSelectedTabs"],
405
+ watch: {
406
+ focusedValue: "invokeOnFocus",
407
+ value: ["invokeOnChange", "setPrevSelectedTabs", "setIndicatorRect", "setContentTabIndex"],
408
+ dir: ["clearMeasured", "setIndicatorRect"]
409
+ },
410
+ on: {
411
+ SET_VALUE: {
412
+ actions: "setValue"
413
+ },
414
+ DELETE: {
415
+ actions: "deleteValue"
472
416
  }
473
417
  },
474
- focused: {
475
- on: {
476
- TAB_CLICK: {
477
- target: "focused",
478
- actions: ["setFocusedValue", "setValue"]
479
- },
480
- ARROW_LEFT: {
481
- guard: "isHorizontal",
482
- actions: "focusPrevTab"
483
- },
484
- ARROW_RIGHT: {
485
- guard: "isHorizontal",
486
- actions: "focusNextTab"
487
- },
488
- ARROW_UP: {
489
- guard: "isVertical",
490
- actions: "focusPrevTab"
491
- },
492
- ARROW_DOWN: {
493
- guard: "isVertical",
494
- actions: "focusNextTab"
495
- },
496
- HOME: {
497
- actions: "focusFirstTab"
498
- },
499
- END: {
500
- actions: "focusLastTab"
501
- },
502
- ENTER: {
503
- guard: not("selectOnFocus"),
504
- actions: "setValue"
505
- },
506
- TAB_FOCUS: [
507
- {
418
+ states: {
419
+ unknown: {
420
+ on: {
421
+ SETUP: {
422
+ target: "idle",
423
+ actions: ["checkRenderedElements", "setIndicatorRect", "setContentTabIndex"]
424
+ }
425
+ }
426
+ },
427
+ idle: {
428
+ on: {
429
+ TAB_FOCUS: {
508
430
  guard: "selectOnFocus",
431
+ target: "focused",
509
432
  actions: ["setFocusedValue", "setValue"]
510
433
  },
511
- { actions: "setFocusedValue" }
512
- ],
513
- TAB_BLUR: {
514
- target: "idle",
515
- actions: "clearFocusedValue"
434
+ TAB_CLICK: {
435
+ target: "focused",
436
+ actions: ["setFocusedValue", "setValue"]
437
+ }
438
+ }
439
+ },
440
+ focused: {
441
+ on: {
442
+ TAB_CLICK: {
443
+ target: "focused",
444
+ actions: ["setFocusedValue", "setValue"]
445
+ },
446
+ ARROW_LEFT: {
447
+ guard: "isHorizontal",
448
+ actions: "focusPrevTab"
449
+ },
450
+ ARROW_RIGHT: {
451
+ guard: "isHorizontal",
452
+ actions: "focusNextTab"
453
+ },
454
+ ARROW_UP: {
455
+ guard: "isVertical",
456
+ actions: "focusPrevTab"
457
+ },
458
+ ARROW_DOWN: {
459
+ guard: "isVertical",
460
+ actions: "focusNextTab"
461
+ },
462
+ HOME: {
463
+ actions: "focusFirstTab"
464
+ },
465
+ END: {
466
+ actions: "focusLastTab"
467
+ },
468
+ ENTER: {
469
+ guard: not("selectOnFocus"),
470
+ actions: "setValue"
471
+ },
472
+ TAB_FOCUS: [
473
+ {
474
+ guard: "selectOnFocus",
475
+ actions: ["setFocusedValue", "setValue"]
476
+ },
477
+ { actions: "setFocusedValue" }
478
+ ],
479
+ TAB_BLUR: {
480
+ target: "idle",
481
+ actions: "clearFocusedValue"
482
+ }
516
483
  }
517
484
  }
518
485
  }
519
- }
520
- }, {
521
- guards: {
522
- isVertical: (ctx2) => ctx2.isVertical,
523
- isHorizontal: (ctx2) => ctx2.isHorizontal,
524
- selectOnFocus: (ctx2) => ctx2.activationMode === "automatic"
525
486
  },
526
- actions: {
527
- setFocusedValue(ctx2, evt) {
528
- ctx2.focusedValue = evt.value;
529
- },
530
- clearFocusedValue(ctx2) {
531
- ctx2.focusedValue = null;
487
+ {
488
+ guards: {
489
+ isVertical: (ctx2) => ctx2.isVertical,
490
+ isHorizontal: (ctx2) => ctx2.isHorizontal,
491
+ selectOnFocus: (ctx2) => ctx2.activationMode === "automatic"
532
492
  },
533
- setValue(ctx2, evt) {
534
- ctx2.value = evt.value;
535
- },
536
- invokeOnDelete(ctx2, evt) {
537
- var _a;
538
- (_a = ctx2.onDelete) == null ? void 0 : _a.call(ctx2, { value: evt.value });
539
- },
540
- focusFirstTab(ctx2) {
541
- raf(() => {
542
- var _a;
543
- return (_a = dom.getFirstEl(ctx2)) == null ? void 0 : _a.focus();
544
- });
545
- },
546
- focusLastTab(ctx2) {
547
- raf(() => {
493
+ actions: {
494
+ setFocusedValue(ctx2, evt) {
495
+ ctx2.focusedValue = evt.value;
496
+ },
497
+ clearFocusedValue(ctx2) {
498
+ ctx2.focusedValue = null;
499
+ },
500
+ setValue(ctx2, evt) {
501
+ ctx2.value = evt.value;
502
+ },
503
+ invokeOnDelete(ctx2, evt) {
548
504
  var _a;
549
- return (_a = dom.getLastEl(ctx2)) == null ? void 0 : _a.focus();
550
- });
551
- },
552
- focusNextTab(ctx2) {
553
- if (!ctx2.focusedValue)
554
- return;
555
- const next = dom.getNextEl(ctx2, ctx2.focusedValue);
556
- raf(() => next == null ? void 0 : next.focus());
557
- },
558
- focusPrevTab(ctx2) {
559
- if (!ctx2.focusedValue)
560
- return;
561
- const prev = dom.getPrevEl(ctx2, ctx2.focusedValue);
562
- raf(() => prev == null ? void 0 : prev.focus());
563
- },
564
- setIndicatorRect(ctx2) {
565
- nextTick(() => {
566
- if (!ctx2.isIndicatorRendered || !ctx2.value)
505
+ (_a = ctx2.onDelete) == null ? void 0 : _a.call(ctx2, { value: evt.value });
506
+ },
507
+ focusFirstTab(ctx2) {
508
+ raf(() => {
509
+ var _a;
510
+ return (_a = dom.getFirstEl(ctx2)) == null ? void 0 : _a.focus();
511
+ });
512
+ },
513
+ focusLastTab(ctx2) {
514
+ raf(() => {
515
+ var _a;
516
+ return (_a = dom.getLastEl(ctx2)) == null ? void 0 : _a.focus();
517
+ });
518
+ },
519
+ focusNextTab(ctx2) {
520
+ if (!ctx2.focusedValue)
567
521
  return;
568
- ctx2.indicatorRect = dom.getRectById(ctx2, ctx2.value);
569
- if (ctx2.hasMeasuredRect)
522
+ const next = dom.getNextEl(ctx2, ctx2.focusedValue);
523
+ raf(() => next == null ? void 0 : next.focus());
524
+ },
525
+ focusPrevTab(ctx2) {
526
+ if (!ctx2.focusedValue)
570
527
  return;
528
+ const prev = dom.getPrevEl(ctx2, ctx2.focusedValue);
529
+ raf(() => prev == null ? void 0 : prev.focus());
530
+ },
531
+ setIndicatorRect(ctx2) {
571
532
  nextTick(() => {
572
- ctx2.hasMeasuredRect = true;
533
+ if (!ctx2.isIndicatorRendered || !ctx2.value)
534
+ return;
535
+ ctx2.indicatorRect = dom.getRectById(ctx2, ctx2.value);
536
+ if (ctx2.hasMeasuredRect)
537
+ return;
538
+ nextTick(() => {
539
+ ctx2.hasMeasuredRect = true;
540
+ });
573
541
  });
574
- });
575
- },
576
- checkRenderedElements(ctx2) {
577
- ctx2.isIndicatorRendered = !!dom.getIndicatorEl(ctx2);
578
- },
579
- clearMeasured(ctx2) {
580
- ctx2.hasMeasuredRect = false;
581
- },
582
- invokeOnChange(ctx2) {
583
- var _a;
584
- (_a = ctx2.onChange) == null ? void 0 : _a.call(ctx2, { value: ctx2.value });
585
- },
586
- invokeOnFocus(ctx2) {
587
- var _a;
588
- (_a = ctx2.onFocus) == null ? void 0 : _a.call(ctx2, { value: ctx2.focusedValue });
589
- },
590
- setPrevSelectedTabs(ctx2) {
591
- if (ctx2.value != null) {
592
- const newSelected = Array.from(ctx2.previousValues).concat(ctx2.value);
593
- ctx2.previousValues = Array.from(new Set(newSelected));
594
- }
595
- },
596
- setContentTabIndex(ctx2) {
597
- raf(() => {
598
- const panel = dom.getActiveContentEl(ctx2);
599
- if (!panel)
600
- return;
601
- const focusables = getFocusables(panel);
602
- if (focusables.length > 0) {
603
- panel.removeAttribute("tabindex");
604
- } else {
605
- panel.setAttribute("tabindex", "0");
542
+ },
543
+ checkRenderedElements(ctx2) {
544
+ ctx2.isIndicatorRendered = !!dom.getIndicatorEl(ctx2);
545
+ },
546
+ clearMeasured(ctx2) {
547
+ ctx2.hasMeasuredRect = false;
548
+ },
549
+ invokeOnChange(ctx2) {
550
+ var _a;
551
+ (_a = ctx2.onChange) == null ? void 0 : _a.call(ctx2, { value: ctx2.value });
552
+ },
553
+ invokeOnFocus(ctx2) {
554
+ var _a;
555
+ (_a = ctx2.onFocus) == null ? void 0 : _a.call(ctx2, { value: ctx2.focusedValue });
556
+ },
557
+ setPrevSelectedTabs(ctx2) {
558
+ if (ctx2.value != null) {
559
+ const newSelected = Array.from(ctx2.previousValues).concat(ctx2.value);
560
+ ctx2.previousValues = Array.from(new Set(newSelected));
606
561
  }
607
- });
562
+ },
563
+ setContentTabIndex(ctx2) {
564
+ raf(() => {
565
+ const panel = dom.getActiveContentEl(ctx2);
566
+ if (!panel)
567
+ return;
568
+ const focusables = getFocusables(panel);
569
+ if (focusables.length > 0) {
570
+ panel.removeAttribute("tabindex");
571
+ } else {
572
+ panel.setAttribute("tabindex", "0");
573
+ }
574
+ });
575
+ }
608
576
  }
609
577
  }
610
- });
578
+ );
611
579
  }
580
+ // Annotate the CommonJS export names for ESM import in node:
581
+ 0 && (module.exports = {
582
+ connect,
583
+ machine
584
+ });