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

Tooltip Component: Add custom class name support #63157

Merged
merged 17 commits into from
Jul 8, 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
1 change: 1 addition & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Enhancements

- `Tooltip`: Add support for `className` prop ([#63157](https://github.com/WordPress/gutenberg/pull/63157)).
- `Toolbar`: Add support for `vertical` orientation ([#60123](https://github.com/WordPress/gutenberg/pull/60123)).

### Bug Fixes
Expand Down
4 changes: 3 additions & 1 deletion packages/components/src/tooltip/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* External dependencies
*/
import * as Ariakit from '@ariakit/react';
import clsx from 'clsx';

/**
* WordPress dependencies
Expand Down Expand Up @@ -44,6 +45,7 @@ function UnforwardedTooltip(
) {
const {
children,
className,
delay = TOOLTIP_DELAY,
hideOnClick = true,
placement,
Expand Down Expand Up @@ -112,7 +114,7 @@ function UnforwardedTooltip(
{ isOnlyChild && ( text || shortcut ) && (
<Ariakit.Tooltip
{ ...restProps }
className="components-tooltip"
className={ clsx( 'components-tooltip', className ) }
unmountOnHide
gutter={ 4 }
id={ describedById }
Expand Down
18 changes: 18 additions & 0 deletions packages/components/src/tooltip/test/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,24 @@ describe( 'Tooltip', () => {
screen.getByRole( 'button', { name: 'Anchor' } )
).not.toHaveAttribute( 'data-foo' );
} );

it( 'should add default and custom class names to the tooltip', async () => {
render( <Tooltip { ...props } className="foo" /> );

// Hover over the anchor, tooltip should show
await hover(
screen.getByRole( 'button', { name: 'Tooltip anchor' } )
);

// Check default and custom classnames
await waitFor( () =>
expect(
screen.getByRole( 'tooltip', {
name: 'tooltip text',
} )
).toHaveClass( 'components-tooltip', 'foo' )
);
} );
} );

describe( 'keyboard focus', () => {
Expand Down
4 changes: 4 additions & 0 deletions packages/components/src/tooltip/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ export type TooltipProps = {
* **Note**: Accepts only one child element.
*/
children: React.ReactElement;
/**
* Custom class name for the tooltip.
*/
className?: string;
/**
* Option to hide the tooltip when the anchor is clicked.
*
Expand Down
Loading