@zag-js/tabs 0.2.0 → 0.2.2

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
@@ -99,6 +99,7 @@ declare function connect<T extends PropTypes>(state: State, send: Send, normaliz
99
99
  focusedValue: string | null;
100
100
  previousValues: string[];
101
101
  setValue(value: string): void;
102
+ clearValue(): void;
102
103
  rootProps: T["element"];
103
104
  triggerGroupProps: T["element"];
104
105
  getTriggerProps(props: TabProps): T["button"];
@@ -110,6 +111,6 @@ declare function connect<T extends PropTypes>(state: State, send: Send, normaliz
110
111
  indicatorProps: T["element"];
111
112
  };
112
113
 
113
- declare function machine(ctx: UserDefinedContext): _zag_js_core.Machine<MachineContext, MachineState, _zag_js_core.StateMachine.AnyEventObject>;
114
+ declare function machine(userContext: UserDefinedContext): _zag_js_core.Machine<MachineContext, MachineState, _zag_js_core.StateMachine.AnyEventObject>;
114
115
 
115
116
  export { UserDefinedContext as Context, connect, machine };
package/dist/index.js CHANGED
@@ -194,6 +194,15 @@ function prevById(v, id, loop = true) {
194
194
  // ../../utilities/core/dist/index.mjs
195
195
  var first = (v) => v[0];
196
196
  var last = (v) => v[v.length - 1];
197
+ var isArray = (v) => Array.isArray(v);
198
+ var isObject = (v) => !(v == null || typeof v !== "object" || isArray(v));
199
+ function compact(obj) {
200
+ if (obj === void 0)
201
+ return obj;
202
+ return Object.fromEntries(
203
+ Object.entries(obj).filter(([, value]) => value !== void 0).map(([key, value]) => [key, isObject(value) ? compact(value) : value])
204
+ );
205
+ }
197
206
 
198
207
  // src/tabs.dom.ts
199
208
  var dom = defineDomHelpers({
@@ -270,6 +279,9 @@ function connect(state, send, normalize) {
270
279
  setValue(value) {
271
280
  send({ type: "SET_VALUE", value });
272
281
  },
282
+ clearValue() {
283
+ send({ type: "CLEAR_VALUE" });
284
+ },
273
285
  rootProps: normalize.element({
274
286
  "data-part": "root",
275
287
  id: dom.getRootId(state.context),
@@ -405,7 +417,8 @@ function connect(state, send, normalize) {
405
417
  // src/tabs.machine.ts
406
418
  var import_core = require("@zag-js/core");
407
419
  var { not } = import_core.guards;
408
- function machine(ctx) {
420
+ function machine(userContext) {
421
+ const ctx = compact(userContext);
409
422
  return (0, import_core.createMachine)(
410
423
  {
411
424
  initial: "unknown",
@@ -437,8 +450,8 @@ function machine(ctx) {
437
450
  SET_VALUE: {
438
451
  actions: "setValue"
439
452
  },
440
- DELETE: {
441
- actions: "deleteValue"
453
+ CLEAR_VALUE: {
454
+ actions: "clearValue"
442
455
  }
443
456
  },
444
457
  states: {
@@ -526,6 +539,9 @@ function machine(ctx) {
526
539
  setValue(ctx2, evt) {
527
540
  ctx2.value = evt.value;
528
541
  },
542
+ clearValue(ctx2) {
543
+ ctx2.value = null;
544
+ },
529
545
  invokeOnDelete(ctx2, evt) {
530
546
  var _a;
531
547
  (_a = ctx2.onDelete) == null ? void 0 : _a.call(ctx2, { value: evt.value });
@@ -582,8 +598,7 @@ function machine(ctx) {
582
598
  },
583
599
  setPrevSelectedTabs(ctx2) {
584
600
  if (ctx2.value != null) {
585
- const newSelected = Array.from(ctx2.previousValues).concat(ctx2.value);
586
- ctx2.previousValues = Array.from(new Set(newSelected));
601
+ ctx2.previousValues = pushUnique(Array.from(ctx2.previousValues), ctx2.value);
587
602
  }
588
603
  },
589
604
  setContentTabIndex(ctx2) {
@@ -603,6 +618,15 @@ function machine(ctx) {
603
618
  }
604
619
  );
605
620
  }
621
+ function pushUnique(arr, value) {
622
+ const newArr = arr.slice();
623
+ const index = newArr.indexOf(value);
624
+ if (index > -1) {
625
+ newArr.splice(index, 1);
626
+ }
627
+ newArr.push(value);
628
+ return newArr;
629
+ }
606
630
  // Annotate the CommonJS export names for ESM import in node:
607
631
  0 && (module.exports = {
608
632
  connect,
package/dist/index.mjs CHANGED
@@ -167,6 +167,15 @@ function prevById(v, id, loop = true) {
167
167
  // ../../utilities/core/dist/index.mjs
168
168
  var first = (v) => v[0];
169
169
  var last = (v) => v[v.length - 1];
170
+ var isArray = (v) => Array.isArray(v);
171
+ var isObject = (v) => !(v == null || typeof v !== "object" || isArray(v));
172
+ function compact(obj) {
173
+ if (obj === void 0)
174
+ return obj;
175
+ return Object.fromEntries(
176
+ Object.entries(obj).filter(([, value]) => value !== void 0).map(([key, value]) => [key, isObject(value) ? compact(value) : value])
177
+ );
178
+ }
170
179
 
171
180
  // src/tabs.dom.ts
172
181
  var dom = defineDomHelpers({
@@ -243,6 +252,9 @@ function connect(state, send, normalize) {
243
252
  setValue(value) {
244
253
  send({ type: "SET_VALUE", value });
245
254
  },
255
+ clearValue() {
256
+ send({ type: "CLEAR_VALUE" });
257
+ },
246
258
  rootProps: normalize.element({
247
259
  "data-part": "root",
248
260
  id: dom.getRootId(state.context),
@@ -378,7 +390,8 @@ function connect(state, send, normalize) {
378
390
  // src/tabs.machine.ts
379
391
  import { createMachine, guards } from "@zag-js/core";
380
392
  var { not } = guards;
381
- function machine(ctx) {
393
+ function machine(userContext) {
394
+ const ctx = compact(userContext);
382
395
  return createMachine(
383
396
  {
384
397
  initial: "unknown",
@@ -410,8 +423,8 @@ function machine(ctx) {
410
423
  SET_VALUE: {
411
424
  actions: "setValue"
412
425
  },
413
- DELETE: {
414
- actions: "deleteValue"
426
+ CLEAR_VALUE: {
427
+ actions: "clearValue"
415
428
  }
416
429
  },
417
430
  states: {
@@ -499,6 +512,9 @@ function machine(ctx) {
499
512
  setValue(ctx2, evt) {
500
513
  ctx2.value = evt.value;
501
514
  },
515
+ clearValue(ctx2) {
516
+ ctx2.value = null;
517
+ },
502
518
  invokeOnDelete(ctx2, evt) {
503
519
  var _a;
504
520
  (_a = ctx2.onDelete) == null ? void 0 : _a.call(ctx2, { value: evt.value });
@@ -555,8 +571,7 @@ function machine(ctx) {
555
571
  },
556
572
  setPrevSelectedTabs(ctx2) {
557
573
  if (ctx2.value != null) {
558
- const newSelected = Array.from(ctx2.previousValues).concat(ctx2.value);
559
- ctx2.previousValues = Array.from(new Set(newSelected));
574
+ ctx2.previousValues = pushUnique(Array.from(ctx2.previousValues), ctx2.value);
560
575
  }
561
576
  },
562
577
  setContentTabIndex(ctx2) {
@@ -576,6 +591,15 @@ function machine(ctx) {
576
591
  }
577
592
  );
578
593
  }
594
+ function pushUnique(arr, value) {
595
+ const newArr = arr.slice();
596
+ const index = newArr.indexOf(value);
597
+ if (index > -1) {
598
+ newArr.splice(index, 1);
599
+ }
600
+ newArr.push(value);
601
+ return newArr;
602
+ }
579
603
  export {
580
604
  connect,
581
605
  machine
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zag-js/tabs",
3
- "version": "0.2.0",
3
+ "version": "0.2.2",
4
4
  "description": "Core logic for the tabs widget implemented as a state machine",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -29,12 +29,12 @@
29
29
  "url": "https://github.com/chakra-ui/zag/issues"
30
30
  },
31
31
  "dependencies": {
32
- "@zag-js/core": "0.2.0",
33
- "@zag-js/types": "0.3.0"
32
+ "@zag-js/core": "0.2.2",
33
+ "@zag-js/types": "0.3.1"
34
34
  },
35
35
  "devDependencies": {
36
- "@zag-js/dom-utils": "0.2.0",
37
- "@zag-js/utils": "0.2.0"
36
+ "@zag-js/dom-utils": "0.2.1",
37
+ "@zag-js/utils": "0.3.1"
38
38
  },
39
39
  "scripts": {
40
40
  "build-fast": "tsup src/index.ts --format=esm,cjs",