@zag-js/tabs 0.49.0 → 0.51.0

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,532 +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
- getFirstEl: (ctx) => (0, import_utils.first)(dom.getElements(ctx)),
63
- getLastEl: (ctx) => (0, import_utils.last)(dom.getElements(ctx)),
64
- getNextEl: (ctx, id) => (0, import_dom_query.nextById)(dom.getElements(ctx), dom.getTriggerId(ctx, id), ctx.loopFocus),
65
- getPrevEl: (ctx, id) => (0, import_dom_query.prevById)(dom.getElements(ctx), dom.getTriggerId(ctx, id), ctx.loopFocus),
66
- getActiveContentEl: (ctx) => {
67
- if (!ctx.value)
68
- return;
69
- return dom.getContentEl(ctx, ctx.value);
70
- },
71
- getActiveTabEl: (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
- function getTriggerState(props2) {
101
- return {
102
- selected: state.context.value === props2.value,
103
- focused: state.context.focusedValue === props2.value,
104
- disabled: !!props2.disabled
105
- };
106
- }
107
- return {
108
- value: state.context.value,
109
- focusedValue: state.context.focusedValue,
110
- setValue(value) {
111
- send({ type: "SET_VALUE", value });
112
- },
113
- clearValue() {
114
- send({ type: "CLEAR_VALUE" });
115
- },
116
- setIndicatorRect(value) {
117
- const id = dom.getTriggerId(state.context, value);
118
- send({ type: "SET_INDICATOR_RECT", id });
119
- },
120
- getTriggerState,
121
- rootProps: normalize.element({
122
- ...parts.root.attrs,
123
- id: dom.getRootId(state.context),
124
- "data-orientation": state.context.orientation,
125
- "data-focus": (0, import_dom_query2.dataAttr)(focused),
126
- dir: state.context.dir
127
- }),
128
- listProps: normalize.element({
129
- ...parts.list.attrs,
130
- id: dom.getListId(state.context),
131
- role: "tablist",
132
- dir: state.context.dir,
133
- "data-focus": (0, import_dom_query2.dataAttr)(focused),
134
- "aria-orientation": state.context.orientation,
135
- "data-orientation": state.context.orientation,
136
- "aria-label": translations.listLabel,
137
- onKeyDown(event) {
138
- if (event.defaultPrevented)
139
- return;
140
- const evt = (0, import_dom_event.getNativeEvent)(event);
141
- if (!(0, import_dom_query2.isSelfTarget)(evt))
142
- return;
143
- const keyMap = {
144
- ArrowDown() {
145
- send("ARROW_DOWN");
146
- },
147
- ArrowUp() {
148
- send("ARROW_UP");
149
- },
150
- ArrowLeft() {
151
- send("ARROW_LEFT");
152
- },
153
- ArrowRight() {
154
- send("ARROW_RIGHT");
155
- },
156
- Home() {
157
- send("HOME");
158
- },
159
- End() {
160
- send("END");
161
- },
162
- Enter() {
163
- send({ type: "ENTER", value: state.context.focusedValue });
164
- }
165
- };
166
- let key = (0, import_dom_event.getEventKey)(event, state.context);
167
- const exec = keyMap[key];
168
- if (exec) {
169
- event.preventDefault();
170
- exec(event);
171
- }
172
- }
173
- }),
174
- getTriggerProps(props2) {
175
- const { value, disabled } = props2;
176
- const triggerState = getTriggerState(props2);
177
- return normalize.button({
178
- ...parts.trigger.attrs,
179
- role: "tab",
180
- type: "button",
181
- disabled,
182
- dir: state.context.dir,
183
- "data-orientation": state.context.orientation,
184
- "data-disabled": (0, import_dom_query2.dataAttr)(disabled),
185
- "aria-disabled": disabled,
186
- "data-value": value,
187
- "aria-selected": triggerState.selected,
188
- "data-selected": (0, import_dom_query2.dataAttr)(triggerState.selected),
189
- "data-focus": (0, import_dom_query2.dataAttr)(triggerState.focused),
190
- "aria-controls": dom.getContentId(state.context, value),
191
- "data-ownedby": dom.getListId(state.context),
192
- id: dom.getTriggerId(state.context, value),
193
- tabIndex: triggerState.selected ? 0 : -1,
194
- onFocus() {
195
- send({ type: "TAB_FOCUS", value });
196
- },
197
- onBlur(event) {
198
- const target = event.relatedTarget;
199
- if (target?.getAttribute("role") !== "tab") {
200
- send({ type: "TAB_BLUR" });
201
- }
202
- },
203
- onClick(event) {
204
- if (event.defaultPrevented)
205
- return;
206
- if (disabled)
207
- return;
208
- if ((0, import_dom_query2.isSafari)()) {
209
- event.currentTarget.focus();
210
- }
211
- send({ type: "TAB_CLICK", value });
212
- }
213
- });
214
- },
215
- getContentProps(props2) {
216
- const { value } = props2;
217
- const selected = state.context.value === value;
218
- return normalize.element({
219
- ...parts.content.attrs,
220
- dir: state.context.dir,
221
- id: dom.getContentId(state.context, value),
222
- tabIndex: 0,
223
- "aria-labelledby": dom.getTriggerId(state.context, value),
224
- role: "tabpanel",
225
- "data-ownedby": dom.getListId(state.context),
226
- "data-selected": (0, import_dom_query2.dataAttr)(selected),
227
- "data-orientation": state.context.orientation,
228
- hidden: !selected
229
- });
230
- },
231
- indicatorProps: normalize.element({
232
- id: dom.getIndicatorId(state.context),
233
- ...parts.indicator.attrs,
234
- dir: state.context.dir,
235
- "data-orientation": state.context.orientation,
236
- style: {
237
- "--transition-property": "left, right, top, bottom, width, height",
238
- "--left": state.context.indicatorRect?.left,
239
- "--top": state.context.indicatorRect?.top,
240
- "--width": state.context.indicatorRect?.width,
241
- "--height": state.context.indicatorRect?.height,
242
- position: "absolute",
243
- willChange: "var(--transition-property)",
244
- transitionProperty: "var(--transition-property)",
245
- transitionDuration: state.context.canIndicatorTransition ? "var(--transition-duration, 150ms)" : "0ms",
246
- transitionTimingFunction: "var(--transition-timing-function)",
247
- [state.context.orientation === "horizontal" ? "left" : "top"]: state.context.orientation === "horizontal" ? "var(--left)" : "var(--top)"
248
- }
249
- })
250
- };
251
- }
252
-
253
- // src/tabs.machine.ts
254
- var import_core = require("@zag-js/core");
255
- var import_dom_event2 = require("@zag-js/dom-event");
256
- var import_dom_query3 = require("@zag-js/dom-query");
257
- var import_element_rect = require("@zag-js/element-rect");
258
- var import_tabbable = require("@zag-js/tabbable");
259
- var import_utils2 = require("@zag-js/utils");
260
- var { not } = import_core.guards;
261
- function machine(userContext) {
262
- const ctx = (0, import_utils2.compact)(userContext);
263
- return (0, import_core.createMachine)(
264
- {
265
- initial: "idle",
266
- context: {
267
- dir: "ltr",
268
- orientation: "horizontal",
269
- activationMode: "automatic",
270
- value: null,
271
- focusedValue: null,
272
- indicatorRect: {
273
- left: "0px",
274
- top: "0px",
275
- width: "0px",
276
- height: "0px"
277
- },
278
- canIndicatorTransition: false,
279
- isIndicatorRendered: false,
280
- loopFocus: true,
281
- translations: {},
282
- ...ctx
283
- },
284
- computed: {
285
- isHorizontal: (ctx2) => ctx2.orientation === "horizontal",
286
- isVertical: (ctx2) => ctx2.orientation === "vertical"
287
- },
288
- entry: ["checkRenderedElements", "syncIndicatorRect", "setContentTabIndex"],
289
- exit: ["cleanupObserver"],
290
- watch: {
291
- value: ["enableIndicatorTransition", "syncIndicatorRect", "setContentTabIndex", "clickIfLink"],
292
- dir: ["syncIndicatorRect"],
293
- orientation: ["syncIndicatorRect"]
294
- },
295
- on: {
296
- SET_VALUE: {
297
- actions: "setValue"
298
- },
299
- CLEAR_VALUE: {
300
- actions: "clearValue"
301
- },
302
- SET_INDICATOR_RECT: {
303
- actions: "setIndicatorRect"
304
- }
305
- },
306
- states: {
307
- idle: {
308
- on: {
309
- TAB_FOCUS: [
310
- {
311
- guard: "selectOnFocus",
312
- target: "focused",
313
- actions: ["setFocusedValue", "setValue"]
314
- },
315
- {
316
- target: "focused",
317
- actions: "setFocusedValue"
318
- }
319
- ],
320
- TAB_CLICK: {
321
- target: "focused",
322
- actions: ["setFocusedValue", "setValue"]
323
- }
324
- }
325
- },
326
- focused: {
327
- on: {
328
- TAB_CLICK: {
329
- target: "focused",
330
- actions: ["setFocusedValue", "setValue"]
331
- },
332
- ARROW_LEFT: {
333
- guard: "isHorizontal",
334
- actions: "focusPrevTab"
335
- },
336
- ARROW_RIGHT: {
337
- guard: "isHorizontal",
338
- actions: "focusNextTab"
339
- },
340
- ARROW_UP: {
341
- guard: "isVertical",
342
- actions: "focusPrevTab"
343
- },
344
- ARROW_DOWN: {
345
- guard: "isVertical",
346
- actions: "focusNextTab"
347
- },
348
- HOME: {
349
- actions: "focusFirstTab"
350
- },
351
- END: {
352
- actions: "focusLastTab"
353
- },
354
- ENTER: {
355
- guard: not("selectOnFocus"),
356
- actions: "setValue"
357
- },
358
- TAB_FOCUS: [
359
- {
360
- guard: "selectOnFocus",
361
- actions: ["setFocusedValue", "setValue"]
362
- },
363
- { actions: "setFocusedValue" }
364
- ],
365
- TAB_BLUR: {
366
- target: "idle",
367
- actions: "clearFocusedValue"
368
- }
369
- }
370
- }
371
- }
372
- },
373
- {
374
- guards: {
375
- isVertical: (ctx2) => ctx2.isVertical,
376
- isHorizontal: (ctx2) => ctx2.isHorizontal,
377
- selectOnFocus: (ctx2) => ctx2.activationMode === "automatic"
378
- },
379
- actions: {
380
- setFocusedValue(ctx2, evt) {
381
- set.focusedValue(ctx2, evt.value);
382
- },
383
- clearFocusedValue(ctx2) {
384
- set.focusedValue(ctx2, null);
385
- },
386
- setValue(ctx2, evt) {
387
- set.value(ctx2, evt.value);
388
- },
389
- clearValue(ctx2) {
390
- set.value(ctx2, null);
391
- },
392
- focusFirstTab(ctx2) {
393
- (0, import_dom_query3.raf)(() => dom.getFirstEl(ctx2)?.focus());
394
- },
395
- focusLastTab(ctx2) {
396
- (0, import_dom_query3.raf)(() => dom.getLastEl(ctx2)?.focus());
397
- },
398
- focusNextTab(ctx2) {
399
- if (!ctx2.focusedValue)
400
- return;
401
- const next = dom.getNextEl(ctx2, ctx2.focusedValue);
402
- (0, import_dom_query3.raf)(() => next?.focus());
403
- },
404
- focusPrevTab(ctx2) {
405
- if (!ctx2.focusedValue)
406
- return;
407
- const prev = dom.getPrevEl(ctx2, ctx2.focusedValue);
408
- (0, import_dom_query3.raf)(() => prev?.focus());
409
- },
410
- checkRenderedElements(ctx2) {
411
- ctx2.isIndicatorRendered = !!dom.getIndicatorEl(ctx2);
412
- },
413
- // if tab panel contains focusable elements, remove the tabindex attribute
414
- setContentTabIndex(ctx2) {
415
- (0, import_dom_query3.raf)(() => {
416
- const panel = dom.getActiveContentEl(ctx2);
417
- if (!panel)
418
- return;
419
- const focusables = (0, import_tabbable.getFocusables)(panel);
420
- if (focusables.length > 0) {
421
- panel.removeAttribute("tabindex");
422
- } else {
423
- panel.setAttribute("tabindex", "0");
424
- }
425
- });
426
- },
427
- cleanupObserver(ctx2) {
428
- ctx2.indicatorCleanup?.();
429
- },
430
- enableIndicatorTransition(ctx2) {
431
- ctx2.canIndicatorTransition = true;
432
- },
433
- setIndicatorRect(ctx2, evt) {
434
- const value = evt.id ?? ctx2.value;
435
- if (!ctx2.isIndicatorRendered || !value)
436
- return;
437
- const tabEl = dom.getTriggerEl(ctx2, value);
438
- if (!tabEl)
439
- return;
440
- ctx2.indicatorRect = dom.getRectById(ctx2, value);
441
- (0, import_dom_query3.nextTick)(() => {
442
- ctx2.canIndicatorTransition = false;
443
- });
444
- },
445
- syncIndicatorRect(ctx2) {
446
- ctx2.indicatorCleanup?.();
447
- const value = ctx2.value;
448
- if (!ctx2.isIndicatorRendered || !value)
449
- return;
450
- const tabEl = dom.getActiveTabEl(ctx2);
451
- if (!tabEl)
452
- return;
453
- ctx2.indicatorCleanup = (0, import_element_rect.trackElementRect)(tabEl, {
454
- getRect(el) {
455
- return dom.getOffsetRect(el);
456
- },
457
- onChange(rect) {
458
- ctx2.indicatorRect = dom.resolveRect(rect);
459
- (0, import_dom_query3.nextTick)(() => {
460
- ctx2.canIndicatorTransition = false;
461
- });
462
- }
463
- });
464
- },
465
- clickIfLink(ctx2) {
466
- (0, import_dom_event2.clickIfLink)(dom.getActiveTabEl(ctx2));
467
- }
468
- }
469
- }
470
- );
471
- }
472
- var invoke = {
473
- change: (ctx) => {
474
- if (ctx.value == null)
475
- return;
476
- ctx.onValueChange?.({ value: ctx.value });
477
- },
478
- focusChange: (ctx) => {
479
- if (ctx.focusedValue == null)
480
- return;
481
- ctx.onFocusChange?.({ focusedValue: ctx.focusedValue });
482
- }
483
- };
484
- var set = {
485
- value: (ctx, value) => {
486
- if ((0, import_utils2.isEqual)(value, ctx.value))
487
- return;
488
- ctx.value = value;
489
- invoke.change(ctx);
490
- },
491
- focusedValue: (ctx, value) => {
492
- if ((0, import_utils2.isEqual)(value, ctx.focusedValue))
493
- return;
494
- ctx.focusedValue = value;
495
- invoke.focusChange(ctx);
496
- }
497
- };
498
-
499
- // src/tabs.props.ts
500
- var import_types = require("@zag-js/types");
501
- var import_utils3 = require("@zag-js/utils");
502
- var props = (0, import_types.createProps)()([
503
- "activationMode",
504
- "dir",
505
- "getRootNode",
506
- "id",
507
- "ids",
508
- "loopFocus",
509
- "onFocusChange",
510
- "onValueChange",
511
- "orientation",
512
- "translations",
513
- "value"
514
- ]);
515
- var splitProps = (0, import_utils3.createSplitProps)(props);
516
- var triggerProps = (0, import_types.createProps)()(["disabled", "value"]);
517
- var splitTriggerProps = (0, import_utils3.createSplitProps)(triggerProps);
518
- var contentProps = (0, import_types.createProps)()(["value"]);
519
- var splitContentProps = (0, import_utils3.createSplitProps)(contentProps);
520
- // Annotate the CommonJS export names for ESM import in node:
521
- 0 && (module.exports = {
522
- anatomy,
523
- connect,
524
- contentProps,
525
- machine,
526
- props,
527
- splitContentProps,
528
- splitProps,
529
- splitTriggerProps,
530
- triggerProps
531
- });
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});
532
2
  //# sourceMappingURL=index.js.map