@zag-js/slider 1.26.3 → 1.26.5

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.mts CHANGED
@@ -199,6 +199,16 @@ interface SliderSchema {
199
199
  state: "idle" | "dragging" | "focus";
200
200
  props: RequiredBy<SliderProps, PropsWithDefault>;
201
201
  context: Context;
202
+ refs: {
203
+ /**
204
+ * The offset from the thumb center when pointer down occurs.
205
+ * Used to maintain constant offset during drag.
206
+ */
207
+ thumbDragOffset: {
208
+ x: number;
209
+ y: number;
210
+ } | null;
211
+ };
202
212
  computed: Computed;
203
213
  event: EventObject;
204
214
  action: string;
package/dist/index.d.ts CHANGED
@@ -199,6 +199,16 @@ interface SliderSchema {
199
199
  state: "idle" | "dragging" | "focus";
200
200
  props: RequiredBy<SliderProps, PropsWithDefault>;
201
201
  context: Context;
202
+ refs: {
203
+ /**
204
+ * The offset from the thumb center when pointer down occurs.
205
+ * Used to maintain constant offset during drag.
206
+ */
207
+ thumbDragOffset: {
208
+ x: number;
209
+ y: number;
210
+ } | null;
211
+ };
202
212
  computed: Computed;
203
213
  event: EventObject;
204
214
  action: string;
package/dist/index.js CHANGED
@@ -36,10 +36,15 @@ var getFirstThumbEl = (ctx) => getThumbEls(ctx)[0];
36
36
  var getHiddenInputEl = (ctx, index) => ctx.getById(getHiddenInputId(ctx, index));
37
37
  var getControlEl = (ctx) => ctx.getById(getControlId(ctx));
38
38
  var getPointValue = (params, point) => {
39
- const { prop, scope } = params;
39
+ const { prop, scope, refs } = params;
40
40
  const controlEl = getControlEl(scope);
41
41
  if (!controlEl) return;
42
- const relativePoint = domQuery.getRelativePoint(point, controlEl);
42
+ const offset = refs.get("thumbDragOffset");
43
+ const adjustedPoint = {
44
+ x: point.x - (offset?.x ?? 0),
45
+ y: point.y - (offset?.y ?? 0)
46
+ };
47
+ const relativePoint = domQuery.getRelativePoint(adjustedPoint, controlEl);
43
48
  const percent = relativePoint.getPercentValue({
44
49
  orientation: prop("orientation"),
45
50
  dir: prop("dir"),
@@ -415,7 +420,17 @@ function connect(service, normalize2) {
415
420
  onPointerDown(event) {
416
421
  if (!interactive) return;
417
422
  if (!domQuery.isLeftClick(event)) return;
418
- send({ type: "THUMB_POINTER_DOWN", index });
423
+ const thumbEl = event.currentTarget;
424
+ const rect = thumbEl.getBoundingClientRect();
425
+ const midpoint = {
426
+ x: rect.left + rect.width / 2,
427
+ y: rect.top + rect.height / 2
428
+ };
429
+ const offset = {
430
+ x: event.clientX - midpoint.x,
431
+ y: event.clientY - midpoint.y
432
+ };
433
+ send({ type: "THUMB_POINTER_DOWN", index, offset });
419
434
  event.stopPropagation();
420
435
  },
421
436
  onBlur() {
@@ -634,6 +649,11 @@ var machine = core.createMachine({
634
649
  }))
635
650
  };
636
651
  },
652
+ refs() {
653
+ return {
654
+ thumbDragOffset: null
655
+ };
656
+ },
637
657
  computed: {
638
658
  isHorizontal: ({ prop }) => prop("orientation") === "horizontal",
639
659
  isVertical: ({ prop }) => prop("orientation") === "vertical",
@@ -687,7 +707,7 @@ var machine = core.createMachine({
687
707
  },
688
708
  THUMB_POINTER_DOWN: {
689
709
  target: "dragging",
690
- actions: ["setFocusedIndex", "focusActiveThumb"]
710
+ actions: ["setFocusedIndex", "setThumbDragOffset", "focusActiveThumb"]
691
711
  }
692
712
  }
693
713
  },
@@ -700,7 +720,7 @@ var machine = core.createMachine({
700
720
  },
701
721
  THUMB_POINTER_DOWN: {
702
722
  target: "dragging",
703
- actions: ["setFocusedIndex", "focusActiveThumb"]
723
+ actions: ["setFocusedIndex", "setThumbDragOffset", "focusActiveThumb"]
704
724
  },
705
725
  ARROW_DEC: {
706
726
  actions: ["decrementThumbAtIndex", "invokeOnChangeEnd"]
@@ -726,14 +746,14 @@ var machine = core.createMachine({
726
746
  on: {
727
747
  POINTER_UP: {
728
748
  target: "focus",
729
- actions: ["invokeOnChangeEnd"]
749
+ actions: ["invokeOnChangeEnd", "clearThumbDragOffset"]
730
750
  },
731
751
  POINTER_MOVE: {
732
752
  actions: ["setPointerValue"]
733
753
  },
734
754
  POINTER_CANCEL: {
735
755
  target: "idle",
736
- actions: ["clearFocusedIndex"]
756
+ actions: ["clearFocusedIndex", "clearThumbDragOffset"]
737
757
  }
738
758
  }
739
759
  }
@@ -809,6 +829,13 @@ var machine = core.createMachine({
809
829
  clearFocusedIndex({ context }) {
810
830
  context.set("focusedIndex", -1);
811
831
  },
832
+ setThumbDragOffset(params) {
833
+ const { refs, event } = params;
834
+ refs.set("thumbDragOffset", event.offset ?? null);
835
+ },
836
+ clearThumbDragOffset({ refs }) {
837
+ refs.set("thumbDragOffset", null);
838
+ },
812
839
  setPointerValue(params) {
813
840
  queueMicrotask(() => {
814
841
  const { context, event } = params;
package/dist/index.mjs CHANGED
@@ -34,10 +34,15 @@ var getFirstThumbEl = (ctx) => getThumbEls(ctx)[0];
34
34
  var getHiddenInputEl = (ctx, index) => ctx.getById(getHiddenInputId(ctx, index));
35
35
  var getControlEl = (ctx) => ctx.getById(getControlId(ctx));
36
36
  var getPointValue = (params, point) => {
37
- const { prop, scope } = params;
37
+ const { prop, scope, refs } = params;
38
38
  const controlEl = getControlEl(scope);
39
39
  if (!controlEl) return;
40
- const relativePoint = getRelativePoint(point, controlEl);
40
+ const offset = refs.get("thumbDragOffset");
41
+ const adjustedPoint = {
42
+ x: point.x - (offset?.x ?? 0),
43
+ y: point.y - (offset?.y ?? 0)
44
+ };
45
+ const relativePoint = getRelativePoint(adjustedPoint, controlEl);
41
46
  const percent = relativePoint.getPercentValue({
42
47
  orientation: prop("orientation"),
43
48
  dir: prop("dir"),
@@ -413,7 +418,17 @@ function connect(service, normalize2) {
413
418
  onPointerDown(event) {
414
419
  if (!interactive) return;
415
420
  if (!isLeftClick(event)) return;
416
- send({ type: "THUMB_POINTER_DOWN", index });
421
+ const thumbEl = event.currentTarget;
422
+ const rect = thumbEl.getBoundingClientRect();
423
+ const midpoint = {
424
+ x: rect.left + rect.width / 2,
425
+ y: rect.top + rect.height / 2
426
+ };
427
+ const offset = {
428
+ x: event.clientX - midpoint.x,
429
+ y: event.clientY - midpoint.y
430
+ };
431
+ send({ type: "THUMB_POINTER_DOWN", index, offset });
417
432
  event.stopPropagation();
418
433
  },
419
434
  onBlur() {
@@ -632,6 +647,11 @@ var machine = createMachine({
632
647
  }))
633
648
  };
634
649
  },
650
+ refs() {
651
+ return {
652
+ thumbDragOffset: null
653
+ };
654
+ },
635
655
  computed: {
636
656
  isHorizontal: ({ prop }) => prop("orientation") === "horizontal",
637
657
  isVertical: ({ prop }) => prop("orientation") === "vertical",
@@ -685,7 +705,7 @@ var machine = createMachine({
685
705
  },
686
706
  THUMB_POINTER_DOWN: {
687
707
  target: "dragging",
688
- actions: ["setFocusedIndex", "focusActiveThumb"]
708
+ actions: ["setFocusedIndex", "setThumbDragOffset", "focusActiveThumb"]
689
709
  }
690
710
  }
691
711
  },
@@ -698,7 +718,7 @@ var machine = createMachine({
698
718
  },
699
719
  THUMB_POINTER_DOWN: {
700
720
  target: "dragging",
701
- actions: ["setFocusedIndex", "focusActiveThumb"]
721
+ actions: ["setFocusedIndex", "setThumbDragOffset", "focusActiveThumb"]
702
722
  },
703
723
  ARROW_DEC: {
704
724
  actions: ["decrementThumbAtIndex", "invokeOnChangeEnd"]
@@ -724,14 +744,14 @@ var machine = createMachine({
724
744
  on: {
725
745
  POINTER_UP: {
726
746
  target: "focus",
727
- actions: ["invokeOnChangeEnd"]
747
+ actions: ["invokeOnChangeEnd", "clearThumbDragOffset"]
728
748
  },
729
749
  POINTER_MOVE: {
730
750
  actions: ["setPointerValue"]
731
751
  },
732
752
  POINTER_CANCEL: {
733
753
  target: "idle",
734
- actions: ["clearFocusedIndex"]
754
+ actions: ["clearFocusedIndex", "clearThumbDragOffset"]
735
755
  }
736
756
  }
737
757
  }
@@ -807,6 +827,13 @@ var machine = createMachine({
807
827
  clearFocusedIndex({ context }) {
808
828
  context.set("focusedIndex", -1);
809
829
  },
830
+ setThumbDragOffset(params) {
831
+ const { refs, event } = params;
832
+ refs.set("thumbDragOffset", event.offset ?? null);
833
+ },
834
+ clearThumbDragOffset({ refs }) {
835
+ refs.set("thumbDragOffset", null);
836
+ },
810
837
  setPointerValue(params) {
811
838
  queueMicrotask(() => {
812
839
  const { context, event } = params;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zag-js/slider",
3
- "version": "1.26.3",
3
+ "version": "1.26.5",
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.3",
31
- "@zag-js/core": "1.26.3",
32
- "@zag-js/dom-query": "1.26.3",
33
- "@zag-js/utils": "1.26.3",
34
- "@zag-js/types": "1.26.3"
30
+ "@zag-js/anatomy": "1.26.5",
31
+ "@zag-js/core": "1.26.5",
32
+ "@zag-js/dom-query": "1.26.5",
33
+ "@zag-js/utils": "1.26.5",
34
+ "@zag-js/types": "1.26.5"
35
35
  },
36
36
  "devDependencies": {
37
37
  "clean-package": "2.2.0"