Skip to content

Commit

Permalink
feat: 🎸 onScrubStop provide value where scrub stopped
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Apr 24, 2020
1 parent 113aa58 commit 138b43c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
7 changes: 5 additions & 2 deletions src/useSlider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export interface State {
export interface Options {
onScrub: (value: number) => void;
onScrubStart: () => void;
onScrubStop: () => void;
onScrubStop: (value: number) => void;
reverse: boolean;
styles: boolean | CSSProperties;
vertical?: boolean;
Expand All @@ -24,12 +24,15 @@ const noop = () => {};
const useSlider = (ref: RefObject<HTMLElement>, options: Partial<Options> = {}): State => {
const isMounted = useMountedState();
const isSliding = useRef(false);
const valueRef = useRef(0);
const frame = useRef(0);
const [state, setState] = useSetState<State>({
isSliding: false,
value: 0,
});

valueRef.current = state.value;

useEffect(() => {
if (isClient) {
const styles = options.styles === undefined ? true : options.styles;
Expand All @@ -50,7 +53,7 @@ const useSlider = (ref: RefObject<HTMLElement>, options: Partial<Options> = {}):

const stopScrubbing = () => {
if (isSliding.current && isMounted()) {
(options.onScrubStop || noop)();
(options.onScrubStop || noop)(valueRef.current);
isSliding.current = false;
setState({ isSliding: false });
unbindEvents();
Expand Down
6 changes: 5 additions & 1 deletion stories/useSlider.story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import ShowDocs from './util/ShowDocs';

const Demo = () => {
const ref = React.useRef(null);
const state = useSlider(ref);
const state = useSlider(ref, {
onScrubStop: (value) => {
console.log('onScrubStop', value);
},
});

return (
<div>
Expand Down

1 comment on commit 138b43c

@trevorblades
Copy link

Choose a reason for hiding this comment

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

Awesome! Was just implementing a slider and thought "I wish I had the value in onScrubStop" and... πŸŽ‰

Please sign in to comment.