Skip to content

Commit

Permalink
Tooltip: Add new hideOnClick prop (#54406)
Browse files Browse the repository at this point in the history
* Tooltip: Add new `hideOnClick` prop

* Update changelog
  • Loading branch information
brookewp authored Sep 13, 2023
1 parent 2e70fa1 commit db0ec5c
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- `Tooltip`: Replace the existing tooltip to simplify the implementation and improve accessibility while maintaining the same behaviors and API ([#48440](https://github.com/WordPress/gutenberg/pull/48440)).
- `Dropdown` and `DropdownMenu`: support controlled mode for the dropdown's open/closed state ([#54257](https://github.com/WordPress/gutenberg/pull/54257)).
- `BorderControl`: Apply proper metrics and simpler text ([#53998](https://github.com/WordPress/gutenberg/pull/53998)).
- `Tooltip`: Add new `hideOnClick` prop ([#54406](https://github.com/WordPress/gutenberg/pull/54406)).

### Bug Fix

Expand Down
7 changes: 7 additions & 0 deletions packages/components/src/tooltip/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ The amount of time in milliseconds to wait before showing the tooltip.
- Required: No
- Default: `700`

#### `hideOnClick`: `boolean`

Option to hide the tooltip when the anchor is clicked.

- Required: No
- Default: `true`

#### `position`: `string`

The direction in which the tooltip should open relative to its parent node. Specify y- and x-axis as a space-separated string. Supports `"top"`, `"middle"`, `"bottom"` y axis, and `"left"`, `"center"`, `"right"` x axis.
Expand Down
3 changes: 2 additions & 1 deletion packages/components/src/tooltip/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ function Tooltip( props: TooltipProps ) {
const {
children,
delay = TOOLTIP_DELAY,
hideOnClick = true,
position = 'bottom',
shortcut,
text,
Expand Down Expand Up @@ -55,7 +56,7 @@ function Tooltip( props: TooltipProps ) {
<>
<Ariakit.TooltipAnchor
onBlur={ tooltipStore.hide }
onClick={ tooltipStore.hide }
onClick={ hideOnClick ? tooltipStore.hide : undefined }
store={ tooltipStore }
render={ isOnlyChild ? children : undefined }
>
Expand Down
22 changes: 22 additions & 0 deletions packages/components/src/tooltip/test/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -310,4 +310,26 @@ describe( 'Tooltip', () => {
await screen.findByRole( 'button', { description: 'tooltip text' } )
).toBeInTheDocument();
} );

it( 'should not hide tooltip when the anchor is clicked if hideOnClick is false', async () => {
const user = userEvent.setup();

render( <Tooltip { ...props } hideOnClick={ false } /> );

const button = screen.getByRole( 'button', { name: /Button/i } );

await user.hover( button );

expect(
await screen.findByRole( 'tooltip', { name: /tooltip text/i } )
).toBeVisible();

await user.click( button );

expect(
screen.getByRole( 'tooltip', { name: /tooltip text/i } )
).toBeVisible();

await cleanupTooltip( user );
} );
} );
6 changes: 6 additions & 0 deletions packages/components/src/tooltip/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ export type TooltipProps = {
* **Note**: Accepts only one child element.
*/
children: React.ReactElement;
/**
* Option to hide the tooltip when the anchor is clicked.
*
* @default true
*/
hideOnClick?: boolean;
/**
* The amount of time in milliseconds to wait before showing the tooltip.
*
Expand Down

1 comment on commit db0ec5c

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in db0ec5c.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/6176619602
📝 Reported issues:

Please sign in to comment.