diff --git a/.changeset/slimy-ads-build.md b/.changeset/slimy-ads-build.md new file mode 100644 index 0000000000..2ef0d63ba6 --- /dev/null +++ b/.changeset/slimy-ads-build.md @@ -0,0 +1,10 @@ +--- +"@zag-js/slider": minor +--- + +Merge the slider and range slider machines into one to prevent duplication. + +Some notable changes: + +- `value` and `onValueChange` type has been updated to be `number[]` +- Update `api.getThumbProps(index)` to `api.getThumbProps({ index })` diff --git a/.xstate/slider.js b/.xstate/slider.js index 8589aaf5da..9249ca3aca 100644 --- a/.xstate/slider.js +++ b/.xstate/slider.js @@ -13,24 +13,28 @@ const fetchMachine = createMachine({ id: "slider", initial: "idle", context: { + "hasIndex": false, "isHorizontal": false, "isHorizontal": false, "isVertical": false, "isVertical": false }, - activities: ["trackFormControlState", "trackThumbSize"], + entry: ["coarseValue"], + activities: ["trackFormControlState", "trackThumbsSize"], on: { - SET_VALUE: { + SET_VALUE: [{ + cond: "hasIndex", + actions: "setValueAtIndex" + }, { actions: "setValue" - }, + }], INCREMENT: { - actions: "increment" + actions: "incrementAtIndex" }, DECREMENT: { - actions: "decrement" + actions: "decrementAtIndex" } }, - entry: ["checkValue"], on: { UPDATE_CONTEXT: { actions: "updateContext" @@ -41,59 +45,65 @@ const fetchMachine = createMachine({ on: { POINTER_DOWN: { target: "dragging", - actions: ["setPointerValue", "invokeOnChangeStart", "focusThumb"] + actions: ["setClosestThumbIndex", "setPointerValue", "focusActiveThumb"] + }, + FOCUS: { + target: "focus", + actions: "setFocusedIndex" }, - FOCUS: "focus", THUMB_POINTER_DOWN: { target: "dragging", - actions: ["invokeOnChangeStart", "focusThumb"] + actions: ["setFocusedIndex", "focusActiveThumb"] } } }, focus: { - entry: "focusThumb", + entry: "focusActiveThumb", on: { POINTER_DOWN: { target: "dragging", - actions: ["setPointerValue", "invokeOnChangeStart", "focusThumb"] + actions: ["setClosestThumbIndex", "setPointerValue", "focusActiveThumb"] }, THUMB_POINTER_DOWN: { target: "dragging", - actions: ["invokeOnChangeStart", "focusThumb"] + actions: ["setFocusedIndex", "focusActiveThumb"] }, ARROW_LEFT: { cond: "isHorizontal", - actions: "decrement" + actions: "decrementAtIndex" }, ARROW_RIGHT: { cond: "isHorizontal", - actions: "increment" + actions: "incrementAtIndex" }, ARROW_UP: { cond: "isVertical", - actions: "increment" + actions: "incrementAtIndex" }, ARROW_DOWN: { cond: "isVertical", - actions: "decrement" + actions: "decrementAtIndex" }, PAGE_UP: { - actions: "increment" + actions: "incrementAtIndex" }, PAGE_DOWN: { - actions: "decrement" + actions: "decrementAtIndex" }, HOME: { - actions: "setToMin" + actions: "setActiveThumbToMin" }, END: { - actions: "setToMax" + actions: "setActiveThumbToMax" }, - BLUR: "idle" + BLUR: { + target: "idle", + actions: "clearFocusedIndex" + } } }, dragging: { - entry: "focusThumb", + entry: "focusActiveThumb", activities: "trackPointerMove", on: { POINTER_UP: { @@ -115,6 +125,7 @@ const fetchMachine = createMachine({ }) }, guards: { + "hasIndex": ctx => ctx["hasIndex"], "isHorizontal": ctx => ctx["isHorizontal"], "isVertical": ctx => ctx["isVertical"] } diff --git a/e2e/slider.e2e.ts b/e2e/slider.e2e.ts index fd5ec06a79..3b604b34b9 100644 --- a/e2e/slider.e2e.ts +++ b/e2e/slider.e2e.ts @@ -1,9 +1,9 @@ import { expect, test } from "@playwright/test" -import { testid, a11y, rect } from "./__utils" +import { part, a11y, rect } from "./__utils" -const output = testid("output") -const thumb = testid("thumb") -const track = testid("track") +const output = part("output") +const thumb = part("thumb") +const track = part("track") test.describe("slider", () => { test.beforeEach(async ({ page }) => { diff --git a/examples/next-ts/CHANGELOG.md b/examples/next-ts/CHANGELOG.md index afeff01eb5..38b9ec0c00 100644 --- a/examples/next-ts/CHANGELOG.md +++ b/examples/next-ts/CHANGELOG.md @@ -23,7 +23,7 @@ - @zag-js/number-input@0.1.13 - @zag-js/pin-input@0.1.11 - @zag-js/popover@0.1.11 - - @zag-js/range-slider@0.1.11 + - @zag-js/slider@0.1.11 - @zag-js/rating@0.1.11 - @zag-js/slider@0.1.11 - @zag-js/splitter@0.1.11 diff --git a/examples/next-ts/package.json b/examples/next-ts/package.json index 37a5151b4f..57b440f2c4 100644 --- a/examples/next-ts/package.json +++ b/examples/next-ts/package.json @@ -55,14 +55,13 @@ "@zag-js/presence": "workspace:*", "@zag-js/pressable": "workspace:*", "@zag-js/radio-group": "workspace:*", - "@zag-js/range-slider": "workspace:*", + "@zag-js/slider": "workspace:*", "@zag-js/rating-group": "workspace:*", "@zag-js/react": "workspace:*", "@zag-js/rect-utils": "workspace:*", "@zag-js/remove-scroll": "workspace:*", "@zag-js/select": "workspace:*", "@zag-js/shared": "workspace:*", - "@zag-js/slider": "workspace:*", "@zag-js/splitter": "workspace:*", "@zag-js/store": "workspace:*", "@zag-js/switch": "workspace:*", @@ -92,4 +91,4 @@ "typescript": "5.2.2" }, "license": "MIT" -} \ No newline at end of file +} diff --git a/examples/next-ts/pages/range-slider.tsx b/examples/next-ts/pages/range-slider.tsx index 4c4ea2cd76..5fc64be298 100644 --- a/examples/next-ts/pages/range-slider.tsx +++ b/examples/next-ts/pages/range-slider.tsx @@ -1,6 +1,6 @@ -import * as slider from "@zag-js/range-slider" +import * as slider from "@zag-js/slider" import { normalizeProps, useMachine } from "@zag-js/react" -import { rangeSliderControls } from "@zag-js/shared" +import { sliderControls } from "@zag-js/shared" import serialize from "form-serialize" import { useId } from "react" import { StateVisualizer } from "../components/state-visualizer" @@ -8,7 +8,7 @@ import { Toolbar } from "../components/toolbar" import { useControls } from "../hooks/use-controls" export default function Page() { - const controls = useControls(rangeSliderControls) + const controls = useControls(sliderControls) const [state, send] = useMachine( slider.machine({ @@ -42,8 +42,8 @@ export default function Page() {
{api.value.map((_, index) => ( -