@zag-js/tabs 0.50.0 → 0.51.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,585 +1,2 @@
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/index.ts
21
- var src_exports = {};
22
- __export(src_exports, {
23
- anatomy: () => anatomy,
24
- connect: () => connect,
25
- contentProps: () => contentProps,
26
- machine: () => machine,
27
- props: () => props,
28
- splitContentProps: () => splitContentProps,
29
- splitProps: () => splitProps,
30
- splitTriggerProps: () => splitTriggerProps,
31
- triggerProps: () => triggerProps
32
- });
33
- module.exports = __toCommonJS(src_exports);
34
-
35
- // src/tabs.anatomy.ts
36
- var import_anatomy = require("@zag-js/anatomy");
37
- var anatomy = (0, import_anatomy.createAnatomy)("tabs").parts("root", "list", "trigger", "content", "indicator");
38
- var parts = anatomy.build();
39
-
40
- // src/tabs.connect.ts
41
- var import_dom_event = require("@zag-js/dom-event");
42
- var import_dom_query2 = require("@zag-js/dom-query");
43
-
44
- // src/tabs.dom.ts
45
- var import_dom_query = require("@zag-js/dom-query");
46
- var import_utils = require("@zag-js/utils");
47
- var dom = (0, import_dom_query.createScope)({
48
- getRootId: (ctx) => ctx.ids?.root ?? `tabs:${ctx.id}`,
49
- getListId: (ctx) => ctx.ids?.list ?? `tabs:${ctx.id}:list`,
50
- getContentId: (ctx, id) => ctx.ids?.content ?? `tabs:${ctx.id}:content-${id}`,
51
- getTriggerId: (ctx, id) => ctx.ids?.trigger ?? `tabs:${ctx.id}:trigger-${id}`,
52
- getIndicatorId: (ctx) => ctx.ids?.indicator ?? `tabs:${ctx.id}:indicator`,
53
- getListEl: (ctx) => dom.getById(ctx, dom.getListId(ctx)),
54
- getContentEl: (ctx, id) => dom.getById(ctx, dom.getContentId(ctx, id)),
55
- getTriggerEl: (ctx, id) => dom.getById(ctx, dom.getTriggerId(ctx, id)),
56
- getIndicatorEl: (ctx) => dom.getById(ctx, dom.getIndicatorId(ctx)),
57
- getElements: (ctx) => {
58
- const ownerId = CSS.escape(dom.getListId(ctx));
59
- const selector = `[role=tab][data-ownedby='${ownerId}']:not([disabled])`;
60
- return (0, import_dom_query.queryAll)(dom.getListEl(ctx), selector);
61
- },
62
- getFirstTriggerEl: (ctx) => (0, import_utils.first)(dom.getElements(ctx)),
63
- getLastTriggerEl: (ctx) => (0, import_utils.last)(dom.getElements(ctx)),
64
- getNextTriggerEl: (ctx, id) => (0, import_dom_query.nextById)(dom.getElements(ctx), dom.getTriggerId(ctx, id), ctx.loopFocus),
65
- getPrevTriggerEl: (ctx, id) => (0, import_dom_query.prevById)(dom.getElements(ctx), dom.getTriggerId(ctx, id), ctx.loopFocus),
66
- getSelectedContentEl: (ctx) => {
67
- if (!ctx.value)
68
- return;
69
- return dom.getContentEl(ctx, ctx.value);
70
- },
71
- getSelectedTriggerEl: (ctx) => {
72
- if (!ctx.value)
73
- return;
74
- return dom.getTriggerEl(ctx, ctx.value);
75
- },
76
- getOffsetRect: (el) => {
77
- return {
78
- left: el?.offsetLeft ?? 0,
79
- top: el?.offsetTop ?? 0,
80
- width: el?.offsetWidth ?? 0,
81
- height: el?.offsetHeight ?? 0
82
- };
83
- },
84
- getRectById: (ctx, id) => {
85
- const tab = (0, import_dom_query.itemById)(dom.getElements(ctx), dom.getTriggerId(ctx, id));
86
- return dom.resolveRect(dom.getOffsetRect(tab));
87
- },
88
- resolveRect: (rect) => ({
89
- width: `${rect.width}px`,
90
- height: `${rect.height}px`,
91
- left: `${rect.left}px`,
92
- top: `${rect.top}px`
93
- })
94
- });
95
-
96
- // src/tabs.connect.ts
97
- function connect(state, send, normalize) {
98
- const translations = state.context.translations;
99
- const focused = state.matches("focused");
100
- const isVertical = state.context.orientation === "vertical";
101
- const isHorizontal = state.context.orientation === "horizontal";
102
- const composite = state.context.composite;
103
- const indicator = state.context.indicatorState;
104
- function getTriggerState(props2) {
105
- return {
106
- selected: state.context.value === props2.value,
107
- focused: state.context.focusedValue === props2.value,
108
- disabled: !!props2.disabled
109
- };
110
- }
111
- return {
112
- value: state.context.value,
113
- focusedValue: state.context.focusedValue,
114
- setValue(value) {
115
- send({ type: "SET_VALUE", value });
116
- },
117
- clearValue() {
118
- send({ type: "CLEAR_VALUE" });
119
- },
120
- setIndicatorRect(value) {
121
- const id = dom.getTriggerId(state.context, value);
122
- send({ type: "SET_INDICATOR_RECT", id });
123
- },
124
- syncTabIndex() {
125
- send("SYNC_TAB_INDEX");
126
- },
127
- selectNext(fromValue) {
128
- send({ type: "TAB_FOCUS", value: fromValue, src: "selectNext" });
129
- send({ type: "ARROW_NEXT", src: "selectNext" });
130
- },
131
- selectPrev(fromValue) {
132
- send({ type: "TAB_FOCUS", value: fromValue, src: "selectPrev" });
133
- send({ type: "ARROW_PREV", src: "selectPrev" });
134
- },
135
- focus() {
136
- dom.getSelectedTriggerEl(state.context)?.focus();
137
- },
138
- getTriggerState,
139
- rootProps: normalize.element({
140
- ...parts.root.attrs,
141
- id: dom.getRootId(state.context),
142
- "data-orientation": state.context.orientation,
143
- "data-focus": (0, import_dom_query2.dataAttr)(focused),
144
- dir: state.context.dir
145
- }),
146
- listProps: normalize.element({
147
- ...parts.list.attrs,
148
- id: dom.getListId(state.context),
149
- role: "tablist",
150
- dir: state.context.dir,
151
- "data-focus": (0, import_dom_query2.dataAttr)(focused),
152
- "aria-orientation": state.context.orientation,
153
- "data-orientation": state.context.orientation,
154
- "aria-label": translations?.listLabel,
155
- onKeyDown(event) {
156
- if (event.defaultPrevented)
157
- return;
158
- const evt = (0, import_dom_event.getNativeEvent)(event);
159
- if (!(0, import_dom_query2.isSelfTarget)(evt))
160
- return;
161
- if (evt.isComposing)
162
- return;
163
- const keyMap = {
164
- ArrowDown() {
165
- if (isHorizontal)
166
- return;
167
- send({ type: "ARROW_NEXT", key: "ArrowDown" });
168
- },
169
- ArrowUp() {
170
- if (isHorizontal)
171
- return;
172
- send({ type: "ARROW_PREV", key: "ArrowUp" });
173
- },
174
- ArrowLeft() {
175
- if (isVertical)
176
- return;
177
- send({ type: "ARROW_PREV", key: "ArrowLeft" });
178
- },
179
- ArrowRight() {
180
- if (isVertical)
181
- return;
182
- send({ type: "ARROW_NEXT", key: "ArrowRight" });
183
- },
184
- Home() {
185
- send("HOME");
186
- },
187
- End() {
188
- send("END");
189
- },
190
- Enter() {
191
- send({ type: "ENTER" });
192
- }
193
- };
194
- let key = (0, import_dom_event.getEventKey)(event, state.context);
195
- const exec = keyMap[key];
196
- if (exec) {
197
- event.preventDefault();
198
- exec(event);
199
- }
200
- }
201
- }),
202
- getTriggerProps(props2) {
203
- const { value, disabled } = props2;
204
- const triggerState = getTriggerState(props2);
205
- return normalize.button({
206
- ...parts.trigger.attrs,
207
- role: "tab",
208
- type: "button",
209
- disabled,
210
- dir: state.context.dir,
211
- "data-orientation": state.context.orientation,
212
- "data-disabled": (0, import_dom_query2.dataAttr)(disabled),
213
- "aria-disabled": disabled,
214
- "data-value": value,
215
- "aria-selected": triggerState.selected,
216
- "data-selected": (0, import_dom_query2.dataAttr)(triggerState.selected),
217
- "data-focus": (0, import_dom_query2.dataAttr)(triggerState.focused),
218
- "aria-controls": triggerState.selected ? dom.getContentId(state.context, value) : void 0,
219
- "data-ownedby": dom.getListId(state.context),
220
- id: dom.getTriggerId(state.context, value),
221
- tabIndex: triggerState.selected && composite ? 0 : -1,
222
- onFocus() {
223
- send({ type: "TAB_FOCUS", value });
224
- },
225
- onBlur(event) {
226
- const target = event.relatedTarget;
227
- if (target?.getAttribute("role") !== "tab") {
228
- send({ type: "TAB_BLUR" });
229
- }
230
- },
231
- onClick(event) {
232
- if (event.defaultPrevented)
233
- return;
234
- if (disabled)
235
- return;
236
- if ((0, import_dom_query2.isSafari)()) {
237
- event.currentTarget.focus();
238
- }
239
- send({ type: "TAB_CLICK", value });
240
- }
241
- });
242
- },
243
- getContentProps(props2) {
244
- const { value } = props2;
245
- const selected = state.context.value === value;
246
- return normalize.element({
247
- ...parts.content.attrs,
248
- dir: state.context.dir,
249
- id: dom.getContentId(state.context, value),
250
- tabIndex: composite ? 0 : -1,
251
- "aria-labelledby": dom.getTriggerId(state.context, value),
252
- role: "tabpanel",
253
- "data-ownedby": dom.getListId(state.context),
254
- "data-selected": (0, import_dom_query2.dataAttr)(selected),
255
- "data-orientation": state.context.orientation,
256
- hidden: !selected
257
- });
258
- },
259
- indicatorProps: normalize.element({
260
- id: dom.getIndicatorId(state.context),
261
- ...parts.indicator.attrs,
262
- dir: state.context.dir,
263
- "data-orientation": state.context.orientation,
264
- style: {
265
- "--transition-property": "left, right, top, bottom, width, height",
266
- "--left": indicator.rect?.left,
267
- "--top": indicator.rect?.top,
268
- "--width": indicator.rect?.width,
269
- "--height": indicator.rect?.height,
270
- position: "absolute",
271
- willChange: "var(--transition-property)",
272
- transitionProperty: "var(--transition-property)",
273
- transitionDuration: indicator.transition ? "var(--transition-duration, 150ms)" : "0ms",
274
- transitionTimingFunction: "var(--transition-timing-function)",
275
- [isHorizontal ? "left" : "top"]: isHorizontal ? "var(--left)" : "var(--top)"
276
- }
277
- })
278
- };
279
- }
280
-
281
- // src/tabs.machine.ts
282
- var import_core = require("@zag-js/core");
283
- var import_dom_event2 = require("@zag-js/dom-event");
284
- var import_dom_query3 = require("@zag-js/dom-query");
285
- var import_element_rect = require("@zag-js/element-rect");
286
- var import_utils2 = require("@zag-js/utils");
287
- var { not } = import_core.guards;
288
- function machine(userContext) {
289
- const ctx = (0, import_utils2.compact)(userContext);
290
- return (0, import_core.createMachine)(
291
- {
292
- initial: "idle",
293
- context: {
294
- dir: "ltr",
295
- orientation: "horizontal",
296
- activationMode: "automatic",
297
- value: null,
298
- loopFocus: true,
299
- composite: true,
300
- ...ctx,
301
- focusedValue: ctx.value ?? null,
302
- indicatorState: {
303
- rendered: false,
304
- transition: false,
305
- rect: { left: "0px", top: "0px", width: "0px", height: "0px" }
306
- }
307
- },
308
- watch: {
309
- value: ["allowIndicatorTransition", "syncIndicatorRect", "syncTabIndex", "clickIfLink"],
310
- dir: ["syncIndicatorRect"],
311
- orientation: ["syncIndicatorRect"]
312
- },
313
- on: {
314
- SET_VALUE: {
315
- actions: "setValue"
316
- },
317
- CLEAR_VALUE: {
318
- actions: "clearValue"
319
- },
320
- SET_INDICATOR_RECT: {
321
- actions: "setIndicatorRect"
322
- },
323
- SYNC_TAB_INDEX: {
324
- actions: "syncTabIndex"
325
- }
326
- },
327
- created: ["syncFocusedValue"],
328
- entry: ["checkRenderedElements", "syncIndicatorRect", "syncTabIndex"],
329
- exit: ["cleanupObserver"],
330
- states: {
331
- idle: {
332
- on: {
333
- TAB_FOCUS: {
334
- target: "focused",
335
- actions: "setFocusedValue"
336
- },
337
- TAB_CLICK: {
338
- target: "focused",
339
- actions: ["setFocusedValue", "setValue"]
340
- }
341
- }
342
- },
343
- focused: {
344
- on: {
345
- TAB_CLICK: {
346
- target: "focused",
347
- actions: ["setFocusedValue", "setValue"]
348
- },
349
- ARROW_PREV: [
350
- {
351
- guard: "selectOnFocus",
352
- actions: ["focusPrevTab", "selectFocusedTab"]
353
- },
354
- {
355
- actions: "focusPrevTab"
356
- }
357
- ],
358
- ARROW_NEXT: [
359
- {
360
- guard: "selectOnFocus",
361
- actions: ["focusNextTab", "selectFocusedTab"]
362
- },
363
- {
364
- actions: "focusNextTab"
365
- }
366
- ],
367
- HOME: [
368
- {
369
- guard: "selectOnFocus",
370
- actions: ["focusFirstTab", "selectFocusedTab"]
371
- },
372
- {
373
- actions: "focusFirstTab"
374
- }
375
- ],
376
- END: [
377
- {
378
- guard: "selectOnFocus",
379
- actions: ["focusLastTab", "selectFocusedTab"]
380
- },
381
- {
382
- actions: "focusLastTab"
383
- }
384
- ],
385
- ENTER: {
386
- guard: not("selectOnFocus"),
387
- actions: "selectFocusedTab"
388
- },
389
- TAB_FOCUS: {
390
- actions: ["setFocusedValue"]
391
- },
392
- TAB_BLUR: {
393
- target: "idle",
394
- actions: "clearFocusedValue"
395
- }
396
- }
397
- }
398
- }
399
- },
400
- {
401
- guards: {
402
- selectOnFocus: (ctx2) => ctx2.activationMode === "automatic"
403
- },
404
- actions: {
405
- syncFocusedValue(ctx2) {
406
- if (ctx2.value != null && ctx2.focusedValue == null) {
407
- ctx2.focusedValue = ctx2.value;
408
- }
409
- },
410
- selectFocusedTab(ctx2) {
411
- (0, import_dom_query3.raf)(() => {
412
- set.value(ctx2, ctx2.focusedValue);
413
- });
414
- },
415
- setFocusedValue(ctx2, evt) {
416
- if (evt.value == null)
417
- return;
418
- set.focusedValue(ctx2, evt.value);
419
- },
420
- clearFocusedValue(ctx2) {
421
- set.focusedValue(ctx2, null);
422
- },
423
- setValue(ctx2, evt) {
424
- set.value(ctx2, evt.value);
425
- },
426
- clearValue(ctx2) {
427
- set.value(ctx2, null);
428
- },
429
- focusFirstTab(ctx2) {
430
- (0, import_dom_query3.raf)(() => {
431
- dom.getFirstTriggerEl(ctx2)?.focus();
432
- });
433
- },
434
- focusLastTab(ctx2) {
435
- (0, import_dom_query3.raf)(() => {
436
- dom.getLastTriggerEl(ctx2)?.focus();
437
- });
438
- },
439
- focusNextTab(ctx2) {
440
- if (!ctx2.focusedValue)
441
- return;
442
- const triggerEl = dom.getNextTriggerEl(ctx2, ctx2.focusedValue);
443
- (0, import_dom_query3.raf)(() => {
444
- if (ctx2.composite) {
445
- triggerEl?.focus();
446
- } else if (triggerEl?.dataset.value != null) {
447
- set.focusedValue(ctx2, triggerEl.dataset.value);
448
- }
449
- });
450
- },
451
- focusPrevTab(ctx2) {
452
- if (!ctx2.focusedValue)
453
- return;
454
- const triggerEl = dom.getPrevTriggerEl(ctx2, ctx2.focusedValue);
455
- (0, import_dom_query3.raf)(() => {
456
- if (ctx2.composite) {
457
- triggerEl?.focus();
458
- } else if (triggerEl?.dataset.value != null) {
459
- set.focusedValue(ctx2, triggerEl.dataset.value);
460
- }
461
- });
462
- },
463
- checkRenderedElements(ctx2) {
464
- ctx2.indicatorState.rendered = !!dom.getIndicatorEl(ctx2);
465
- },
466
- syncTabIndex(ctx2) {
467
- (0, import_dom_query3.raf)(() => {
468
- const contentEl = dom.getSelectedContentEl(ctx2);
469
- if (!contentEl)
470
- return;
471
- const focusables = (0, import_dom_query3.getFocusables)(contentEl);
472
- if (focusables.length > 0) {
473
- contentEl.removeAttribute("tabindex");
474
- } else {
475
- contentEl.setAttribute("tabindex", "0");
476
- }
477
- });
478
- },
479
- cleanupObserver(ctx2) {
480
- ctx2.indicatorCleanup?.();
481
- },
482
- allowIndicatorTransition(ctx2) {
483
- ctx2.indicatorState.transition = true;
484
- },
485
- setIndicatorRect(ctx2, evt) {
486
- const value = evt.id ?? ctx2.value;
487
- if (!ctx2.indicatorState.rendered || !value)
488
- return;
489
- const triggerEl = dom.getTriggerEl(ctx2, value);
490
- if (!triggerEl)
491
- return;
492
- ctx2.indicatorState.rect = dom.getRectById(ctx2, value);
493
- (0, import_dom_query3.nextTick)(() => {
494
- ctx2.indicatorState.transition = false;
495
- });
496
- },
497
- syncIndicatorRect(ctx2) {
498
- ctx2.indicatorCleanup?.();
499
- const value = ctx2.value;
500
- if (!ctx2.indicatorState.rendered || !value)
501
- return;
502
- const triggerEl = dom.getSelectedTriggerEl(ctx2);
503
- if (!triggerEl)
504
- return;
505
- ctx2.indicatorCleanup = (0, import_element_rect.trackElementRect)(triggerEl, {
506
- getRect(el) {
507
- return dom.getOffsetRect(el);
508
- },
509
- onChange(rect) {
510
- ctx2.indicatorState.rect = dom.resolveRect(rect);
511
- (0, import_dom_query3.nextTick)(() => {
512
- ctx2.indicatorState.transition = false;
513
- });
514
- }
515
- });
516
- },
517
- clickIfLink(ctx2) {
518
- (0, import_dom_event2.clickIfLink)(dom.getSelectedTriggerEl(ctx2));
519
- }
520
- }
521
- }
522
- );
523
- }
524
- var invoke = {
525
- change: (ctx) => {
526
- if (ctx.value == null)
527
- return;
528
- ctx.onValueChange?.({ value: ctx.value });
529
- },
530
- focusChange: (ctx) => {
531
- if (ctx.focusedValue == null)
532
- return;
533
- ctx.onFocusChange?.({ focusedValue: ctx.focusedValue });
534
- }
535
- };
536
- var set = {
537
- value: (ctx, value) => {
538
- if ((0, import_utils2.isEqual)(value, ctx.value))
539
- return;
540
- ctx.value = value;
541
- invoke.change(ctx);
542
- },
543
- focusedValue: (ctx, value) => {
544
- if ((0, import_utils2.isEqual)(value, ctx.focusedValue))
545
- return;
546
- ctx.focusedValue = value;
547
- invoke.focusChange(ctx);
548
- }
549
- };
550
-
551
- // src/tabs.props.ts
552
- var import_types = require("@zag-js/types");
553
- var import_utils3 = require("@zag-js/utils");
554
- var props = (0, import_types.createProps)()([
555
- "activationMode",
556
- "composite",
557
- "dir",
558
- "getRootNode",
559
- "id",
560
- "ids",
561
- "loopFocus",
562
- "onFocusChange",
563
- "onValueChange",
564
- "orientation",
565
- "translations",
566
- "value"
567
- ]);
568
- var splitProps = (0, import_utils3.createSplitProps)(props);
569
- var triggerProps = (0, import_types.createProps)()(["disabled", "value"]);
570
- var splitTriggerProps = (0, import_utils3.createSplitProps)(triggerProps);
571
- var contentProps = (0, import_types.createProps)()(["value"]);
572
- var splitContentProps = (0, import_utils3.createSplitProps)(contentProps);
573
- // Annotate the CommonJS export names for ESM import in node:
574
- 0 && (module.exports = {
575
- anatomy,
576
- connect,
577
- contentProps,
578
- machine,
579
- props,
580
- splitContentProps,
581
- splitProps,
582
- splitTriggerProps,
583
- triggerProps
584
- });
1
+ "use strict";var __defProp=Object.defineProperty;var __getOwnPropDesc=Object.getOwnPropertyDescriptor;var __getOwnPropNames=Object.getOwnPropertyNames;var __hasOwnProp=Object.prototype.hasOwnProperty;var __export=(target,all)=>{for(var name in all)__defProp(target,name,{get:all[name],enumerable:true})};var __copyProps=(to,from,except,desc)=>{if(from&&typeof from==="object"||typeof from==="function"){for(let key of __getOwnPropNames(from))if(!__hasOwnProp.call(to,key)&&key!==except)__defProp(to,key,{get:()=>from[key],enumerable:!(desc=__getOwnPropDesc(from,key))||desc.enumerable})}return to};var __toCommonJS=mod=>__copyProps(__defProp({},"__esModule",{value:true}),mod);var src_exports={};__export(src_exports,{anatomy:()=>anatomy,connect:()=>connect,contentProps:()=>contentProps,machine:()=>machine,props:()=>props,splitContentProps:()=>splitContentProps,splitProps:()=>splitProps,splitTriggerProps:()=>splitTriggerProps,triggerProps:()=>triggerProps});module.exports=__toCommonJS(src_exports);var import_anatomy=require("@zag-js/anatomy");var anatomy=(0,import_anatomy.createAnatomy)("tabs").parts("root","list","trigger","content","indicator");var parts=anatomy.build();var import_dom_event=require("@zag-js/dom-event");var import_dom_query2=require("@zag-js/dom-query");var import_dom_query=require("@zag-js/dom-query");var import_utils=require("@zag-js/utils");var dom=(0,import_dom_query.createScope)({getRootId:ctx=>ctx.ids?.root??`tabs:${ctx.id}`,getListId:ctx=>ctx.ids?.list??`tabs:${ctx.id}:list`,getContentId:(ctx,id)=>ctx.ids?.content??`tabs:${ctx.id}:content-${id}`,getTriggerId:(ctx,id)=>ctx.ids?.trigger??`tabs:${ctx.id}:trigger-${id}`,getIndicatorId:ctx=>ctx.ids?.indicator??`tabs:${ctx.id}:indicator`,getListEl:ctx=>dom.getById(ctx,dom.getListId(ctx)),getContentEl:(ctx,id)=>dom.getById(ctx,dom.getContentId(ctx,id)),getTriggerEl:(ctx,id)=>dom.getById(ctx,dom.getTriggerId(ctx,id)),getIndicatorEl:ctx=>dom.getById(ctx,dom.getIndicatorId(ctx)),getElements:ctx=>{const ownerId=CSS.escape(dom.getListId(ctx));const selector=`[role=tab][data-ownedby='${ownerId}']:not([disabled])`;return(0,import_dom_query.queryAll)(dom.getListEl(ctx),selector)},getFirstTriggerEl:ctx=>(0,import_utils.first)(dom.getElements(ctx)),getLastTriggerEl:ctx=>(0,import_utils.last)(dom.getElements(ctx)),getNextTriggerEl:(ctx,id)=>(0,import_dom_query.nextById)(dom.getElements(ctx),dom.getTriggerId(ctx,id),ctx.loopFocus),getPrevTriggerEl:(ctx,id)=>(0,import_dom_query.prevById)(dom.getElements(ctx),dom.getTriggerId(ctx,id),ctx.loopFocus),getSelectedContentEl:ctx=>{if(!ctx.value)return;return dom.getContentEl(ctx,ctx.value)},getSelectedTriggerEl:ctx=>{if(!ctx.value)return;return dom.getTriggerEl(ctx,ctx.value)},getOffsetRect:el=>{return{left:el?.offsetLeft??0,top:el?.offsetTop??0,width:el?.offsetWidth??0,height:el?.offsetHeight??0}},getRectById:(ctx,id)=>{const tab=(0,import_dom_query.itemById)(dom.getElements(ctx),dom.getTriggerId(ctx,id));return dom.resolveRect(dom.getOffsetRect(tab))},resolveRect:rect=>({width:`${rect.width}px`,height:`${rect.height}px`,left:`${rect.left}px`,top:`${rect.top}px`})});function connect(state,send,normalize){const translations=state.context.translations;const focused=state.matches("focused");const isVertical=state.context.orientation==="vertical";const isHorizontal=state.context.orientation==="horizontal";const composite=state.context.composite;const indicator=state.context.indicatorState;function getTriggerState(props2){return{selected:state.context.value===props2.value,focused:state.context.focusedValue===props2.value,disabled:!!props2.disabled}}return{value:state.context.value,focusedValue:state.context.focusedValue,setValue(value){send({type:"SET_VALUE",value})},clearValue(){send({type:"CLEAR_VALUE"})},setIndicatorRect(value){const id=dom.getTriggerId(state.context,value);send({type:"SET_INDICATOR_RECT",id})},syncTabIndex(){send("SYNC_TAB_INDEX")},selectNext(fromValue){send({type:"TAB_FOCUS",value:fromValue,src:"selectNext"});send({type:"ARROW_NEXT",src:"selectNext"})},selectPrev(fromValue){send({type:"TAB_FOCUS",value:fromValue,src:"selectPrev"});send({type:"ARROW_PREV",src:"selectPrev"})},focus(){dom.getSelectedTriggerEl(state.context)?.focus()},getTriggerState,rootProps:normalize.element({...parts.root.attrs,id:dom.getRootId(state.context),"data-orientation":state.context.orientation,"data-focus":(0,import_dom_query2.dataAttr)(focused),dir:state.context.dir}),listProps:normalize.element({...parts.list.attrs,id:dom.getListId(state.context),role:"tablist",dir:state.context.dir,"data-focus":(0,import_dom_query2.dataAttr)(focused),"aria-orientation":state.context.orientation,"data-orientation":state.context.orientation,"aria-label":translations?.listLabel,onKeyDown(event){if(event.defaultPrevented)return;const evt=(0,import_dom_event.getNativeEvent)(event);if(!(0,import_dom_query2.isSelfTarget)(evt))return;if(evt.isComposing)return;const keyMap={ArrowDown(){if(isHorizontal)return;send({type:"ARROW_NEXT",key:"ArrowDown"})},ArrowUp(){if(isHorizontal)return;send({type:"ARROW_PREV",key:"ArrowUp"})},ArrowLeft(){if(isVertical)return;send({type:"ARROW_PREV",key:"ArrowLeft"})},ArrowRight(){if(isVertical)return;send({type:"ARROW_NEXT",key:"ArrowRight"})},Home(){send("HOME")},End(){send("END")},Enter(){send({type:"ENTER"})}};let key=(0,import_dom_event.getEventKey)(event,state.context);const exec=keyMap[key];if(exec){event.preventDefault();exec(event)}}}),getTriggerProps(props2){const{value,disabled}=props2;const triggerState=getTriggerState(props2);return normalize.button({...parts.trigger.attrs,role:"tab",type:"button",disabled,dir:state.context.dir,"data-orientation":state.context.orientation,"data-disabled":(0,import_dom_query2.dataAttr)(disabled),"aria-disabled":disabled,"data-value":value,"aria-selected":triggerState.selected,"data-selected":(0,import_dom_query2.dataAttr)(triggerState.selected),"data-focus":(0,import_dom_query2.dataAttr)(triggerState.focused),"aria-controls":triggerState.selected?dom.getContentId(state.context,value):void 0,"data-ownedby":dom.getListId(state.context),id:dom.getTriggerId(state.context,value),tabIndex:triggerState.selected&&composite?0:-1,onFocus(){send({type:"TAB_FOCUS",value})},onBlur(event){const target=event.relatedTarget;if(target?.getAttribute("role")!=="tab"){send({type:"TAB_BLUR"})}},onClick(event){if(event.defaultPrevented)return;if(disabled)return;if((0,import_dom_query2.isSafari)()){event.currentTarget.focus()}send({type:"TAB_CLICK",value})}})},getContentProps(props2){const{value}=props2;const selected=state.context.value===value;return normalize.element({...parts.content.attrs,dir:state.context.dir,id:dom.getContentId(state.context,value),tabIndex:composite?0:-1,"aria-labelledby":dom.getTriggerId(state.context,value),role:"tabpanel","data-ownedby":dom.getListId(state.context),"data-selected":(0,import_dom_query2.dataAttr)(selected),"data-orientation":state.context.orientation,hidden:!selected})},indicatorProps:normalize.element({id:dom.getIndicatorId(state.context),...parts.indicator.attrs,dir:state.context.dir,"data-orientation":state.context.orientation,style:{"--transition-property":"left, right, top, bottom, width, height","--left":indicator.rect?.left,"--top":indicator.rect?.top,"--width":indicator.rect?.width,"--height":indicator.rect?.height,position:"absolute",willChange:"var(--transition-property)",transitionProperty:"var(--transition-property)",transitionDuration:indicator.transition?"var(--transition-duration, 150ms)":"0ms",transitionTimingFunction:"var(--transition-timing-function)",[isHorizontal?"left":"top"]:isHorizontal?"var(--left)":"var(--top)"}})}}var import_core=require("@zag-js/core");var import_dom_event2=require("@zag-js/dom-event");var import_dom_query3=require("@zag-js/dom-query");var import_element_rect=require("@zag-js/element-rect");var import_utils2=require("@zag-js/utils");var{not}=import_core.guards;function machine(userContext){const ctx=(0,import_utils2.compact)(userContext);return(0,import_core.createMachine)({initial:"idle",context:{dir:"ltr",orientation:"horizontal",activationMode:"automatic",value:null,loopFocus:true,composite:true,...ctx,focusedValue:ctx.value??null,indicatorState:{rendered:false,transition:false,rect:{left:"0px",top:"0px",width:"0px",height:"0px"}}},watch:{value:["allowIndicatorTransition","syncIndicatorRect","syncTabIndex","clickIfLink"],dir:["syncIndicatorRect"],orientation:["syncIndicatorRect"]},on:{SET_VALUE:{actions:"setValue"},CLEAR_VALUE:{actions:"clearValue"},SET_INDICATOR_RECT:{actions:"setIndicatorRect"},SYNC_TAB_INDEX:{actions:"syncTabIndex"}},created:["syncFocusedValue"],entry:["checkRenderedElements","syncIndicatorRect","syncTabIndex"],exit:["cleanupObserver"],states:{idle:{on:{TAB_FOCUS:{target:"focused",actions:"setFocusedValue"},TAB_CLICK:{target:"focused",actions:["setFocusedValue","setValue"]}}},focused:{on:{TAB_CLICK:{target:"focused",actions:["setFocusedValue","setValue"]},ARROW_PREV:[{guard:"selectOnFocus",actions:["focusPrevTab","selectFocusedTab"]},{actions:"focusPrevTab"}],ARROW_NEXT:[{guard:"selectOnFocus",actions:["focusNextTab","selectFocusedTab"]},{actions:"focusNextTab"}],HOME:[{guard:"selectOnFocus",actions:["focusFirstTab","selectFocusedTab"]},{actions:"focusFirstTab"}],END:[{guard:"selectOnFocus",actions:["focusLastTab","selectFocusedTab"]},{actions:"focusLastTab"}],ENTER:{guard:not("selectOnFocus"),actions:"selectFocusedTab"},TAB_FOCUS:{actions:["setFocusedValue"]},TAB_BLUR:{target:"idle",actions:"clearFocusedValue"}}}}},{guards:{selectOnFocus:ctx2=>ctx2.activationMode==="automatic"},actions:{syncFocusedValue(ctx2){if(ctx2.value!=null&&ctx2.focusedValue==null){ctx2.focusedValue=ctx2.value}},selectFocusedTab(ctx2){(0,import_dom_query3.raf)(()=>{set.value(ctx2,ctx2.focusedValue)})},setFocusedValue(ctx2,evt){if(evt.value==null)return;set.focusedValue(ctx2,evt.value)},clearFocusedValue(ctx2){set.focusedValue(ctx2,null)},setValue(ctx2,evt){set.value(ctx2,evt.value)},clearValue(ctx2){set.value(ctx2,null)},focusFirstTab(ctx2){(0,import_dom_query3.raf)(()=>{dom.getFirstTriggerEl(ctx2)?.focus()})},focusLastTab(ctx2){(0,import_dom_query3.raf)(()=>{dom.getLastTriggerEl(ctx2)?.focus()})},focusNextTab(ctx2){if(!ctx2.focusedValue)return;const triggerEl=dom.getNextTriggerEl(ctx2,ctx2.focusedValue);(0,import_dom_query3.raf)(()=>{if(ctx2.composite){triggerEl?.focus()}else if(triggerEl?.dataset.value!=null){set.focusedValue(ctx2,triggerEl.dataset.value)}})},focusPrevTab(ctx2){if(!ctx2.focusedValue)return;const triggerEl=dom.getPrevTriggerEl(ctx2,ctx2.focusedValue);(0,import_dom_query3.raf)(()=>{if(ctx2.composite){triggerEl?.focus()}else if(triggerEl?.dataset.value!=null){set.focusedValue(ctx2,triggerEl.dataset.value)}})},checkRenderedElements(ctx2){ctx2.indicatorState.rendered=!!dom.getIndicatorEl(ctx2)},syncTabIndex(ctx2){(0,import_dom_query3.raf)(()=>{const contentEl=dom.getSelectedContentEl(ctx2);if(!contentEl)return;const focusables=(0,import_dom_query3.getFocusables)(contentEl);if(focusables.length>0){contentEl.removeAttribute("tabindex")}else{contentEl.setAttribute("tabindex","0")}})},cleanupObserver(ctx2){ctx2.indicatorCleanup?.()},allowIndicatorTransition(ctx2){ctx2.indicatorState.transition=true},setIndicatorRect(ctx2,evt){const value=evt.id??ctx2.value;if(!ctx2.indicatorState.rendered||!value)return;const triggerEl=dom.getTriggerEl(ctx2,value);if(!triggerEl)return;ctx2.indicatorState.rect=dom.getRectById(ctx2,value);(0,import_dom_query3.nextTick)(()=>{ctx2.indicatorState.transition=false})},syncIndicatorRect(ctx2){ctx2.indicatorCleanup?.();const value=ctx2.value;if(!ctx2.indicatorState.rendered||!value)return;const triggerEl=dom.getSelectedTriggerEl(ctx2);if(!triggerEl)return;ctx2.indicatorCleanup=(0,import_element_rect.trackElementRect)(triggerEl,{getRect(el){return dom.getOffsetRect(el)},onChange(rect){ctx2.indicatorState.rect=dom.resolveRect(rect);(0,import_dom_query3.nextTick)(()=>{ctx2.indicatorState.transition=false})}})},clickIfLink(ctx2){(0,import_dom_event2.clickIfLink)(dom.getSelectedTriggerEl(ctx2))}}})}var invoke={change:ctx=>{if(ctx.value==null)return;ctx.onValueChange?.({value:ctx.value})},focusChange:ctx=>{if(ctx.focusedValue==null)return;ctx.onFocusChange?.({focusedValue:ctx.focusedValue})}};var set={value:(ctx,value)=>{if((0,import_utils2.isEqual)(value,ctx.value))return;ctx.value=value;invoke.change(ctx)},focusedValue:(ctx,value)=>{if((0,import_utils2.isEqual)(value,ctx.focusedValue))return;ctx.focusedValue=value;invoke.focusChange(ctx)}};var import_types=require("@zag-js/types");var import_utils3=require("@zag-js/utils");var props=(0,import_types.createProps)()(["activationMode","composite","dir","getRootNode","id","ids","loopFocus","onFocusChange","onValueChange","orientation","translations","value"]);var splitProps=(0,import_utils3.createSplitProps)(props);var triggerProps=(0,import_types.createProps)()(["disabled","value"]);var splitTriggerProps=(0,import_utils3.createSplitProps)(triggerProps);var contentProps=(0,import_types.createProps)()(["value"]);var splitContentProps=(0,import_utils3.createSplitProps)(contentProps);0&&(module.exports={anatomy,connect,contentProps,machine,props,splitContentProps,splitProps,splitTriggerProps,triggerProps});
585
2
  //# sourceMappingURL=index.js.map