Skip to content

Commit 28a35c5

Browse files
committed
feat: add origin end to slider
1 parent 9e3d225 commit 28a35c5

File tree

5 files changed

+29
-8
lines changed

5 files changed

+29
-8
lines changed

.changeset/sixty-sides-fail.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@zag-js/slider": minor
3+
---
4+
5+
Add `end` to slider `origin` property.

packages/machines/slider/src/slider.style.ts

+3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ export function getRangeOffsets(params: Pick<Ctx, "prop" | "computed">) {
2727
const end = isNegative ? "50%" : `${100 - valuePercent[0]}%`
2828
return { start, end }
2929
}
30+
if (prop("origin") === "end") {
31+
return { start: `${lastPercent}%`, end: "0%" }
32+
}
3033

3134
return { start: "0%", end: `${100 - lastPercent}%` }
3235
}

packages/machines/slider/src/slider.types.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,15 @@ export interface SliderProps extends DirectionProperty, CommonProperties {
119119
*/
120120
orientation?: "vertical" | "horizontal" | undefined
121121
/**
122-
* The origin of the slider range
122+
* The origin of the slider range. The track is filled from the origin
123+
* to the thumb for single values.
123124
* - "start": Useful when the value represents an absolute value
124125
* - "center": Useful when the value represents an offset (relative)
126+
* - "end": Useful when the value represents an offset from the end
125127
*
126128
* @default "start"
127129
*/
128-
origin?: "start" | "center" | undefined
130+
origin?: "start" | "center" | "end" | undefined
129131
/**
130132
* The alignment of the slider thumb relative to the track
131133
* - `center`: the thumb will extend beyond the bounds of the slider track.

shared/src/controls.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ export const sliderControls = defineControls({
117117
orientation: { type: "select", options: ["horizontal", "vertical"] as const, defaultValue: "horizontal" },
118118
thumbAlignment: { type: "select", options: ["contain", "center"] as const, defaultValue: "contain" },
119119
dir: { type: "select", options: ["ltr", "rtl"] as const, defaultValue: "ltr" },
120-
origin: { type: "select", options: ["center", "start"] as const, defaultValue: "start" },
120+
origin: { type: "select", options: ["center", "start", "end"] as const, defaultValue: "start" },
121121
min: { type: "number", defaultValue: 0 },
122122
max: { type: "number", defaultValue: 100 },
123123
step: { type: "number", defaultValue: 1 },

website/data/components/slider.mdx

+16-5
Original file line numberDiff line numberDiff line change
@@ -143,19 +143,30 @@ const service = useMachine(slider.machine, {
143143

144144
## Changing the start position
145145

146-
By default, the slider's "zero position" is usually at the start position (left
147-
in LTR and right in RTL).
146+
By default, the slider's origin is at the `start` position (left
147+
in LTR and right in RTL). Change it by setting the `origin` property to these values:
148148

149-
In scenarios where the value represents an offset (or relative value), it might
150-
be useful to change the "zero position" to center. To do this, pass the `origin`
151-
context property to `center`.
149+
- `start`: the track will be filled from start to the thumb (default).
150+
- `center`: the track will be filled from the center (50%) to the thumb.
151+
- `end`: the track will be filled from the thumb to the end.
152+
153+
This applies to sliders with single values.
154+
155+
In scenarios where the value represents an offset (or relative value) on a
156+
diverging scale, it might be useful to change the origin to center. To do this,
157+
set the `origin` context property to `center`.
152158

153159
```jsx {2}
154160
const service = useMachine(slider.machine, {
155161
origin: "center",
156162
})
157163
```
158164

165+
In scenarios where the slider value is used as a threshold to include values above it,
166+
it might make more sense to set the `origin` to `end` to have the track filled from the
167+
thumb to the end.
168+
169+
159170
## Changing the thumb alignment
160171

161172
By default, the thumb is aligned to the start of the track. Set the

0 commit comments

Comments
 (0)