Skip to content

Commit

Permalink
Tooltip Component: Add custom class name support (#63157)
Browse files Browse the repository at this point in the history
* feat: custom class name support for Tooltip component

* test: add custom class name tests for Tooltip component

* chore: changelog update

* chore: improve comment

* chore: fix string literals

Co-authored-by: Marin Atanasov <[email protected]>

* fix: use clx for classname

Co-authored-by: Marin Atanasov <[email protected]>

* fix: changelog

Co-authored-by: Marin Atanasov <[email protected]>

* fix: merge tests

Co-authored-by: Marin Atanasov <[email protected]>

* fix: import clx

* fix: props order

* chore: improve test name

* fix: changelog

* chore: props order

* chore: remove empty line

Co-authored-by: Marin Atanasov <[email protected]>

* chore: remove internal class name from prop description.

Co-authored-by: Marco Ciampini <[email protected]>

---------

Co-authored-by: aliaghdam <[email protected]>
Co-authored-by: tyxla <[email protected]>
Co-authored-by: ciampo <[email protected]>
  • Loading branch information
4 people committed Jul 8, 2024
1 parent 3661cd4 commit e9fd507
Show file tree
Hide file tree
Showing 4 changed files with 26 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 @@ -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)).
- `BaseControl`: forward ref on `VisualLabel` ([#63169](https://github.com/WordPress/gutenberg/pull/63169)).

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

1 comment on commit e9fd507

@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 e9fd507.
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/9839060115
📝 Reported issues:

Please sign in to comment.