|
1 |
| -## Description |
| 1 | +## Overview |
2 | 2 |
|
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. |
4 | 4 |
|
5 | 5 | ### Usage
|
6 | 6 |
|
7 | 7 | [](https://www.npmjs.com/package/@spectrum-web-components/tooltip)
|
8 | 8 | [](https://bundlephobia.com/result?p=@spectrum-web-components/tooltip)
|
9 | 9 | [](https://stackblitz.com/edit/vitejs-vite-cizgmpks)
|
10 | 10 |
|
11 |
| -``` |
| 11 | +```bash |
12 | 12 | yarn add @spectrum-web-components/tooltip
|
13 | 13 | ```
|
14 | 14 |
|
15 | 15 | Import the side effectful registration of `<sp-tooltip>` via:
|
16 | 16 |
|
17 |
| -``` |
| 17 | +```javascript |
18 | 18 | import '@spectrum-web-components/tooltip/sp-tooltip.js';
|
19 | 19 | ```
|
20 | 20 |
|
21 | 21 | When looking to leverage the `Tooltip` base class as a type and/or for extension purposes, do so via:
|
22 | 22 |
|
23 |
| -``` |
| 23 | +```javascript |
24 | 24 | import { Tooltip } from '@spectrum-web-components/tooltip';
|
25 | 25 | ```
|
26 | 26 |
|
27 |
| -## Example |
| 27 | +### Anatomy |
28 | 28 |
|
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: |
43 | 30 |
|
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 |
50 | 34 |
|
51 | 35 | ```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> |
56 | 40 | ```
|
57 | 41 |
|
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 |
62 | 43 |
|
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 |
64 | 45 |
|
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: |
66 | 47 |
|
67 | 48 | ```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> |
74 | 53 | ```
|
75 | 54 |
|
76 |
| -## Variants |
| 55 | +#### Variants |
| 56 | + |
| 57 | +The tooltip supports several variants to convey different types of messages: |
77 | 58 |
|
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"> |
79 | 62 |
|
80 |
| -This is the informative or info variant of Tooltip |
| 63 | +Use `variant="info"` for informational messages. |
81 | 64 |
|
82 | 65 | ```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> |
87 | 66 | <sp-tooltip open placement="top" variant="info">
|
88 | 67 | <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. |
94 | 69 | </sp-tooltip>
|
95 | 70 | ```
|
96 | 71 |
|
97 |
| -### Positive |
| 72 | +</sp-tab-panel> |
| 73 | +<sp-tab value="positive">Positive</sp-tab> |
| 74 | +<sp-tab-panel value="positive"> |
98 | 75 |
|
99 |
| -This is the postive (a.k.a.) success variant of Tooltip |
| 76 | +Use `variant="positive"` for success messages. |
100 | 77 |
|
101 | 78 | ```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> |
106 | 79 | <sp-tooltip open placement="top" variant="positive">
|
107 | 80 | <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! |
113 | 82 | </sp-tooltip>
|
114 | 83 | ```
|
115 | 84 |
|
116 |
| -### Negative |
| 85 | +</sp-tab-panel> |
| 86 | +<sp-tab value="negative">Negative</sp-tab> |
| 87 | +<sp-tab-panel value="negative"> |
117 | 88 |
|
118 |
| -This is the negative a.k.a. error variant of Tooltip |
| 89 | +Use `variant="negative"` for error messages. |
119 | 90 |
|
120 | 91 | ```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> |
125 | 92 | <sp-tooltip open placement="top" variant="negative">
|
126 | 93 | <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! |
132 | 95 | </sp-tooltip>
|
133 | 96 | ```
|
| 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. |
0 commit comments