@marimo-team/islands 0.21.2-dev20 → 0.21.2-dev21
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/main.js +111 -55
- package/dist/style.css +1 -1
- package/package.json +1 -1
- package/src/components/ui/range-slider.tsx +108 -1
package/package.json
CHANGED
|
@@ -18,14 +18,116 @@ const RangeSlider = React.forwardRef<
|
|
|
18
18
|
React.ElementRef<typeof SliderPrimitive.Root>,
|
|
19
19
|
React.ComponentPropsWithoutRef<typeof SliderPrimitive.Root> & {
|
|
20
20
|
valueMap: (sliderValue: number) => number;
|
|
21
|
+
steps?: number[];
|
|
21
22
|
}
|
|
22
23
|
>(({ className, valueMap, ...props }, ref) => {
|
|
23
24
|
const [open, openActions] = useBoolean(false);
|
|
24
25
|
const { locale } = useLocale();
|
|
25
26
|
|
|
27
|
+
const isDraggingRange = React.useRef(false);
|
|
28
|
+
const dragStartX = React.useRef(0);
|
|
29
|
+
const dragStartY = React.useRef(0);
|
|
30
|
+
const dragStartValue = React.useRef<number[]>([]);
|
|
31
|
+
const currentDragValue = React.useRef<number[]>([]);
|
|
32
|
+
const rootRef =
|
|
33
|
+
React.useRef<React.ElementRef<typeof SliderPrimitive.Root>>(null);
|
|
34
|
+
const trackRef = React.useRef<HTMLSpanElement>(null);
|
|
35
|
+
const dragTrackRect = React.useRef<DOMRect | null>(null);
|
|
36
|
+
|
|
37
|
+
const mergedRef = React.useCallback(
|
|
38
|
+
(node: React.ElementRef<typeof SliderPrimitive.Root>) => {
|
|
39
|
+
rootRef.current = node;
|
|
40
|
+
if (typeof ref === "function") {
|
|
41
|
+
ref(node);
|
|
42
|
+
} else if (ref) {
|
|
43
|
+
ref.current = node;
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
[ref],
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
const handleRangePointerDown = (e: React.PointerEvent<HTMLSpanElement>) => {
|
|
50
|
+
if (!props.value || props.value.length !== 2) {
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
if (props.disabled) {
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
e.preventDefault();
|
|
57
|
+
e.stopPropagation();
|
|
58
|
+
|
|
59
|
+
isDraggingRange.current = true;
|
|
60
|
+
dragStartX.current = e.clientX;
|
|
61
|
+
dragStartY.current = e.clientY;
|
|
62
|
+
dragStartValue.current = [...props.value];
|
|
63
|
+
currentDragValue.current = [...props.value];
|
|
64
|
+
dragTrackRect.current = trackRef.current?.getBoundingClientRect() ?? null;
|
|
65
|
+
|
|
66
|
+
(e.currentTarget as HTMLElement).setPointerCapture(e.pointerId);
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
const handleRangePointerMove = (e: React.PointerEvent<HTMLSpanElement>) => {
|
|
70
|
+
if (!isDraggingRange.current) {
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
e.stopPropagation();
|
|
74
|
+
|
|
75
|
+
const trackRect = dragTrackRect.current;
|
|
76
|
+
if (!trackRect) {
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const isVertical = props.orientation === "vertical";
|
|
81
|
+
const min = props.min ?? 0;
|
|
82
|
+
const max = props.max ?? 100;
|
|
83
|
+
const totalRange = max - min;
|
|
84
|
+
|
|
85
|
+
let delta: number;
|
|
86
|
+
if (isVertical) {
|
|
87
|
+
const trackLength = trackRect.height;
|
|
88
|
+
delta = -((e.clientY - dragStartY.current) / trackLength) * totalRange;
|
|
89
|
+
} else {
|
|
90
|
+
const trackLength = trackRect.width;
|
|
91
|
+
delta = ((e.clientX - dragStartX.current) / trackLength) * totalRange;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const [origLeft, origRight] = dragStartValue.current;
|
|
95
|
+
const rangeWidth = origRight - origLeft;
|
|
96
|
+
|
|
97
|
+
const steps = props.steps;
|
|
98
|
+
const step: number =
|
|
99
|
+
steps && steps.length > 1
|
|
100
|
+
? Math.min(...steps.slice(1).map((s, i) => s - steps[i]))
|
|
101
|
+
: (props.step ?? 1);
|
|
102
|
+
const snappedDelta = Math.round(delta / step) * step;
|
|
103
|
+
|
|
104
|
+
const clampedDelta = Math.max(
|
|
105
|
+
min - origLeft,
|
|
106
|
+
Math.min(max - origRight, snappedDelta),
|
|
107
|
+
);
|
|
108
|
+
|
|
109
|
+
const newLeft = origLeft + clampedDelta;
|
|
110
|
+
const newRight = newLeft + rangeWidth;
|
|
111
|
+
|
|
112
|
+
currentDragValue.current = [newLeft, newRight];
|
|
113
|
+
props.onValueChange?.([newLeft, newRight]);
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
const handleRangePointerUp = (e: React.PointerEvent<HTMLSpanElement>) => {
|
|
117
|
+
if (!isDraggingRange.current) {
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
(e.currentTarget as HTMLElement).releasePointerCapture(e.pointerId);
|
|
121
|
+
isDraggingRange.current = false;
|
|
122
|
+
|
|
123
|
+
if (currentDragValue.current.length === 2) {
|
|
124
|
+
props.onValueCommit?.(currentDragValue.current);
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
|
|
26
128
|
return (
|
|
27
129
|
<SliderPrimitive.Root
|
|
28
|
-
ref={
|
|
130
|
+
ref={mergedRef}
|
|
29
131
|
className={cn(
|
|
30
132
|
"relative flex touch-none select-none hover:cursor-pointer",
|
|
31
133
|
"data-[orientation=horizontal]:w-full data-[orientation=horizontal]:items-center",
|
|
@@ -36,6 +138,7 @@ const RangeSlider = React.forwardRef<
|
|
|
36
138
|
{...props}
|
|
37
139
|
>
|
|
38
140
|
<SliderPrimitive.Track
|
|
141
|
+
ref={trackRef}
|
|
39
142
|
data-testid="track"
|
|
40
143
|
className={cn(
|
|
41
144
|
"relative grow overflow-hidden rounded-full bg-slate-200 dark:bg-accent/60",
|
|
@@ -50,7 +153,11 @@ const RangeSlider = React.forwardRef<
|
|
|
50
153
|
"data-[orientation=horizontal]:h-full",
|
|
51
154
|
"data-[orientation=vertical]:w-full",
|
|
52
155
|
"data-disabled:opacity-50",
|
|
156
|
+
"hover:cursor-grab active:cursor-grabbing",
|
|
53
157
|
)}
|
|
158
|
+
onPointerDown={handleRangePointerDown}
|
|
159
|
+
onPointerMove={handleRangePointerMove}
|
|
160
|
+
onPointerUp={handleRangePointerUp}
|
|
54
161
|
/>
|
|
55
162
|
</SliderPrimitive.Track>
|
|
56
163
|
<TooltipProvider>
|