mirror of
https://github.com/streamich/react-use.git
synced 2026-01-18 14:06:52 +00:00
feat: 🎸 onScrubStop provide value where scrub stopped
This commit is contained in:
parent
113aa58130
commit
138b43cd1a
@ -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;
|
||||
@ -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;
|
||||
@ -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();
|
||||
|
||||
@ -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>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user