@human-kit/ui 1.0.0-beta.8 → 1.0.0-beta.9
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/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/internal/localized-strings.d.ts +24 -0
- package/dist/internal/localized-strings.js +24 -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/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/package.json +6 -1
package/dist/index.d.ts
CHANGED
|
@@ -26,6 +26,7 @@ export { Switch } from './switch/index.js';
|
|
|
26
26
|
export { Tooltip } from './tooltip/index.js';
|
|
27
27
|
export { Table } from './table/index.js';
|
|
28
28
|
export { Tabs } from './tabs/index.js';
|
|
29
|
+
export { Toast } from './toast/index.js';
|
|
29
30
|
export { Toggle } from './toggle/index.js';
|
|
30
31
|
export { ToggleGroup } from './toggle-group/index.js';
|
|
31
32
|
export { TransferList } from './transfer-list/index.js';
|
|
@@ -69,6 +70,7 @@ export * from './switch/index.js';
|
|
|
69
70
|
export * from './tooltip/index.js';
|
|
70
71
|
export * from './table/index.js';
|
|
71
72
|
export * from './tabs/index.js';
|
|
73
|
+
export * from './toast/index.js';
|
|
72
74
|
export * from './toggle/index.js';
|
|
73
75
|
export * from './toggle-group/index.js';
|
|
74
76
|
export * from './transfer-list/index.js';
|
package/dist/index.js
CHANGED
|
@@ -28,6 +28,7 @@ export { Switch } from './switch/index.js';
|
|
|
28
28
|
export { Tooltip } from './tooltip/index.js';
|
|
29
29
|
export { Table } from './table/index.js';
|
|
30
30
|
export { Tabs } from './tabs/index.js';
|
|
31
|
+
export { Toast } from './toast/index.js';
|
|
31
32
|
export { Toggle } from './toggle/index.js';
|
|
32
33
|
export { ToggleGroup } from './toggle-group/index.js';
|
|
33
34
|
export { TransferList } from './transfer-list/index.js';
|
|
@@ -72,6 +73,7 @@ export * from './switch/index.js';
|
|
|
72
73
|
export * from './tooltip/index.js';
|
|
73
74
|
export * from './table/index.js';
|
|
74
75
|
export * from './tabs/index.js';
|
|
76
|
+
export * from './toast/index.js';
|
|
75
77
|
export * from './toggle/index.js';
|
|
76
78
|
export * from './toggle-group/index.js';
|
|
77
79
|
export * from './transfer-list/index.js';
|
|
@@ -360,6 +360,30 @@ declare const LOCALIZED_STRINGS: {
|
|
|
360
360
|
readonly de: "Leer";
|
|
361
361
|
readonly it: "Vuoto";
|
|
362
362
|
};
|
|
363
|
+
readonly 'toast.notifications': {
|
|
364
|
+
readonly en: "Notifications";
|
|
365
|
+
readonly es: "Notificaciones";
|
|
366
|
+
readonly pt: "Notificações";
|
|
367
|
+
readonly fr: "Notifications";
|
|
368
|
+
readonly de: "Benachrichtigungen";
|
|
369
|
+
readonly it: "Notifiche";
|
|
370
|
+
};
|
|
371
|
+
readonly 'toast.oneNotification': {
|
|
372
|
+
readonly en: "1 notification";
|
|
373
|
+
readonly es: "1 notificación";
|
|
374
|
+
readonly pt: "1 notificação";
|
|
375
|
+
readonly fr: "1 notification";
|
|
376
|
+
readonly de: "1 Benachrichtigung";
|
|
377
|
+
readonly it: "1 notifica";
|
|
378
|
+
};
|
|
379
|
+
readonly 'toast.multipleNotifications': {
|
|
380
|
+
readonly en: "{count} notifications";
|
|
381
|
+
readonly es: "{count} notificaciones";
|
|
382
|
+
readonly pt: "{count} notificações";
|
|
383
|
+
readonly fr: "{count} notifications";
|
|
384
|
+
readonly de: "{count} Benachrichtigungen";
|
|
385
|
+
readonly it: "{count} notifiche";
|
|
386
|
+
};
|
|
363
387
|
};
|
|
364
388
|
export type LocalizedStringKey = keyof typeof LOCALIZED_STRINGS;
|
|
365
389
|
export declare function resolveLocalizedString(locale: string | undefined, key: LocalizedStringKey, params?: Record<string, string | number>): string;
|
|
@@ -359,6 +359,30 @@ const LOCALIZED_STRINGS = {
|
|
|
359
359
|
fr: 'Vide',
|
|
360
360
|
de: 'Leer',
|
|
361
361
|
it: 'Vuoto'
|
|
362
|
+
},
|
|
363
|
+
'toast.notifications': {
|
|
364
|
+
en: 'Notifications',
|
|
365
|
+
es: 'Notificaciones',
|
|
366
|
+
pt: 'Notificações',
|
|
367
|
+
fr: 'Notifications',
|
|
368
|
+
de: 'Benachrichtigungen',
|
|
369
|
+
it: 'Notifiche'
|
|
370
|
+
},
|
|
371
|
+
'toast.oneNotification': {
|
|
372
|
+
en: '1 notification',
|
|
373
|
+
es: '1 notificación',
|
|
374
|
+
pt: '1 notificação',
|
|
375
|
+
fr: '1 notification',
|
|
376
|
+
de: '1 Benachrichtigung',
|
|
377
|
+
it: '1 notifica'
|
|
378
|
+
},
|
|
379
|
+
'toast.multipleNotifications': {
|
|
380
|
+
en: '{count} notifications',
|
|
381
|
+
es: '{count} notificaciones',
|
|
382
|
+
pt: '{count} notificações',
|
|
383
|
+
fr: '{count} notifications',
|
|
384
|
+
de: '{count} Benachrichtigungen',
|
|
385
|
+
it: '{count} notifiche'
|
|
362
386
|
}
|
|
363
387
|
};
|
|
364
388
|
function resolveLanguage(locale) {
|
|
@@ -7,6 +7,19 @@
|
|
|
7
7
|
* hides nodes appended to the body afterwards (portalled content, toasts), with
|
|
8
8
|
* two exemptions (see `EXEMPT_SELECTOR`).
|
|
9
9
|
*/
|
|
10
|
+
/**
|
|
11
|
+
* Live regions are exempt from hiding ENTIRELY (neither `aria-hidden` nor `inert` is applied,
|
|
12
|
+
* and their subtree is left untouched): `aria-hidden` would silence their announcements, and
|
|
13
|
+
* `inert` would block interacting with e.g. a toast's dismiss button. The trade-off — a live
|
|
14
|
+
* region staying reachable behind a modal — is intentional: status/alert content is transient
|
|
15
|
+
* and announcing it is the whole point.
|
|
16
|
+
*/
|
|
17
|
+
/**
|
|
18
|
+
* An element that opts out of the hide, without being a live region itself: a toast viewport,
|
|
19
|
+
* whose toasts are announced by a live region beside it, but whose buttons must stay reachable
|
|
20
|
+
* behind a modal.
|
|
21
|
+
*/
|
|
22
|
+
export declare const HIDE_OUTSIDE_EXEMPT_ATTRIBUTE = "data-hk-hide-outside-exempt";
|
|
10
23
|
interface HideOutsideResult {
|
|
11
24
|
/** Call this to restore the original state */
|
|
12
25
|
restore: () => void;
|
|
@@ -15,7 +15,13 @@ import { TOP_LAYER_SELECTOR } from './click-outside';
|
|
|
15
15
|
* region staying reachable behind a modal — is intentional: status/alert content is transient
|
|
16
16
|
* and announcing it is the whole point.
|
|
17
17
|
*/
|
|
18
|
-
|
|
18
|
+
/**
|
|
19
|
+
* An element that opts out of the hide, without being a live region itself: a toast viewport,
|
|
20
|
+
* whose toasts are announced by a live region beside it, but whose buttons must stay reachable
|
|
21
|
+
* behind a modal.
|
|
22
|
+
*/
|
|
23
|
+
export const HIDE_OUTSIDE_EXEMPT_ATTRIBUTE = 'data-hk-hide-outside-exempt';
|
|
24
|
+
const LIVE_REGION_SELECTOR = `[aria-live], [role="status"], [role="alert"], [role="log"], [${HIDE_OUTSIDE_EXEMPT_ATTRIBUTE}]`;
|
|
19
25
|
/**
|
|
20
26
|
* Elements the MutationObserver must never hide when they appear while a modal is active:
|
|
21
27
|
* - top layers (dialog/popover/menu — the shared `TOP_LAYER_SELECTOR` from click-outside):
|
|
@@ -7,6 +7,8 @@ export type FocusTrapOptions = {
|
|
|
7
7
|
restoreFocus?: boolean;
|
|
8
8
|
initialFocus?: HTMLElement | string | (() => HTMLElement | null | undefined);
|
|
9
9
|
};
|
|
10
|
+
/** The elements in `container` that a `Tab` can reach, in DOM order. */
|
|
11
|
+
export declare function getFocusableElements(container: HTMLElement): HTMLElement[];
|
|
10
12
|
/**
|
|
11
13
|
* Svelte action that traps focus within an element.
|
|
12
14
|
*
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
* Focus trap primitive.
|
|
3
3
|
* Traps keyboard focus within a container element.
|
|
4
4
|
*/
|
|
5
|
+
import { HIDE_OUTSIDE_EXEMPT_ATTRIBUTE } from './aria-hide-outside';
|
|
5
6
|
import { focusWithModality, getInteractionModality } from './input-modality';
|
|
6
7
|
const FOCUSABLE_SELECTOR = [
|
|
7
8
|
'a[href]',
|
|
@@ -49,7 +50,8 @@ function resolveInitialFocus(container, initialFocus) {
|
|
|
49
50
|
}
|
|
50
51
|
return initialFocus;
|
|
51
52
|
}
|
|
52
|
-
|
|
53
|
+
/** The elements in `container` that a `Tab` can reach, in DOM order. */
|
|
54
|
+
export function getFocusableElements(container) {
|
|
53
55
|
return Array.from(container.querySelectorAll(FOCUSABLE_SELECTOR)).filter((element) =>
|
|
54
56
|
// getClientRects covers display:none and detached nodes while still
|
|
55
57
|
// accepting position:fixed descendants (whose offsetParent is null).
|
|
@@ -81,6 +83,10 @@ export function focusTrap(node, options = true) {
|
|
|
81
83
|
// nested overlay must not intercept it.
|
|
82
84
|
if (!isTopmostTrap(node))
|
|
83
85
|
return;
|
|
86
|
+
// A surface that stays reachable behind the modal, such as a toast viewport, manages its
|
|
87
|
+
// own Tab while the focus is in it, and sends the focus back on its own.
|
|
88
|
+
if (document.activeElement?.closest(`[${HIDE_OUTSIDE_EXEMPT_ATTRIBUTE}]`))
|
|
89
|
+
return;
|
|
84
90
|
const focusableElements = getFocusableElements(node);
|
|
85
91
|
if (focusableElements.length === 0) {
|
|
86
92
|
event.preventDefault();
|
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
* Must be called during component initialization — it wires its own `$effect`s, so the machine
|
|
10
10
|
* starts on mount and tears down (canceling any in-flight motion tracker) on destroy.
|
|
11
11
|
*/
|
|
12
|
+
import { untrack } from 'svelte';
|
|
12
13
|
import { trackMotionEnd } from './motion';
|
|
13
14
|
/**
|
|
14
15
|
* Creates a presence machine driven by `getOpen`, timing enter/exit phases from `getElement`'s
|
|
@@ -20,7 +21,11 @@ import { trackMotionEnd } from './motion';
|
|
|
20
21
|
*/
|
|
21
22
|
export function createPresence(getOpen, getElement, options = {}) {
|
|
22
23
|
let isMounted = $state(options.initiallyMounted ?? false);
|
|
23
|
-
|
|
24
|
+
// A node that is in the DOM from the first render, such as a toast, must paint its first frame
|
|
25
|
+
// in the enter state. The effect below runs after the mount, and a layout read in between
|
|
26
|
+
// (a measure, a floating position) would fix the final styles first: the enter would then
|
|
27
|
+
// play backwards before it plays forwards. So the enter state starts here, at creation.
|
|
28
|
+
let isEntering = $state(!(options.initiallyMounted ?? false) && untrack(getOpen));
|
|
24
29
|
let isExiting = $state(false);
|
|
25
30
|
let tracker;
|
|
26
31
|
function clearTracker() {
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# Toast
|
|
2
|
+
|
|
3
|
+
## Description
|
|
4
|
+
|
|
5
|
+
`Toast` shows a short message about an event, for a time, without a stop of the work. The provider holds the list, and the viewport is the landmark where the toasts land. A screen reader hears each message from a live region beside them.
|
|
6
|
+
|
|
7
|
+
## Anatomy
|
|
8
|
+
|
|
9
|
+
- `Toast.Provider`
|
|
10
|
+
- `Toast.Viewport`
|
|
11
|
+
- `Toast.Positioner`
|
|
12
|
+
- `Toast.Root`
|
|
13
|
+
- `Toast.Content`
|
|
14
|
+
- `Toast.Title`
|
|
15
|
+
- `Toast.Description`
|
|
16
|
+
- `Toast.Action`
|
|
17
|
+
- `Toast.Close`
|
|
18
|
+
|
|
19
|
+
```svelte
|
|
20
|
+
<Toast.Provider>
|
|
21
|
+
<App />
|
|
22
|
+
<Toast.Viewport>
|
|
23
|
+
{#snippet children(toast)}
|
|
24
|
+
<Toast.Root {toast}>
|
|
25
|
+
<Toast.Content>
|
|
26
|
+
<Toast.Title />
|
|
27
|
+
<Toast.Description />
|
|
28
|
+
</Toast.Content>
|
|
29
|
+
<Toast.Close aria-label="Close">x</Toast.Close>
|
|
30
|
+
</Toast.Root>
|
|
31
|
+
{/snippet}
|
|
32
|
+
</Toast.Viewport>
|
|
33
|
+
</Toast.Provider>
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## Usage guidelines
|
|
37
|
+
|
|
38
|
+
- Put one `Toast.Provider` around the app, and one `Toast.Viewport` in it.
|
|
39
|
+
- Call `useToastManager()` in a component under the provider, or `bind:manager` on the provider, to `add`, `update`, `close` and `promise`.
|
|
40
|
+
- Give `title` and `description` as text: that text is the announcement. The parts show it without children.
|
|
41
|
+
- Keep the buttons out of `Toast.Content`, thus the announcement does not read them.
|
|
42
|
+
- Use `priority: 'high'` only for a message that cannot wait. It interrupts the screen reader.
|
|
43
|
+
- Keep the default `timeout` at 5 seconds or more. A user who reads slowly needs the time.
|
|
44
|
+
- Give `anchor` to `add`, and put `Toast.Positioner` around the root, for a toast against an element.
|
|
45
|
+
- Style the stack with `--toast-index`, `--toast-offset-y`, `--toast-height`, `--toast-frontmost-height`, `data-front` and `data-expanded`. Move the toast with `--toast-swipe-movement-x` and `--toast-swipe-movement-y`.
|
|
46
|
+
- A toast that closes is out of the stack at once, and it keeps the place it had for its exit. It drops `data-front`, thus a rule that hides the toasts behind needs `:not([data-ending])`.
|
|
47
|
+
- Move the enter motion with `translate`, and leave `transform` to the place in the stack. A toast pushed back mid-enter then slides.
|
|
48
|
+
- A toast past the limit waits in the place behind the stack, and it comes forward one step when a place frees.
|
|
49
|
+
- Cover the gap between two spread toasts with a pseudo-element, or the pointer leaves the viewport between them.
|
|
50
|
+
|
|
51
|
+
## API reference
|
|
52
|
+
|
|
53
|
+
- `Toast.Provider`
|
|
54
|
+
- `timeout?: number` (5000), `limit?: number` (3)
|
|
55
|
+
- `manager?: ToastManager` (bindable)
|
|
56
|
+
- `Toast.Viewport`
|
|
57
|
+
- `children: Snippet<[ToastItem]>`, `portal?: boolean` (true)
|
|
58
|
+
- `Toast.Positioner`
|
|
59
|
+
- `toast: ToastItem`
|
|
60
|
+
- `Toast.Root`
|
|
61
|
+
- `toast: ToastItem`, `swipeDirection?: SwipeSide[]` (`['bottom', 'right']`)
|
|
62
|
+
- `Toast.Action`
|
|
63
|
+
- `keepOpen?: boolean`
|
|
64
|
+
- `ToastManager`
|
|
65
|
+
- `add({ title, description, type, timeout, priority, data, anchor, placement, offset }) => id`, `update(id, options | (toast) => options)`, `close(id?)`, `promise(promise, { loading, success, error })`
|
|
66
|
+
- `pauseTimers()`, `resumeTimers()`, `toasts`, `visibleToasts`
|
|
67
|
+
|
|
68
|
+
## Accessibility
|
|
69
|
+
|
|
70
|
+
- `Toast.Viewport` is a `role="region"` landmark, named with the count of toasts. `F6` moves the focus into it from anywhere on the page, and back.
|
|
71
|
+
- Two live regions beside the viewport announce each toast: `role="status"` for the normal priority, and `role="alert"` for the high priority. The message is the title and the description.
|
|
72
|
+
- `Toast.Root` is a `role="dialog"` that is not modal, or an `alertdialog` for the high priority. It has `aria-labelledby` from the title and `aria-describedby` from the description, and it is a tab stop.
|
|
73
|
+
- `Escape` closes the focused toast, and the focus moves to the next toast, or back to where it was.
|
|
74
|
+
- The timers stop while the pointer rests on the viewport, and while the focus is in it. A tap on a toast, not a swipe, holds the stack open, and the timers with it, until a touch outside the viewport. They also stop while the tab is hidden.
|
|
75
|
+
- The viewport stays reachable behind a modal dialog, with `F6` and `Tab`.
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { ButtonRoot } from '../../button/index.js';
|
|
3
|
+
import { SWIPE_IGNORE_ATTRIBUTE } from '../../primitives/swipe-gesture';
|
|
4
|
+
import { useToastContext } from '../root/context';
|
|
5
|
+
import type { ToastActionProps } from '../types.js';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Toast.Action — a button that does the one thing the toast offers, such as `Undo`.
|
|
9
|
+
*
|
|
10
|
+
* The press closes the toast, because the offer is taken. `keepOpen` holds it for an action
|
|
11
|
+
* that changes the toast instead. A swipe never starts on it.
|
|
12
|
+
*/
|
|
13
|
+
let {
|
|
14
|
+
children,
|
|
15
|
+
class: className = '',
|
|
16
|
+
keepOpen = false,
|
|
17
|
+
element = $bindable<HTMLButtonElement | null>(null),
|
|
18
|
+
onclick,
|
|
19
|
+
...restProps
|
|
20
|
+
}: ToastActionProps = $props();
|
|
21
|
+
|
|
22
|
+
const ctx = useToastContext('Toast.Action');
|
|
23
|
+
const swipeIgnore = { [SWIPE_IGNORE_ATTRIBUTE]: '' };
|
|
24
|
+
|
|
25
|
+
function handleClick(event: MouseEvent & { currentTarget: EventTarget & HTMLButtonElement }) {
|
|
26
|
+
onclick?.(event);
|
|
27
|
+
if (event.defaultPrevented || keepOpen) return;
|
|
28
|
+
ctx.close();
|
|
29
|
+
}
|
|
30
|
+
</script>
|
|
31
|
+
|
|
32
|
+
<ButtonRoot
|
|
33
|
+
{...restProps}
|
|
34
|
+
{...swipeIgnore}
|
|
35
|
+
bind:element
|
|
36
|
+
class={className}
|
|
37
|
+
type="button"
|
|
38
|
+
data-toast-action="true"
|
|
39
|
+
onclick={handleClick}
|
|
40
|
+
>
|
|
41
|
+
{@render children?.()}
|
|
42
|
+
</ButtonRoot>
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { ButtonRoot } from '../../button/index.js';
|
|
3
|
+
import { SWIPE_IGNORE_ATTRIBUTE } from '../../primitives/swipe-gesture';
|
|
4
|
+
import { useToastContext } from '../root/context';
|
|
5
|
+
import type { ToastCloseProps } from '../types.js';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Toast.Close — a button that closes the toast.
|
|
9
|
+
*
|
|
10
|
+
* Put it outside `Toast.Content`, thus the announcement of the toast does not read it. Give
|
|
11
|
+
* it a name, in its text or in `aria-label`. A swipe never starts on it.
|
|
12
|
+
*/
|
|
13
|
+
let {
|
|
14
|
+
children,
|
|
15
|
+
class: className = '',
|
|
16
|
+
element = $bindable<HTMLButtonElement | null>(null),
|
|
17
|
+
onclick,
|
|
18
|
+
...restProps
|
|
19
|
+
}: ToastCloseProps = $props();
|
|
20
|
+
|
|
21
|
+
const ctx = useToastContext('Toast.Close');
|
|
22
|
+
const swipeIgnore = { [SWIPE_IGNORE_ATTRIBUTE]: '' };
|
|
23
|
+
|
|
24
|
+
function handleClick(event: MouseEvent & { currentTarget: EventTarget & HTMLButtonElement }) {
|
|
25
|
+
onclick?.(event);
|
|
26
|
+
if (event.defaultPrevented) return;
|
|
27
|
+
ctx.close();
|
|
28
|
+
}
|
|
29
|
+
</script>
|
|
30
|
+
|
|
31
|
+
<ButtonRoot
|
|
32
|
+
{...restProps}
|
|
33
|
+
{...swipeIgnore}
|
|
34
|
+
bind:element
|
|
35
|
+
class={className}
|
|
36
|
+
type="button"
|
|
37
|
+
data-toast-close="true"
|
|
38
|
+
onclick={handleClick}
|
|
39
|
+
>
|
|
40
|
+
{@render children?.()}
|
|
41
|
+
</ButtonRoot>
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { useToastContext } from '../root/context';
|
|
3
|
+
import type { ToastContentProps } from '../types.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Toast.Content — the message: the title and the description.
|
|
7
|
+
*
|
|
8
|
+
* Keep the buttons out of it. The message is what the viewport announces, and the buttons
|
|
9
|
+
* are what the user presses after it.
|
|
10
|
+
*/
|
|
11
|
+
let { children, class: className = '', ...restProps }: ToastContentProps = $props();
|
|
12
|
+
|
|
13
|
+
const ctx = useToastContext('Toast.Content');
|
|
14
|
+
</script>
|
|
15
|
+
|
|
16
|
+
<div
|
|
17
|
+
{...restProps}
|
|
18
|
+
class={className}
|
|
19
|
+
data-toast-content="true"
|
|
20
|
+
data-type={ctx.toast.type}
|
|
21
|
+
data-priority={ctx.toast.priority}
|
|
22
|
+
>
|
|
23
|
+
{@render children?.()}
|
|
24
|
+
</div>
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { useToastContext } from '../root/context';
|
|
3
|
+
import type { ToastDescriptionProps } from '../types.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Toast.Description — the message of the toast.
|
|
7
|
+
*
|
|
8
|
+
* It registers its id with the root, which gives it to the toast as `aria-describedby`.
|
|
9
|
+
* Without children, it shows the `description` of the item.
|
|
10
|
+
*/
|
|
11
|
+
let {
|
|
12
|
+
children,
|
|
13
|
+
class: className = '',
|
|
14
|
+
id: idProp,
|
|
15
|
+
...restProps
|
|
16
|
+
}: ToastDescriptionProps = $props();
|
|
17
|
+
|
|
18
|
+
const ctx = useToastContext('Toast.Description');
|
|
19
|
+
const generatedId = $props.id();
|
|
20
|
+
const id = $derived(idProp ?? generatedId);
|
|
21
|
+
|
|
22
|
+
// Without children and without a `description` on the item there is nothing to say: the
|
|
23
|
+
// part renders nothing, and it describes nothing.
|
|
24
|
+
const hasContent = $derived(Boolean(children) || Boolean(ctx.toast.description));
|
|
25
|
+
|
|
26
|
+
// The returned unregister runs on destroy, and again whenever the id changes.
|
|
27
|
+
$effect(() => {
|
|
28
|
+
if (!hasContent) return;
|
|
29
|
+
return ctx.registerDescription(id);
|
|
30
|
+
});
|
|
31
|
+
</script>
|
|
32
|
+
|
|
33
|
+
{#if hasContent}
|
|
34
|
+
<p {...restProps} {id} class={className} data-toast-description="true">
|
|
35
|
+
{#if children}
|
|
36
|
+
{@render children()}
|
|
37
|
+
{:else}
|
|
38
|
+
{ctx.toast.description}
|
|
39
|
+
{/if}
|
|
40
|
+
</p>
|
|
41
|
+
{/if}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { ComponentProps } from 'svelte';
|
|
2
|
+
import type ToastActionComponent from './action/toast-action.svelte';
|
|
3
|
+
import type ToastCloseComponent from './close/toast-close.svelte';
|
|
4
|
+
import type ToastContentComponent from './content/toast-content.svelte';
|
|
5
|
+
import type ToastDescriptionComponent from './description/toast-description.svelte';
|
|
6
|
+
import type ToastPositionerComponent from './positioner/toast-positioner.svelte';
|
|
7
|
+
import type ToastProviderComponent from './provider/toast-provider.svelte';
|
|
8
|
+
import type ToastRootComponent from './root/toast-root.svelte';
|
|
9
|
+
import type ToastTitleComponent from './title/toast-title.svelte';
|
|
10
|
+
import type ToastViewportComponent from './viewport/toast-viewport.svelte';
|
|
11
|
+
export * as Toast from './index.parts.js';
|
|
12
|
+
export { default as ToastProvider } from './provider/toast-provider.svelte';
|
|
13
|
+
export { default as ToastViewport } from './viewport/toast-viewport.svelte';
|
|
14
|
+
export { default as ToastPositioner } from './positioner/toast-positioner.svelte';
|
|
15
|
+
export { default as ToastRoot } from './root/toast-root.svelte';
|
|
16
|
+
export { default as ToastContent } from './content/toast-content.svelte';
|
|
17
|
+
export { default as ToastTitle } from './title/toast-title.svelte';
|
|
18
|
+
export { default as ToastDescription } from './description/toast-description.svelte';
|
|
19
|
+
export { default as ToastClose } from './close/toast-close.svelte';
|
|
20
|
+
export { default as ToastAction } from './action/toast-action.svelte';
|
|
21
|
+
export type ToastProviderProps = ComponentProps<typeof ToastProviderComponent>;
|
|
22
|
+
export type ToastViewportProps = ComponentProps<typeof ToastViewportComponent>;
|
|
23
|
+
export type ToastPositionerProps = ComponentProps<typeof ToastPositionerComponent>;
|
|
24
|
+
export type ToastRootProps = ComponentProps<typeof ToastRootComponent>;
|
|
25
|
+
export type ToastContentProps = ComponentProps<typeof ToastContentComponent>;
|
|
26
|
+
export type ToastTitleProps = ComponentProps<typeof ToastTitleComponent>;
|
|
27
|
+
export type ToastDescriptionProps = ComponentProps<typeof ToastDescriptionComponent>;
|
|
28
|
+
export type ToastCloseProps = ComponentProps<typeof ToastCloseComponent>;
|
|
29
|
+
export type ToastActionProps = ComponentProps<typeof ToastActionComponent>;
|
|
30
|
+
export { getToastProviderContext, setToastProviderContext, useToastManager, useToastProviderContext, type ToastProviderContext } from './provider/context.js';
|
|
31
|
+
export { getToastContext, setToastContext, useToastContext, type ToastContext } from './root/context.js';
|
|
32
|
+
export { createToastManager, DEFAULT_TOAST_LIMIT, DEFAULT_TOAST_TIMEOUT, type ToastItem, type ToastManager, type ToastOptions, type ToastPriority, type ToastPromiseOptions, type ToastStatus, type ToastUpdate } from './provider/toast-manager.svelte.js';
|
|
33
|
+
import * as ToastParts from './index.parts.js';
|
|
34
|
+
export default ToastParts;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export * as Toast from './index.parts.js';
|
|
2
|
+
export { default as ToastProvider } from './provider/toast-provider.svelte';
|
|
3
|
+
export { default as ToastViewport } from './viewport/toast-viewport.svelte';
|
|
4
|
+
export { default as ToastPositioner } from './positioner/toast-positioner.svelte';
|
|
5
|
+
export { default as ToastRoot } from './root/toast-root.svelte';
|
|
6
|
+
export { default as ToastContent } from './content/toast-content.svelte';
|
|
7
|
+
export { default as ToastTitle } from './title/toast-title.svelte';
|
|
8
|
+
export { default as ToastDescription } from './description/toast-description.svelte';
|
|
9
|
+
export { default as ToastClose } from './close/toast-close.svelte';
|
|
10
|
+
export { default as ToastAction } from './action/toast-action.svelte';
|
|
11
|
+
export { getToastProviderContext, setToastProviderContext, useToastManager, useToastProviderContext } from './provider/context.js';
|
|
12
|
+
export { getToastContext, setToastContext, useToastContext } from './root/context.js';
|
|
13
|
+
export { createToastManager, DEFAULT_TOAST_LIMIT, DEFAULT_TOAST_TIMEOUT } from './provider/toast-manager.svelte.js';
|
|
14
|
+
import * as ToastParts from './index.parts.js';
|
|
15
|
+
export default ToastParts;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export { default as Provider } from './provider/toast-provider.svelte';
|
|
2
|
+
export { default as Viewport } from './viewport/toast-viewport.svelte';
|
|
3
|
+
export { default as Positioner } from './positioner/toast-positioner.svelte';
|
|
4
|
+
export { default as Root } from './root/toast-root.svelte';
|
|
5
|
+
export { default as Content } from './content/toast-content.svelte';
|
|
6
|
+
export { default as Title } from './title/toast-title.svelte';
|
|
7
|
+
export { default as Description } from './description/toast-description.svelte';
|
|
8
|
+
export { default as Close } from './close/toast-close.svelte';
|
|
9
|
+
export { default as Action } from './action/toast-action.svelte';
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export { default as Provider } from './provider/toast-provider.svelte';
|
|
2
|
+
export { default as Viewport } from './viewport/toast-viewport.svelte';
|
|
3
|
+
export { default as Positioner } from './positioner/toast-positioner.svelte';
|
|
4
|
+
export { default as Root } from './root/toast-root.svelte';
|
|
5
|
+
export { default as Content } from './content/toast-content.svelte';
|
|
6
|
+
export { default as Title } from './title/toast-title.svelte';
|
|
7
|
+
export { default as Description } from './description/toast-description.svelte';
|
|
8
|
+
export { default as Close } from './close/toast-close.svelte';
|
|
9
|
+
export { default as Action } from './action/toast-action.svelte';
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { floating, type ExtendedPlacement } from '../../primitives/floating';
|
|
3
|
+
import { getFloatingLayerZIndex } from '../../primitives/layer-stack';
|
|
4
|
+
import { useToastProviderContext } from '../provider/context';
|
|
5
|
+
import type { ToastPositionerProps } from '../types.js';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Toast.Positioner — puts a toast against the `anchor` of its item.
|
|
9
|
+
*
|
|
10
|
+
* Put it around `Toast.Root` in the viewport snippet. For a toast without an anchor, it is a
|
|
11
|
+
* plain wrapper, thus one snippet serves both kinds. For a toast with one, it is a fixed
|
|
12
|
+
* element that follows the anchor, flips when the space is not sufficient, and draws above
|
|
13
|
+
* the topmost dialog of the moment.
|
|
14
|
+
*/
|
|
15
|
+
let {
|
|
16
|
+
toast,
|
|
17
|
+
children,
|
|
18
|
+
class: className = '',
|
|
19
|
+
element = $bindable<HTMLDivElement | null>(null),
|
|
20
|
+
style,
|
|
21
|
+
...restProps
|
|
22
|
+
}: ToastPositionerProps = $props();
|
|
23
|
+
|
|
24
|
+
useToastProviderContext('Toast.Positioner');
|
|
25
|
+
|
|
26
|
+
let positionerRef: HTMLDivElement | null = $state(null);
|
|
27
|
+
let resolvedPlacement = $state<string | undefined>(undefined);
|
|
28
|
+
// Resolved when the toast appears, thus it draws above the topmost dialog of that moment.
|
|
29
|
+
const zIndex = getFloatingLayerZIndex();
|
|
30
|
+
|
|
31
|
+
const anchor = $derived(toast.anchor && toast.anchor.isConnected ? toast.anchor : null);
|
|
32
|
+
const placement = $derived<ExtendedPlacement>(toast.placement ?? 'top');
|
|
33
|
+
|
|
34
|
+
$effect(() => {
|
|
35
|
+
element = positionerRef;
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
function resolvePlacementSide(value: string) {
|
|
39
|
+
const side = value.split(/[-\s]/)[0];
|
|
40
|
+
return side === 'top' || side === 'right' || side === 'left' ? side : 'bottom';
|
|
41
|
+
}
|
|
42
|
+
</script>
|
|
43
|
+
|
|
44
|
+
{#if anchor}
|
|
45
|
+
<div
|
|
46
|
+
{...restProps}
|
|
47
|
+
bind:this={positionerRef}
|
|
48
|
+
class={className}
|
|
49
|
+
style="position: fixed; z-index: {zIndex};{style ? ` ${style}` : ''}"
|
|
50
|
+
data-toast-positioner="true"
|
|
51
|
+
data-anchored="true"
|
|
52
|
+
data-placement={resolvedPlacement}
|
|
53
|
+
use:floating={{
|
|
54
|
+
anchor,
|
|
55
|
+
placement,
|
|
56
|
+
offset: toast.offset ?? 8,
|
|
57
|
+
onPositionUpdate: (_, __, finalPlacement) => {
|
|
58
|
+
resolvedPlacement = resolvePlacementSide(finalPlacement);
|
|
59
|
+
}
|
|
60
|
+
}}
|
|
61
|
+
>
|
|
62
|
+
{@render children?.()}
|
|
63
|
+
</div>
|
|
64
|
+
{:else}
|
|
65
|
+
<!-- Out of the layout: the root keeps its place in the stack of the viewport. -->
|
|
66
|
+
<div
|
|
67
|
+
{...restProps}
|
|
68
|
+
bind:this={positionerRef}
|
|
69
|
+
class={className}
|
|
70
|
+
style="display: contents;{style ? ` ${style}` : ''}"
|
|
71
|
+
data-toast-positioner="true"
|
|
72
|
+
>
|
|
73
|
+
{@render children?.()}
|
|
74
|
+
</div>
|
|
75
|
+
{/if}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { InternalToastManager, ToastManager } from './toast-manager.svelte';
|
|
2
|
+
export type ToastProviderContext = {
|
|
3
|
+
manager: InternalToastManager;
|
|
4
|
+
/** The pointer rests on the viewport. */
|
|
5
|
+
readonly hovering: boolean;
|
|
6
|
+
/** The focus is in the viewport. */
|
|
7
|
+
readonly focused: boolean;
|
|
8
|
+
/** A touch tapped a toast, lifted where it landed, and no touch outside came after it. */
|
|
9
|
+
readonly tapped: boolean;
|
|
10
|
+
/**
|
|
11
|
+
* The toasts are spread out: the pointer rests on them, the focus is in them, or a touch
|
|
12
|
+
* tapped them.
|
|
13
|
+
*/
|
|
14
|
+
readonly expanded: boolean;
|
|
15
|
+
/** The viewport element. */
|
|
16
|
+
readonly viewportElement: HTMLElement | null;
|
|
17
|
+
/** The height of each toast on the screen, by id, for a stack. */
|
|
18
|
+
readonly heights: ReadonlyMap<string, number>;
|
|
19
|
+
setHovering: (hovering: boolean) => void;
|
|
20
|
+
setFocused: (focused: boolean) => void;
|
|
21
|
+
setTapped: (tapped: boolean) => void;
|
|
22
|
+
setViewportElement: (element: HTMLElement | null) => void;
|
|
23
|
+
setHeight: (id: string, height: number | null) => void;
|
|
24
|
+
/** Moves the focus to the next toast, or back to where it was before the viewport. */
|
|
25
|
+
focusAfterClose: (id: string) => void;
|
|
26
|
+
/** The viewport gives the function behind `focusAfterClose`. */
|
|
27
|
+
setFocusAfterClose: (handler: ((id: string) => void) | null) => void;
|
|
28
|
+
};
|
|
29
|
+
export declare function setToastProviderContext(context: ToastProviderContext): void;
|
|
30
|
+
export declare function getToastProviderContext(): ToastProviderContext | undefined;
|
|
31
|
+
export declare function useToastProviderContext(part?: string): ToastProviderContext;
|
|
32
|
+
/**
|
|
33
|
+
* The manager of the nearest `Toast.Provider`: `add`, `update`, `close` and `promise`. Call it
|
|
34
|
+
* in a component under the provider, at the start of its script.
|
|
35
|
+
*/
|
|
36
|
+
export declare function useToastManager<Data = unknown>(): ToastManager<Data>;
|