Skip to content
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

Components: Add optional hover support to the IconTooltip component #39916

Merged
merged 2 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: added

IconTooltip: add support for showing tooltip on hover.
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
Loading