@zag-js/slider 1.26.0 → 1.26.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.js CHANGED
@@ -137,13 +137,15 @@ function getVisibility(params) {
137
137
  return visibility;
138
138
  }
139
139
  function getThumbStyle(params, index) {
140
- const { computed } = params;
140
+ const { computed, context } = params;
141
141
  const placementProp = computed("isVertical") ? "bottom" : "insetInlineStart";
142
+ const focusedIndex = context.get("focusedIndex");
142
143
  return {
143
144
  visibility: getVisibility(params),
144
145
  position: "absolute",
145
146
  transform: "var(--slider-thumb-transform)",
146
- [placementProp]: `var(--slider-thumb-offset-${index})`
147
+ [placementProp]: `var(--slider-thumb-offset-${index})`,
148
+ zIndex: focusedIndex === index ? 1 : void 0
147
149
  };
148
150
  }
149
151
  function getControlStyle() {
@@ -237,7 +239,31 @@ function increment(params, index, step) {
237
239
  }
238
240
  function getClosestIndex(params, pointValue) {
239
241
  const { context } = params;
240
- return utils.getClosestValueIndex(context.get("value"), pointValue);
242
+ const values = context.get("value");
243
+ let closestIndex = 0;
244
+ let minDistance = Math.abs(values[0] - pointValue);
245
+ for (let i = 1; i < values.length; i++) {
246
+ const distance = Math.abs(values[i] - pointValue);
247
+ if (distance <= minDistance) {
248
+ closestIndex = i;
249
+ minDistance = distance;
250
+ }
251
+ }
252
+ return selectMovableThumb(params, closestIndex);
253
+ }
254
+ function selectMovableThumb(params, index) {
255
+ const { context, prop } = params;
256
+ const values = context.get("value");
257
+ const max = prop("max");
258
+ const thumbValue = values[index];
259
+ if (thumbValue === max) {
260
+ let movableIndex = index;
261
+ while (movableIndex > 0 && values[movableIndex - 1] === max) {
262
+ movableIndex -= 1;
263
+ }
264
+ return movableIndex;
265
+ }
266
+ return index;
241
267
  }
242
268
 
243
269
  // src/slider.connect.ts
@@ -626,7 +652,7 @@ var machine = core.createMachine({
626
652
  });
627
653
  track([() => computed("isDisabled")], () => {
628
654
  if (computed("isDisabled")) {
629
- send({ type: "BLUR" });
655
+ send({ type: "POINTER_CANCEL" });
630
656
  }
631
657
  });
632
658
  },
@@ -705,7 +731,7 @@ var machine = core.createMachine({
705
731
  POINTER_MOVE: {
706
732
  actions: ["setPointerValue"]
707
733
  },
708
- BLUR: {
734
+ POINTER_CANCEL: {
709
735
  target: "idle",
710
736
  actions: ["clearFocusedIndex"]
711
737
  }
@@ -775,8 +801,10 @@ var machine = core.createMachine({
775
801
  const focusedIndex = getClosestIndex(params, pointValue);
776
802
  context.set("focusedIndex", focusedIndex);
777
803
  },
778
- setFocusedIndex({ context, event }) {
779
- context.set("focusedIndex", event.index);
804
+ setFocusedIndex(params) {
805
+ const { context, event } = params;
806
+ const movableIndex = selectMovableThumb(params, event.index);
807
+ context.set("focusedIndex", movableIndex);
780
808
  },
781
809
  clearFocusedIndex({ context }) {
782
810
  context.set("focusedIndex", -1);
package/dist/index.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  import { createAnatomy } from '@zag-js/anatomy';
2
2
  import { raf, setElementValue, trackElementRect, queryAll, trackPointerMove, trackFormControl, getRelativePoint, dispatchInputValueEvent, dataAttr, isLeftClick, isModifierKey, getEventPoint, ariaAttr, getEventStep, getEventKey } from '@zag-js/dom-query';
3
- import { setValueAtIndex, pick, getValuePercent, isEqual, createSplitProps, snapValueToStep, clampValue, getValueRanges, getNextStepValue, getPreviousStepValue, getPercentValue, getClosestValueIndex, isValueWithinRange, first, last, toPx, getValueTransformer } from '@zag-js/utils';
3
+ import { setValueAtIndex, pick, getValuePercent, isEqual, createSplitProps, snapValueToStep, clampValue, getValueRanges, getNextStepValue, getPreviousStepValue, getPercentValue, isValueWithinRange, first, last, toPx, getValueTransformer } from '@zag-js/utils';
4
4
  import { createMachine, memo } from '@zag-js/core';
5
5
  import { createProps } from '@zag-js/types';
6
6
 
@@ -135,13 +135,15 @@ function getVisibility(params) {
135
135
  return visibility;
136
136
  }
137
137
  function getThumbStyle(params, index) {
138
- const { computed } = params;
138
+ const { computed, context } = params;
139
139
  const placementProp = computed("isVertical") ? "bottom" : "insetInlineStart";
140
+ const focusedIndex = context.get("focusedIndex");
140
141
  return {
141
142
  visibility: getVisibility(params),
142
143
  position: "absolute",
143
144
  transform: "var(--slider-thumb-transform)",
144
- [placementProp]: `var(--slider-thumb-offset-${index})`
145
+ [placementProp]: `var(--slider-thumb-offset-${index})`,
146
+ zIndex: focusedIndex === index ? 1 : void 0
145
147
  };
146
148
  }
147
149
  function getControlStyle() {
@@ -235,7 +237,31 @@ function increment(params, index, step) {
235
237
  }
236
238
  function getClosestIndex(params, pointValue) {
237
239
  const { context } = params;
238
- return getClosestValueIndex(context.get("value"), pointValue);
240
+ const values = context.get("value");
241
+ let closestIndex = 0;
242
+ let minDistance = Math.abs(values[0] - pointValue);
243
+ for (let i = 1; i < values.length; i++) {
244
+ const distance = Math.abs(values[i] - pointValue);
245
+ if (distance <= minDistance) {
246
+ closestIndex = i;
247
+ minDistance = distance;
248
+ }
249
+ }
250
+ return selectMovableThumb(params, closestIndex);
251
+ }
252
+ function selectMovableThumb(params, index) {
253
+ const { context, prop } = params;
254
+ const values = context.get("value");
255
+ const max = prop("max");
256
+ const thumbValue = values[index];
257
+ if (thumbValue === max) {
258
+ let movableIndex = index;
259
+ while (movableIndex > 0 && values[movableIndex - 1] === max) {
260
+ movableIndex -= 1;
261
+ }
262
+ return movableIndex;
263
+ }
264
+ return index;
239
265
  }
240
266
 
241
267
  // src/slider.connect.ts
@@ -624,7 +650,7 @@ var machine = createMachine({
624
650
  });
625
651
  track([() => computed("isDisabled")], () => {
626
652
  if (computed("isDisabled")) {
627
- send({ type: "BLUR" });
653
+ send({ type: "POINTER_CANCEL" });
628
654
  }
629
655
  });
630
656
  },
@@ -703,7 +729,7 @@ var machine = createMachine({
703
729
  POINTER_MOVE: {
704
730
  actions: ["setPointerValue"]
705
731
  },
706
- BLUR: {
732
+ POINTER_CANCEL: {
707
733
  target: "idle",
708
734
  actions: ["clearFocusedIndex"]
709
735
  }
@@ -773,8 +799,10 @@ var machine = createMachine({
773
799
  const focusedIndex = getClosestIndex(params, pointValue);
774
800
  context.set("focusedIndex", focusedIndex);
775
801
  },
776
- setFocusedIndex({ context, event }) {
777
- context.set("focusedIndex", event.index);
802
+ setFocusedIndex(params) {
803
+ const { context, event } = params;
804
+ const movableIndex = selectMovableThumb(params, event.index);
805
+ context.set("focusedIndex", movableIndex);
778
806
  },
779
807
  clearFocusedIndex({ context }) {
780
808
  context.set("focusedIndex", -1);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zag-js/slider",
3
- "version": "1.26.0",
3
+ "version": "1.26.2",
4
4
  "description": "Core logic for the slider widget implemented as a state machine",
5
5
  "keywords": [
6
6
  "js",
@@ -27,11 +27,11 @@
27
27
  "url": "https://github.com/chakra-ui/zag/issues"
28
28
  },
29
29
  "dependencies": {
30
- "@zag-js/anatomy": "1.26.0",
31
- "@zag-js/core": "1.26.0",
32
- "@zag-js/dom-query": "1.26.0",
33
- "@zag-js/utils": "1.26.0",
34
- "@zag-js/types": "1.26.0"
30
+ "@zag-js/anatomy": "1.26.2",
31
+ "@zag-js/core": "1.26.2",
32
+ "@zag-js/dom-query": "1.26.2",
33
+ "@zag-js/utils": "1.26.2",
34
+ "@zag-js/types": "1.26.2"
35
35
  },
36
36
  "devDependencies": {
37
37
  "clean-package": "2.2.0"