Skip to content

Commit 195ea66

Browse files
authored
docs(tooltip): updated docs for accessbility (#5497)
* docs(tooltip): updating tooltip docs * docs(tooltip): updating tooltip docs * docs(tooltip): updated tooltip directive docs * docs(tooltip): renamed tooltip-directive readme * docs(tooltip): updated based on PR feedback * docs(tooltip): fixed typo
1 parent eca64ee commit 195ea66

File tree

2 files changed

+166
-91
lines changed

2 files changed

+166
-91
lines changed

packages/tooltip/README.md

Lines changed: 136 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,133 +1,193 @@
1-
## Description
1+
## Overview
22

3-
`sp-tooltip` allow users to get contextual help or information about specific components when hovering or focusing on them.
3+
`<sp-tooltip>` allow users to get contextual help or information about specific components when hovering or focusing on them.
44

55
### Usage
66

77
[![See it on NPM!](https://img.shields.io/npm/v/@spectrum-web-components/tooltip?style=for-the-badge)](https://www.npmjs.com/package/@spectrum-web-components/tooltip)
88
[![How big is this package in your project?](https://img.shields.io/bundlephobia/minzip/@spectrum-web-components/tooltip?style=for-the-badge)](https://bundlephobia.com/result?p=@spectrum-web-components/tooltip)
99
[![Try it on Stackblitz](https://img.shields.io/badge/Try%20it%20on-Stackblitz-blue?style=for-the-badge)](https://stackblitz.com/edit/vitejs-vite-cizgmpks)
1010

11-
```
11+
```bash
1212
yarn add @spectrum-web-components/tooltip
1313
```
1414

1515
Import the side effectful registration of `<sp-tooltip>` via:
1616

17-
```
17+
```javascript
1818
import '@spectrum-web-components/tooltip/sp-tooltip.js';
1919
```
2020

2121
When looking to leverage the `Tooltip` base class as a type and/or for extension purposes, do so via:
2222

23-
```
23+
```javascript
2424
import { Tooltip } from '@spectrum-web-components/tooltip';
2525
```
2626

27-
## Example
27+
### Anatomy
2828

29-
Tooltips can be top, bottom, left, or right.
30-
31-
```html
32-
<sp-tooltip open placement="top">Label</sp-tooltip>
33-
<br />
34-
<br />
35-
<sp-tooltip open placement="bottom">Label</sp-tooltip>
36-
<br />
37-
<br />
38-
<sp-tooltip open placement="left">Label</sp-tooltip>
39-
<br />
40-
<br />
41-
<sp-tooltip open placement="right">Label</sp-tooltip>
42-
```
29+
The tooltip consists of several key parts:
4330

44-
### Self-managed overlays
45-
46-
By default, Tooltip provides styling without behavior.
47-
You must combine it with an [Overlay Trigger](https://opensource.adobe.com/spectrum-web-components/components/overlay-trigger/#%22hover%22-content-only) in order to manage its overlay behavior.
48-
49-
You can use the tooltip as the descendant of an interactive element, such as [Action Button](https://opensource.adobe.com/spectrum-web-components/components/action-button/), by applying the `self-managed` attribute which automatically binds the Tooltip to its first interactive ancestor element's focus/hover interactions:
31+
- The message content in its default slot
32+
- An optional icon using `slot="icon"`
33+
- A tip element that points to the trigger
5034

5135
```html
52-
<sp-action-button>
53-
Trigger
54-
<sp-tooltip self-managed>Content</sp-tooltip>
55-
</sp-action-button>
36+
<sp-tooltip open>
37+
<sp-icon-info slot="icon"></sp-icon-info>
38+
Tooltip message
39+
</sp-tooltip>
5640
```
5741

58-
This is especially useful when inserting an intermediate `<overlay-trigger>` would interfere with
59-
parent/child relationships, such as between `<sp-action-group>` and `<sp-action-button>`.
60-
61-
Note that an interactive element is an element that can receive focus during tab navigation, such as `sp-action-button`, `sp-action-menu`, `sp-field-label` etc.
42+
### Options
6243

63-
Given that tooltip is not focusable by itself, it would not show up during tab navigation. Thus a tooltip would not be accessible if used with a non-interactive element, such as `<sp-icon-*>`.
44+
#### Placement
6445

65-
The correct way to make it accessible would be to wrap it under an interactive element, such as `sp-action-button`:
46+
Tooltips can be positioned relative to their trigger element using the `placement` attribute:
6647

6748
```html
68-
<sp-action-button size="s">
69-
<sp-icon-info slot="icon"></sp-icon-info>
70-
<sp-tooltip self-managed placement="right">
71-
Display something here
72-
</sp-tooltip>
73-
</sp-action-button>
49+
<sp-tooltip open placement="left">Left</sp-tooltip>
50+
<sp-tooltip open placement="bottom">Bottom</sp-tooltip>
51+
<sp-tooltip open placement="right">Right</sp-tooltip>
52+
<sp-tooltip open placement="top">Top</sp-tooltip>
7453
```
7554

76-
## Variants
55+
#### Variants
56+
57+
The tooltip supports several variants to convey different types of messages:
7758

78-
### Informative
59+
<sp-tabs selected="info" auto label="Variant Options">
60+
<sp-tab value="info">Info</sp-tab>
61+
<sp-tab-panel value="info">
7962

80-
This is the informative or info variant of Tooltip
63+
Use `variant="info"` for informational messages.
8164

8265
```html
83-
<sp-tooltip open placement="top" variant="info">Label</sp-tooltip>
84-
<sp-tooltip open placement="top" variant="info">
85-
Lorem ipsum dolor sit amet, consectetur adipiscing elit
86-
</sp-tooltip>
8766
<sp-tooltip open placement="top" variant="info">
8867
<sp-icon-info slot="icon" size="s"></sp-icon-info>
89-
Label
90-
</sp-tooltip>
91-
<sp-tooltip open placement="top" variant="info">
92-
<sp-icon-info slot="icon" size="s"></sp-icon-info>
93-
Lorem ipsum dolor sit amet, consectetur adipiscing elit
68+
Embark on a side quest.
9469
</sp-tooltip>
9570
```
9671

97-
### Positive
72+
</sp-tab-panel>
73+
<sp-tab value="positive">Positive</sp-tab>
74+
<sp-tab-panel value="positive">
9875

99-
This is the postive (a.k.a.) success variant of Tooltip
76+
Use `variant="positive"` for success messages.
10077

10178
```html
102-
<sp-tooltip open placement="top" variant="positive">Label</sp-tooltip>
103-
<sp-tooltip open placement="top" variant="positive">
104-
Lorem ipsum dolor sit amet, consectetur adipiscing elit
105-
</sp-tooltip>
10679
<sp-tooltip open placement="top" variant="positive">
10780
<sp-icon-checkmark-circle slot="icon" size="s"></sp-icon-checkmark-circle>
108-
Label
109-
</sp-tooltip>
110-
<sp-tooltip open placement="top" variant="positive">
111-
<sp-icon-checkmark-circle slot="icon" size="s"></sp-icon-checkmark-circle>
112-
Lorem ipsum dolor sit amet, consectetur adipiscing elit
81+
Quest completed!
11382
</sp-tooltip>
11483
```
11584

116-
### Negative
85+
</sp-tab-panel>
86+
<sp-tab value="negative">Negative</sp-tab>
87+
<sp-tab-panel value="negative">
11788

118-
This is the negative a.k.a. error variant of Tooltip
89+
Use `variant="negative"` for error messages.
11990

12091
```html
121-
<sp-tooltip open placement="top" variant="negative">Label</sp-tooltip>
122-
<sp-tooltip open placement="top" variant="negative">
123-
Lorem ipsum dolor sit amet, consectetur adipiscing elit
124-
</sp-tooltip>
12592
<sp-tooltip open placement="top" variant="negative">
12693
<sp-icon-alert slot="icon" size="s"></sp-icon-alert>
127-
Label
128-
</sp-tooltip>
129-
<sp-tooltip open placement="top" variant="negative">
130-
<sp-icon-alert slot="icon" size="s"></sp-icon-alert>
131-
Lorem ipsum dolor sit amet, consectetur adipiscing elit
94+
Quest failed!
13295
</sp-tooltip>
13396
```
97+
98+
</sp-tab-panel>
99+
</sp-tabs>
100+
101+
### Behaviors
102+
103+
#### Overlay
104+
105+
By default, Tooltip provides styling without behavior.
106+
107+
<sp-tabs selected="overlay-trigger" auto label="Overlay Behaviors">
108+
<sp-tab value="overlay-trigger">Overlay Trigger</sp-tab>
109+
<sp-tab-panel value="overlay-trigger">
110+
111+
You must combine it with an [Overlay Trigger](https://opensource.adobe.com/spectrum-web-components/components/overlay-trigger/#%22hover%22-content-only) to manage its overlay behavior.
112+
113+
```html
114+
<overlay-trigger triggered-by="hover">
115+
<sp-button slot="trigger" variant="secondary">Hover me</sp-button>
116+
<sp-tooltip slot="hover-content" placement="bottom">
117+
Tooltip overlay triggered by hover
118+
</sp-tooltip>
119+
</overlay-trigger>
120+
```
121+
122+
</sp-tab-panel>
123+
<sp-tab value="self-managed">Self-managed</sp-tab>
124+
<sp-tab-panel value="self-managed">
125+
126+
For simpler use cases, you can use the `self-managed` attribute which automatically binds the Tooltip to its first interactive ancestor element's focus/hover interactions:
127+
128+
```html
129+
<sp-action-button>
130+
Items
131+
<sp-tooltip self-managed>Use items during battle.</sp-tooltip>
132+
</sp-action-button>
133+
```
134+
135+
This is especially useful when inserting an intermediate `<overlay-trigger>` would interfere with parent/child relationships, such as between `<sp-action-group>` and `<sp-action-button>`.
136+
137+
</sp-tab-panel>
138+
</sp-tabs>
139+
140+
#### Delayed Opening
141+
142+
A tooltip can be configured to delay its opening using the `delayed` attribute. This adds a warm-up period of 1000ms before showing the tooltip:
143+
144+
```html
145+
<sp-action-button>
146+
Show delayed tooltip
147+
<sp-tooltip self-managed delayed>
148+
This tooltip will show after a delay
149+
</sp-tooltip>
150+
</sp-action-button>
151+
```
152+
153+
### Accessibility
154+
155+
The tooltip is automatically assigned appropriate ARIA attributes:
156+
157+
- `role="tooltip"` is applied to the tooltip content
158+
- The tooltip is associated with its trigger element via `aria-describedby`
159+
160+
When using `self-managed` tooltips:
161+
162+
- The tooltip appears on hover or focus of the trigger element
163+
- The tooltip disappears when focus or hover leaves the trigger element
164+
- <kbd>Escape</kbd> dismisses the tooltip
165+
166+
#### Use tooltips to describe icons
167+
168+
Icons are not always easy to identify on their own. When you use components that don’t have labels — for example, icon-only action buttons and tabs — make sure to use tooltips to provide context for the icons.
169+
170+
Given that tooltip is not focusable by itself, it would not show up during tab navigation. A tooltip should only be used with interactive elements that can receive focus during tab navigation, such as:
171+
172+
- `<sp-action-button>`
173+
- `<sp-action-menu>`
174+
- `<sp-field-label>`
175+
176+
For non-interactive elements like icons, wrap them in an interactive element:
177+
178+
```html
179+
<sp-action-button size="s">
180+
<sp-icon-book slot="icon"></sp-icon-book>
181+
<sp-tooltip self-managed placement="right">Save progress.</sp-tooltip>
182+
</sp-action-button>
183+
```
184+
185+
#### Use plain text in your tooltips
186+
187+
Because a tooltip is not focusable by itself, it should not contain any interactive elements. Additionally, because a tooltip is referenced in an `aria-describedby` attribute, it should not contain any rich formatting, such as headings, lists, bold, italic, or other complex content.
188+
189+
#### Don't use tooltips to communicate crucial information
190+
191+
Show crucial information at all times, not just when a tooltip is displayed. A tooltip should only be used to provide supplementary context or hints to the message shown in help text.
192+
193+
For example, in a scenario where a user is entering their password into a field, the crucial information would be to state the password requirements. Supplementary context would be a message about how to get help if they have forgotten their password.

packages/tooltip/directive.md renamed to packages/tooltip/tooltip-directive.md

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
1-
## Description
1+
## Overview
22

3-
While `<sp-tooltip>` element are general fairly innocumous amounts of DOM, it is possible that your page will quickly leverage so many Tooltips that their presence could begin to negatively effect its performane. To support consumers that leverage `lit-html`, Spectrum Web Components also vends a [directive](https://lit.dev/docs/api/directives/) to further simplify the delivery of `<sp-tooltip>` elements.
3+
While `<sp-tooltip>` elements are generally fairly innocuous amounts of DOM, it is possible impact performance with too many Tooltips. To support consumers that use `lit-html`, Spectrum Web Components also proives a [directive](https://lit.dev/docs/api/directives/) to improve performance when using many `<sp-tooltip>` elements.
44

5-
### Uusage
5+
### Usage
66

77
```
88
yarn add @spectrum-web-components/tooltip
99
```
1010

11-
Import the `trigger` directive as follows:
11+
Import the tooltip directive as follows:
1212

1313
```
1414
import { tooltip } from '@spectrum-web-components/tooltip/src/tooltip-directive.js';
1515
```
1616

17-
## Arguments
17+
### Anatomy
1818

19-
The `trigger()` directive accepts two arguments:
19+
The tooltip directive consists of two main parts:
2020

21-
- a required method returning the `TemplateResult` defining the content of the open overlay
21+
1. A method returning the `TemplateResult` defining the content of the open overlay:
2222

2323
```ts
2424
() => TemplateResult;
2525
```
2626

27-
- an optional options object which is shaped as follows:
27+
2. An optional options object for configuring the tooltip behavior:
2828

2929
```ts
3030
{
@@ -36,9 +36,15 @@ The `trigger()` directive accepts two arguments:
3636
}
3737
```
3838

39-
The `triggerInteraction` is applied as `hover` when using the `tooltip()` directive.
39+
### Options
4040

41-
`OverlayOptions` are leveraged in the same way as outlined [here](https://opensource.adobe.com/spectrum-web-components/components/imperative-api/#overlayoptions) and `InsertionOptions` are leveraged to outline where in the DOM the Overlay should be inserted:
41+
#### Overlay Options
42+
43+
The `overlayOptions` are leveraged in the same way as outlined [here](https://opensource.adobe.com/spectrum-web-components/components/imperative-api/#overlayoptions).
44+
45+
#### Insertion Options
46+
47+
The `insertionOptions` define where in the DOM the Overlay should be inserted:
4248

4349
```ts
4450
type InsertionOptions = {
@@ -47,14 +53,13 @@ type InsertionOptions = {
4753
};
4854
```
4955

50-
## Consumption via `lit-html`
56+
### Behaviors
5157

52-
The `tooltip()` directive will automatically wrap whatever content you provide in an `<sp-tooltip>` element for you, so you will not need to supply one in this case.
58+
#### Consumption via `lit-html`
5359

54-
Pass a `TemplateResult` into the `tooltip()` directive, as follows in order to have it rendered to the DOM when the associated Tooltip is about to open and the removed after the Tooltip has closed.
60+
The `tooltip()` directive will automatically wrap whatever content you provide in an `<sp-tooltip>` element. Pass a `TemplateResult` into the `tooltip()` directive to have it rendered to the DOM when the associated Tooltip is about to open and then removed after the Tooltip has closed.
5561

5662
```html-live
57-
5863
<div id="root"></div>
5964
6065
<script type="module">
@@ -71,7 +76,6 @@ Pass a `TemplateResult` into the `tooltip()` directive, as follows in order to h
7176
})}>Trigger</sp-button>
7277
`;
7378
74-
7579
customElements.whenDefined('code-example').then(() => {
7680
Promise.all([...document.querySelectorAll('code-example')].map(example => example.updateComplete)).then(() => {
7781
const appRoot = document.querySelector('#root');
@@ -104,3 +108,14 @@ Pass a `TemplateResult` into the `tooltip()` directive, as follows in order to h
104108
});
105109
});
106110
</script>
111+
112+
### Accessibility
113+
114+
The tooltip directive automatically manages accessibility features:
115+
116+
- Tooltips are associated with their trigger elements via `aria-describedby`
117+
- Content is only rendered when needed, reducing DOM complexity
118+
- Hover and focus interactions are handled automatically
119+
- Keyboard navigation support is built-in
120+
121+
For more information on accessibility, see the [Accessibility](https://opensource.adobe.com/spectrum-web-components/components/tooltip/#accessibility) section of the tooltip component.

0 commit comments

Comments
 (0)