Skip to content

Commit

Permalink
Button: always render the Tooltip component even when a tooltip sho…
Browse files Browse the repository at this point in the history
…uld not be shown
  • Loading branch information
ciampo committed Nov 23, 2023
1 parent a67469e commit 681af93
Showing 1 changed file with 31 additions and 32 deletions.
63 changes: 31 additions & 32 deletions packages/components/src/button/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import type {
* WordPress dependencies
*/
import deprecated from '@wordpress/deprecated';
import { forwardRef } from '@wordpress/element';
import { forwardRef, useMemo } from '@wordpress/element';
import { useInstanceId } from '@wordpress/compose';

/**
Expand Down Expand Up @@ -249,40 +249,39 @@ export function UnforwardedButton(
</button>
);

// Convert legacy `position` values to be used with the new `placement` prop
let computedPlacement;
// if `tooltipPosition` is defined, compute value to `placement`
if ( tooltipPosition !== undefined ) {
computedPlacement = positionToPlacement( tooltipPosition );
}

if ( ! shouldShowTooltip ) {
return (
<>
{ element }
{ describedBy && (
<VisuallyHidden>
<span id={ descriptionId }>{ describedBy }</span>
</VisuallyHidden>
) }
</>
);
}
// In order to avoid some React reconciliation issues, we are always rendering
// the `Tooltip` component even when `shouldShowTooltip` is `false`.
// In order to make sure that the tooltip doesn't show when it shouldn't,
// we don't pass the props to the `Tooltip` componet.
const tooltipProps = useMemo(
() =>
( shouldShowTooltip
? {
text:
( children as string | ReactElement[] )?.length &&
describedBy
? describedBy
: label,
shortcut,
placement:
tooltipPosition &&
// Convert legacy `position` values to be used with the new `placement` prop
positionToPlacement( tooltipPosition ),
}
: {} ) as Partial< React.ComponentProps< typeof Tooltip > >,
[
shouldShowTooltip,
children,
describedBy,
label,
shortcut,
tooltipPosition,
]
);

return (
<>
<Tooltip
text={
( children as string | ReactElement[] )?.length &&
describedBy
? describedBy
: label
}
shortcut={ shortcut }
placement={ computedPlacement }
>
{ element }
</Tooltip>
<Tooltip { ...tooltipProps }>{ element }</Tooltip>
{ describedBy && (
<VisuallyHidden>
<span id={ descriptionId }>{ describedBy }</span>
Expand Down

0 comments on commit 681af93

Please sign in to comment.