Skip to content

Commit

Permalink
refactor: merge slider to range slider (#927)
Browse files Browse the repository at this point in the history
Co-authored-by: Segun Adebayo <[email protected]>
  • Loading branch information
Omikorin and segunadebayo authored Oct 16, 2023
1 parent e9030ba commit 4406e66
Show file tree
Hide file tree
Showing 65 changed files with 934 additions and 2,917 deletions.
10 changes: 10 additions & 0 deletions .changeset/slimy-ads-build.md
Original file line number Diff line number Diff line change
@@ -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 })`
55 changes: 33 additions & 22 deletions .xstate/slider.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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: {
Expand All @@ -115,6 +125,7 @@ const fetchMachine = createMachine({
})
},
guards: {
"hasIndex": ctx => ctx["hasIndex"],
"isHorizontal": ctx => ctx["isHorizontal"],
"isVertical": ctx => ctx["isVertical"]
}
Expand Down
8 changes: 4 additions & 4 deletions e2e/slider.e2e.ts
Original file line number Diff line number Diff line change
@@ -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 }) => {
Expand Down
2 changes: 1 addition & 1 deletion examples/next-ts/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions examples/next-ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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:*",
Expand Down Expand Up @@ -92,4 +91,4 @@
"typescript": "5.2.2"
},
"license": "MIT"
}
}
10 changes: 5 additions & 5 deletions examples/next-ts/pages/range-slider.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
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"
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({
Expand Down Expand Up @@ -42,8 +42,8 @@ export default function Page() {
<div {...api.rangeProps} />
</div>
{api.value.map((_, index) => (
<div key={index} {...api.getThumbProps(index)}>
<input {...api.getHiddenInputProps(index)} />
<div key={index} {...api.getThumbProps({ index })}>
<input {...api.getHiddenInputProps({ index })} />
</div>
))}
</div>
Expand Down
9 changes: 6 additions & 3 deletions examples/next-ts/pages/slider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export default function Page() {
slider.machine({
id: useId(),
name: "quantity",
value: [0],
}),
{
context: controls.context,
Expand Down Expand Up @@ -46,9 +47,11 @@ export default function Page() {
<div data-testid="track" {...api.trackProps}>
<div {...api.rangeProps} />
</div>
<div data-testid="thumb" {...api.thumbProps}>
<input {...api.hiddenInputProps} />
</div>
{api.value.map((_, index) => (
<div key={index} {...api.getThumbProps({ index })}>
<input {...api.getHiddenInputProps({ index })} />
</div>
))}
</div>
<div {...api.markerGroupProps}>
<span {...api.getMarkerProps({ value: 10 })}>*</span>
Expand Down
5 changes: 2 additions & 3 deletions examples/nuxt-ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,12 @@
"@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/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:*",
Expand All @@ -83,4 +82,4 @@
"@types/node": "^20.8.4",
"nuxt": "^3.7.3"
}
}
}
10 changes: 5 additions & 5 deletions examples/nuxt-ts/pages/range-slider.vue
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<script setup lang="ts">
import * as slider from "@zag-js/range-slider"
import { rangeSliderControls } from "@zag-js/shared"
import * as slider from "@zag-js/slider"
import { sliderControls } from "@zag-js/shared"
import { normalizeProps, useMachine } from "@zag-js/vue"
import serialize from "form-serialize"
const controls = useControls(rangeSliderControls)
const controls = useControls(sliderControls)
const [state, send] = useMachine(
slider.machine({
Expand Down Expand Up @@ -38,8 +38,8 @@ const api = computed(() => slider.connect(state.value, send, normalizeProps))
<div v-bind="api.trackProps">
<div v-bind="api.rangeProps" />
</div>
<div v-for="(_, index) in api.value" :key="index" v-bind="api.getThumbProps(index)">
<input v-bind="api.getHiddenInputProps(index)" />
<div v-for="(_, index) in api.value" :key="index" v-bind="api.getThumbProps({ index })">
<input v-bind="api.getHiddenInputProps({ index })" />
</div>
</div>
</div>
Expand Down
7 changes: 4 additions & 3 deletions examples/nuxt-ts/pages/slider.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const [state, send] = useMachine(
slider.machine({
id: "1",
name: "quantity",
value: [0],
}),
{ context: controls.context },
)
Expand All @@ -31,15 +32,15 @@ const api = computed(() => slider.connect(state.value, send, normalizeProps))
<div v-bind="api.rootProps">
<div>
<label data-testid="label" v-bind="api.labelProps"> Slider Label </label>
<output data-testid="output" v-bind="api.outputProps"> {api.value} </output>
<output data-testid="output" v-bind="api.outputProps"> {{ api.value.at(0) }} </output>
</div>
<div class="control-area">
<div v-bind="api.controlProps">
<div data-testid="track" v-bind="api.trackProps">
<div v-bind="api.rangeProps" />
</div>
<div data-testid="thumb" v-bind="api.thumbProps">
<input v-bind="api.hiddenInputProps" />
<div v-for="(_, index) in api.value" :key="index" v-bind="api.getThumbProps({ index })">
<input v-bind="api.getHiddenInputProps({ index })" />
</div>
</div>
<div v-bind="api.markerGroupProps">
Expand Down
5 changes: 2 additions & 3 deletions examples/shadow-dom/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,12 @@
"@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/slider": "workspace:*",
"@zag-js/splitter": "workspace:*",
"@zag-js/store": "workspace:*",
"@zag-js/switch": "workspace:*",
Expand All @@ -83,4 +82,4 @@
"typescript": "^5.2.2",
"vite": "^4.4.9"
}
}
}
2 changes: 1 addition & 1 deletion examples/solid-ts/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions examples/solid-ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,12 @@
"@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/rect-utils": "workspace:*",
"@zag-js/remove-scroll": "workspace:*",
"@zag-js/select": "workspace:*",
"@zag-js/shared": "workspace:*",
"@zag-js/slider": "workspace:*",
"@zag-js/solid": "workspace:*",
"@zag-js/splitter": "workspace:*",
"@zag-js/store": "workspace:*",
Expand All @@ -87,4 +86,4 @@
"form-serialize": "0.7.2",
"solid-js": "1.7.12"
}
}
}
10 changes: 5 additions & 5 deletions examples/solid-ts/src/pages/range-slider.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import * as slider from "@zag-js/range-slider"
import * as slider from "@zag-js/slider"
import { normalizeProps, useMachine } from "@zag-js/solid"
import serialize from "form-serialize"
import { createMemo, For, createUniqueId } from "solid-js"
import { rangeSliderControls } from "@zag-js/shared"
import { sliderControls } from "@zag-js/shared"
import { StateVisualizer } from "../components/state-visualizer"
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({
Expand Down Expand Up @@ -43,8 +43,8 @@ export default function Page() {
</div>
<For each={api().value}>
{(_, index) => (
<div class="slider__thumb" {...api().getThumbProps(index())}>
<input {...api().getHiddenInputProps(index())} />
<div {...api().getThumbProps({ index: index() })}>
<input {...api().getHiddenInputProps({ index: index() })} />
</div>
)}
</For>
Expand Down
Loading

4 comments on commit 4406e66

@vercel
Copy link

@vercel vercel bot commented on 4406e66 Oct 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 4406e66 Oct 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

zag-vue – ./examples/vue-ts

zag-vue-chakra-ui.vercel.app
zag-vue.vercel.app
zag-vue-git-main-chakra-ui.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 4406e66 Oct 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

zag-nextjs – ./examples/next-ts

zag-nextjs-chakra-ui.vercel.app
zag-nextjs-git-main-chakra-ui.vercel.app
zag-two.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 4406e66 Oct 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

zag-solid – ./examples/solid-ts

zag-solid-git-main-chakra-ui.vercel.app
zag-solid.vercel.app
zag-solid-chakra-ui.vercel.app

Please sign in to comment.