@zag-js/tabs 0.1.7 → 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.d.ts CHANGED
@@ -1,4 +1,3 @@
1
1
  export { connect } from "./tabs.connect";
2
2
  export { machine } from "./tabs.machine";
3
3
  export type { UserDefinedContext as Context } from "./tabs.types";
4
- //# sourceMappingURL=index.d.ts.map
package/dist/index.js CHANGED
@@ -1,3 +1,4 @@
1
+ "use strict";
1
2
  var __defProp = Object.defineProperty;
2
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
@@ -39,99 +40,99 @@ __export(src_exports, {
39
40
  module.exports = __toCommonJS(src_exports);
40
41
 
41
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
+ };
42
59
  var dataAttr = (guard) => {
43
60
  return guard ? "" : void 0;
44
61
  };
45
- function getStyleCache() {
46
- ;
47
- globalThis.__styleCache__ = globalThis.__styleCache__ || /* @__PURE__ */ new WeakMap();
48
- return globalThis.__styleCache__;
49
- }
50
- function getComputedStyle2(el) {
62
+ var isDom = () => typeof window !== "undefined";
63
+ function getPlatform() {
51
64
  var _a;
52
- if (!el)
53
- return {};
54
- const cache = getStyleCache();
55
- let style = cache.get(el);
56
- if (!style) {
57
- const win = (_a = el == null ? void 0 : el.ownerDocument.defaultView) != null ? _a : window;
58
- style = win.getComputedStyle(el);
59
- cache.set(el, style);
60
- }
61
- return style;
65
+ const agent = navigator.userAgentData;
66
+ return (_a = agent == null ? void 0 : agent.platform) != null ? _a : navigator.platform;
62
67
  }
63
- function nextTick(fn) {
64
- const set = /* @__PURE__ */ new Set();
65
- function raf2(fn2) {
66
- const id = globalThis.requestAnimationFrame(fn2);
67
- set.add(() => globalThis.cancelAnimationFrame(id));
68
- }
69
- raf2(() => raf2(fn));
70
- return function cleanup() {
71
- set.forEach(function(fn2) {
72
- fn2();
73
- });
74
- };
68
+ var pt = (v) => isDom() && v.test(getPlatform());
69
+ var vn = (v) => isDom() && v.test(navigator.vendor);
70
+ var isSafari = () => isApple() && vn(/apple/i);
71
+ var isApple = () => pt(/mac|iphone|ipad|ipod/i);
72
+ function isDocument(el) {
73
+ return el.nodeType === Node.DOCUMENT_NODE;
75
74
  }
76
- function raf(fn) {
77
- const id = globalThis.requestAnimationFrame(fn);
78
- return function cleanup() {
79
- globalThis.cancelAnimationFrame(id);
75
+ function isWindow(value) {
76
+ return (value == null ? void 0 : value.toString()) === "[object Window]";
77
+ }
78
+ function isFrame(element) {
79
+ return element.localName === "iframe";
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)
80
102
  };
103
+ return __spreadValues2(__spreadValues2({}, dom2), helpers);
81
104
  }
82
- var isDom = () => typeof window !== "undefined";
83
- var isTouchDevice = isDom() && !!navigator.maxTouchPoints;
84
105
  function isHTMLElement(v) {
85
106
  return typeof v === "object" && (v == null ? void 0 : v.nodeType) === Node.ELEMENT_NODE && typeof (v == null ? void 0 : v.nodeName) === "string";
86
107
  }
87
- var isDisabled = (el) => {
88
- return (el == null ? void 0 : el.getAttribute("disabled")) != null || !!(el == null ? void 0 : el.getAttribute("aria-disabled")) === true;
89
- };
90
- var focusableSelector = /* @__PURE__ */ [
91
- "input:not([disabled]):not([type=hidden])",
92
- "select:not([disabled])",
93
- "textarea:not([disabled])",
94
- "button:not([disabled])",
95
- "embed",
96
- "iframe",
97
- "object",
98
- "a[href]",
99
- "area[href]",
100
- "[tabindex]",
101
- "audio[controls]",
102
- "video[controls]",
103
- "*[tabindex]:not([aria-disabled])",
104
- "[contenteditable]:not([contenteditable=false])",
105
- "details > summary:first-of-type"
106
- ].join(",");
107
- function isHidden(el, until) {
108
- const style = getComputedStyle2(el);
109
- if (!el || style.getPropertyValue("visibility") === "hidden")
110
- return true;
111
- while (el) {
112
- if (until != null && el === until)
113
- return false;
114
- if (style.getPropertyValue("display") === "none")
115
- return true;
116
- el = el.parentElement;
117
- }
118
- return false;
108
+ function isVisible(el) {
109
+ if (!isHTMLElement(el))
110
+ return false;
111
+ return el.offsetWidth > 0 || el.offsetHeight > 0 || el.getClientRects().length > 0;
119
112
  }
120
- var getFocusables = (el, includeContainer = false) => {
121
- if (!el)
113
+ 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";
114
+ var getFocusables = (container, includeContainer = false) => {
115
+ if (!container)
122
116
  return [];
123
- let els = Array.from(el.querySelectorAll(focusableSelector));
124
- const shouldAddContainer = includeContainer == true || includeContainer == "if-empty" && els.length === 0;
125
- if (shouldAddContainer && isHTMLElement(el)) {
126
- els.unshift(el);
117
+ const elements = Array.from(container.querySelectorAll(focusableSelector));
118
+ const include = includeContainer == true || includeContainer == "if-empty" && elements.length === 0;
119
+ if (include && isHTMLElement(container) && isFocusable(container)) {
120
+ elements.unshift(container);
127
121
  }
128
- return els.filter((el2) => isFocusable(el2) && !isHidden(el2));
122
+ const focusableElements = elements.filter(isFocusable);
123
+ focusableElements.forEach((element, i) => {
124
+ if (isFrame(element) && element.contentDocument) {
125
+ const frameBody = element.contentDocument.body;
126
+ focusableElements.splice(i, 1, ...getFocusables(frameBody));
127
+ }
128
+ });
129
+ return focusableElements;
129
130
  };
130
- var isFocusable = (el) => {
131
- if (!isHTMLElement(el) || isHidden(el) || isDisabled(el))
131
+ function isFocusable(element) {
132
+ if (!element)
132
133
  return false;
133
- return el == null ? void 0 : el.matches(focusableSelector);
134
- };
134
+ return element.matches(focusableSelector) && isVisible(element);
135
+ }
135
136
  var rtlKeyMap = {
136
137
  ArrowLeft: "ArrowRight",
137
138
  ArrowRight: "ArrowLeft",
@@ -158,6 +159,25 @@ function getEventKey(event, options = {}) {
158
159
  }
159
160
  return key;
160
161
  }
162
+ function nextTick(fn) {
163
+ const set = /* @__PURE__ */ new Set();
164
+ function raf2(fn2) {
165
+ const id = globalThis.requestAnimationFrame(fn2);
166
+ set.add(() => globalThis.cancelAnimationFrame(id));
167
+ }
168
+ raf2(() => raf2(fn));
169
+ return function cleanup() {
170
+ set.forEach(function(fn2) {
171
+ fn2();
172
+ });
173
+ };
174
+ }
175
+ function raf(fn) {
176
+ const id = globalThis.requestAnimationFrame(fn);
177
+ return function cleanup() {
178
+ globalThis.cancelAnimationFrame(id);
179
+ };
180
+ }
161
181
  function queryAll(root, selector) {
162
182
  var _a;
163
183
  return Array.from((_a = root == null ? void 0 : root.querySelectorAll(selector)) != null ? _a : []);
@@ -181,112 +201,38 @@ function prevById(v, id, loop = true) {
181
201
  idx = loop ? (idx - 1 + v.length) % v.length : Math.max(0, idx - 1);
182
202
  return v[idx];
183
203
  }
184
- var getValueText = (item) => {
185
- var _a, _b;
186
- return (_b = (_a = item.dataset.valuetext) != null ? _a : item.textContent) != null ? _b : "";
187
- };
188
- var match = (valueText, query2) => valueText.toLowerCase().startsWith(query2.toLowerCase());
189
- var wrap = (v, idx) => {
190
- return v.map((_, index) => v[(Math.max(idx, 0) + index) % v.length]);
191
- };
192
- function findByText(v, text, currentId) {
193
- const index = currentId ? indexOfId(v, currentId) : -1;
194
- let items = currentId ? wrap(v, index) : v;
195
- const isSingleKey = text.length === 1;
196
- if (isSingleKey) {
197
- items = items.filter((item) => item.id !== currentId);
198
- }
199
- return items.find((item) => match(getValueText(item), text));
200
- }
201
- function findByTypeahead(_items, options) {
202
- const { state: state2, activeId, key, timeout = 350 } = options;
203
- const search = state2.keysSoFar + key;
204
- const isRepeated = search.length > 1 && Array.from(search).every((char) => char === search[0]);
205
- const query2 = isRepeated ? search[0] : search;
206
- let items = _items.slice();
207
- const next = findByText(items, query2, activeId);
208
- function cleanup() {
209
- clearTimeout(state2.timer);
210
- state2.timer = -1;
211
- }
212
- function update(value) {
213
- state2.keysSoFar = value;
214
- cleanup();
215
- if (value !== "") {
216
- state2.timer = +setTimeout(() => {
217
- update("");
218
- cleanup();
219
- }, timeout);
220
- }
221
- }
222
- update(search);
223
- return next;
224
- }
225
- findByTypeahead.defaultOptions = {
226
- keysSoFar: "",
227
- timer: -1
228
- };
229
-
230
- // ../../types/dist/index.mjs
231
- function createNormalizer(fn) {
232
- return new Proxy({}, {
233
- get() {
234
- return fn;
235
- }
236
- });
237
- }
238
- var normalizeProp = createNormalizer((v) => v);
239
204
 
240
205
  // ../../utilities/core/dist/index.mjs
241
206
  var first = (v) => v[0];
242
207
  var last = (v) => v[v.length - 1];
243
- var isDom2 = () => typeof window !== "undefined";
244
- function getPlatform() {
245
- var _a;
246
- const agent = navigator.userAgentData;
247
- return (_a = agent == null ? void 0 : agent.platform) != null ? _a : navigator.platform;
248
- }
249
- var pt = (v) => isDom2() && v.test(getPlatform());
250
- var vn = (v) => isDom2() && v.test(navigator.vendor);
251
- var isTouchDevice2 = isDom2() && !!navigator.maxTouchPoints;
252
- var isSafari = () => isApple() && vn(/apple/i);
253
- var isApple = () => pt(/mac|iphone|ipad|ipod/i);
254
208
 
255
209
  // src/tabs.dom.ts
256
- var dom = {
257
- getDoc: (ctx) => {
258
- var _a;
259
- return (_a = ctx.doc) != null ? _a : document;
260
- },
261
- getRootNode: (ctx) => {
262
- var _a;
263
- return (_a = ctx.rootNode) != null ? _a : dom.getDoc(ctx);
264
- },
210
+ var dom = defineDomHelpers({
265
211
  getRootId: (ctx) => {
266
212
  var _a, _b;
267
- 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}`;
268
214
  },
269
215
  getTriggerGroupId: (ctx) => {
270
216
  var _a, _b;
271
- 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`;
272
218
  },
273
219
  getContentId: (ctx, id) => {
274
220
  var _a, _b;
275
- 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}`;
276
222
  },
277
223
  getContentGroupId: (ctx) => {
278
224
  var _a, _b;
279
- 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`;
280
226
  },
281
227
  getTriggerId: (ctx, id) => {
282
228
  var _a, _b;
283
- 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}`;
284
230
  },
285
- getIndicatorId: (ctx) => `tabs:${ctx.uid}:indicator`,
286
- getTriggerGroupEl: (ctx) => dom.getRootNode(ctx).getElementById(dom.getTriggerGroupId(ctx)),
287
- getContentEl: (ctx, id) => dom.getRootNode(ctx).getElementById(dom.getContentId(ctx, id)),
288
- getTriggerEl: (ctx, id) => dom.getRootNode(ctx).getElementById(dom.getTriggerId(ctx, id)),
289
- 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)),
290
236
  getElements: (ctx) => {
291
237
  const ownerId = CSS.escape(dom.getTriggerGroupId(ctx));
292
238
  const selector = `[role=tab][data-ownedby='${ownerId}']:not([disabled])`;
@@ -300,7 +246,7 @@ var dom = {
300
246
  if (!ctx.value)
301
247
  return;
302
248
  const id = dom.getContentId(ctx, ctx.value);
303
- return dom.getRootNode(ctx).getElementById(id);
249
+ return dom.getById(ctx, id);
304
250
  },
305
251
  getRectById: (ctx, id) => {
306
252
  var _a;
@@ -322,10 +268,10 @@ var dom = {
322
268
  width: `${tab.offsetWidth}px`
323
269
  };
324
270
  }
325
- };
271
+ });
326
272
 
327
273
  // src/tabs.connect.ts
328
- function connect(state, send, normalize = normalizeProp) {
274
+ function connect(state, send, normalize) {
329
275
  const messages = state.context.messages;
330
276
  const isFocused = state.matches("focused");
331
277
  return {
@@ -469,7 +415,7 @@ function connect(state, send, normalize = normalizeProp) {
469
415
  // src/tabs.machine.ts
470
416
  var import_core = require("@zag-js/core");
471
417
  var { not } = import_core.guards;
472
- function machine(ctx = {}) {
418
+ function machine(ctx) {
473
419
  return (0, import_core.createMachine)({
474
420
  initial: "unknown",
475
421
  context: __spreadValues({
@@ -478,7 +424,6 @@ function machine(ctx = {}) {
478
424
  activationMode: "automatic",
479
425
  value: null,
480
426
  focusedValue: null,
481
- uid: "",
482
427
  previousValues: [],
483
428
  indicatorRect: { left: "0px", top: "0px", width: "0px", height: "0px" },
484
429
  hasMeasuredRect: false,
@@ -509,7 +454,7 @@ function machine(ctx = {}) {
509
454
  on: {
510
455
  SETUP: {
511
456
  target: "idle",
512
- actions: ["setupDocument", "checkRenderedElements", "setIndicatorRect", "setContentTabIndex"]
457
+ actions: ["checkRenderedElements", "setIndicatorRect", "setContentTabIndex"]
513
458
  }
514
459
  }
515
460
  },
@@ -579,13 +524,6 @@ function machine(ctx = {}) {
579
524
  selectOnFocus: (ctx2) => ctx2.activationMode === "automatic"
580
525
  },
581
526
  actions: {
582
- setupDocument(ctx2, evt) {
583
- ctx2.uid = evt.id;
584
- if (evt.doc)
585
- ctx2.doc = (0, import_core.ref)(evt.doc);
586
- if (evt.root)
587
- ctx2.rootNode = (0, import_core.ref)(evt.root);
588
- },
589
527
  setFocusedValue(ctx2, evt) {
590
528
  ctx2.focusedValue = evt.value;
591
529
  },
@@ -636,9 +574,7 @@ function machine(ctx = {}) {
636
574
  });
637
575
  },
638
576
  checkRenderedElements(ctx2) {
639
- nextTick(() => {
640
- ctx2.isIndicatorRendered = !!dom.getIndicatorEl(ctx2);
641
- });
577
+ ctx2.isIndicatorRendered = !!dom.getIndicatorEl(ctx2);
642
578
  },
643
579
  clearMeasured(ctx2) {
644
580
  ctx2.hasMeasuredRect = false;
@@ -673,4 +609,3 @@ function machine(ctx = {}) {
673
609
  }
674
610
  });
675
611
  }
676
- //# sourceMappingURL=index.js.map