-
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 1 commit
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,96 @@ | ||
/** | ||
* 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 = useCallback( | ||
( nextValue ) => { | ||
_setValue( floatClamp( nextValue, min, max ) ); | ||
}, | ||
[ _setValue, min, max ] | ||
); | ||
aduth marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
useEffect( () => { | ||
if ( valueRef.current !== valueProp ) { | ||
setValue( valueProp ); | ||
valueRef.current = valueProp; | ||
} | ||
}, [ valueRef, valueProp, setValue ] ); | ||
aduth marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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 ) => { | ||
if ( timeoutRef.current ) { | ||
window.clearTimeout( timeoutRef.current ); | ||
} | ||
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. Minor, but: 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. Oh! Thanks for the heads up :). I'll make that update Edit: I've noticed the 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. I could imagine an argument on the basis of: Being explicit makes it clearer that there's an expectation that it might be unassigned. So even if not necessary, one might think it could help maintainability. For me, I think the benefits of simplifying it are slightly more (both in expressiveness, and smaller bundle size), so I'd lean more on the side of avoiding it. |
||
|
||
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(); | ||
} ); | ||
}, [] ); | ||
|
||
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)