Skip to content

Commit

Permalink
fix: reactive slider
Browse files Browse the repository at this point in the history
Make it possible to change slider from outside the component.
  • Loading branch information
Steffen Sande committed Apr 28, 2023
1 parent fe5b060 commit ee33cea
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
6 changes: 3 additions & 3 deletions packages/slider/src/component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export function Slider({ min = 0, max = 100, ...rest }: SliderProps) {
}, [sliderLine]);

const [value, setValue] = useState(rest.value);
const [position, setPosition] = useState(0);
const [position, setPosition] = useState(rest.value);
const [dimensions, setDimensions] = useState({ left: 0, width: 0 });
const [sliderPressed, setSliderPressed] = useState(false);

Expand Down Expand Up @@ -97,10 +97,10 @@ export function Slider({ min = 0, max = 100, ...rest }: SliderProps) {
}, [position, rest.value, rest.step]);

useEffect(() => {
if (sliderPressed || position === rest.value) return;
if (sliderPressed || position === rest.value || value === rest.value) return;
setPosition(rest.value);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [sliderPressed]);
}, [sliderPressed, rest.value]);

return (
<div className={c.wrapper}>
Expand Down
3 changes: 2 additions & 1 deletion packages/slider/stories/Slider.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ export const Regular = () => {
<div>
<output>{value}</output>
<Slider
onChange={(value) => setValue(value)}
onChange={(val) => setValue(val)}
value={value}
min={1000}
max={10_000_000}
step={1000}
/>
<button onClick={() => setValue(2_500_000)}>Reset</button>
</div>
);
};
Expand Down

0 comments on commit ee33cea

Please sign in to comment.