@zag-js/tabs 0.0.0-dev-20220416104915 → 0.0.0-dev-20220418165817

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,4 @@
1
1
  export { connect } from "./tabs.connect";
2
2
  export { machine } from "./tabs.machine";
3
- export type { MachineContext, MachineState } from "./tabs.types";
3
+ export type { UserDefinedContext as Context } from "./tabs.types";
4
4
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAA;AACxC,OAAO,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAA;AACxC,YAAY,EAAE,cAAc,EAAE,YAAY,EAAE,MAAM,cAAc,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAA;AACxC,OAAO,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAA;AACxC,YAAY,EAAE,kBAAkB,IAAI,OAAO,EAAE,MAAM,cAAc,CAAA"}
package/dist/index.js CHANGED
@@ -194,25 +194,25 @@ var dom = {
194
194
  },
195
195
  getRootId: (ctx) => {
196
196
  var _a, _b;
197
- return (_b = (_a = ctx.ids) == null ? void 0 : _a.root) != null ? _b : `tabs-${ctx.uid}`;
197
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.root) != null ? _b : `tabs:${ctx.uid}`;
198
198
  },
199
199
  getTriggerGroupId: (ctx) => {
200
200
  var _a, _b;
201
- return (_b = (_a = ctx.ids) == null ? void 0 : _a.triggerGroup) != null ? _b : `tabs-${ctx.uid}-trigger-group`;
201
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.triggerGroup) != null ? _b : `tabs:${ctx.uid}:trigger-group`;
202
202
  },
203
203
  getContentId: (ctx, id) => {
204
204
  var _a, _b;
205
- return (_b = (_a = ctx.ids) == null ? void 0 : _a.content) != null ? _b : `tabs-${ctx.uid}-content-${id}`;
205
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.content) != null ? _b : `tabs:${ctx.uid}:content-${id}`;
206
206
  },
207
207
  getContentGroupId: (ctx) => {
208
208
  var _a, _b;
209
- return (_b = (_a = ctx.ids) == null ? void 0 : _a.contentGroup) != null ? _b : `tabs-${ctx.uid}-content-group`;
209
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.contentGroup) != null ? _b : `tabs:${ctx.uid}:content-group`;
210
210
  },
211
211
  getTriggerId: (ctx, id) => {
212
212
  var _a, _b;
213
- return (_b = (_a = ctx.ids) == null ? void 0 : _a.trigger) != null ? _b : `tabs-${ctx.uid}-trigger-${id}`;
213
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.trigger) != null ? _b : `tabs:${ctx.uid}:trigger-${id}`;
214
214
  },
215
- getIndicatorId: (ctx) => `tabs-${ctx.uid}-indicator`,
215
+ getIndicatorId: (ctx) => `tabs:${ctx.uid}:indicator`,
216
216
  getTriggerGroupEl: (ctx) => dom.getDoc(ctx).getElementById(dom.getTriggerGroupId(ctx)),
217
217
  getContentEl: (ctx, id) => dom.getDoc(ctx).getElementById(dom.getContentId(ctx, id)),
218
218
  getTriggerEl: (ctx, id) => dom.getDoc(ctx).getElementById(dom.getTriggerId(ctx, id)),
@@ -228,12 +228,23 @@ var dom = {
228
228
  getPrevEl: (ctx, id) => prevById(dom.getElements(ctx), dom.getTriggerId(ctx, id), ctx.loop),
229
229
  getRectById: (ctx, id) => {
230
230
  var _a;
231
- const empty = { offsetLeft: 0, offsetTop: 0, offsetWidth: 0, offsetHeight: 0 };
231
+ const empty = {
232
+ offsetLeft: 0,
233
+ offsetTop: 0,
234
+ offsetWidth: 0,
235
+ offsetHeight: 0
236
+ };
232
237
  const tab = (_a = itemById(dom.getElements(ctx), dom.getTriggerId(ctx, id))) != null ? _a : empty;
233
238
  if (ctx.isVertical) {
234
- return { top: `${tab.offsetTop}px`, height: `${tab.offsetHeight}px` };
239
+ return {
240
+ top: `${tab.offsetTop}px`,
241
+ height: `${tab.offsetHeight}px`
242
+ };
235
243
  }
236
- return { left: `${tab.offsetLeft}px`, width: `${tab.offsetWidth}px` };
244
+ return {
245
+ left: `${tab.offsetLeft}px`,
246
+ width: `${tab.offsetWidth}px`
247
+ };
237
248
  },
238
249
  getActiveContentEl: (ctx) => {
239
250
  if (!ctx.value)
@@ -388,204 +399,206 @@ function connect(state, send, normalize = normalizeProp) {
388
399
  // src/tabs.machine.ts
389
400
  var import_core = require("@zag-js/core");
390
401
  var { not } = import_core.guards;
391
- var machine = (0, import_core.createMachine)({
392
- initial: "unknown",
393
- context: {
394
- dir: "ltr",
395
- orientation: "horizontal",
396
- activationMode: "automatic",
397
- value: null,
398
- focusedValue: null,
399
- uid: "",
400
- previousValues: [],
401
- indicatorRect: { left: "0px", top: "0px", width: "0px", height: "0px" },
402
- hasMeasuredRect: false,
403
- isIndicatorRendered: false,
404
- loop: true,
405
- messages: {}
406
- },
407
- computed: {
408
- isHorizontal: (ctx) => ctx.orientation === "horizontal",
409
- isVertical: (ctx) => ctx.orientation === "vertical"
410
- },
411
- created: ["setPrevSelectedTabs"],
412
- watch: {
413
- focusedValue: "invokeOnFocus",
414
- value: ["invokeOnChange", "setPrevSelectedTabs", "setIndicatorRect", "setContentTabIndex"],
415
- dir: ["clearMeasured", "setIndicatorRect"]
416
- },
417
- on: {
418
- SET_VALUE: {
419
- actions: "setValue"
402
+ function machine(ctx = {}) {
403
+ return (0, import_core.createMachine)({
404
+ initial: "unknown",
405
+ context: __spreadValues({
406
+ dir: "ltr",
407
+ orientation: "horizontal",
408
+ activationMode: "automatic",
409
+ value: null,
410
+ focusedValue: null,
411
+ uid: "",
412
+ previousValues: [],
413
+ indicatorRect: { left: "0px", top: "0px", width: "0px", height: "0px" },
414
+ hasMeasuredRect: false,
415
+ isIndicatorRendered: false,
416
+ loop: true,
417
+ messages: {}
418
+ }, ctx),
419
+ computed: {
420
+ isHorizontal: (ctx2) => ctx2.orientation === "horizontal",
421
+ isVertical: (ctx2) => ctx2.orientation === "vertical"
420
422
  },
421
- DELETE: {
422
- actions: "deleteValue"
423
- }
424
- },
425
- states: {
426
- unknown: {
427
- on: {
428
- SETUP: {
429
- target: "idle",
430
- actions: ["setupDocument", "checkRenderedElements", "setIndicatorRect", "setContentTabIndex"]
431
- }
432
- }
423
+ created: ["setPrevSelectedTabs"],
424
+ watch: {
425
+ focusedValue: "invokeOnFocus",
426
+ value: ["invokeOnChange", "setPrevSelectedTabs", "setIndicatorRect", "setContentTabIndex"],
427
+ dir: ["clearMeasured", "setIndicatorRect"]
433
428
  },
434
- idle: {
435
- on: {
436
- TAB_FOCUS: {
437
- guard: "selectOnFocus",
438
- target: "focused",
439
- actions: ["setFocusedValue", "setValue"]
440
- },
441
- TAB_CLICK: {
442
- target: "focused",
443
- actions: ["setFocusedValue", "setValue"]
444
- }
429
+ on: {
430
+ SET_VALUE: {
431
+ actions: "setValue"
432
+ },
433
+ DELETE: {
434
+ actions: "deleteValue"
445
435
  }
446
436
  },
447
- focused: {
448
- on: {
449
- TAB_CLICK: {
450
- target: "focused",
451
- actions: ["setFocusedValue", "setValue"]
452
- },
453
- ARROW_LEFT: {
454
- guard: "isHorizontal",
455
- actions: "focusPrevTab"
456
- },
457
- ARROW_RIGHT: {
458
- guard: "isHorizontal",
459
- actions: "focusNextTab"
460
- },
461
- ARROW_UP: {
462
- guard: "isVertical",
463
- actions: "focusPrevTab"
464
- },
465
- ARROW_DOWN: {
466
- guard: "isVertical",
467
- actions: "focusNextTab"
468
- },
469
- HOME: {
470
- actions: "focusFirstTab"
471
- },
472
- END: {
473
- actions: "focusLastTab"
474
- },
475
- ENTER: {
476
- guard: not("selectOnFocus"),
477
- actions: "setValue"
478
- },
479
- TAB_FOCUS: [
480
- {
437
+ states: {
438
+ unknown: {
439
+ on: {
440
+ SETUP: {
441
+ target: "idle",
442
+ actions: ["setupDocument", "checkRenderedElements", "setIndicatorRect", "setContentTabIndex"]
443
+ }
444
+ }
445
+ },
446
+ idle: {
447
+ on: {
448
+ TAB_FOCUS: {
481
449
  guard: "selectOnFocus",
450
+ target: "focused",
482
451
  actions: ["setFocusedValue", "setValue"]
483
452
  },
484
- { actions: "setFocusedValue" }
485
- ],
486
- TAB_BLUR: {
487
- target: "idle",
488
- actions: "clearFocusedValue"
453
+ TAB_CLICK: {
454
+ target: "focused",
455
+ actions: ["setFocusedValue", "setValue"]
456
+ }
457
+ }
458
+ },
459
+ focused: {
460
+ on: {
461
+ TAB_CLICK: {
462
+ target: "focused",
463
+ actions: ["setFocusedValue", "setValue"]
464
+ },
465
+ ARROW_LEFT: {
466
+ guard: "isHorizontal",
467
+ actions: "focusPrevTab"
468
+ },
469
+ ARROW_RIGHT: {
470
+ guard: "isHorizontal",
471
+ actions: "focusNextTab"
472
+ },
473
+ ARROW_UP: {
474
+ guard: "isVertical",
475
+ actions: "focusPrevTab"
476
+ },
477
+ ARROW_DOWN: {
478
+ guard: "isVertical",
479
+ actions: "focusNextTab"
480
+ },
481
+ HOME: {
482
+ actions: "focusFirstTab"
483
+ },
484
+ END: {
485
+ actions: "focusLastTab"
486
+ },
487
+ ENTER: {
488
+ guard: not("selectOnFocus"),
489
+ actions: "setValue"
490
+ },
491
+ TAB_FOCUS: [
492
+ {
493
+ guard: "selectOnFocus",
494
+ actions: ["setFocusedValue", "setValue"]
495
+ },
496
+ { actions: "setFocusedValue" }
497
+ ],
498
+ TAB_BLUR: {
499
+ target: "idle",
500
+ actions: "clearFocusedValue"
501
+ }
489
502
  }
490
503
  }
491
504
  }
492
- }
493
- }, {
494
- guards: {
495
- isVertical: (ctx) => ctx.isVertical,
496
- isHorizontal: (ctx) => ctx.isHorizontal,
497
- selectOnFocus: (ctx) => ctx.activationMode === "automatic"
498
- },
499
- actions: {
500
- setupDocument(ctx, evt) {
501
- ctx.uid = evt.id;
502
- if (evt.doc)
503
- ctx.doc = (0, import_core.ref)(evt.doc);
504
- },
505
- setFocusedValue(ctx, evt) {
506
- ctx.focusedValue = evt.value;
507
- },
508
- clearFocusedValue(ctx) {
509
- ctx.focusedValue = null;
510
- },
511
- setValue(ctx, evt) {
512
- ctx.value = evt.value;
513
- },
514
- invokeOnDelete(ctx, evt) {
515
- var _a;
516
- (_a = ctx.onDelete) == null ? void 0 : _a.call(ctx, { value: evt.value });
505
+ }, {
506
+ guards: {
507
+ isVertical: (ctx2) => ctx2.isVertical,
508
+ isHorizontal: (ctx2) => ctx2.isHorizontal,
509
+ selectOnFocus: (ctx2) => ctx2.activationMode === "automatic"
517
510
  },
518
- focusFirstTab(ctx) {
519
- nextTick(() => {
511
+ actions: {
512
+ setupDocument(ctx2, evt) {
513
+ ctx2.uid = evt.id;
514
+ if (evt.doc)
515
+ ctx2.doc = (0, import_core.ref)(evt.doc);
516
+ },
517
+ setFocusedValue(ctx2, evt) {
518
+ ctx2.focusedValue = evt.value;
519
+ },
520
+ clearFocusedValue(ctx2) {
521
+ ctx2.focusedValue = null;
522
+ },
523
+ setValue(ctx2, evt) {
524
+ ctx2.value = evt.value;
525
+ },
526
+ invokeOnDelete(ctx2, evt) {
520
527
  var _a;
521
- return (_a = dom.getFirstEl(ctx)) == null ? void 0 : _a.focus();
522
- });
523
- },
524
- focusLastTab(ctx) {
525
- nextTick(() => {
526
- var _a;
527
- return (_a = dom.getLastEl(ctx)) == null ? void 0 : _a.focus();
528
- });
529
- },
530
- focusNextTab(ctx) {
531
- if (!ctx.focusedValue)
532
- return;
533
- const next = dom.getNextEl(ctx, ctx.focusedValue);
534
- nextTick(() => next == null ? void 0 : next.focus());
535
- },
536
- focusPrevTab(ctx) {
537
- if (!ctx.focusedValue)
538
- return;
539
- const prev = dom.getPrevEl(ctx, ctx.focusedValue);
540
- nextTick(() => prev == null ? void 0 : prev.focus());
541
- },
542
- setIndicatorRect(ctx) {
543
- nextTick(() => {
544
- if (!ctx.isIndicatorRendered || !ctx.value)
528
+ (_a = ctx2.onDelete) == null ? void 0 : _a.call(ctx2, { value: evt.value });
529
+ },
530
+ focusFirstTab(ctx2) {
531
+ nextTick(() => {
532
+ var _a;
533
+ return (_a = dom.getFirstEl(ctx2)) == null ? void 0 : _a.focus();
534
+ });
535
+ },
536
+ focusLastTab(ctx2) {
537
+ nextTick(() => {
538
+ var _a;
539
+ return (_a = dom.getLastEl(ctx2)) == null ? void 0 : _a.focus();
540
+ });
541
+ },
542
+ focusNextTab(ctx2) {
543
+ if (!ctx2.focusedValue)
545
544
  return;
546
- ctx.indicatorRect = dom.getRectById(ctx, ctx.value);
547
- if (ctx.hasMeasuredRect)
545
+ const next = dom.getNextEl(ctx2, ctx2.focusedValue);
546
+ nextTick(() => next == null ? void 0 : next.focus());
547
+ },
548
+ focusPrevTab(ctx2) {
549
+ if (!ctx2.focusedValue)
548
550
  return;
551
+ const prev = dom.getPrevEl(ctx2, ctx2.focusedValue);
552
+ nextTick(() => prev == null ? void 0 : prev.focus());
553
+ },
554
+ setIndicatorRect(ctx2) {
549
555
  nextTick(() => {
550
- ctx.hasMeasuredRect = true;
556
+ if (!ctx2.isIndicatorRendered || !ctx2.value)
557
+ return;
558
+ ctx2.indicatorRect = dom.getRectById(ctx2, ctx2.value);
559
+ if (ctx2.hasMeasuredRect)
560
+ return;
561
+ nextTick(() => {
562
+ ctx2.hasMeasuredRect = true;
563
+ });
551
564
  });
552
- });
553
- },
554
- checkRenderedElements(ctx) {
555
- nextTick(() => {
556
- ctx.isIndicatorRendered = !!dom.getIndicatorEl(ctx);
557
- });
558
- },
559
- clearMeasured(ctx) {
560
- ctx.hasMeasuredRect = false;
561
- },
562
- invokeOnChange(ctx) {
563
- var _a;
564
- (_a = ctx.onChange) == null ? void 0 : _a.call(ctx, { value: ctx.value });
565
- },
566
- invokeOnFocus(ctx) {
567
- var _a;
568
- (_a = ctx.onFocus) == null ? void 0 : _a.call(ctx, { value: ctx.focusedValue });
569
- },
570
- setPrevSelectedTabs(ctx) {
571
- if (ctx.value != null) {
572
- const newSelected = Array.from(ctx.previousValues).concat(ctx.value);
573
- ctx.previousValues = Array.from(new Set(newSelected));
574
- }
575
- },
576
- setContentTabIndex(ctx) {
577
- nextTick(() => {
578
- const panel = dom.getActiveContentEl(ctx);
579
- if (!panel)
580
- return;
581
- const focusables = getFocusables(panel);
582
- if (focusables.length > 0) {
583
- panel.removeAttribute("tabindex");
584
- } else {
585
- panel.setAttribute("tabindex", "0");
565
+ },
566
+ checkRenderedElements(ctx2) {
567
+ nextTick(() => {
568
+ ctx2.isIndicatorRendered = !!dom.getIndicatorEl(ctx2);
569
+ });
570
+ },
571
+ clearMeasured(ctx2) {
572
+ ctx2.hasMeasuredRect = false;
573
+ },
574
+ invokeOnChange(ctx2) {
575
+ var _a;
576
+ (_a = ctx2.onChange) == null ? void 0 : _a.call(ctx2, { value: ctx2.value });
577
+ },
578
+ invokeOnFocus(ctx2) {
579
+ var _a;
580
+ (_a = ctx2.onFocus) == null ? void 0 : _a.call(ctx2, { value: ctx2.focusedValue });
581
+ },
582
+ setPrevSelectedTabs(ctx2) {
583
+ if (ctx2.value != null) {
584
+ const newSelected = Array.from(ctx2.previousValues).concat(ctx2.value);
585
+ ctx2.previousValues = Array.from(new Set(newSelected));
586
586
  }
587
- });
587
+ },
588
+ setContentTabIndex(ctx2) {
589
+ nextTick(() => {
590
+ const panel = dom.getActiveContentEl(ctx2);
591
+ if (!panel)
592
+ return;
593
+ const focusables = getFocusables(panel);
594
+ if (focusables.length > 0) {
595
+ panel.removeAttribute("tabindex");
596
+ } else {
597
+ panel.setAttribute("tabindex", "0");
598
+ }
599
+ });
600
+ }
588
601
  }
589
- }
590
- });
602
+ });
603
+ }
591
604
  //# sourceMappingURL=index.js.map