Skip to content

Commit

Permalink
feat(tooltip): use standard visually-hidden class in closeMode=hide
Browse files Browse the repository at this point in the history
  • Loading branch information
gcornut committed Oct 9, 2024
1 parent f1e7683 commit f166e93
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 13 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- `ImageLightbox`: fix closing transition triggering multiple times.

### Changed

- `Tooltip`: use the standard class `visually-hidden` when closed and with `closeMode="hide"`.

## [3.9.2][] - 2024-10-04

### Fixed
Expand Down
4 changes: 0 additions & 4 deletions packages/lumx-core/src/scss/components/tooltip/_index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@
border-radius: var(--lumx-border-radius);
will-change: transform;

&--is-hidden {
visibility: hidden;
}

&__arrow {
position: absolute;
width: 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import React from 'react';
import { render, screen, waitFor } from '@testing-library/react';
import { getByClassName, queryByClassName } from '@lumx/react/testing/utils/queries';
import { commonTestsSuiteRTL } from '@lumx/react/testing/utils';

import userEvent from '@testing-library/user-event';
import { VISUALLY_HIDDEN } from '@lumx/react/constants';

import { DatePickerControlled, DatePickerControlledProps } from './DatePickerControlled';
import { CLASSNAME } from './constants';

Expand Down Expand Up @@ -39,7 +40,7 @@ const queries = {
screen.getByRole('spinbutton', {
name: /année/i,
}),
getAccessibleMonthYear: (container: HTMLElement) => getByClassName(container, 'visually-hidden'),
getAccessibleMonthYear: (container: HTMLElement) => getByClassName(container, VISUALLY_HIDDEN),
};

describe(`<${DatePickerControlled.displayName}>`, () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { getYearDisplayName } from '@lumx/react/utils/date/getYearDisplayName';
import { onEnterPressed } from '@lumx/react/utils/event';
import { addMonthResetDay } from '@lumx/react/utils/date/addMonthResetDay';
import { formatDayNumber } from '@lumx/react/utils/date/formatDayNumber';
import { VISUALLY_HIDDEN } from '@lumx/react/constants';
import { CLASSNAME } from './constants';

/**
Expand Down Expand Up @@ -147,7 +148,7 @@ export const DatePickerControlled: Comp<DatePickerControlledProps, HTMLDivElemen
}
label={
<>
<span aria-live={labelAriaLive} className={onMonthChange ? 'visually-hidden' : ''} dir="auto">
<span aria-live={labelAriaLive} className={onMonthChange ? VISUALLY_HIDDEN : ''} dir="auto">
{monthYear}
</span>
{onMonthChange && (
Expand Down Expand Up @@ -222,7 +223,7 @@ export const DatePickerControlled: Comp<DatePickerControlledProps, HTMLDivElemen
onClick={() => onChange(date)}
>
<span aria-hidden>{formatDayNumber(locale, date)}</span>
<span className="visually-hidden">
<span className={VISUALLY_HIDDEN}>
{date.toLocaleDateString(locale, {
day: 'numeric',
month: 'long',
Expand Down
7 changes: 4 additions & 3 deletions packages/lumx-react/src/components/tooltip/Tooltip.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import { screen, render } from '@testing-library/react';
import { queryAllByTagName, queryByClassName } from '@lumx/react/testing/utils/queries';
import { commonTestsSuiteRTL } from '@lumx/react/testing/utils';
import userEvent from '@testing-library/user-event';

import { isFocusVisible } from '@lumx/react/utils/isFocusVisible';
import { VISUALLY_HIDDEN } from '@lumx/react/constants';

import { Tooltip, TooltipProps } from './Tooltip';

const CLASSNAME = Tooltip.className as string;
Expand Down Expand Up @@ -142,11 +143,11 @@ describe(`<${Tooltip.displayName}>`, () => {
forceOpen: false,
});
expect(tooltip).toBeInTheDocument();
expect(tooltip).toHaveClass('lumx-tooltip--is-hidden');
expect(tooltip).toHaveClass(VISUALLY_HIDDEN);

const anchor = screen.getByRole('button', { name: 'Anchor' });
await userEvent.hover(anchor);
expect(tooltip).not.toHaveClass('lumx-tooltip--is-hidden');
expect(tooltip).not.toHaveClass(VISUALLY_HIDDEN);
});
});

Expand Down
5 changes: 3 additions & 2 deletions packages/lumx-react/src/components/tooltip/Tooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { createPortal } from 'react-dom';

import classNames from 'classnames';

import { DOCUMENT } from '@lumx/react/constants';
import { DOCUMENT, VISUALLY_HIDDEN } from '@lumx/react/constants';
import { Comp, GenericProps, HasCloseMode } from '@lumx/react/utils/type';
import { getRootClassName, handleBasicClasses } from '@lumx/react/utils/className';
import { useMergeRefs } from '@lumx/react/utils/mergeRefs';
Expand Down Expand Up @@ -106,6 +106,7 @@ export const Tooltip: Comp<TooltipProps, HTMLDivElement> = forwardRef((props, re
const { isOpen: isActivated, onPopperMount } = useTooltipOpen(delay, anchorElement);
const isOpen = (isActivated || forceOpen) && !!label;
const isMounted = !!label && (isOpen || closeMode === 'hide');
const isHidden = !isOpen && closeMode === 'hide';
const wrappedChildren = useInjectTooltipRef({
children,
setAnchorElement,
Expand Down Expand Up @@ -139,8 +140,8 @@ export const Tooltip: Comp<TooltipProps, HTMLDivElement> = forwardRef((props, re
handleBasicClasses({
prefix: CLASSNAME,
position,
hidden: !isOpen && closeMode === 'hide',
}),
isHidden && VISUALLY_HIDDEN,
)}
style={{ ...styles.popper, zIndex }}
{...attributes.popper}
Expand Down
5 changes: 5 additions & 0 deletions packages/lumx-react/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,8 @@ export const DOCUMENT = typeof document !== 'undefined' ? document : undefined;
* Check if we are running in a true browser
*/
export const IS_BROWSER = typeof navigator !== 'undefined' && !navigator.userAgent.includes('jsdom');

/**
* Visually hidden a11y utility class name
*/
export const VISUALLY_HIDDEN = 'visually-hidden';

0 comments on commit f166e93

Please sign in to comment.