Skip to content

Commit

Permalink
breaking: Slider type for number or number[] value (#1032)
Browse files Browse the repository at this point in the history
  • Loading branch information
huntabyte authored Dec 31, 2024
1 parent abbdae0 commit 91913f4
Show file tree
Hide file tree
Showing 18 changed files with 648 additions and 211 deletions.
5 changes: 5 additions & 0 deletions .changeset/dry-spiders-learn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"bits-ui": patch
---

breaking: `Slider.Root` now requires a `type` prop to specify whether the slider should be a `"single"` or `"multiple"` slider, which determines whether the value and change function arguments should be of type `number` or `number[]`
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class AccordionBaseState {
this.#ref = props.ref;

useRefById({
id: props.id,
id: this.#id,
ref: this.#ref,
});

Expand Down
15 changes: 12 additions & 3 deletions packages/bits-ui/src/lib/bits/slider/components/slider.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts">
import { box, mergeProps } from "svelte-toolbelt";
import { box, mergeProps, type WritableBox } from "svelte-toolbelt";
import type { SliderRootProps } from "../types.js";
import { useSliderRoot } from "../slider.svelte.js";
import { useId } from "$lib/internal/use-id.js";
Expand All @@ -10,7 +10,8 @@
child,
id = useId(),
ref = $bindable(null),
value = $bindable([]),
value = $bindable(),
type,
onValueChange = noop,
onValueCommit = noop,
disabled = false,
Expand All @@ -24,6 +25,10 @@
...restProps
}: SliderRootProps = $props();
if (value === undefined) {
value = type === "single" ? 0 : [];
}
const rootState = useSliderRoot({
id: box.with(() => id),
ref: box.with(
Expand All @@ -34,13 +39,16 @@
() => value,
(v) => {
if (controlledValue) {
// @ts-expect-error - we know
onValueChange(v);
} else {
value = v;
// @ts-expect-error - we know
onValueChange(v);
}
}
),
) as WritableBox<number> | WritableBox<number[]>,
// @ts-expect-error - we know
onValueCommit: box.with(() => onValueCommit),
disabled: box.with(() => disabled),
min: box.with(() => min),
Expand All @@ -49,6 +57,7 @@
dir: box.with(() => dir),
autoSort: box.with(() => autoSort),
orientation: box.with(() => orientation),
type,
});
const mergedProps = $derived(mergeProps(restProps, rootState.props));
Expand Down
Loading

0 comments on commit 91913f4

Please sign in to comment.