Skip to content

Commit

Permalink
Ensure tooltip renders only if one child is passed
Browse files Browse the repository at this point in the history
  • Loading branch information
brookewp committed Mar 17, 2023
1 parent a1eb09a commit c133d33
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions packages/components/src/ui/ariakit-tooltip/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
*/
import { Tooltip, TooltipAnchor, useTooltipState } from 'ariakit/tooltip';

/**
* WordPress dependencies
*/
import { Children } from '@wordpress/element';

/**
* Internal dependencies
*/
Expand All @@ -23,8 +28,19 @@ function UnforwardedToolTip(
timeout: delay,
} );

const cx = useCx();
const isOnlyChild = () => {
if ( Children.toArray( children?.props?.children ).length === 1 ) {
return children;
}
if ( 'development' === process.env.NODE_ENV ) {
// eslint-disable-next-line no-console
return console.error(
'ToolTip should be called with only a single child element.'
);
}
};

const cx = useCx();
const ToolTipClassName = cx( styles.ToolTip );
const ToolTipAnchorClassName = cx( styles.ToolTipAnchor );
const ShortcutClassName = cx( styles.Shortcut );
Expand All @@ -38,7 +54,7 @@ function UnforwardedToolTip(
>
{ children }
</TooltipAnchor>
{ ( text || shortcut ) && (
{ ( text || shortcut ) && isOnlyChild() && (
<Tooltip className={ ToolTipClassName } state={ tooltipState }>
{ text }
{ shortcut && (
Expand Down

0 comments on commit c133d33

Please sign in to comment.