@zag-js/tabs 0.0.0-dev-20220415160434 → 0.0.0-dev-20220417135151

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.mjs CHANGED
@@ -171,25 +171,25 @@ var dom = {
171
171
  },
172
172
  getRootId: (ctx) => {
173
173
  var _a, _b;
174
- return (_b = (_a = ctx.ids) == null ? void 0 : _a.root) != null ? _b : `tabs-${ctx.uid}`;
174
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.root) != null ? _b : `tabs:${ctx.uid}`;
175
175
  },
176
176
  getTriggerGroupId: (ctx) => {
177
177
  var _a, _b;
178
- return (_b = (_a = ctx.ids) == null ? void 0 : _a.triggerGroup) != null ? _b : `tabs-${ctx.uid}-trigger-group`;
178
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.triggerGroup) != null ? _b : `tabs:${ctx.uid}:trigger-group`;
179
179
  },
180
180
  getContentId: (ctx, id) => {
181
181
  var _a, _b;
182
- return (_b = (_a = ctx.ids) == null ? void 0 : _a.content) != null ? _b : `tabs-${ctx.uid}-content-${id}`;
182
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.content) != null ? _b : `tabs:${ctx.uid}:content-${id}`;
183
183
  },
184
184
  getContentGroupId: (ctx) => {
185
185
  var _a, _b;
186
- return (_b = (_a = ctx.ids) == null ? void 0 : _a.contentGroup) != null ? _b : `tabs-${ctx.uid}-content-group`;
186
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.contentGroup) != null ? _b : `tabs:${ctx.uid}:content-group`;
187
187
  },
188
188
  getTriggerId: (ctx, id) => {
189
189
  var _a, _b;
190
- return (_b = (_a = ctx.ids) == null ? void 0 : _a.trigger) != null ? _b : `tabs-${ctx.uid}-trigger-${id}`;
190
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.trigger) != null ? _b : `tabs:${ctx.uid}:trigger-${id}`;
191
191
  },
192
- getIndicatorId: (ctx) => `tabs-${ctx.uid}-indicator`,
192
+ getIndicatorId: (ctx) => `tabs:${ctx.uid}:indicator`,
193
193
  getTriggerGroupEl: (ctx) => dom.getDoc(ctx).getElementById(dom.getTriggerGroupId(ctx)),
194
194
  getContentEl: (ctx, id) => dom.getDoc(ctx).getElementById(dom.getContentId(ctx, id)),
195
195
  getTriggerEl: (ctx, id) => dom.getDoc(ctx).getElementById(dom.getTriggerId(ctx, id)),
@@ -205,12 +205,23 @@ var dom = {
205
205
  getPrevEl: (ctx, id) => prevById(dom.getElements(ctx), dom.getTriggerId(ctx, id), ctx.loop),
206
206
  getRectById: (ctx, id) => {
207
207
  var _a;
208
- const empty = { offsetLeft: 0, offsetTop: 0, offsetWidth: 0, offsetHeight: 0 };
208
+ const empty = {
209
+ offsetLeft: 0,
210
+ offsetTop: 0,
211
+ offsetWidth: 0,
212
+ offsetHeight: 0
213
+ };
209
214
  const tab = (_a = itemById(dom.getElements(ctx), dom.getTriggerId(ctx, id))) != null ? _a : empty;
210
215
  if (ctx.isVertical) {
211
- return { top: `${tab.offsetTop}px`, height: `${tab.offsetHeight}px` };
216
+ return {
217
+ top: `${tab.offsetTop}px`,
218
+ height: `${tab.offsetHeight}px`
219
+ };
212
220
  }
213
- return { left: `${tab.offsetLeft}px`, width: `${tab.offsetWidth}px` };
221
+ return {
222
+ left: `${tab.offsetLeft}px`,
223
+ width: `${tab.offsetWidth}px`
224
+ };
214
225
  },
215
226
  getActiveContentEl: (ctx) => {
216
227
  if (!ctx.value)
@@ -365,206 +376,208 @@ function connect(state, send, normalize = normalizeProp) {
365
376
  // src/tabs.machine.ts
366
377
  import { createMachine, guards, ref } from "@zag-js/core";
367
378
  var { not } = guards;
368
- var machine = createMachine({
369
- initial: "unknown",
370
- context: {
371
- dir: "ltr",
372
- orientation: "horizontal",
373
- activationMode: "automatic",
374
- value: null,
375
- focusedValue: null,
376
- uid: "",
377
- previousValues: [],
378
- indicatorRect: { left: "0px", top: "0px", width: "0px", height: "0px" },
379
- hasMeasuredRect: false,
380
- isIndicatorRendered: false,
381
- loop: true,
382
- messages: {}
383
- },
384
- computed: {
385
- isHorizontal: (ctx) => ctx.orientation === "horizontal",
386
- isVertical: (ctx) => ctx.orientation === "vertical"
387
- },
388
- created: ["setPrevSelectedTabs"],
389
- watch: {
390
- focusedValue: "invokeOnFocus",
391
- value: ["invokeOnChange", "setPrevSelectedTabs", "setIndicatorRect", "setContentTabIndex"],
392
- dir: ["clearMeasured", "setIndicatorRect"]
393
- },
394
- on: {
395
- SET_VALUE: {
396
- actions: "setValue"
379
+ function machine(ctx = {}) {
380
+ return createMachine({
381
+ initial: "unknown",
382
+ context: __spreadValues({
383
+ dir: "ltr",
384
+ orientation: "horizontal",
385
+ activationMode: "automatic",
386
+ value: null,
387
+ focusedValue: null,
388
+ uid: "",
389
+ previousValues: [],
390
+ indicatorRect: { left: "0px", top: "0px", width: "0px", height: "0px" },
391
+ hasMeasuredRect: false,
392
+ isIndicatorRendered: false,
393
+ loop: true,
394
+ messages: {}
395
+ }, ctx),
396
+ computed: {
397
+ isHorizontal: (ctx2) => ctx2.orientation === "horizontal",
398
+ isVertical: (ctx2) => ctx2.orientation === "vertical"
397
399
  },
398
- DELETE: {
399
- actions: "deleteValue"
400
- }
401
- },
402
- states: {
403
- unknown: {
404
- on: {
405
- SETUP: {
406
- target: "idle",
407
- actions: ["setupDocument", "checkRenderedElements", "setIndicatorRect", "setContentTabIndex"]
408
- }
409
- }
400
+ created: ["setPrevSelectedTabs"],
401
+ watch: {
402
+ focusedValue: "invokeOnFocus",
403
+ value: ["invokeOnChange", "setPrevSelectedTabs", "setIndicatorRect", "setContentTabIndex"],
404
+ dir: ["clearMeasured", "setIndicatorRect"]
410
405
  },
411
- idle: {
412
- on: {
413
- TAB_FOCUS: {
414
- guard: "selectOnFocus",
415
- target: "focused",
416
- actions: ["setFocusedValue", "setValue"]
417
- },
418
- TAB_CLICK: {
419
- target: "focused",
420
- actions: ["setFocusedValue", "setValue"]
421
- }
406
+ on: {
407
+ SET_VALUE: {
408
+ actions: "setValue"
409
+ },
410
+ DELETE: {
411
+ actions: "deleteValue"
422
412
  }
423
413
  },
424
- focused: {
425
- on: {
426
- TAB_CLICK: {
427
- target: "focused",
428
- actions: ["setFocusedValue", "setValue"]
429
- },
430
- ARROW_LEFT: {
431
- guard: "isHorizontal",
432
- actions: "focusPrevTab"
433
- },
434
- ARROW_RIGHT: {
435
- guard: "isHorizontal",
436
- actions: "focusNextTab"
437
- },
438
- ARROW_UP: {
439
- guard: "isVertical",
440
- actions: "focusPrevTab"
441
- },
442
- ARROW_DOWN: {
443
- guard: "isVertical",
444
- actions: "focusNextTab"
445
- },
446
- HOME: {
447
- actions: "focusFirstTab"
448
- },
449
- END: {
450
- actions: "focusLastTab"
451
- },
452
- ENTER: {
453
- guard: not("selectOnFocus"),
454
- actions: "setValue"
455
- },
456
- TAB_FOCUS: [
457
- {
414
+ states: {
415
+ unknown: {
416
+ on: {
417
+ SETUP: {
418
+ target: "idle",
419
+ actions: ["setupDocument", "checkRenderedElements", "setIndicatorRect", "setContentTabIndex"]
420
+ }
421
+ }
422
+ },
423
+ idle: {
424
+ on: {
425
+ TAB_FOCUS: {
458
426
  guard: "selectOnFocus",
427
+ target: "focused",
459
428
  actions: ["setFocusedValue", "setValue"]
460
429
  },
461
- { actions: "setFocusedValue" }
462
- ],
463
- TAB_BLUR: {
464
- target: "idle",
465
- actions: "clearFocusedValue"
430
+ TAB_CLICK: {
431
+ target: "focused",
432
+ actions: ["setFocusedValue", "setValue"]
433
+ }
434
+ }
435
+ },
436
+ focused: {
437
+ on: {
438
+ TAB_CLICK: {
439
+ target: "focused",
440
+ actions: ["setFocusedValue", "setValue"]
441
+ },
442
+ ARROW_LEFT: {
443
+ guard: "isHorizontal",
444
+ actions: "focusPrevTab"
445
+ },
446
+ ARROW_RIGHT: {
447
+ guard: "isHorizontal",
448
+ actions: "focusNextTab"
449
+ },
450
+ ARROW_UP: {
451
+ guard: "isVertical",
452
+ actions: "focusPrevTab"
453
+ },
454
+ ARROW_DOWN: {
455
+ guard: "isVertical",
456
+ actions: "focusNextTab"
457
+ },
458
+ HOME: {
459
+ actions: "focusFirstTab"
460
+ },
461
+ END: {
462
+ actions: "focusLastTab"
463
+ },
464
+ ENTER: {
465
+ guard: not("selectOnFocus"),
466
+ actions: "setValue"
467
+ },
468
+ TAB_FOCUS: [
469
+ {
470
+ guard: "selectOnFocus",
471
+ actions: ["setFocusedValue", "setValue"]
472
+ },
473
+ { actions: "setFocusedValue" }
474
+ ],
475
+ TAB_BLUR: {
476
+ target: "idle",
477
+ actions: "clearFocusedValue"
478
+ }
466
479
  }
467
480
  }
468
481
  }
469
- }
470
- }, {
471
- guards: {
472
- isVertical: (ctx) => ctx.isVertical,
473
- isHorizontal: (ctx) => ctx.isHorizontal,
474
- selectOnFocus: (ctx) => ctx.activationMode === "automatic"
475
- },
476
- actions: {
477
- setupDocument(ctx, evt) {
478
- ctx.uid = evt.id;
479
- if (evt.doc)
480
- ctx.doc = ref(evt.doc);
481
- },
482
- setFocusedValue(ctx, evt) {
483
- ctx.focusedValue = evt.value;
484
- },
485
- clearFocusedValue(ctx) {
486
- ctx.focusedValue = null;
487
- },
488
- setValue(ctx, evt) {
489
- ctx.value = evt.value;
490
- },
491
- invokeOnDelete(ctx, evt) {
492
- var _a;
493
- (_a = ctx.onDelete) == null ? void 0 : _a.call(ctx, { value: evt.value });
482
+ }, {
483
+ guards: {
484
+ isVertical: (ctx2) => ctx2.isVertical,
485
+ isHorizontal: (ctx2) => ctx2.isHorizontal,
486
+ selectOnFocus: (ctx2) => ctx2.activationMode === "automatic"
494
487
  },
495
- focusFirstTab(ctx) {
496
- nextTick(() => {
488
+ actions: {
489
+ setupDocument(ctx2, evt) {
490
+ ctx2.uid = evt.id;
491
+ if (evt.doc)
492
+ ctx2.doc = ref(evt.doc);
493
+ },
494
+ setFocusedValue(ctx2, evt) {
495
+ ctx2.focusedValue = evt.value;
496
+ },
497
+ clearFocusedValue(ctx2) {
498
+ ctx2.focusedValue = null;
499
+ },
500
+ setValue(ctx2, evt) {
501
+ ctx2.value = evt.value;
502
+ },
503
+ invokeOnDelete(ctx2, evt) {
497
504
  var _a;
498
- return (_a = dom.getFirstEl(ctx)) == null ? void 0 : _a.focus();
499
- });
500
- },
501
- focusLastTab(ctx) {
502
- nextTick(() => {
503
- var _a;
504
- return (_a = dom.getLastEl(ctx)) == null ? void 0 : _a.focus();
505
- });
506
- },
507
- focusNextTab(ctx) {
508
- if (!ctx.focusedValue)
509
- return;
510
- const next = dom.getNextEl(ctx, ctx.focusedValue);
511
- nextTick(() => next == null ? void 0 : next.focus());
512
- },
513
- focusPrevTab(ctx) {
514
- if (!ctx.focusedValue)
515
- return;
516
- const prev = dom.getPrevEl(ctx, ctx.focusedValue);
517
- nextTick(() => prev == null ? void 0 : prev.focus());
518
- },
519
- setIndicatorRect(ctx) {
520
- nextTick(() => {
521
- if (!ctx.isIndicatorRendered || !ctx.value)
505
+ (_a = ctx2.onDelete) == null ? void 0 : _a.call(ctx2, { value: evt.value });
506
+ },
507
+ focusFirstTab(ctx2) {
508
+ nextTick(() => {
509
+ var _a;
510
+ return (_a = dom.getFirstEl(ctx2)) == null ? void 0 : _a.focus();
511
+ });
512
+ },
513
+ focusLastTab(ctx2) {
514
+ nextTick(() => {
515
+ var _a;
516
+ return (_a = dom.getLastEl(ctx2)) == null ? void 0 : _a.focus();
517
+ });
518
+ },
519
+ focusNextTab(ctx2) {
520
+ if (!ctx2.focusedValue)
522
521
  return;
523
- ctx.indicatorRect = dom.getRectById(ctx, ctx.value);
524
- if (ctx.hasMeasuredRect)
522
+ const next = dom.getNextEl(ctx2, ctx2.focusedValue);
523
+ nextTick(() => next == null ? void 0 : next.focus());
524
+ },
525
+ focusPrevTab(ctx2) {
526
+ if (!ctx2.focusedValue)
525
527
  return;
528
+ const prev = dom.getPrevEl(ctx2, ctx2.focusedValue);
529
+ nextTick(() => prev == null ? void 0 : prev.focus());
530
+ },
531
+ setIndicatorRect(ctx2) {
526
532
  nextTick(() => {
527
- ctx.hasMeasuredRect = true;
533
+ if (!ctx2.isIndicatorRendered || !ctx2.value)
534
+ return;
535
+ ctx2.indicatorRect = dom.getRectById(ctx2, ctx2.value);
536
+ if (ctx2.hasMeasuredRect)
537
+ return;
538
+ nextTick(() => {
539
+ ctx2.hasMeasuredRect = true;
540
+ });
528
541
  });
529
- });
530
- },
531
- checkRenderedElements(ctx) {
532
- nextTick(() => {
533
- ctx.isIndicatorRendered = !!dom.getIndicatorEl(ctx);
534
- });
535
- },
536
- clearMeasured(ctx) {
537
- ctx.hasMeasuredRect = false;
538
- },
539
- invokeOnChange(ctx) {
540
- var _a;
541
- (_a = ctx.onChange) == null ? void 0 : _a.call(ctx, { value: ctx.value });
542
- },
543
- invokeOnFocus(ctx) {
544
- var _a;
545
- (_a = ctx.onFocus) == null ? void 0 : _a.call(ctx, { value: ctx.focusedValue });
546
- },
547
- setPrevSelectedTabs(ctx) {
548
- if (ctx.value != null) {
549
- const newSelected = Array.from(ctx.previousValues).concat(ctx.value);
550
- ctx.previousValues = Array.from(new Set(newSelected));
551
- }
552
- },
553
- setContentTabIndex(ctx) {
554
- nextTick(() => {
555
- const panel = dom.getActiveContentEl(ctx);
556
- if (!panel)
557
- return;
558
- const focusables = getFocusables(panel);
559
- if (focusables.length > 0) {
560
- panel.removeAttribute("tabindex");
561
- } else {
562
- panel.setAttribute("tabindex", "0");
542
+ },
543
+ checkRenderedElements(ctx2) {
544
+ nextTick(() => {
545
+ ctx2.isIndicatorRendered = !!dom.getIndicatorEl(ctx2);
546
+ });
547
+ },
548
+ clearMeasured(ctx2) {
549
+ ctx2.hasMeasuredRect = false;
550
+ },
551
+ invokeOnChange(ctx2) {
552
+ var _a;
553
+ (_a = ctx2.onChange) == null ? void 0 : _a.call(ctx2, { value: ctx2.value });
554
+ },
555
+ invokeOnFocus(ctx2) {
556
+ var _a;
557
+ (_a = ctx2.onFocus) == null ? void 0 : _a.call(ctx2, { value: ctx2.focusedValue });
558
+ },
559
+ setPrevSelectedTabs(ctx2) {
560
+ if (ctx2.value != null) {
561
+ const newSelected = Array.from(ctx2.previousValues).concat(ctx2.value);
562
+ ctx2.previousValues = Array.from(new Set(newSelected));
563
563
  }
564
- });
564
+ },
565
+ setContentTabIndex(ctx2) {
566
+ nextTick(() => {
567
+ const panel = dom.getActiveContentEl(ctx2);
568
+ if (!panel)
569
+ return;
570
+ const focusables = getFocusables(panel);
571
+ if (focusables.length > 0) {
572
+ panel.removeAttribute("tabindex");
573
+ } else {
574
+ panel.setAttribute("tabindex", "0");
575
+ }
576
+ });
577
+ }
565
578
  }
566
- }
567
- });
579
+ });
580
+ }
568
581
  export {
569
582
  connect,
570
583
  machine