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 6 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
@@ -1,6 +1,7 @@
<!-- Learn how to maintain this file at https://github.com/WordPress/gutenberg/tree/HEAD/packages#maintaining-changelogs. -->

## Unreleased
- `Tooltip`: Add support for `className` prop ([#63157](https://github.com/WordPress/gutenberg/pull/63157)).
aliaghdam marked this conversation as resolved.
Show resolved Hide resolved

aliaghdam marked this conversation as resolved.
Show resolved Hide resolved
### Enhancements

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 @@ -50,6 +50,7 @@ function UnforwardedTooltip(
position,
shortcut,
text,
className,
aliaghdam marked this conversation as resolved.
Show resolved Hide resolved

...restProps
} = props;
Expand Down Expand Up @@ -112,7 +113,7 @@ function UnforwardedTooltip(
{ isOnlyChild && ( text || shortcut ) && (
<Ariakit.Tooltip
{ ...restProps }
className="components-tooltip"
className={ className === undefined ? "components-tooltip" : "components-tooltip " + className }
aliaghdam marked this conversation as resolved.
Show resolved Hide resolved
unmountOnHide
gutter={ 4 }
id={ describedById }
Expand Down
27 changes: 27 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,33 @@ describe( 'Tooltip', () => {
screen.getByRole( 'button', { name: 'Anchor' } )
).not.toHaveAttribute( 'data-foo' );
} );

it( 'should add custom class to the tooltip', async () => {
aliaghdam marked this conversation as resolved.
Show resolved Hide resolved
render( <Tooltip { ...props } className='foo' /> );
aliaghdam marked this conversation as resolved.
Show resolved Hide resolved

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

// Check core class
await waitFor( () =>
expect(
screen.getByRole( 'tooltip', {
name: 'tooltip text',
} )
).toHaveClass('components-tooltip')
);

// Check custom class
await waitFor( () =>
expect(
screen.getByRole( 'tooltip', {
name: 'tooltip text',
} )
).toHaveClass('foo')
);
aliaghdam marked this conversation as resolved.
Show resolved Hide resolved
} );
} );

describe( 'keyboard focus', () => {
Expand Down
6 changes: 6 additions & 0 deletions packages/components/src/tooltip/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ export type TooltipProps = {
* The text shown in the tooltip when anchor element is focused or hovered.
*/
text?: string;
/**
* The custom class name for the tooltip.
* The default class for tooltip is `components-tooltip` and always will be added.
* The custom class name will be appended to the default class.
aliaghdam marked this conversation as resolved.
Show resolved Hide resolved
*/
className?: string;
};

export type TooltipInternalContext = {
Expand Down
Loading