@human-kit/ui 1.0.0-beta.8 → 1.0.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 +27 -60
- package/dist/FOCUS_STATE_CONTRACT.md +1 -0
- package/dist/color-picker/README.md +78 -0
- package/dist/color-picker/area/color-picker-area.svelte +195 -0
- package/dist/color-picker/area/color-picker-area.svelte.d.ts +4 -0
- package/dist/color-picker/area-thumb/color-picker-area-thumb.svelte +231 -0
- package/dist/color-picker/area-thumb/color-picker-area-thumb.svelte.d.ts +4 -0
- package/dist/color-picker/channel-field/color-picker-channel-field.svelte +95 -0
- package/dist/color-picker/channel-field/color-picker-channel-field.svelte.d.ts +4 -0
- package/dist/color-picker/eye-dropper/color-picker-eye-dropper.svelte +79 -0
- package/dist/color-picker/eye-dropper/color-picker-eye-dropper.svelte.d.ts +4 -0
- package/dist/color-picker/hex-field/color-picker-hex-field.svelte +105 -0
- package/dist/color-picker/hex-field/color-picker-hex-field.svelte.d.ts +4 -0
- package/dist/color-picker/index.d.ts +16 -0
- package/dist/color-picker/index.js +15 -0
- package/dist/color-picker/index.parts.d.ts +12 -0
- package/dist/color-picker/index.parts.js +12 -0
- package/dist/color-picker/label/color-picker-label.svelte +34 -0
- package/dist/color-picker/label/color-picker-label.svelte.d.ts +4 -0
- package/dist/color-picker/preview/color-picker-preview.svelte +43 -0
- package/dist/color-picker/preview/color-picker-preview.svelte.d.ts +4 -0
- package/dist/color-picker/root/color-picker-root.svelte +365 -0
- package/dist/color-picker/root/color-picker-root.svelte.d.ts +4 -0
- package/dist/color-picker/root/color.d.ts +68 -0
- package/dist/color-picker/root/color.js +266 -0
- package/dist/color-picker/root/context.d.ts +127 -0
- package/dist/color-picker/root/context.js +44 -0
- package/dist/color-picker/slider/color-picker-slider.svelte +188 -0
- package/dist/color-picker/slider/color-picker-slider.svelte.d.ts +4 -0
- package/dist/color-picker/slider-thumb/color-picker-slider-thumb.svelte +208 -0
- package/dist/color-picker/slider-thumb/color-picker-slider-thumb.svelte.d.ts +4 -0
- package/dist/color-picker/swatch/color-picker-swatch.svelte +149 -0
- package/dist/color-picker/swatch/color-picker-swatch.svelte.d.ts +4 -0
- package/dist/color-picker/swatch-list/color-picker-swatch-list.svelte +133 -0
- package/dist/color-picker/swatch-list/color-picker-swatch-list.svelte.d.ts +4 -0
- package/dist/color-picker/types.d.ts +176 -0
- package/dist/color-picker/types.js +1 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +10 -0
- package/dist/internal/localized-strings.d.ts +176 -0
- package/dist/internal/localized-strings.js +176 -0
- package/dist/pin-input/README.md +55 -0
- package/dist/pin-input/cell/pin-input-cell.svelte +210 -0
- package/dist/pin-input/cell/pin-input-cell.svelte.d.ts +4 -0
- package/dist/pin-input/index.d.ts +6 -0
- package/dist/pin-input/index.js +5 -0
- package/dist/pin-input/index.parts.d.ts +3 -0
- package/dist/pin-input/index.parts.js +3 -0
- package/dist/pin-input/label/pin-input-label.svelte +43 -0
- package/dist/pin-input/label/pin-input-label.svelte.d.ts +4 -0
- package/dist/pin-input/root/context.d.ts +70 -0
- package/dist/pin-input/root/context.js +15 -0
- package/dist/pin-input/root/pin-input-root.svelte +337 -0
- package/dist/pin-input/root/pin-input-root.svelte.d.ts +4 -0
- package/dist/pin-input/root/pin-input-utils.d.ts +18 -0
- package/dist/pin-input/root/pin-input-utils.js +44 -0
- package/dist/pin-input/types.d.ts +84 -0
- package/dist/pin-input/types.js +1 -0
- package/dist/primitives/aria-hide-outside.d.ts +13 -0
- package/dist/primitives/aria-hide-outside.js +7 -1
- package/dist/primitives/focus-trap.d.ts +2 -0
- package/dist/primitives/focus-trap.js +7 -1
- package/dist/primitives/presence.svelte.js +6 -1
- package/dist/rating/README.md +58 -0
- package/dist/rating/index.d.ts +7 -0
- package/dist/rating/index.js +6 -0
- package/dist/rating/index.parts.d.ts +4 -0
- package/dist/rating/index.parts.js +4 -0
- package/dist/rating/item/rating-item.svelte +177 -0
- package/dist/rating/item/rating-item.svelte.d.ts +4 -0
- package/dist/rating/label/rating-label.svelte +42 -0
- package/dist/rating/label/rating-label.svelte.d.ts +4 -0
- package/dist/rating/output/rating-output.svelte +38 -0
- package/dist/rating/output/rating-output.svelte.d.ts +4 -0
- package/dist/rating/root/context.d.ts +87 -0
- package/dist/rating/root/context.js +15 -0
- package/dist/rating/root/rating-root.svelte +521 -0
- package/dist/rating/root/rating-root.svelte.d.ts +4 -0
- package/dist/rating/root/rating-utils.d.ts +28 -0
- package/dist/rating/root/rating-utils.js +53 -0
- package/dist/rating/types.d.ts +124 -0
- package/dist/rating/types.js +1 -0
- package/dist/search-field/README.md +29 -0
- package/dist/search-field/clear/README.md +5 -0
- package/dist/search-field/clear/search-field-clear.svelte +99 -0
- package/dist/search-field/clear/search-field-clear.svelte.d.ts +10 -0
- package/dist/search-field/index.d.ts +15 -0
- package/dist/search-field/index.js +6 -0
- package/dist/search-field/index.parts.d.ts +4 -0
- package/dist/search-field/index.parts.js +4 -0
- package/dist/search-field/input/README.md +5 -0
- package/dist/search-field/input/search-field-input.svelte +158 -0
- package/dist/search-field/input/search-field-input.svelte.d.ts +14 -0
- package/dist/search-field/label/README.md +3 -0
- package/dist/search-field/label/search-field-label.svelte +27 -0
- package/dist/search-field/label/search-field-label.svelte.d.ts +7 -0
- package/dist/search-field/root/README.md +5 -0
- package/dist/search-field/root/context.d.ts +28 -0
- package/dist/search-field/root/context.js +15 -0
- package/dist/search-field/root/search-field-root.svelte +230 -0
- package/dist/search-field/root/search-field-root.svelte.d.ts +33 -0
- package/dist/toast/README.md +75 -0
- package/dist/toast/action/toast-action.svelte +42 -0
- package/dist/toast/action/toast-action.svelte.d.ts +4 -0
- package/dist/toast/close/toast-close.svelte +41 -0
- package/dist/toast/close/toast-close.svelte.d.ts +4 -0
- package/dist/toast/content/toast-content.svelte +24 -0
- package/dist/toast/content/toast-content.svelte.d.ts +4 -0
- package/dist/toast/description/toast-description.svelte +41 -0
- package/dist/toast/description/toast-description.svelte.d.ts +4 -0
- package/dist/toast/index.d.ts +34 -0
- package/dist/toast/index.js +15 -0
- package/dist/toast/index.parts.d.ts +9 -0
- package/dist/toast/index.parts.js +9 -0
- package/dist/toast/positioner/toast-positioner.svelte +75 -0
- package/dist/toast/positioner/toast-positioner.svelte.d.ts +4 -0
- package/dist/toast/provider/context.d.ts +36 -0
- package/dist/toast/provider/context.js +22 -0
- package/dist/toast/provider/toast-manager.svelte.d.ts +113 -0
- package/dist/toast/provider/toast-manager.svelte.js +210 -0
- package/dist/toast/provider/toast-provider.svelte +98 -0
- package/dist/toast/provider/toast-provider.svelte.d.ts +4 -0
- package/dist/toast/root/context.d.ts +18 -0
- package/dist/toast/root/context.js +15 -0
- package/dist/toast/root/toast-root.svelte +296 -0
- package/dist/toast/root/toast-root.svelte.d.ts +4 -0
- package/dist/toast/title/toast-title.svelte +38 -0
- package/dist/toast/title/toast-title.svelte.d.ts +4 -0
- package/dist/toast/types.d.ts +100 -0
- package/dist/toast/types.js +1 -0
- package/dist/toast/viewport/toast-viewport.svelte +389 -0
- package/dist/toast/viewport/toast-viewport.svelte.d.ts +4 -0
- package/dist/tooltip/root/tooltip-root.svelte +16 -11
- package/package.json +26 -1
|
@@ -0,0 +1,389 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { untrack } from 'svelte';
|
|
3
|
+
import { readable } from 'svelte/store';
|
|
4
|
+
import { resolveLocalizedString } from '../../internal/localized-strings';
|
|
5
|
+
import { useLocaleContextOptional } from '../../locale-provider/context';
|
|
6
|
+
import { Portal } from '../../portal';
|
|
7
|
+
import { HIDE_OUTSIDE_EXEMPT_ATTRIBUTE } from '../../primitives/aria-hide-outside';
|
|
8
|
+
import { getFocusableElements } from '../../primitives/focus-trap';
|
|
9
|
+
import { shouldShowFocusVisible } from '../../primitives/input-modality';
|
|
10
|
+
import { SWIPE_IGNORE_ATTRIBUTE } from '../../primitives/swipe-gesture';
|
|
11
|
+
import { visuallyHiddenStyle } from '../../table/utils/visually-hidden-style';
|
|
12
|
+
import { useToastProviderContext } from '../provider/context';
|
|
13
|
+
import type { ToastItem } from '../provider/toast-manager.svelte';
|
|
14
|
+
import type { ToastViewportProps } from '../types.js';
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Toast.Viewport — the region where the toasts land.
|
|
18
|
+
*
|
|
19
|
+
* It is a `role="region"` landmark, thus a screen reader user finds it in the list of
|
|
20
|
+
* landmarks, and `F6` moves the focus into it from anywhere on the page. The announcement of a
|
|
21
|
+
* toast does not come from the region: two live regions beside it, one polite and one
|
|
22
|
+
* assertive, say the title and the description of each new toast. Thus the screen reader does
|
|
23
|
+
* not read the buttons of the toast as part of the message.
|
|
24
|
+
*
|
|
25
|
+
* A touch has no hover: a tap on a toast spreads the stack out, and a touch outside the
|
|
26
|
+
* region folds it back. A tap is a touch that lifts where it landed: a swipe is not one, and
|
|
27
|
+
* a press on a button is not one. The timers stop while the pointer rests on the region, while the
|
|
28
|
+
* focus is in it, while a tap holds it open, and while the tab is hidden. A toast that closes
|
|
29
|
+
* while the user reads it is a toast the user did not read.
|
|
30
|
+
*/
|
|
31
|
+
let {
|
|
32
|
+
children,
|
|
33
|
+
class: className = '',
|
|
34
|
+
portal = true,
|
|
35
|
+
element = $bindable<HTMLDivElement | null>(null),
|
|
36
|
+
onpointerenter,
|
|
37
|
+
onpointerleave,
|
|
38
|
+
onpointerdown,
|
|
39
|
+
onpointerup,
|
|
40
|
+
onpointercancel,
|
|
41
|
+
onfocusin,
|
|
42
|
+
onfocusout,
|
|
43
|
+
onkeydown,
|
|
44
|
+
'aria-label': ariaLabelProp,
|
|
45
|
+
...restProps
|
|
46
|
+
}: ToastViewportProps = $props();
|
|
47
|
+
|
|
48
|
+
const ctx = useToastProviderContext('Toast.Viewport');
|
|
49
|
+
const manager = ctx.manager;
|
|
50
|
+
|
|
51
|
+
const localeContext = useLocaleContextOptional();
|
|
52
|
+
const emptyLocaleStore = readable<string | undefined>(undefined);
|
|
53
|
+
const localeStore = localeContext?.locale ?? emptyLocaleStore;
|
|
54
|
+
|
|
55
|
+
let regionRef: HTMLDivElement | null = $state(null);
|
|
56
|
+
// Where the focus was before `F6` or a click moved it into the region, thus a close can send
|
|
57
|
+
// it back there.
|
|
58
|
+
let previousFocus: HTMLElement | null = null;
|
|
59
|
+
// The visibility of the tab, not the focus of the window: a window without the focus can
|
|
60
|
+
// still be in view, and a frame beside others loses the focus all the time.
|
|
61
|
+
let documentVisible = typeof document === 'undefined' || document.visibilityState !== 'hidden';
|
|
62
|
+
|
|
63
|
+
// A toast on its way out is not one the user can act on: the name does not count it.
|
|
64
|
+
const count = $derived(manager.visibleToasts.filter((toast) => toast.status !== 'ending').length);
|
|
65
|
+
const ariaLabel = $derived.by(() => {
|
|
66
|
+
if (ariaLabelProp) return ariaLabelProp;
|
|
67
|
+
if (count === 1) return resolveLocalizedString($localeStore, 'toast.oneNotification');
|
|
68
|
+
if (count > 1) {
|
|
69
|
+
return resolveLocalizedString($localeStore, 'toast.multipleNotifications', { count });
|
|
70
|
+
}
|
|
71
|
+
return resolveLocalizedString($localeStore, 'toast.notifications');
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
$effect(() => {
|
|
75
|
+
element = regionRef;
|
|
76
|
+
ctx.setViewportElement(regionRef);
|
|
77
|
+
return () => ctx.setViewportElement(null);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
// --- Announcements ------------------------------------------------------------------------
|
|
81
|
+
|
|
82
|
+
// Each new toast, and each update, is one message with a key of its own. Two toasts in the
|
|
83
|
+
// same tick are two nodes, thus the live region reads both, and the same text twice when two
|
|
84
|
+
// toasts say the same. A message leaves the region after a while, or the next one would be a
|
|
85
|
+
// change of a text the screen reader already read.
|
|
86
|
+
type Announcement = { key: number; text: string };
|
|
87
|
+
const ANNOUNCEMENT_MS = 2000;
|
|
88
|
+
let announcementKey = 0;
|
|
89
|
+
let politeMessages = $state<Announcement[]>([]);
|
|
90
|
+
let assertiveMessages = $state<Announcement[]>([]);
|
|
91
|
+
// eslint-disable-next-line svelte/prefer-svelte-reactivity -- a record of what was said, not state: nothing renders from it.
|
|
92
|
+
const announced = new Map<string, number>();
|
|
93
|
+
|
|
94
|
+
function messageOf(toast: ToastItem) {
|
|
95
|
+
return [toast.title, toast.description].filter(Boolean).join('. ');
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function announce(text: string, priority: 'low' | 'high') {
|
|
99
|
+
announcementKey += 1;
|
|
100
|
+
const entry = { key: announcementKey, text };
|
|
101
|
+
if (priority === 'high') assertiveMessages = [...assertiveMessages, entry];
|
|
102
|
+
else politeMessages = [...politeMessages, entry];
|
|
103
|
+
// By key, not by identity: the state holds a proxy of the entry, not the entry.
|
|
104
|
+
setTimeout(() => {
|
|
105
|
+
politeMessages = politeMessages.filter((candidate) => candidate.key !== entry.key);
|
|
106
|
+
assertiveMessages = assertiveMessages.filter((candidate) => candidate.key !== entry.key);
|
|
107
|
+
}, ANNOUNCEMENT_MS);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
$effect(() => {
|
|
111
|
+
const toasts = manager.toasts;
|
|
112
|
+
untrack(() => {
|
|
113
|
+
// eslint-disable-next-line svelte/prefer-svelte-reactivity -- a scratch set for one pass.
|
|
114
|
+
const ids = new Set<string>();
|
|
115
|
+
// The list is the newest first; the screen reader hears the oldest first.
|
|
116
|
+
for (const toast of [...toasts].reverse()) {
|
|
117
|
+
ids.add(toast.id);
|
|
118
|
+
if (toast.status === 'ending' || announced.get(toast.id) === toast.updateKey) continue;
|
|
119
|
+
announced.set(toast.id, toast.updateKey);
|
|
120
|
+
const message = messageOf(toast);
|
|
121
|
+
if (!message) continue;
|
|
122
|
+
announce(message, toast.priority);
|
|
123
|
+
}
|
|
124
|
+
for (const id of announced.keys()) {
|
|
125
|
+
if (!ids.has(id)) announced.delete(id);
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
// --- Timers -------------------------------------------------------------------------------
|
|
131
|
+
|
|
132
|
+
function syncTimers() {
|
|
133
|
+
if (ctx.hovering || ctx.focused || ctx.tapped || !documentVisible) manager.pauseTimers();
|
|
134
|
+
else manager.resumeTimers();
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// A tap lifts within this distance of where it landed. Past it, the touch is a swipe.
|
|
138
|
+
const TAP_SLOP_PX = 10;
|
|
139
|
+
let tapStart: { id: number; x: number; y: number } | null = null;
|
|
140
|
+
|
|
141
|
+
function handlePointerDown(
|
|
142
|
+
event: PointerEvent & { currentTarget: EventTarget & HTMLDivElement }
|
|
143
|
+
) {
|
|
144
|
+
onpointerdown?.(event);
|
|
145
|
+
tapStart = null;
|
|
146
|
+
if (event.pointerType === 'mouse') return;
|
|
147
|
+
// A press on a button is a press: it must not spread the stack under the finger.
|
|
148
|
+
if (event.target instanceof Element && event.target.closest(`[${SWIPE_IGNORE_ATTRIBUTE}]`)) {
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
tapStart = { id: event.pointerId, x: event.clientX, y: event.clientY };
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
function handlePointerUp(event: PointerEvent & { currentTarget: EventTarget & HTMLDivElement }) {
|
|
155
|
+
onpointerup?.(event);
|
|
156
|
+
const start = tapStart;
|
|
157
|
+
tapStart = null;
|
|
158
|
+
if (!start || start.id !== event.pointerId) return;
|
|
159
|
+
if (Math.hypot(event.clientX - start.x, event.clientY - start.y) > TAP_SLOP_PX) return;
|
|
160
|
+
if (ctx.tapped) return;
|
|
161
|
+
ctx.setTapped(true);
|
|
162
|
+
syncTimers();
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function handlePointerCancel(
|
|
166
|
+
event: PointerEvent & { currentTarget: EventTarget & HTMLDivElement }
|
|
167
|
+
) {
|
|
168
|
+
onpointercancel?.(event);
|
|
169
|
+
tapStart = null;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// A touch anywhere else folds the stack back. The listener is on the document, thus a touch
|
|
173
|
+
// on a page that stops its own events folds it too.
|
|
174
|
+
function handleDocumentPointerDown(event: PointerEvent) {
|
|
175
|
+
if (!ctx.tapped || event.pointerType === 'mouse') return;
|
|
176
|
+
if (event.target instanceof Node && regionRef?.contains(event.target)) return;
|
|
177
|
+
ctx.setTapped(false);
|
|
178
|
+
syncTimers();
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function handlePointerEnter(
|
|
182
|
+
event: PointerEvent & { currentTarget: EventTarget & HTMLDivElement }
|
|
183
|
+
) {
|
|
184
|
+
onpointerenter?.(event);
|
|
185
|
+
if (event.pointerType === 'touch') return;
|
|
186
|
+
ctx.setHovering(true);
|
|
187
|
+
syncTimers();
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function handlePointerLeave(
|
|
191
|
+
event: PointerEvent & { currentTarget: EventTarget & HTMLDivElement }
|
|
192
|
+
) {
|
|
193
|
+
onpointerleave?.(event);
|
|
194
|
+
if (event.pointerType === 'touch') return;
|
|
195
|
+
ctx.setHovering(false);
|
|
196
|
+
syncTimers();
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
function handleFocusIn(event: FocusEvent & { currentTarget: EventTarget & HTMLDivElement }) {
|
|
200
|
+
onfocusin?.(event);
|
|
201
|
+
const target = event.target instanceof HTMLElement ? event.target : null;
|
|
202
|
+
// A press on a button in a toast is a press, not a visit: the timers keep going.
|
|
203
|
+
if (!shouldShowFocusVisible(target)) return;
|
|
204
|
+
if (!ctx.focused) {
|
|
205
|
+
const related = event.relatedTarget instanceof HTMLElement ? event.relatedTarget : null;
|
|
206
|
+
if (related && !regionRef?.contains(related)) previousFocus = related;
|
|
207
|
+
}
|
|
208
|
+
ctx.setFocused(true);
|
|
209
|
+
syncTimers();
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
function handleFocusOut(event: FocusEvent & { currentTarget: EventTarget & HTMLDivElement }) {
|
|
213
|
+
onfocusout?.(event);
|
|
214
|
+
const related = event.relatedTarget;
|
|
215
|
+
if (related instanceof Node && regionRef?.contains(related)) return;
|
|
216
|
+
ctx.setFocused(false);
|
|
217
|
+
syncTimers();
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
function handleVisibilityChange() {
|
|
221
|
+
documentVisible = document.visibilityState !== 'hidden';
|
|
222
|
+
syncTimers();
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
// --- Focus --------------------------------------------------------------------------------
|
|
226
|
+
|
|
227
|
+
function toastElements(): HTMLElement[] {
|
|
228
|
+
if (!regionRef) return [];
|
|
229
|
+
return Array.from(
|
|
230
|
+
regionRef.querySelectorAll<HTMLElement>('[data-toast-root]:not([inert]):not([data-ending])')
|
|
231
|
+
);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
function focusFirstToast() {
|
|
235
|
+
const [first] = toastElements();
|
|
236
|
+
if (!first) return false;
|
|
237
|
+
const active = document.activeElement;
|
|
238
|
+
if (active instanceof HTMLElement && !regionRef?.contains(active)) previousFocus = active;
|
|
239
|
+
first.focus();
|
|
240
|
+
return true;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
function restorePreviousFocus() {
|
|
244
|
+
const target = previousFocus;
|
|
245
|
+
previousFocus = null;
|
|
246
|
+
if (target && target.isConnected) {
|
|
247
|
+
target.focus();
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
(document.activeElement as HTMLElement | null)?.blur?.();
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
// `F6` is the key that moves between the panes of an app. From anywhere on the page, it
|
|
254
|
+
// lands on the first toast; from the region, it goes back.
|
|
255
|
+
function handleWindowKeyDown(event: KeyboardEvent) {
|
|
256
|
+
if (event.key !== 'F6' || event.defaultPrevented) return;
|
|
257
|
+
const active = document.activeElement;
|
|
258
|
+
if (active instanceof HTMLElement && regionRef?.contains(active)) {
|
|
259
|
+
event.preventDefault();
|
|
260
|
+
restorePreviousFocus();
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
263
|
+
if (focusFirstToast()) event.preventDefault();
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
function focusAfterClose(id: string) {
|
|
267
|
+
const active = document.activeElement;
|
|
268
|
+
const closing = regionRef?.querySelector<HTMLElement>(
|
|
269
|
+
`[data-toast-root][data-toast-id="${id}"]`
|
|
270
|
+
);
|
|
271
|
+
if (!closing || !(active instanceof Node) || !closing.contains(active)) return;
|
|
272
|
+
// The closing toast may already be on its way out: the list keeps it, thus the next one is
|
|
273
|
+
// the one after it in the order, and the one before it when it was last.
|
|
274
|
+
const all = Array.from(
|
|
275
|
+
regionRef?.querySelectorAll<HTMLElement>('[data-toast-root]:not([inert])') ?? []
|
|
276
|
+
);
|
|
277
|
+
const open = (candidate: HTMLElement | undefined) =>
|
|
278
|
+
candidate && candidate !== closing && !candidate.hasAttribute('data-ending')
|
|
279
|
+
? candidate
|
|
280
|
+
: null;
|
|
281
|
+
const index = all.indexOf(closing);
|
|
282
|
+
const next =
|
|
283
|
+
all
|
|
284
|
+
.slice(index + 1)
|
|
285
|
+
.map(open)
|
|
286
|
+
.find(Boolean) ?? all.slice(0, Math.max(0, index)).reverse().map(open).find(Boolean);
|
|
287
|
+
if (next) {
|
|
288
|
+
next.focus();
|
|
289
|
+
return;
|
|
290
|
+
}
|
|
291
|
+
restorePreviousFocus();
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
$effect(() => {
|
|
295
|
+
ctx.setFocusAfterClose(focusAfterClose);
|
|
296
|
+
return () => ctx.setFocusAfterClose(null);
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
// A `Tab` past the last button of the last toast, or a `Shift+Tab` before the first one, goes
|
|
300
|
+
// back to where the focus was. The region is at the end of the body, and the page after it is
|
|
301
|
+
// nothing.
|
|
302
|
+
function handleKeyDown(event: KeyboardEvent & { currentTarget: EventTarget & HTMLDivElement }) {
|
|
303
|
+
onkeydown?.(event);
|
|
304
|
+
if (event.defaultPrevented || event.key !== 'Tab' || !regionRef) return;
|
|
305
|
+
const tabbables = getFocusableElements(regionRef);
|
|
306
|
+
if (tabbables.length === 0) return;
|
|
307
|
+
const active = document.activeElement;
|
|
308
|
+
const first = tabbables[0];
|
|
309
|
+
const last = tabbables[tabbables.length - 1];
|
|
310
|
+
if ((event.shiftKey && active === first) || (!event.shiftKey && active === last)) {
|
|
311
|
+
event.preventDefault();
|
|
312
|
+
restorePreviousFocus();
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
// The region leaves with the last toast, and a tap does not outlive it.
|
|
317
|
+
$effect(() => {
|
|
318
|
+
if (manager.toasts.length > 0 || !ctx.tapped) return;
|
|
319
|
+
untrack(() => {
|
|
320
|
+
ctx.setTapped(false);
|
|
321
|
+
syncTimers();
|
|
322
|
+
});
|
|
323
|
+
});
|
|
324
|
+
|
|
325
|
+
$effect(() => {
|
|
326
|
+
window.addEventListener('keydown', handleWindowKeyDown);
|
|
327
|
+
document.addEventListener('visibilitychange', handleVisibilityChange);
|
|
328
|
+
document.addEventListener('pointerdown', handleDocumentPointerDown, true);
|
|
329
|
+
untrack(syncTimers);
|
|
330
|
+
return () => {
|
|
331
|
+
document.removeEventListener('pointerdown', handleDocumentPointerDown, true);
|
|
332
|
+
document.removeEventListener('visibilitychange', handleVisibilityChange);
|
|
333
|
+
window.removeEventListener('keydown', handleWindowKeyDown);
|
|
334
|
+
};
|
|
335
|
+
});
|
|
336
|
+
|
|
337
|
+
const frontmostHeight = $derived.by(() => {
|
|
338
|
+
const front = manager.stackedToasts[0];
|
|
339
|
+
return front ? (ctx.heights.get(front.id) ?? null) : null;
|
|
340
|
+
});
|
|
341
|
+
const exemptAttribute = { [HIDE_OUTSIDE_EXEMPT_ATTRIBUTE]: '' };
|
|
342
|
+
</script>
|
|
343
|
+
|
|
344
|
+
{#snippet viewport()}
|
|
345
|
+
<div role="status" aria-live="polite" aria-relevant="additions" style={visuallyHiddenStyle}>
|
|
346
|
+
{#each politeMessages as message (message.key)}
|
|
347
|
+
<div>{message.text}</div>
|
|
348
|
+
{/each}
|
|
349
|
+
</div>
|
|
350
|
+
<div role="alert" aria-live="assertive" aria-relevant="additions" style={visuallyHiddenStyle}>
|
|
351
|
+
{#each assertiveMessages as message (message.key)}
|
|
352
|
+
<div>{message.text}</div>
|
|
353
|
+
{/each}
|
|
354
|
+
</div>
|
|
355
|
+
{#if manager.toasts.length > 0}
|
|
356
|
+
<div
|
|
357
|
+
{...restProps}
|
|
358
|
+
{...exemptAttribute}
|
|
359
|
+
bind:this={regionRef}
|
|
360
|
+
role="region"
|
|
361
|
+
tabindex="-1"
|
|
362
|
+
aria-label={ariaLabel}
|
|
363
|
+
class={className}
|
|
364
|
+
style="--toast-frontmost-height: {frontmostHeight === null
|
|
365
|
+
? ''
|
|
366
|
+
: `${frontmostHeight}px`};{restProps.style ? ` ${restProps.style}` : ''}"
|
|
367
|
+
data-toast-viewport="true"
|
|
368
|
+
data-expanded={ctx.expanded || undefined}
|
|
369
|
+
onpointerenter={handlePointerEnter}
|
|
370
|
+
onpointerleave={handlePointerLeave}
|
|
371
|
+
onpointerdown={handlePointerDown}
|
|
372
|
+
onpointerup={handlePointerUp}
|
|
373
|
+
onpointercancel={handlePointerCancel}
|
|
374
|
+
onfocusin={handleFocusIn}
|
|
375
|
+
onfocusout={handleFocusOut}
|
|
376
|
+
onkeydown={handleKeyDown}
|
|
377
|
+
>
|
|
378
|
+
{#each manager.toasts as toast (toast.id)}
|
|
379
|
+
{@render children(toast)}
|
|
380
|
+
{/each}
|
|
381
|
+
</div>
|
|
382
|
+
{/if}
|
|
383
|
+
{/snippet}
|
|
384
|
+
|
|
385
|
+
{#if portal}
|
|
386
|
+
<Portal>{@render viewport()}</Portal>
|
|
387
|
+
{:else}
|
|
388
|
+
{@render viewport()}
|
|
389
|
+
{/if}
|
|
@@ -21,13 +21,7 @@
|
|
|
21
21
|
shouldSkipDelay,
|
|
22
22
|
unregisterOpenTooltip
|
|
23
23
|
} from './tooltip-group';
|
|
24
|
-
import {
|
|
25
|
-
buildSafePolygon,
|
|
26
|
-
isPointInPolygon,
|
|
27
|
-
isPointInRect,
|
|
28
|
-
type Point,
|
|
29
|
-
type Side
|
|
30
|
-
} from './safe-polygon';
|
|
24
|
+
import { buildSafePolygon, isPointInPolygon, type Point, type Side } from './safe-polygon';
|
|
31
25
|
|
|
32
26
|
/**
|
|
33
27
|
* Tooltip.Root — the open state, the delays, and the interaction with the trigger.
|
|
@@ -164,7 +158,11 @@
|
|
|
164
158
|
$effect(() => {
|
|
165
159
|
if (!isOpen) return;
|
|
166
160
|
registerOpenTooltip(groupEntry);
|
|
167
|
-
return () =>
|
|
161
|
+
return () => {
|
|
162
|
+
unregisterOpenTooltip(groupEntry);
|
|
163
|
+
// The panel may go with the pointer still on it, and no `pointerleave` follows.
|
|
164
|
+
pointerOnContent = false;
|
|
165
|
+
};
|
|
168
166
|
});
|
|
169
167
|
|
|
170
168
|
$effect(() => {
|
|
@@ -252,8 +250,12 @@
|
|
|
252
250
|
function handleGapPointerMove(event: PointerEvent) {
|
|
253
251
|
if (!gapPolygon) return;
|
|
254
252
|
const point = { x: event.clientX, y: event.clientY };
|
|
255
|
-
|
|
256
|
-
|
|
253
|
+
// What the pointer is on, as the browser hit-tests it, not the bounding box: the corner of a
|
|
254
|
+
// rounded panel is inside its box but not on it, and no `pointerenter` comes from there to
|
|
255
|
+
// take over — tracking stopped on it left the tooltip open for good.
|
|
256
|
+
const target = event.target instanceof Node ? event.target : null;
|
|
257
|
+
const onContent = target !== null && (contentRef?.contains(target) ?? false);
|
|
258
|
+
const onTrigger = target !== null && (triggerRef?.contains(target) ?? false);
|
|
257
259
|
if (onContent || onTrigger) {
|
|
258
260
|
// The `pointerenter` of the content or the trigger takes over from here.
|
|
259
261
|
stopGapTracking();
|
|
@@ -326,7 +328,10 @@
|
|
|
326
328
|
}
|
|
327
329
|
|
|
328
330
|
function handleContentPointerEnter(event: PointerEvent) {
|
|
329
|
-
|
|
331
|
+
// A closed panel is still in the DOM through its exit animation, and a pointer that lands
|
|
332
|
+
// on it then is not reading it: counted, it would hold the next open forever, because the
|
|
333
|
+
// panel leaves the DOM under the pointer and its `pointerleave` never comes.
|
|
334
|
+
if (event.pointerType === 'touch' || !isOpen) return;
|
|
330
335
|
pointerOnContent = true;
|
|
331
336
|
stopGapTracking();
|
|
332
337
|
if (closeTimer !== null) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@human-kit/ui",
|
|
3
|
-
"version": "1.0.0
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"description": "Accessible, typed UI components for Svelte 5 — shipped as native ESM with per-component subpath exports so bundlers only include what you import.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Agustin Delgado <agustindariodelgado@gmail.com>",
|
|
@@ -128,6 +128,11 @@
|
|
|
128
128
|
"svelte": "./dist/timepicker/index.js",
|
|
129
129
|
"default": "./dist/timepicker/index.js"
|
|
130
130
|
},
|
|
131
|
+
"./toast": {
|
|
132
|
+
"types": "./dist/toast/index.d.ts",
|
|
133
|
+
"svelte": "./dist/toast/index.js",
|
|
134
|
+
"default": "./dist/toast/index.js"
|
|
135
|
+
},
|
|
131
136
|
"./dialog": {
|
|
132
137
|
"types": "./dist/dialog/index.d.ts",
|
|
133
138
|
"svelte": "./dist/dialog/index.js",
|
|
@@ -173,6 +178,26 @@
|
|
|
173
178
|
"svelte": "./dist/radio-group/index.js",
|
|
174
179
|
"default": "./dist/radio-group/index.js"
|
|
175
180
|
},
|
|
181
|
+
"./rating": {
|
|
182
|
+
"types": "./dist/rating/index.d.ts",
|
|
183
|
+
"svelte": "./dist/rating/index.js",
|
|
184
|
+
"default": "./dist/rating/index.js"
|
|
185
|
+
},
|
|
186
|
+
"./pin-input": {
|
|
187
|
+
"types": "./dist/pin-input/index.d.ts",
|
|
188
|
+
"svelte": "./dist/pin-input/index.js",
|
|
189
|
+
"default": "./dist/pin-input/index.js"
|
|
190
|
+
},
|
|
191
|
+
"./color-picker": {
|
|
192
|
+
"types": "./dist/color-picker/index.d.ts",
|
|
193
|
+
"svelte": "./dist/color-picker/index.js",
|
|
194
|
+
"default": "./dist/color-picker/index.js"
|
|
195
|
+
},
|
|
196
|
+
"./search-field": {
|
|
197
|
+
"types": "./dist/search-field/index.d.ts",
|
|
198
|
+
"svelte": "./dist/search-field/index.js",
|
|
199
|
+
"default": "./dist/search-field/index.js"
|
|
200
|
+
},
|
|
176
201
|
"./select": {
|
|
177
202
|
"types": "./dist/select/index.d.ts",
|
|
178
203
|
"svelte": "./dist/select/index.js",
|