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

Sarka/a11y fixes tooltip #4515

Merged
merged 3 commits into from
Nov 28, 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
---
DSil marked this conversation as resolved.
Show resolved Hide resolved
title: Accessibility
redirect_from:
- /components/tooltip/accessibility/
---

## Accessibility

### Tooltip

The Tooltip component has been designed with accessibility in mind. It can be used with keyboard navigation and includes properties that enhance the experience for users of assistive technologies.

While the `aria-label` and `aria-labelledby` attributes are not needed for the Tooltip component itself, it is important to ensure that the components passed as children can be perceived by assistive technologies. This ensures that the Tooltip component is accessible.

#### Example 1

The content of children component is text, so it is read by screen reader.

```jsx
<Tooltip
content={
<div>
<p>Lorem ipsum dolor sit amet.</p>
</div>
}
>
<Text>Learn more.</Text>
</Tooltip>
```

The screen reader will announce: "Learn more. Lorem ipsum dolor sit amet.".

#### Example 2

The children element is an icon component. To achieve the accessibility of the Tooltip, adding an `aria-label` to the icon component is necessary.

```jsx
<Tooltip
content={
<div>
<p>Lorem ipsum dolor sit amet.</p>
</div>
}
>
<Airplane ariaLabel="More information" />
</Tooltip>
```

Once the icon is focused by the screen reader, it will announce: "More information", followed by announcing the content of the tooltip: "Lorem ipsum dolor sit amet.".
6 changes: 3 additions & 3 deletions packages/orbit-components/src/Tooltip/Tooltip.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { action } from "@storybook/addon-actions";
import type { Meta, StoryObj } from "@storybook/react";

import { PLACEMENTS, AUTO_PLACEMENTS } from "../common/placements";
import * as Icons from "../icons";
import Airplane from "../icons/Airplane";
import { SIZE_OPTIONS } from "./consts";
import Stack from "../Stack";
import Alert from "../Alert";
Expand Down Expand Up @@ -47,7 +47,7 @@ export const Default: Story = {
render: args => (
<Stack justify="center">
<Tooltip {...args}>
<Icons.Airplane />
<Airplane ariaLabel="More information" />
</Tooltip>
</Stack>
),
Expand Down Expand Up @@ -149,7 +149,7 @@ export const WithImageInside: Story = {
</Stack>
}
>
<Icons.Airplane />
<Airplane ariaLabel="More information" />
</Tooltip>
),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as React from "react";
import { action } from "@storybook/addon-actions";
import type { Meta, StoryObj } from "@storybook/react";

import * as Icons from "../../icons";
import Airplane from "../../icons/Airplane";
import Stack from "../../Stack";
import Alert from "../../Alert";
import Text from "../../Text";
Expand Down Expand Up @@ -43,7 +43,7 @@ export const Default: Story = {
render: args => (
<Stack justify="center">
<TooltipPrimitive {...args}>
<Icons.Airplane />
<Airplane ariaLabel="More information" />
</TooltipPrimitive>
</Stack>
),
Expand Down Expand Up @@ -147,7 +147,7 @@ export const WithImageInside: Story = {
</Stack>
}
>
<Icons.Airplane />
<Airplane ariaLabel="More information" />
</TooltipPrimitive>
),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,54 +105,55 @@ const TooltipContent = ({
[onClose, onCloseMobile, elements.floating],
);

const handleCombinedClick = (ev: React.MouseEvent<HTMLDivElement, MouseEvent>) => {
handleInnerClick(ev);
onClick(ev);
};

return (
// Disabling because the onClick exists just to stop propagation of events
// Disabling because the onClick exists to close tooltip when clicking in interactive elements, which should not happen with keyboard.
// eslint-disable-next-line jsx-a11y/click-events-have-key-events, jsx-a11y/no-noninteractive-element-interactions
<div className="w-full" role="tooltip" id={tooltipId} data-test={dataTest} onClick={onClick}>
{/* Disabling because the onClick exists to close tooltip when clicking in interactibe elements, which should not happen with keyboard */}
{/* eslint-disable-next-line jsx-a11y/click-events-have-key-events, jsx-a11y/no-noninteractive-element-interactions */}
<div
DSil marked this conversation as resolved.
Show resolved Hide resolved
className={cx(
"rounded-100 px-300 shadow-level3 z-[10012] box-border block w-auto overflow-visible",
"duration-fast transition-[visibility,_opacity] ease-in-out",
"[&_img]:max-w-full]",
contentHeight <= Math.floor(parseFloat(theme.orbit.lineHeightNormal)) ? "py-200" : "py-300",
shown ? "visible opacity-100" : "invisible opacity-0",
size === "small" && "max-w-[240px]",
size === "medium" && "max-w-[380px]",
error && "bg-red-normal",
!error && help && "bg-blue-normal",
!error && !help && "bg-ink-dark",
)}
ref={refs.setFloating}
role="tooltip"
id={tooltipId}
aria-hidden={!shown}
onMouseEnter={onEnter}
onMouseLeave={onClose}
onClick={handleCombinedClick}
style={floatingStyles}
data-test={dataTest}
>
<div
className={cx(
"rounded-100 px-300 shadow-level3 z-[10012] box-border block w-auto overflow-visible",
"duration-fast transition-[visibility,_opacity] ease-in-out",
"[&_img]:max-w-full]",
contentHeight <= Math.floor(parseFloat(theme.orbit.lineHeightNormal))
? "py-200"
: "py-300",
shown ? "visible opacity-100" : "invisible opacity-0",
size === "small" && "max-w-[240px]",
size === "medium" && "max-w-[380px]",
error && "bg-red-normal",
!error && help && "bg-blue-normal",
!error && !help && "bg-ink-dark",
"font-base text-small text-white-normal mb-0 font-medium leading-normal",
"[&_.orbit-text]:text-small [&_.orbit-text]:text-white-normal [&_.orbit-text]:font-medium",
"[&_.orbit-list-item]:text-small [&_.orbit-list-item]:text-white-normal [&_.orbit-list-item]:font-medium",
"[&_.orbit-text-link]:text-white-normal",
)}
ref={refs.setFloating}
role="tooltip"
aria-hidden={!shown}
onMouseEnter={onEnter}
onMouseLeave={onClose}
onClick={handleInnerClick}
style={floatingStyles}
ref={content}
>
<div
className={cx(
"font-base text-small text-white-normal mb-0 font-medium leading-normal",
"[&_.orbit-text]:text-small [&_.orbit-text]:text-white-normal [&_.orbit-text]:font-medium",
"[&_.orbit-list-item]:text-small [&_.orbit-list-item]:text-white-normal [&_.orbit-list-item]:font-medium",
"[&_.orbit-text-link]:text-white-normal",
)}
ref={content}
>
{children}
</div>
<FloatingArrow
ref={arrowRef}
context={context}
height={ARROW_SIZE}
width={ARROW_SIZE * 2}
fill={arrowColor}
/>
{children}
</div>
<FloatingArrow
ref={arrowRef}
context={context}
height={ARROW_SIZE}
width={ARROW_SIZE * 2}
fill={arrowColor}
/>
</div>
);
};
Expand Down