dssoca 0.19.0 → 0.21.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -3
- package/dist/components/Accordion.svelte +30 -54
- package/dist/components/Badge.svelte +12 -7
- package/dist/components/Badge.svelte.d.ts +2 -1
- package/dist/components/BottomNav.svelte +18 -28
- package/dist/components/BoxPlot.svelte +34 -7
- package/dist/components/BumpChart.svelte +65 -26
- package/dist/components/Button.svelte +23 -1
- package/dist/components/Button.svelte.d.ts +5 -1
- package/dist/components/Card.svelte +32 -14
- package/dist/components/Card.svelte.d.ts +4 -1
- package/dist/components/Chart.svelte +166 -32
- package/dist/components/Chart.svelte.d.ts +13 -1
- package/dist/components/Checkbox.svelte +210 -0
- package/dist/components/Checkbox.svelte.d.ts +38 -0
- package/dist/components/DateField.svelte +1 -1
- package/dist/components/FileDrop.svelte +113 -77
- package/dist/components/FileDrop.svelte.d.ts +12 -2
- package/dist/components/Heatmap.svelte +128 -50
- package/dist/components/Heatmap.svelte.d.ts +2 -1
- package/dist/components/Icon.svelte +12 -3
- package/dist/components/Icon.svelte.d.ts +4 -0
- package/dist/components/Image.svelte +44 -44
- package/dist/components/Input.svelte +11 -6
- package/dist/components/Link.svelte +25 -14
- package/dist/components/LogStream.svelte +48 -33
- package/dist/components/Menu.svelte +81 -102
- package/dist/components/Menu.svelte.d.ts +8 -2
- package/dist/components/MetricTile.svelte +11 -1
- package/dist/components/MetricTile.svelte.d.ts +6 -0
- package/dist/components/Modal.svelte +108 -21
- package/dist/components/Modal.svelte.d.ts +18 -5
- package/dist/components/NumberField.svelte +44 -22
- package/dist/components/Pagination.svelte +54 -41
- package/dist/components/Pagination.svelte.d.ts +6 -2
- package/dist/components/Popover.svelte +247 -0
- package/dist/components/Popover.svelte.d.ts +36 -0
- package/dist/components/RadioGroup.svelte +227 -0
- package/dist/components/RadioGroup.svelte.d.ts +42 -0
- package/dist/components/ScatterPlot.svelte +42 -7
- package/dist/components/SearchPalette.svelte +216 -136
- package/dist/components/SearchPalette.svelte.d.ts +11 -4
- package/dist/components/SegmentedControl.svelte +47 -59
- package/dist/components/SegmentedControl.svelte.d.ts +7 -2
- package/dist/components/Select.svelte +3 -4
- package/dist/components/ServiceCard.svelte +105 -68
- package/dist/components/ServiceCard.svelte.d.ts +8 -3
- package/dist/components/ShortcutsHelp.svelte +49 -48
- package/dist/components/Sidebar.svelte +88 -117
- package/dist/components/Switch.svelte +24 -18
- package/dist/components/Switch.svelte.d.ts +6 -2
- package/dist/components/Table.svelte +29 -44
- package/dist/components/Table.svelte.d.ts +6 -2
- package/dist/components/Tabs.svelte +223 -0
- package/dist/components/Tabs.svelte.d.ts +51 -0
- package/dist/components/Textarea.svelte +1 -1
- package/dist/components/TierList.svelte +45 -49
- package/dist/components/TierList.svelte.d.ts +7 -3
- package/dist/components/Toaster.svelte +154 -32
- package/dist/components/Tooltip.svelte +122 -18
- package/dist/components/Tooltip.svelte.d.ts +1 -1
- package/dist/components/Topbar.svelte +29 -49
- package/dist/core/accordion.d.ts +32 -0
- package/dist/core/accordion.js +53 -0
- package/dist/core/behaviors/focusable.d.ts +5 -0
- package/dist/core/behaviors/focusable.js +14 -0
- package/dist/core/behaviors/roving.d.ts +13 -0
- package/dist/core/behaviors/roving.js +29 -0
- package/dist/core/bottom-nav.d.ts +36 -0
- package/dist/core/bottom-nav.js +33 -0
- package/dist/core/chart-nav.d.ts +53 -0
- package/dist/core/chart-nav.js +62 -0
- package/dist/core/checkbox.d.ts +36 -0
- package/dist/core/checkbox.js +29 -0
- package/dist/core/file-drop.d.ts +75 -0
- package/dist/core/file-drop.js +101 -0
- package/dist/core/image.d.ts +59 -0
- package/dist/core/image.js +64 -0
- package/dist/core/input.d.ts +21 -0
- package/dist/core/input.js +17 -0
- package/dist/core/log-stream.d.ts +51 -0
- package/dist/core/log-stream.js +50 -0
- package/dist/core/menu.d.ts +70 -0
- package/dist/core/menu.js +84 -0
- package/dist/core/modal.d.ts +59 -0
- package/dist/core/modal.js +44 -0
- package/dist/core/number-field.d.ts +47 -0
- package/dist/core/number-field.js +64 -0
- package/dist/core/pagination.d.ts +59 -0
- package/dist/core/pagination.js +65 -0
- package/dist/core/popover.d.ts +68 -0
- package/dist/core/popover.js +55 -0
- package/dist/core/radio.d.ts +36 -0
- package/dist/core/radio.js +48 -0
- package/dist/core/search-palette.d.ts +124 -0
- package/dist/core/search-palette.js +149 -0
- package/dist/core/segmented.d.ts +46 -0
- package/dist/core/segmented.js +66 -0
- package/dist/core/shortcuts-help.d.ts +56 -0
- package/dist/core/shortcuts-help.js +50 -0
- package/dist/core/sidebar.d.ts +63 -0
- package/dist/core/sidebar.js +84 -0
- package/dist/core/switch.d.ts +19 -0
- package/dist/core/switch.js +11 -0
- package/dist/core/table.d.ts +40 -0
- package/dist/core/table.js +57 -0
- package/dist/core/tabs.d.ts +84 -0
- package/dist/core/tabs.js +90 -0
- package/dist/{tierlist-core.d.ts → core/tierlist.d.ts} +77 -5
- package/dist/{tierlist-core.js → core/tierlist.js} +64 -7
- package/dist/core/toast.d.ts +112 -0
- package/dist/{toast-core.js → core/toast.js} +48 -2
- package/dist/core/tooltip.d.ts +47 -0
- package/dist/core/tooltip.js +51 -0
- package/dist/core/topbar.d.ts +41 -0
- package/dist/core/topbar.js +52 -0
- package/dist/dssoca.config.d.ts +1 -1
- package/dist/dssoca.config.js +4 -0
- package/dist/icons.d.ts +19 -2
- package/dist/icons.js +42 -3
- package/dist/index.d.ts +15 -2
- package/dist/index.js +10 -2
- package/dist/theme.css +47 -0
- package/dist/toast.svelte.d.ts +2 -2
- package/dist/toast.svelte.js +1 -1
- package/dist/tokens.css +47 -0
- package/dist/vanilla/accordion.js +35 -32
- package/dist/vanilla/checkbox.d.ts +8 -0
- package/dist/vanilla/checkbox.js +43 -0
- package/dist/vanilla/delegate.d.ts +2 -0
- package/dist/vanilla/delegate.js +9 -0
- package/dist/vanilla/fields.js +46 -28
- package/dist/vanilla/index.d.ts +5 -1
- package/dist/vanilla/index.js +17 -4
- package/dist/vanilla/menu.js +101 -84
- package/dist/vanilla/modal.d.ts +7 -1
- package/dist/vanilla/modal.js +120 -15
- package/dist/vanilla/popover.d.ts +10 -0
- package/dist/vanilla/popover.js +129 -0
- package/dist/vanilla/radio.d.ts +3 -0
- package/dist/vanilla/radio.js +54 -0
- package/dist/vanilla/segmented.js +50 -43
- package/dist/vanilla/switch.d.ts +2 -1
- package/dist/vanilla/switch.js +13 -8
- package/dist/vanilla/tabs.d.ts +2 -0
- package/dist/vanilla/tabs.js +87 -0
- package/dist/vanilla/tierlist.d.ts +1 -1
- package/dist/vanilla/tierlist.js +34 -34
- package/dist/vanilla/toast.d.ts +13 -5
- package/dist/vanilla/toast.js +132 -16
- package/dist/vanilla/tooltip.d.ts +5 -1
- package/dist/vanilla/tooltip.js +78 -19
- package/dist/vanilla.css +675 -160
- package/package.json +1 -1
- package/dist/toast-core.d.ts +0 -61
package/dist/vanilla/toast.js
CHANGED
|
@@ -7,13 +7,21 @@
|
|
|
7
7
|
* toasts.dismiss(id) / clear() / update(id, patch) / max
|
|
8
8
|
*
|
|
9
9
|
* The stack lives in the first `.ss-toaster` on the page (author it to choose `data-position`
|
|
10
|
-
* / size) or in one auto-created under `<body>`. Hover/focus pause the timer
|
|
11
|
-
*
|
|
12
|
-
*
|
|
10
|
+
* / size) or in one auto-created under `<body>`. Hover/focus pause the timer (it resumes only
|
|
11
|
+
* once the pointer has left AND focus is outside the toast), Escape and the × button dismiss,
|
|
12
|
+
* exit runs the `.leaving` CSS animation from vanilla.css. Dismissing the focused toast moves
|
|
13
|
+
* focus to a neighbouring toast, or back to where it came from. Swipe-to-dismiss is not ported.
|
|
14
|
+
*
|
|
15
|
+
* Announcements go through two persistent, visually hidden live regions inside the container
|
|
16
|
+
* (`.live[role=status]` polite, `.live[role=alert]` assertive — the same DOM Toaster.svelte
|
|
17
|
+
* renders; author them in the markup or they are added on DOM ready / first use). Each visible
|
|
18
|
+
* toast mirrors its message into one of them; the toasts themselves carry no live role.
|
|
13
19
|
*/
|
|
14
|
-
import { DEFAULT_TIMEOUT, isSticky, resolveTimeout, normalizeOptions, ToastTimer, } from '../toast
|
|
20
|
+
import { DEFAULT_TIMEOUT, isSticky, resolveTimeout, normalizeOptions, toastActionDismisses, toastFocusAfterDismiss, toastKey, toastShouldResume, ToastTimer, } from '../core/toast.js';
|
|
15
21
|
const GLYPH = { success: '✓', error: '✕', info: 'i', loading: '' };
|
|
16
22
|
const LEAVE_FALLBACK_MS = 400;
|
|
23
|
+
/** Screen readers need a moment to register a fresh live region before it changes. */
|
|
24
|
+
const LIVE_READY_MS = 100;
|
|
17
25
|
const reduceMotion = () => typeof matchMedia === 'function' && matchMedia('(prefers-reduced-motion: reduce)').matches;
|
|
18
26
|
class VanillaToastStore {
|
|
19
27
|
max = 3;
|
|
@@ -21,6 +29,11 @@ class VanillaToastStore {
|
|
|
21
29
|
#live = [];
|
|
22
30
|
#queue = [];
|
|
23
31
|
#container = null;
|
|
32
|
+
#polite = null;
|
|
33
|
+
#assertive = null;
|
|
34
|
+
/** When the current live regions were created (content inserted earlier may be skipped). */
|
|
35
|
+
#liveSince = 0;
|
|
36
|
+
#returnFocus = null;
|
|
24
37
|
/** Visible toasts (read-only snapshot). */
|
|
25
38
|
get items() {
|
|
26
39
|
return this.#live.map((l) => l.toast);
|
|
@@ -38,8 +51,59 @@ class VanillaToastStore {
|
|
|
38
51
|
document.body.appendChild(el);
|
|
39
52
|
}
|
|
40
53
|
this.#container = el;
|
|
54
|
+
el.addEventListener('focusin', (e) => {
|
|
55
|
+
const from = e.relatedTarget;
|
|
56
|
+
if (from && !el.contains(from))
|
|
57
|
+
this.#returnFocus = from;
|
|
58
|
+
});
|
|
59
|
+
this.#ensureLive(el);
|
|
41
60
|
return el;
|
|
42
61
|
}
|
|
62
|
+
/** Make sure the container holds both persistent live regions (adds any that are missing). */
|
|
63
|
+
#ensureLive(el) {
|
|
64
|
+
const find = (role) => Array.from(el.children).find((c) => c.classList.contains('live') && c.getAttribute('role') === role) ?? null;
|
|
65
|
+
let polite = find('status');
|
|
66
|
+
let assertive = find('alert');
|
|
67
|
+
this.#liveSince = !polite || !assertive ? Date.now() : 0;
|
|
68
|
+
if (!polite) {
|
|
69
|
+
polite = document.createElement('div');
|
|
70
|
+
polite.className = 'live';
|
|
71
|
+
polite.setAttribute('role', 'status');
|
|
72
|
+
polite.setAttribute('aria-live', 'polite');
|
|
73
|
+
el.prepend(polite);
|
|
74
|
+
}
|
|
75
|
+
if (!assertive) {
|
|
76
|
+
assertive = document.createElement('div');
|
|
77
|
+
assertive.className = 'live';
|
|
78
|
+
assertive.setAttribute('role', 'alert');
|
|
79
|
+
assertive.setAttribute('aria-live', 'assertive');
|
|
80
|
+
polite.after(assertive);
|
|
81
|
+
}
|
|
82
|
+
this.#polite = polite;
|
|
83
|
+
this.#assertive = assertive;
|
|
84
|
+
}
|
|
85
|
+
/** Prepare the live regions of an author-placed container ahead of the first toast. */
|
|
86
|
+
prepare() {
|
|
87
|
+
if (document.querySelector('.ss-toaster'))
|
|
88
|
+
void this.container;
|
|
89
|
+
}
|
|
90
|
+
/** Write a toast's message into the live region matching its kind. */
|
|
91
|
+
#announce(live) {
|
|
92
|
+
const region = live.toast.kind === 'error' ? this.#assertive : this.#polite;
|
|
93
|
+
const text = live.toast.message;
|
|
94
|
+
const write = () => {
|
|
95
|
+
if (!this.#live.includes(live))
|
|
96
|
+
return;
|
|
97
|
+
if (live.say.parentElement !== region)
|
|
98
|
+
region.appendChild(live.say);
|
|
99
|
+
live.say.textContent = text;
|
|
100
|
+
};
|
|
101
|
+
const wait = this.#liveSince + LIVE_READY_MS - Date.now();
|
|
102
|
+
if (wait > 0)
|
|
103
|
+
setTimeout(write, wait);
|
|
104
|
+
else
|
|
105
|
+
write();
|
|
106
|
+
}
|
|
43
107
|
push(kind, message, opts) {
|
|
44
108
|
const o = normalizeOptions(opts);
|
|
45
109
|
const id = ++this.#seq;
|
|
@@ -54,15 +118,35 @@ class VanillaToastStore {
|
|
|
54
118
|
const el = document.createElement('div');
|
|
55
119
|
el.tabIndex = -1;
|
|
56
120
|
this.#render(el, t);
|
|
57
|
-
|
|
58
|
-
|
|
121
|
+
const live = {
|
|
122
|
+
toast: t,
|
|
123
|
+
el,
|
|
124
|
+
say: document.createElement('p'),
|
|
125
|
+
timer: null,
|
|
126
|
+
hovered: false,
|
|
127
|
+
};
|
|
128
|
+
el.addEventListener('pointerenter', () => {
|
|
129
|
+
live.hovered = true;
|
|
130
|
+
this.pause(t.id);
|
|
131
|
+
});
|
|
132
|
+
const resumeIfFree = (focused) => {
|
|
133
|
+
if (toastShouldResume({ hovered: live.hovered, focused }))
|
|
134
|
+
this.resume(t.id);
|
|
135
|
+
};
|
|
136
|
+
el.addEventListener('pointerleave', () => {
|
|
137
|
+
live.hovered = false;
|
|
138
|
+
resumeIfFree(holdsFocus(live));
|
|
139
|
+
});
|
|
59
140
|
el.addEventListener('focusin', () => this.pause(t.id));
|
|
60
|
-
|
|
141
|
+
// Focus is only leaving when it lands outside the toast.
|
|
142
|
+
el.addEventListener('focusout', (e) => resumeIfFree(el.contains(e.relatedTarget)));
|
|
61
143
|
el.addEventListener('keydown', (e) => {
|
|
62
|
-
|
|
144
|
+
const result = toastKey(e.key);
|
|
145
|
+
if (!result)
|
|
146
|
+
return;
|
|
147
|
+
if (result.stopPropagation)
|
|
63
148
|
e.stopPropagation();
|
|
64
|
-
|
|
65
|
-
}
|
|
149
|
+
this.dismiss(t.id);
|
|
66
150
|
});
|
|
67
151
|
el.addEventListener('click', (e) => {
|
|
68
152
|
const btn = e.target.closest('button');
|
|
@@ -71,20 +155,21 @@ class VanillaToastStore {
|
|
|
71
155
|
if (btn.classList.contains('x'))
|
|
72
156
|
this.dismiss(t.id);
|
|
73
157
|
else if (btn.classList.contains('action')) {
|
|
74
|
-
|
|
75
|
-
if (!keep)
|
|
158
|
+
if (toastActionDismisses(this.#find(t.id)?.toast.action?.onClick()))
|
|
76
159
|
this.dismiss(t.id);
|
|
77
160
|
}
|
|
78
161
|
});
|
|
79
162
|
this.container.appendChild(el);
|
|
80
|
-
const live = { toast: t, el, timer: null };
|
|
81
163
|
this.#live.push(live);
|
|
164
|
+
this.#announce(live);
|
|
82
165
|
this.#arm(live);
|
|
83
166
|
}
|
|
84
167
|
#render(el, t) {
|
|
85
168
|
el.className = `ss-toast ${t.kind}`;
|
|
86
|
-
|
|
87
|
-
|
|
169
|
+
// A focusable group named by its message (focus can land on the toast itself).
|
|
170
|
+
const msgId = `ss-toast-msg-${t.id}`;
|
|
171
|
+
el.setAttribute('role', 'group');
|
|
172
|
+
el.setAttribute('aria-labelledby', msgId);
|
|
88
173
|
const ic = t.kind === 'loading'
|
|
89
174
|
? `<span class="ic spinner${reduceMotion() ? ' still' : ''}" aria-hidden="true"></span>`
|
|
90
175
|
: `<span class="ic" aria-hidden="true">${GLYPH[t.kind]}</span>`;
|
|
@@ -92,7 +177,7 @@ class VanillaToastStore {
|
|
|
92
177
|
? `<button class="action" type="button">${escapeHtml(t.action.label)}</button>`
|
|
93
178
|
: '';
|
|
94
179
|
el.innerHTML =
|
|
95
|
-
`${ic}<span class="msg">${escapeHtml(t.message)}</span>${action}` +
|
|
180
|
+
`${ic}<span class="msg" id="${msgId}">${escapeHtml(t.message)}</span>${action}` +
|
|
96
181
|
`<button class="x" type="button" aria-label="Dismiss">×</button>`;
|
|
97
182
|
}
|
|
98
183
|
#find(id) {
|
|
@@ -120,7 +205,12 @@ class VanillaToastStore {
|
|
|
120
205
|
else if (patch.kind !== undefined)
|
|
121
206
|
target.timeout = DEFAULT_TIMEOUT[patch.kind];
|
|
122
207
|
if (live) {
|
|
208
|
+
// Re-rendering replaces the toast's buttons; keep focus inside it.
|
|
209
|
+
const hadFocus = live.el.contains(document.activeElement);
|
|
123
210
|
this.#render(live.el, live.toast);
|
|
211
|
+
if (hadFocus && !live.el.contains(document.activeElement))
|
|
212
|
+
live.el.focus();
|
|
213
|
+
this.#announce(live);
|
|
124
214
|
this.#arm(live);
|
|
125
215
|
}
|
|
126
216
|
return true;
|
|
@@ -134,8 +224,11 @@ class VanillaToastStore {
|
|
|
134
224
|
dismiss(id) {
|
|
135
225
|
const i = this.#live.findIndex((l) => l.toast.id === id);
|
|
136
226
|
if (i !== -1) {
|
|
227
|
+
if (holdsFocus(this.#live[i]))
|
|
228
|
+
this.#moveFocusFrom(i);
|
|
137
229
|
const [live] = this.#live.splice(i, 1);
|
|
138
230
|
live.timer?.cancel();
|
|
231
|
+
live.say.remove();
|
|
139
232
|
this.#leave(live.el);
|
|
140
233
|
this.#promote();
|
|
141
234
|
return;
|
|
@@ -144,6 +237,17 @@ class VanillaToastStore {
|
|
|
144
237
|
if (qi !== -1)
|
|
145
238
|
this.#queue.splice(qi, 1);
|
|
146
239
|
}
|
|
240
|
+
/** Move focus off the toast at `i` (about to be removed): next toast, previous, or origin. */
|
|
241
|
+
#moveFocusFrom(i) {
|
|
242
|
+
const order = this.#live.map((l) => l.toast.id);
|
|
243
|
+
const next = this.#find(toastFocusAfterDismiss(order, order[i]) ?? -1);
|
|
244
|
+
if (next)
|
|
245
|
+
next.el.focus();
|
|
246
|
+
else if (this.#returnFocus?.isConnected) {
|
|
247
|
+
this.#returnFocus.focus();
|
|
248
|
+
this.#returnFocus = null;
|
|
249
|
+
}
|
|
250
|
+
}
|
|
147
251
|
#leave(el) {
|
|
148
252
|
if (reduceMotion()) {
|
|
149
253
|
el.remove();
|
|
@@ -167,16 +271,28 @@ class VanillaToastStore {
|
|
|
167
271
|
clear() {
|
|
168
272
|
for (const l of this.#live) {
|
|
169
273
|
l.timer?.cancel();
|
|
274
|
+
l.say.remove();
|
|
170
275
|
l.el.remove();
|
|
171
276
|
}
|
|
172
277
|
this.#live = [];
|
|
173
278
|
this.#queue = [];
|
|
174
279
|
}
|
|
175
280
|
}
|
|
281
|
+
const holdsFocus = (l) => l.el.contains(document.activeElement);
|
|
176
282
|
function escapeHtml(s) {
|
|
177
283
|
return s.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
|
|
178
284
|
}
|
|
179
285
|
export const toasts = new VanillaToastStore();
|
|
286
|
+
// Give an author-placed `.ss-toaster` its live regions as soon as the DOM is ready, so they exist
|
|
287
|
+
// well before the first toast writes into them.
|
|
288
|
+
if (typeof document !== 'undefined') {
|
|
289
|
+
if (document.readyState === 'loading') {
|
|
290
|
+
document.addEventListener('DOMContentLoaded', () => toasts.prepare(), { once: true });
|
|
291
|
+
}
|
|
292
|
+
else {
|
|
293
|
+
toasts.prepare();
|
|
294
|
+
}
|
|
295
|
+
}
|
|
180
296
|
export const toast = {
|
|
181
297
|
success: (message, opts) => toasts.push('success', message, opts),
|
|
182
298
|
error: (message, opts) => toasts.push('error', message, opts),
|
|
@@ -1,3 +1,7 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Open `root`'s tip. `from` is the element that triggered it (the focused element on
|
|
3
|
+
* focusin); `aria-describedby` goes on that element when it sits inside `.trigger`.
|
|
4
|
+
*/
|
|
5
|
+
export declare function showTooltip(root: HTMLElement, from?: EventTarget | null): void;
|
|
2
6
|
export declare function hideTooltip(root: HTMLElement): void;
|
|
3
7
|
export declare function installTooltip(): void;
|
package/dist/vanilla/tooltip.js
CHANGED
|
@@ -2,16 +2,69 @@
|
|
|
2
2
|
* Tooltip (DS-0148) — hover/focus reveal over Tooltip.svelte's DOM:
|
|
3
3
|
* `.ss-tooltip[data-placement] > .trigger + .tip[role=tooltip]`. Basic four-way placement
|
|
4
4
|
* only: the pre-rendered `data-placement` is honoured as-is (no collision engine). Escape
|
|
5
|
-
* hides
|
|
6
|
-
* the
|
|
5
|
+
* hides any open tip wherever focus is (WCAG 1.4.13). While open the focusable trigger inside
|
|
6
|
+
* `.trigger` (not the wrapper) carries `aria-describedby` → the tip (an id is generated when
|
|
7
|
+
* the markup has none); the token is added alongside any description the trigger already has
|
|
8
|
+
* (DS-0163). Which event shows or hides a tip is decided in `core/tooltip.ts`, the same
|
|
9
|
+
* functions Tooltip.svelte calls.
|
|
7
10
|
*/
|
|
8
|
-
import { on } from './delegate.js';
|
|
11
|
+
import { on, all } from './delegate.js';
|
|
12
|
+
import { tooltipDescribedBy, tooltipFocusIn, tooltipFocusOut, tooltipKey, tooltipPointerEnter, tooltipPointerLeave, } from '../core/tooltip.js';
|
|
9
13
|
const ROOT = '.ss-tooltip';
|
|
14
|
+
const FOCUSABLE = 'a[href], button, input, select, textarea, summary, [tabindex]:not([tabindex="-1"]), [contenteditable="true"]';
|
|
10
15
|
let seq = 0;
|
|
16
|
+
/** Element currently described by each open tooltip's tip. */
|
|
17
|
+
const described = new WeakMap();
|
|
11
18
|
function tipOf(root) {
|
|
12
19
|
return root.querySelector(':scope > .tip');
|
|
13
20
|
}
|
|
14
|
-
|
|
21
|
+
/** The focused trigger element, else the first focusable in `.trigger`, else its first element. */
|
|
22
|
+
function describedTarget(root, from) {
|
|
23
|
+
const trigger = root.querySelector(':scope > .trigger');
|
|
24
|
+
if (!trigger)
|
|
25
|
+
return root;
|
|
26
|
+
if (from instanceof HTMLElement && from !== trigger && trigger.contains(from))
|
|
27
|
+
return from;
|
|
28
|
+
return (trigger.querySelector(FOCUSABLE) ??
|
|
29
|
+
trigger.firstElementChild ??
|
|
30
|
+
trigger);
|
|
31
|
+
}
|
|
32
|
+
function setDescribedBy(el, tipId, open) {
|
|
33
|
+
const value = tooltipDescribedBy(el.getAttribute('aria-describedby'), tipId, open);
|
|
34
|
+
if (value === undefined)
|
|
35
|
+
el.removeAttribute('aria-describedby');
|
|
36
|
+
else
|
|
37
|
+
el.setAttribute('aria-describedby', value);
|
|
38
|
+
}
|
|
39
|
+
/** The core's state, read from the DOM. */
|
|
40
|
+
function stateOf(root) {
|
|
41
|
+
return { open: Boolean(tipOf(root)?.classList.contains('open')) };
|
|
42
|
+
}
|
|
43
|
+
/** The other end of a pointer or focus move is still inside `root`. */
|
|
44
|
+
function within(root, node) {
|
|
45
|
+
return node instanceof Node && root.contains(node);
|
|
46
|
+
}
|
|
47
|
+
/** Apply a core action to the DOM. */
|
|
48
|
+
function run(root, action, from) {
|
|
49
|
+
if (!action)
|
|
50
|
+
return;
|
|
51
|
+
if (action.type === 'show')
|
|
52
|
+
showTooltip(root, from);
|
|
53
|
+
else
|
|
54
|
+
hideTooltip(root);
|
|
55
|
+
}
|
|
56
|
+
function undescribe(root, tipId) {
|
|
57
|
+
const el = described.get(root);
|
|
58
|
+
described.delete(root);
|
|
59
|
+
if (!el)
|
|
60
|
+
return;
|
|
61
|
+
setDescribedBy(el, tipId, false);
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Open `root`'s tip. `from` is the element that triggered it (the focused element on
|
|
65
|
+
* focusin); `aria-describedby` goes on that element when it sits inside `.trigger`.
|
|
66
|
+
*/
|
|
67
|
+
export function showTooltip(root, from) {
|
|
15
68
|
const tip = tipOf(root);
|
|
16
69
|
if (!tip)
|
|
17
70
|
return;
|
|
@@ -19,7 +72,12 @@ export function showTooltip(root) {
|
|
|
19
72
|
tip.id = `ss-tip-${++seq}`;
|
|
20
73
|
tip.hidden = false;
|
|
21
74
|
tip.classList.add('open');
|
|
22
|
-
root
|
|
75
|
+
const target = describedTarget(root, from);
|
|
76
|
+
if (described.get(root) !== target) {
|
|
77
|
+
undescribe(root, tip.id);
|
|
78
|
+
setDescribedBy(target, tip.id, true);
|
|
79
|
+
described.set(root, target);
|
|
80
|
+
}
|
|
23
81
|
}
|
|
24
82
|
export function hideTooltip(root) {
|
|
25
83
|
const tip = tipOf(root);
|
|
@@ -27,28 +85,29 @@ export function hideTooltip(root) {
|
|
|
27
85
|
return;
|
|
28
86
|
tip.hidden = true;
|
|
29
87
|
tip.classList.remove('open');
|
|
30
|
-
root.
|
|
88
|
+
undescribe(root, tip.id);
|
|
31
89
|
}
|
|
32
90
|
export function installTooltip() {
|
|
33
91
|
// mouseenter/leave don't bubble; mouseover/out do — filter by relatedTarget.
|
|
34
92
|
on('mouseover', ROOT, (root, e) => {
|
|
35
|
-
|
|
36
|
-
if (!from || !root.contains(from))
|
|
37
|
-
showTooltip(root);
|
|
93
|
+
run(root, tooltipPointerEnter(stateOf(root), { within: within(root, e.relatedTarget) }));
|
|
38
94
|
});
|
|
39
95
|
on('mouseout', ROOT, (root, e) => {
|
|
40
|
-
|
|
41
|
-
if (!to || !root.contains(to))
|
|
42
|
-
hideTooltip(root);
|
|
96
|
+
run(root, tooltipPointerLeave(stateOf(root), { within: within(root, e.relatedTarget) }));
|
|
43
97
|
});
|
|
44
|
-
on('focusin', ROOT, (root) =>
|
|
98
|
+
on('focusin', ROOT, (root, e) => run(root, tooltipFocusIn(stateOf(root)), e.target));
|
|
45
99
|
on('focusout', ROOT, (root, e) => {
|
|
46
|
-
|
|
47
|
-
if (!to || !root.contains(to))
|
|
48
|
-
hideTooltip(root);
|
|
100
|
+
run(root, tooltipFocusOut(stateOf(root), { within: within(root, e.relatedTarget) }));
|
|
49
101
|
});
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
102
|
+
// Document-level Escape: a tip opened by hover closes even when focus is elsewhere.
|
|
103
|
+
document.addEventListener('keydown', (e) => {
|
|
104
|
+
// Cheap exit for every key that could not dismiss a tip, before touching the DOM.
|
|
105
|
+
if (!tooltipKey({ open: true }, e.key))
|
|
106
|
+
return;
|
|
107
|
+
for (const tip of all(document, `${ROOT} > .tip.open`)) {
|
|
108
|
+
const root = tip.parentElement;
|
|
109
|
+
if (root)
|
|
110
|
+
run(root, tooltipKey(stateOf(root), e.key));
|
|
111
|
+
}
|
|
53
112
|
});
|
|
54
113
|
}
|