-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
RangeControl: Improve initial hover interaction with Tooltip (#20219)
* RangeControl: Improve initial hover interaction with Tooltip This update improves the initial hover/mouseenter experience with the RangeControl's Tooltip. This is achieved be debouncing the interaction. By debouncing, the Tooltip rendering is less jarring when there are multiple `RangeControl` components next to each other. A Storybook story was added to test and simulate this experience. Lastly, the utility functions/hooks from `RangeControl` was abstracted to a dedicated `utils.js` file under the component directory. * Simplifies setValue to be a plain function vs using useCallback * Remove local ref from useCallback hook * Remove guard for clearTimeout * Add clearTimeout callback on unmount * Improve Tooltip rendering for current RangeControl changes... ...with other RangeControl hover interactions. * Removed whitespace + adjusted setState naming
- Loading branch information
Jon Quach
authored
Feb 20, 2020
1 parent
5d8701c
commit 56c043c
Showing
4 changed files
with
657 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/** | ||
* 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 | ||
*/ | ||
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 setClampValue = ( nextValue ) => { | ||
setValue( floatClamp( nextValue, min, max ) ); | ||
}; | ||
|
||
useEffect( () => { | ||
if ( valueRef.current !== valueProp ) { | ||
setClampValue( valueProp ); | ||
valueRef.current = valueProp; | ||
} | ||
}, [ valueProp, setClampValue ] ); | ||
|
||
return [ value, setClampValue ]; | ||
} | ||
|
||
/** | ||
* 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 ); | ||
}, | ||
[ 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, | ||
}; | ||
} |
Oops, something went wrong.