@zag-js/tabs 0.1.9 → 0.1.10

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
@@ -40,6 +40,22 @@ __export(src_exports, {
40
40
  module.exports = __toCommonJS(src_exports);
41
41
 
42
42
  // ../../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
+ };
43
59
  var dataAttr = (guard) => {
44
60
  return guard ? "" : void 0;
45
61
  };
@@ -53,9 +69,39 @@ var pt = (v) => isDom() && v.test(getPlatform());
53
69
  var vn = (v) => isDom() && v.test(navigator.vendor);
54
70
  var isSafari = () => isApple() && vn(/apple/i);
55
71
  var isApple = () => pt(/mac|iphone|ipad|ipod/i);
72
+ function isDocument(el) {
73
+ return el.nodeType === Node.DOCUMENT_NODE;
74
+ }
75
+ function isWindow(value) {
76
+ return (value == null ? void 0 : value.toString()) === "[object Window]";
77
+ }
56
78
  function isFrame(element) {
57
79
  return element.localName === "iframe";
58
80
  }
81
+ function getDocument(el) {
82
+ var _a;
83
+ if (isWindow(el))
84
+ return el.document;
85
+ if (isDocument(el))
86
+ return el;
87
+ return (_a = el == null ? void 0 : el.ownerDocument) != null ? _a : document;
88
+ }
89
+ function defineDomHelpers(helpers) {
90
+ const dom2 = {
91
+ 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
+ var _a;
98
+ return (_a = dom2.getDoc(ctx).defaultView) != null ? _a : window;
99
+ },
100
+ getActiveElement: (ctx) => dom2.getDoc(ctx).activeElement,
101
+ getById: (ctx, id) => dom2.getRootNode(ctx).getElementById(id)
102
+ };
103
+ return __spreadValues2(__spreadValues2({}, dom2), helpers);
104
+ }
59
105
  function isHTMLElement(v) {
60
106
  return typeof v === "object" && (v == null ? void 0 : v.nodeType) === Node.ELEMENT_NODE && typeof (v == null ? void 0 : v.nodeName) === "string";
61
107
  }
@@ -161,40 +207,32 @@ var first = (v) => v[0];
161
207
  var last = (v) => v[v.length - 1];
162
208
 
163
209
  // src/tabs.dom.ts
164
- var dom = {
165
- getDoc: (ctx) => {
166
- var _a;
167
- return (_a = ctx.doc) != null ? _a : document;
168
- },
169
- getRootNode: (ctx) => {
170
- var _a;
171
- return (_a = ctx.rootNode) != null ? _a : dom.getDoc(ctx);
172
- },
210
+ var dom = defineDomHelpers({
173
211
  getRootId: (ctx) => {
174
212
  var _a, _b;
175
- return (_b = (_a = ctx.ids) == null ? void 0 : _a.root) != null ? _b : `tabs:${ctx.uid}`;
213
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.root) != null ? _b : `tabs:${ctx.id}`;
176
214
  },
177
215
  getTriggerGroupId: (ctx) => {
178
216
  var _a, _b;
179
- return (_b = (_a = ctx.ids) == null ? void 0 : _a.triggerGroup) != null ? _b : `tabs:${ctx.uid}:trigger-group`;
217
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.triggerGroup) != null ? _b : `tabs:${ctx.id}:trigger-group`;
180
218
  },
181
219
  getContentId: (ctx, id) => {
182
220
  var _a, _b;
183
- return (_b = (_a = ctx.ids) == null ? void 0 : _a.content) != null ? _b : `tabs:${ctx.uid}:content-${id}`;
221
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.content) != null ? _b : `tabs:${ctx.id}:content-${id}`;
184
222
  },
185
223
  getContentGroupId: (ctx) => {
186
224
  var _a, _b;
187
- return (_b = (_a = ctx.ids) == null ? void 0 : _a.contentGroup) != null ? _b : `tabs:${ctx.uid}:content-group`;
225
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.contentGroup) != null ? _b : `tabs:${ctx.id}:content-group`;
188
226
  },
189
227
  getTriggerId: (ctx, id) => {
190
228
  var _a, _b;
191
- return (_b = (_a = ctx.ids) == null ? void 0 : _a.trigger) != null ? _b : `tabs:${ctx.uid}:trigger-${id}`;
229
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.trigger) != null ? _b : `tabs:${ctx.id}:trigger-${id}`;
192
230
  },
193
- getIndicatorId: (ctx) => `tabs:${ctx.uid}:indicator`,
194
- getTriggerGroupEl: (ctx) => dom.getRootNode(ctx).getElementById(dom.getTriggerGroupId(ctx)),
195
- getContentEl: (ctx, id) => dom.getRootNode(ctx).getElementById(dom.getContentId(ctx, id)),
196
- getTriggerEl: (ctx, id) => dom.getRootNode(ctx).getElementById(dom.getTriggerId(ctx, id)),
197
- getIndicatorEl: (ctx) => dom.getRootNode(ctx).getElementById(dom.getIndicatorId(ctx)),
231
+ getIndicatorId: (ctx) => `tabs:${ctx.id}:indicator`,
232
+ getTriggerGroupEl: (ctx) => dom.getById(ctx, dom.getTriggerGroupId(ctx)),
233
+ getContentEl: (ctx, id) => dom.getById(ctx, dom.getContentId(ctx, id)),
234
+ getTriggerEl: (ctx, id) => dom.getById(ctx, dom.getTriggerId(ctx, id)),
235
+ getIndicatorEl: (ctx) => dom.getById(ctx, dom.getIndicatorId(ctx)),
198
236
  getElements: (ctx) => {
199
237
  const ownerId = CSS.escape(dom.getTriggerGroupId(ctx));
200
238
  const selector = `[role=tab][data-ownedby='${ownerId}']:not([disabled])`;
@@ -208,7 +246,7 @@ var dom = {
208
246
  if (!ctx.value)
209
247
  return;
210
248
  const id = dom.getContentId(ctx, ctx.value);
211
- return dom.getRootNode(ctx).getElementById(id);
249
+ return dom.getById(ctx, id);
212
250
  },
213
251
  getRectById: (ctx, id) => {
214
252
  var _a;
@@ -230,7 +268,7 @@ var dom = {
230
268
  width: `${tab.offsetWidth}px`
231
269
  };
232
270
  }
233
- };
271
+ });
234
272
 
235
273
  // src/tabs.connect.ts
236
274
  function connect(state, send, normalize) {
@@ -377,7 +415,7 @@ function connect(state, send, normalize) {
377
415
  // src/tabs.machine.ts
378
416
  var import_core = require("@zag-js/core");
379
417
  var { not } = import_core.guards;
380
- function machine(ctx = {}) {
418
+ function machine(ctx) {
381
419
  return (0, import_core.createMachine)({
382
420
  initial: "unknown",
383
421
  context: __spreadValues({
@@ -386,7 +424,6 @@ function machine(ctx = {}) {
386
424
  activationMode: "automatic",
387
425
  value: null,
388
426
  focusedValue: null,
389
- uid: "",
390
427
  previousValues: [],
391
428
  indicatorRect: { left: "0px", top: "0px", width: "0px", height: "0px" },
392
429
  hasMeasuredRect: false,
@@ -417,7 +454,7 @@ function machine(ctx = {}) {
417
454
  on: {
418
455
  SETUP: {
419
456
  target: "idle",
420
- actions: ["setupDocument", "checkRenderedElements", "setIndicatorRect", "setContentTabIndex"]
457
+ actions: ["checkRenderedElements", "setIndicatorRect", "setContentTabIndex"]
421
458
  }
422
459
  }
423
460
  },
@@ -487,13 +524,6 @@ function machine(ctx = {}) {
487
524
  selectOnFocus: (ctx2) => ctx2.activationMode === "automatic"
488
525
  },
489
526
  actions: {
490
- setupDocument(ctx2, evt) {
491
- ctx2.uid = evt.id;
492
- if (evt.doc)
493
- ctx2.doc = (0, import_core.ref)(evt.doc);
494
- if (evt.root)
495
- ctx2.rootNode = (0, import_core.ref)(evt.root);
496
- },
497
527
  setFocusedValue(ctx2, evt) {
498
528
  ctx2.focusedValue = evt.value;
499
529
  },
@@ -544,9 +574,7 @@ function machine(ctx = {}) {
544
574
  });
545
575
  },
546
576
  checkRenderedElements(ctx2) {
547
- nextTick(() => {
548
- ctx2.isIndicatorRendered = !!dom.getIndicatorEl(ctx2);
549
- });
577
+ ctx2.isIndicatorRendered = !!dom.getIndicatorEl(ctx2);
550
578
  },
551
579
  clearMeasured(ctx2) {
552
580
  ctx2.hasMeasuredRect = false;
package/dist/index.mjs CHANGED
@@ -16,6 +16,22 @@ var __spreadValues = (a, b) => {
16
16
  };
17
17
 
18
18
  // ../../utilities/dom/dist/index.mjs
19
+ var __defProp2 = Object.defineProperty;
20
+ var __getOwnPropSymbols2 = Object.getOwnPropertySymbols;
21
+ var __hasOwnProp2 = Object.prototype.hasOwnProperty;
22
+ var __propIsEnum2 = Object.prototype.propertyIsEnumerable;
23
+ var __defNormalProp2 = (obj, key, value) => key in obj ? __defProp2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
24
+ var __spreadValues2 = (a, b) => {
25
+ for (var prop in b || (b = {}))
26
+ if (__hasOwnProp2.call(b, prop))
27
+ __defNormalProp2(a, prop, b[prop]);
28
+ if (__getOwnPropSymbols2)
29
+ for (var prop of __getOwnPropSymbols2(b)) {
30
+ if (__propIsEnum2.call(b, prop))
31
+ __defNormalProp2(a, prop, b[prop]);
32
+ }
33
+ return a;
34
+ };
19
35
  var dataAttr = (guard) => {
20
36
  return guard ? "" : void 0;
21
37
  };
@@ -29,9 +45,39 @@ var pt = (v) => isDom() && v.test(getPlatform());
29
45
  var vn = (v) => isDom() && v.test(navigator.vendor);
30
46
  var isSafari = () => isApple() && vn(/apple/i);
31
47
  var isApple = () => pt(/mac|iphone|ipad|ipod/i);
48
+ function isDocument(el) {
49
+ return el.nodeType === Node.DOCUMENT_NODE;
50
+ }
51
+ function isWindow(value) {
52
+ return (value == null ? void 0 : value.toString()) === "[object Window]";
53
+ }
32
54
  function isFrame(element) {
33
55
  return element.localName === "iframe";
34
56
  }
57
+ function getDocument(el) {
58
+ var _a;
59
+ if (isWindow(el))
60
+ return el.document;
61
+ if (isDocument(el))
62
+ return el;
63
+ return (_a = el == null ? void 0 : el.ownerDocument) != null ? _a : document;
64
+ }
65
+ function defineDomHelpers(helpers) {
66
+ const dom2 = {
67
+ getRootNode: (ctx) => {
68
+ var _a, _b;
69
+ return (_b = (_a = ctx.getRootNode) == null ? void 0 : _a.call(ctx)) != null ? _b : document;
70
+ },
71
+ getDoc: (ctx) => getDocument(dom2.getRootNode(ctx)),
72
+ getWin: (ctx) => {
73
+ var _a;
74
+ return (_a = dom2.getDoc(ctx).defaultView) != null ? _a : window;
75
+ },
76
+ getActiveElement: (ctx) => dom2.getDoc(ctx).activeElement,
77
+ getById: (ctx, id) => dom2.getRootNode(ctx).getElementById(id)
78
+ };
79
+ return __spreadValues2(__spreadValues2({}, dom2), helpers);
80
+ }
35
81
  function isHTMLElement(v) {
36
82
  return typeof v === "object" && (v == null ? void 0 : v.nodeType) === Node.ELEMENT_NODE && typeof (v == null ? void 0 : v.nodeName) === "string";
37
83
  }
@@ -137,40 +183,32 @@ var first = (v) => v[0];
137
183
  var last = (v) => v[v.length - 1];
138
184
 
139
185
  // src/tabs.dom.ts
140
- var dom = {
141
- getDoc: (ctx) => {
142
- var _a;
143
- return (_a = ctx.doc) != null ? _a : document;
144
- },
145
- getRootNode: (ctx) => {
146
- var _a;
147
- return (_a = ctx.rootNode) != null ? _a : dom.getDoc(ctx);
148
- },
186
+ var dom = defineDomHelpers({
149
187
  getRootId: (ctx) => {
150
188
  var _a, _b;
151
- return (_b = (_a = ctx.ids) == null ? void 0 : _a.root) != null ? _b : `tabs:${ctx.uid}`;
189
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.root) != null ? _b : `tabs:${ctx.id}`;
152
190
  },
153
191
  getTriggerGroupId: (ctx) => {
154
192
  var _a, _b;
155
- return (_b = (_a = ctx.ids) == null ? void 0 : _a.triggerGroup) != null ? _b : `tabs:${ctx.uid}:trigger-group`;
193
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.triggerGroup) != null ? _b : `tabs:${ctx.id}:trigger-group`;
156
194
  },
157
195
  getContentId: (ctx, id) => {
158
196
  var _a, _b;
159
- return (_b = (_a = ctx.ids) == null ? void 0 : _a.content) != null ? _b : `tabs:${ctx.uid}:content-${id}`;
197
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.content) != null ? _b : `tabs:${ctx.id}:content-${id}`;
160
198
  },
161
199
  getContentGroupId: (ctx) => {
162
200
  var _a, _b;
163
- return (_b = (_a = ctx.ids) == null ? void 0 : _a.contentGroup) != null ? _b : `tabs:${ctx.uid}:content-group`;
201
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.contentGroup) != null ? _b : `tabs:${ctx.id}:content-group`;
164
202
  },
165
203
  getTriggerId: (ctx, id) => {
166
204
  var _a, _b;
167
- return (_b = (_a = ctx.ids) == null ? void 0 : _a.trigger) != null ? _b : `tabs:${ctx.uid}:trigger-${id}`;
205
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.trigger) != null ? _b : `tabs:${ctx.id}:trigger-${id}`;
168
206
  },
169
- getIndicatorId: (ctx) => `tabs:${ctx.uid}:indicator`,
170
- getTriggerGroupEl: (ctx) => dom.getRootNode(ctx).getElementById(dom.getTriggerGroupId(ctx)),
171
- getContentEl: (ctx, id) => dom.getRootNode(ctx).getElementById(dom.getContentId(ctx, id)),
172
- getTriggerEl: (ctx, id) => dom.getRootNode(ctx).getElementById(dom.getTriggerId(ctx, id)),
173
- getIndicatorEl: (ctx) => dom.getRootNode(ctx).getElementById(dom.getIndicatorId(ctx)),
207
+ getIndicatorId: (ctx) => `tabs:${ctx.id}:indicator`,
208
+ getTriggerGroupEl: (ctx) => dom.getById(ctx, dom.getTriggerGroupId(ctx)),
209
+ getContentEl: (ctx, id) => dom.getById(ctx, dom.getContentId(ctx, id)),
210
+ getTriggerEl: (ctx, id) => dom.getById(ctx, dom.getTriggerId(ctx, id)),
211
+ getIndicatorEl: (ctx) => dom.getById(ctx, dom.getIndicatorId(ctx)),
174
212
  getElements: (ctx) => {
175
213
  const ownerId = CSS.escape(dom.getTriggerGroupId(ctx));
176
214
  const selector = `[role=tab][data-ownedby='${ownerId}']:not([disabled])`;
@@ -184,7 +222,7 @@ var dom = {
184
222
  if (!ctx.value)
185
223
  return;
186
224
  const id = dom.getContentId(ctx, ctx.value);
187
- return dom.getRootNode(ctx).getElementById(id);
225
+ return dom.getById(ctx, id);
188
226
  },
189
227
  getRectById: (ctx, id) => {
190
228
  var _a;
@@ -206,7 +244,7 @@ var dom = {
206
244
  width: `${tab.offsetWidth}px`
207
245
  };
208
246
  }
209
- };
247
+ });
210
248
 
211
249
  // src/tabs.connect.ts
212
250
  function connect(state, send, normalize) {
@@ -351,9 +389,9 @@ function connect(state, send, normalize) {
351
389
  }
352
390
 
353
391
  // src/tabs.machine.ts
354
- import { createMachine, guards, ref } from "@zag-js/core";
392
+ import { createMachine, guards } from "@zag-js/core";
355
393
  var { not } = guards;
356
- function machine(ctx = {}) {
394
+ function machine(ctx) {
357
395
  return createMachine({
358
396
  initial: "unknown",
359
397
  context: __spreadValues({
@@ -362,7 +400,6 @@ function machine(ctx = {}) {
362
400
  activationMode: "automatic",
363
401
  value: null,
364
402
  focusedValue: null,
365
- uid: "",
366
403
  previousValues: [],
367
404
  indicatorRect: { left: "0px", top: "0px", width: "0px", height: "0px" },
368
405
  hasMeasuredRect: false,
@@ -393,7 +430,7 @@ function machine(ctx = {}) {
393
430
  on: {
394
431
  SETUP: {
395
432
  target: "idle",
396
- actions: ["setupDocument", "checkRenderedElements", "setIndicatorRect", "setContentTabIndex"]
433
+ actions: ["checkRenderedElements", "setIndicatorRect", "setContentTabIndex"]
397
434
  }
398
435
  }
399
436
  },
@@ -463,13 +500,6 @@ function machine(ctx = {}) {
463
500
  selectOnFocus: (ctx2) => ctx2.activationMode === "automatic"
464
501
  },
465
502
  actions: {
466
- setupDocument(ctx2, evt) {
467
- ctx2.uid = evt.id;
468
- if (evt.doc)
469
- ctx2.doc = ref(evt.doc);
470
- if (evt.root)
471
- ctx2.rootNode = ref(evt.root);
472
- },
473
503
  setFocusedValue(ctx2, evt) {
474
504
  ctx2.focusedValue = evt.value;
475
505
  },
@@ -520,9 +550,7 @@ function machine(ctx = {}) {
520
550
  });
521
551
  },
522
552
  checkRenderedElements(ctx2) {
523
- nextTick(() => {
524
- ctx2.isIndicatorRendered = !!dom.getIndicatorEl(ctx2);
525
- });
553
+ ctx2.isIndicatorRendered = !!dom.getIndicatorEl(ctx2);
526
554
  },
527
555
  clearMeasured(ctx2) {
528
556
  ctx2.hasMeasuredRect = false;
@@ -1,7 +1,21 @@
1
1
  import type { MachineContext as Ctx } from "./tabs.types";
2
2
  export declare const dom: {
3
- getDoc: (ctx: Ctx) => Document;
4
- getRootNode: (ctx: Ctx) => Document | ShadowRoot;
3
+ getRootNode: (ctx: {
4
+ getRootNode?: () => Node | Document | ShadowRoot;
5
+ }) => Document | ShadowRoot;
6
+ getDoc: (ctx: {
7
+ getRootNode?: () => Node | Document | ShadowRoot;
8
+ }) => Document;
9
+ getWin: (ctx: {
10
+ getRootNode?: () => Node | Document | ShadowRoot;
11
+ }) => Window & typeof globalThis;
12
+ getActiveElement: (ctx: {
13
+ getRootNode?: () => Node | Document | ShadowRoot;
14
+ }) => HTMLElement;
15
+ getById: <T_1 = HTMLElement>(ctx: {
16
+ getRootNode?: () => Node | Document | ShadowRoot;
17
+ }, id: string) => T_1;
18
+ } & {
5
19
  getRootId: (ctx: Ctx) => string;
6
20
  getTriggerGroupId: (ctx: Ctx) => string;
7
21
  getContentId: (ctx: Ctx, id: string) => string;
@@ -1,2 +1,2 @@
1
1
  import type { MachineContext, MachineState, UserDefinedContext } from "./tabs.types";
2
- export declare function machine(ctx?: UserDefinedContext): import("@zag-js/core").Machine<MachineContext, MachineState, import("@zag-js/core").StateMachine.AnyEventObject>;
2
+ export declare function machine(ctx: UserDefinedContext): import("@zag-js/core").Machine<MachineContext, MachineState, import("@zag-js/core").StateMachine.AnyEventObject>;
@@ -1,5 +1,5 @@
1
1
  import type { StateMachine as S } from "@zag-js/core";
2
- import type { Context, DirectionProperty } from "@zag-js/types";
2
+ import type { CommonProperties, Context, DirectionProperty, RequiredBy } from "@zag-js/types";
3
3
  declare type IntlMessages = {
4
4
  tablistLabel?: string;
5
5
  deleteLabel?(value: string): string;
@@ -11,7 +11,7 @@ declare type ElementIds = Partial<{
11
11
  contentGroup: string;
12
12
  content: string;
13
13
  }>;
14
- declare type PublicContext = DirectionProperty & {
14
+ declare type PublicContext = DirectionProperty & CommonProperties & {
15
15
  /**
16
16
  * The ids of the elements in the tabs. Useful for composition.
17
17
  */
@@ -67,7 +67,7 @@ declare type PublicContext = DirectionProperty & {
67
67
  value: string;
68
68
  }) => void;
69
69
  };
70
- export declare type UserDefinedContext = Partial<PublicContext>;
70
+ export declare type UserDefinedContext = RequiredBy<PublicContext, "id">;
71
71
  declare type ComputedContext = Readonly<{
72
72
  /**
73
73
  * @computed
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zag-js/tabs",
3
- "version": "0.1.9",
3
+ "version": "0.1.10",
4
4
  "description": "Core logic for the tabs widget implemented as a state machine",
5
5
  "keywords": [
6
6
  "js",
@@ -29,9 +29,11 @@
29
29
  "url": "https://github.com/chakra-ui/zag/issues"
30
30
  },
31
31
  "dependencies": {
32
- "@zag-js/core": "0.1.7",
33
- "@zag-js/dom-utils": "0.1.6",
34
- "@zag-js/types": "0.2.1",
32
+ "@zag-js/core": "0.1.8",
33
+ "@zag-js/types": "0.2.2"
34
+ },
35
+ "devDependencies": {
36
+ "@zag-js/dom-utils": "0.1.7",
35
37
  "@zag-js/utils": "0.1.2"
36
38
  },
37
39
  "scripts": {