-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RangeControl: Improve initial hover interaction with Tooltip #20219
Changes from 6 commits
c898977
3454646
1e6528a
ad29531
733c5d4
c8e6cf6
e6752a4
ddc3507
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { clamp, noop } from 'lodash'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useCallback, useRef, useEffect, useState } from '@wordpress/element'; | ||
|
||
/** | ||
* A float supported clamp function for a specific value. | ||
* | ||
* @param {number} value The value to clamp | ||
* @param {number} min The minimum value | ||
* @param {number} max The maxinum value | ||
* @return {number} A (float) number | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Two things:
|
||
*/ | ||
function floatClamp( value, min, max ) { | ||
return parseFloat( clamp( value, min, max ) ); | ||
} | ||
|
||
/** | ||
* Hook to store a clamped value, derived from props. | ||
*/ | ||
export function useControlledRangeValue( { min, max, value: valueProp = 0 } ) { | ||
const [ value, _setValue ] = useState( floatClamp( valueProp, min, max ) ); | ||
const valueRef = useRef( value ); | ||
|
||
const setValue = ( nextValue ) => { | ||
_setValue( floatClamp( nextValue, min, max ) ); | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Personally, I don't usually care for |
||
|
||
useEffect( () => { | ||
if ( valueRef.current !== valueProp ) { | ||
setValue( valueProp ); | ||
valueRef.current = valueProp; | ||
} | ||
}, [ valueProp, setValue ] ); | ||
|
||
return [ value, setValue ]; | ||
} | ||
|
||
/** | ||
* Hook to encapsulate the debouncing "hover" to better handle the showing | ||
* and hiding of the Tooltip. | ||
*/ | ||
export function useDebouncedHoverInteraction( { | ||
onShow = noop, | ||
onHide = noop, | ||
onMouseEnter = noop, | ||
onMouseLeave = noop, | ||
timeout = 300, | ||
} ) { | ||
const [ show, setShow ] = useState( false ); | ||
const timeoutRef = useRef(); | ||
|
||
const setDebouncedTimeout = useCallback( | ||
( callback ) => { | ||
window.clearTimeout( timeoutRef.current ); | ||
|
||
timeoutRef.current = setTimeout( callback, timeout ); | ||
aduth marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, | ||
[ timeout ] | ||
); | ||
|
||
const handleOnMouseEnter = useCallback( ( event ) => { | ||
onMouseEnter( event ); | ||
|
||
setDebouncedTimeout( () => { | ||
if ( ! show ) { | ||
setShow( true ); | ||
onShow(); | ||
} | ||
} ); | ||
}, [] ); | ||
|
||
const handleOnMouseLeave = useCallback( ( event ) => { | ||
onMouseLeave( event ); | ||
|
||
setDebouncedTimeout( () => { | ||
setShow( false ); | ||
onHide(); | ||
} ); | ||
}, [] ); | ||
|
||
useEffect( () => { | ||
return () => { | ||
window.clearTimeout( timeoutRef.current ); | ||
}; | ||
} ); | ||
|
||
return { | ||
onMouseEnter: handleOnMouseEnter, | ||
onMouseLeave: handleOnMouseLeave, | ||
}; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(We shouldn't want or need the extra lines amongst these imports, unless there's rhyme or reason to it)