Skip to content

Commit

Permalink
Components: add hoverShow prop to IconTooltip
Browse files Browse the repository at this point in the history
  • Loading branch information
dkmyta authored and nateweller committed Oct 28, 2024
1 parent be9762c commit 4a87307
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@ const IconTooltip: React.FC< IconTooltipProps > = ( {
children,
popoverAnchorStyle = 'icon',
forceShow = false,
hoverShow = false,
wide = false,
inline = true,
shift = false,
} ) => {
const POPOVER_HELPER_WIDTH = 124;
const [ isVisible, setIsVisible ] = useState( false );
const [ hoverTimeout, setHoverTimeout ] = useState( null );
const hideTooltip = useCallback( () => setIsVisible( false ), [ setIsVisible ] );
const toggleTooltip = useCallback(
e => {
Expand Down Expand Up @@ -78,8 +80,33 @@ const IconTooltip: React.FC< IconTooltipProps > = ( {

const isForcedToShow = isAnchorWrapper && forceShow;

const handleMouseEnter = useCallback( () => {
if ( hoverShow ) {
if ( hoverTimeout ) {
clearTimeout( hoverTimeout );
setHoverTimeout( null );
}
setIsVisible( true );
}
}, [ hoverShow, hoverTimeout ] );

const handleMouseLeave = useCallback( () => {
if ( hoverShow ) {
const id = setTimeout( () => {
setIsVisible( false );
setHoverTimeout( null );
}, 100 );
setHoverTimeout( id );
}
}, [ hoverShow ] );

return (
<div className={ wrapperClassNames } data-testid="icon-tooltip_wrapper">
<div
className={ wrapperClassNames }
data-testid="icon-tooltip_wrapper"
onMouseEnter={ handleMouseEnter }
onMouseLeave={ handleMouseLeave }
>
{ ! isAnchorWrapper && (
<Button variant="link" onMouseDown={ toggleTooltip }>
<Gridicon className={ iconClassName } icon={ iconCode } size={ iconSize } />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ export default {
wide: {
control: { type: 'boolean' },
},
hoverShow: {
control: { type: 'boolean' },
},
},
};

Expand Down Expand Up @@ -106,3 +109,11 @@ Wide.args = {
wide: true,
placement: 'bottom-start',
};

export const HoverShow = Template.bind( {} );
HoverShow.args = {
title: 'This is title!',
children: <div>This is a hover tooltip!</div>,
placement: 'bottom-start',
hoverShow: true,
};
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ export type IconTooltipProps = {
*/
forceShow?: boolean;

/**
* Enables the Popover to show on hover.
*/
hoverShow?: boolean;

/**
* Uses a wider content area when enabled.
*/
Expand Down

0 comments on commit 4a87307

Please sign in to comment.