@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.
Files changed (134) hide show
  1. package/README.md +27 -60
  2. package/dist/FOCUS_STATE_CONTRACT.md +1 -0
  3. package/dist/color-picker/README.md +78 -0
  4. package/dist/color-picker/area/color-picker-area.svelte +195 -0
  5. package/dist/color-picker/area/color-picker-area.svelte.d.ts +4 -0
  6. package/dist/color-picker/area-thumb/color-picker-area-thumb.svelte +231 -0
  7. package/dist/color-picker/area-thumb/color-picker-area-thumb.svelte.d.ts +4 -0
  8. package/dist/color-picker/channel-field/color-picker-channel-field.svelte +95 -0
  9. package/dist/color-picker/channel-field/color-picker-channel-field.svelte.d.ts +4 -0
  10. package/dist/color-picker/eye-dropper/color-picker-eye-dropper.svelte +79 -0
  11. package/dist/color-picker/eye-dropper/color-picker-eye-dropper.svelte.d.ts +4 -0
  12. package/dist/color-picker/hex-field/color-picker-hex-field.svelte +105 -0
  13. package/dist/color-picker/hex-field/color-picker-hex-field.svelte.d.ts +4 -0
  14. package/dist/color-picker/index.d.ts +16 -0
  15. package/dist/color-picker/index.js +15 -0
  16. package/dist/color-picker/index.parts.d.ts +12 -0
  17. package/dist/color-picker/index.parts.js +12 -0
  18. package/dist/color-picker/label/color-picker-label.svelte +34 -0
  19. package/dist/color-picker/label/color-picker-label.svelte.d.ts +4 -0
  20. package/dist/color-picker/preview/color-picker-preview.svelte +43 -0
  21. package/dist/color-picker/preview/color-picker-preview.svelte.d.ts +4 -0
  22. package/dist/color-picker/root/color-picker-root.svelte +365 -0
  23. package/dist/color-picker/root/color-picker-root.svelte.d.ts +4 -0
  24. package/dist/color-picker/root/color.d.ts +68 -0
  25. package/dist/color-picker/root/color.js +266 -0
  26. package/dist/color-picker/root/context.d.ts +127 -0
  27. package/dist/color-picker/root/context.js +44 -0
  28. package/dist/color-picker/slider/color-picker-slider.svelte +188 -0
  29. package/dist/color-picker/slider/color-picker-slider.svelte.d.ts +4 -0
  30. package/dist/color-picker/slider-thumb/color-picker-slider-thumb.svelte +208 -0
  31. package/dist/color-picker/slider-thumb/color-picker-slider-thumb.svelte.d.ts +4 -0
  32. package/dist/color-picker/swatch/color-picker-swatch.svelte +149 -0
  33. package/dist/color-picker/swatch/color-picker-swatch.svelte.d.ts +4 -0
  34. package/dist/color-picker/swatch-list/color-picker-swatch-list.svelte +133 -0
  35. package/dist/color-picker/swatch-list/color-picker-swatch-list.svelte.d.ts +4 -0
  36. package/dist/color-picker/types.d.ts +176 -0
  37. package/dist/color-picker/types.js +1 -0
  38. package/dist/index.d.ts +10 -0
  39. package/dist/index.js +10 -0
  40. package/dist/internal/localized-strings.d.ts +176 -0
  41. package/dist/internal/localized-strings.js +176 -0
  42. package/dist/pin-input/README.md +55 -0
  43. package/dist/pin-input/cell/pin-input-cell.svelte +210 -0
  44. package/dist/pin-input/cell/pin-input-cell.svelte.d.ts +4 -0
  45. package/dist/pin-input/index.d.ts +6 -0
  46. package/dist/pin-input/index.js +5 -0
  47. package/dist/pin-input/index.parts.d.ts +3 -0
  48. package/dist/pin-input/index.parts.js +3 -0
  49. package/dist/pin-input/label/pin-input-label.svelte +43 -0
  50. package/dist/pin-input/label/pin-input-label.svelte.d.ts +4 -0
  51. package/dist/pin-input/root/context.d.ts +70 -0
  52. package/dist/pin-input/root/context.js +15 -0
  53. package/dist/pin-input/root/pin-input-root.svelte +337 -0
  54. package/dist/pin-input/root/pin-input-root.svelte.d.ts +4 -0
  55. package/dist/pin-input/root/pin-input-utils.d.ts +18 -0
  56. package/dist/pin-input/root/pin-input-utils.js +44 -0
  57. package/dist/pin-input/types.d.ts +84 -0
  58. package/dist/pin-input/types.js +1 -0
  59. package/dist/primitives/aria-hide-outside.d.ts +13 -0
  60. package/dist/primitives/aria-hide-outside.js +7 -1
  61. package/dist/primitives/focus-trap.d.ts +2 -0
  62. package/dist/primitives/focus-trap.js +7 -1
  63. package/dist/primitives/presence.svelte.js +6 -1
  64. package/dist/rating/README.md +58 -0
  65. package/dist/rating/index.d.ts +7 -0
  66. package/dist/rating/index.js +6 -0
  67. package/dist/rating/index.parts.d.ts +4 -0
  68. package/dist/rating/index.parts.js +4 -0
  69. package/dist/rating/item/rating-item.svelte +177 -0
  70. package/dist/rating/item/rating-item.svelte.d.ts +4 -0
  71. package/dist/rating/label/rating-label.svelte +42 -0
  72. package/dist/rating/label/rating-label.svelte.d.ts +4 -0
  73. package/dist/rating/output/rating-output.svelte +38 -0
  74. package/dist/rating/output/rating-output.svelte.d.ts +4 -0
  75. package/dist/rating/root/context.d.ts +87 -0
  76. package/dist/rating/root/context.js +15 -0
  77. package/dist/rating/root/rating-root.svelte +521 -0
  78. package/dist/rating/root/rating-root.svelte.d.ts +4 -0
  79. package/dist/rating/root/rating-utils.d.ts +28 -0
  80. package/dist/rating/root/rating-utils.js +53 -0
  81. package/dist/rating/types.d.ts +124 -0
  82. package/dist/rating/types.js +1 -0
  83. package/dist/search-field/README.md +29 -0
  84. package/dist/search-field/clear/README.md +5 -0
  85. package/dist/search-field/clear/search-field-clear.svelte +99 -0
  86. package/dist/search-field/clear/search-field-clear.svelte.d.ts +10 -0
  87. package/dist/search-field/index.d.ts +15 -0
  88. package/dist/search-field/index.js +6 -0
  89. package/dist/search-field/index.parts.d.ts +4 -0
  90. package/dist/search-field/index.parts.js +4 -0
  91. package/dist/search-field/input/README.md +5 -0
  92. package/dist/search-field/input/search-field-input.svelte +158 -0
  93. package/dist/search-field/input/search-field-input.svelte.d.ts +14 -0
  94. package/dist/search-field/label/README.md +3 -0
  95. package/dist/search-field/label/search-field-label.svelte +27 -0
  96. package/dist/search-field/label/search-field-label.svelte.d.ts +7 -0
  97. package/dist/search-field/root/README.md +5 -0
  98. package/dist/search-field/root/context.d.ts +28 -0
  99. package/dist/search-field/root/context.js +15 -0
  100. package/dist/search-field/root/search-field-root.svelte +230 -0
  101. package/dist/search-field/root/search-field-root.svelte.d.ts +33 -0
  102. package/dist/toast/README.md +75 -0
  103. package/dist/toast/action/toast-action.svelte +42 -0
  104. package/dist/toast/action/toast-action.svelte.d.ts +4 -0
  105. package/dist/toast/close/toast-close.svelte +41 -0
  106. package/dist/toast/close/toast-close.svelte.d.ts +4 -0
  107. package/dist/toast/content/toast-content.svelte +24 -0
  108. package/dist/toast/content/toast-content.svelte.d.ts +4 -0
  109. package/dist/toast/description/toast-description.svelte +41 -0
  110. package/dist/toast/description/toast-description.svelte.d.ts +4 -0
  111. package/dist/toast/index.d.ts +34 -0
  112. package/dist/toast/index.js +15 -0
  113. package/dist/toast/index.parts.d.ts +9 -0
  114. package/dist/toast/index.parts.js +9 -0
  115. package/dist/toast/positioner/toast-positioner.svelte +75 -0
  116. package/dist/toast/positioner/toast-positioner.svelte.d.ts +4 -0
  117. package/dist/toast/provider/context.d.ts +36 -0
  118. package/dist/toast/provider/context.js +22 -0
  119. package/dist/toast/provider/toast-manager.svelte.d.ts +113 -0
  120. package/dist/toast/provider/toast-manager.svelte.js +210 -0
  121. package/dist/toast/provider/toast-provider.svelte +98 -0
  122. package/dist/toast/provider/toast-provider.svelte.d.ts +4 -0
  123. package/dist/toast/root/context.d.ts +18 -0
  124. package/dist/toast/root/context.js +15 -0
  125. package/dist/toast/root/toast-root.svelte +296 -0
  126. package/dist/toast/root/toast-root.svelte.d.ts +4 -0
  127. package/dist/toast/title/toast-title.svelte +38 -0
  128. package/dist/toast/title/toast-title.svelte.d.ts +4 -0
  129. package/dist/toast/types.d.ts +100 -0
  130. package/dist/toast/types.js +1 -0
  131. package/dist/toast/viewport/toast-viewport.svelte +389 -0
  132. package/dist/toast/viewport/toast-viewport.svelte.d.ts +4 -0
  133. package/dist/tooltip/root/tooltip-root.svelte +16 -11
  134. package/package.json +26 -1
@@ -0,0 +1,230 @@
1
+ <script lang="ts">
2
+ import { readable } from 'svelte/store';
3
+ import { untrack, type Snippet } from 'svelte';
4
+ import type { HTMLAttributes } from 'svelte/elements';
5
+ import { resolveLocalizedString } from '../../internal/localized-strings';
6
+ import { useLocaleContextOptional } from '../../locale-provider/context';
7
+ import { isAriaInvalidValue } from '../../utils/aria-invalid';
8
+ import { composeEventHandlers } from '../../utils/compose-event-handlers';
9
+ import {
10
+ setSearchFieldContext,
11
+ type SearchFieldChangeReason,
12
+ type SearchFieldContext
13
+ } from './context';
14
+
15
+ type SearchFieldRootProps = Omit<HTMLAttributes<HTMLDivElement>, 'children' | 'class' | 'id'> & {
16
+ children?: Snippet;
17
+ class?: string;
18
+ /** The id of the root element. If you give none, the component makes one. */
19
+ id?: string;
20
+ /** The search text. You can bind it with `bind:value`. */
21
+ value?: string;
22
+ /** The search text at the start, for when you give no `value`. A form reset goes back to it. */
23
+ defaultValue?: string;
24
+ /** The component calls it when the search text changes. */
25
+ onChange?: (value: string) => void;
26
+ /** The component calls it with the search text when the user presses `Enter` in the input. */
27
+ onSubmit?: (value: string) => void;
28
+ /** The component calls it after the `Escape` key or the clear button empties the input. */
29
+ onClear?: () => void;
30
+ /** Disables the input and the clear button. */
31
+ disabled?: boolean;
32
+ /** The user cannot change the text. The `Escape` key and the clear button do nothing. */
33
+ readonly?: boolean;
34
+ /** Tells a form that it must have a value. The input gets the native `required`. */
35
+ required?: boolean;
36
+ /** Marks the value as invalid. The input gets `aria-invalid`. */
37
+ invalid?: boolean;
38
+ /** Marks the value as invalid, the same as `invalid`. */
39
+ 'aria-invalid'?: HTMLAttributes<HTMLDivElement>['aria-invalid'];
40
+ /** The name of the input in a form. The form submits the search text with this name. */
41
+ name?: string;
42
+ };
43
+
44
+ const generatedId = $props.id();
45
+ const localeContext = useLocaleContextOptional();
46
+ const emptyLocaleStore = readable<string | undefined>(undefined);
47
+ const localeStore = localeContext?.locale ?? emptyLocaleStore;
48
+
49
+ let {
50
+ children,
51
+ class: className,
52
+ id,
53
+ value = $bindable<string | undefined>(undefined),
54
+ defaultValue = '',
55
+ onChange,
56
+ onSubmit,
57
+ onClear,
58
+ disabled = false,
59
+ readonly = false,
60
+ required = false,
61
+ invalid = false,
62
+ 'aria-invalid': ariaInvalid,
63
+ name,
64
+ onfocusin: onFocusInExternal,
65
+ onfocusout: onFocusOutExternal,
66
+ ...restProps
67
+ }: SearchFieldRootProps = $props();
68
+
69
+ const rootId = untrack(() => id) ?? generatedId;
70
+ const inputId = `${rootId}-input`;
71
+ const initialDefaultValue = untrack(() => defaultValue) ?? '';
72
+
73
+ const initialValue = untrack(() => value) ?? initialDefaultValue;
74
+
75
+ let currentValue = $state(initialValue);
76
+ let focused = $state(false);
77
+ let focusVisible = $state(false);
78
+ let focusWithin = $state(false);
79
+ let inputRef: HTMLInputElement | null = $state(null);
80
+
81
+ if (untrack(() => value) === undefined) {
82
+ value = initialValue;
83
+ }
84
+
85
+ // A new value from the parent replaces the text. The text that the user types goes to the
86
+ // parent through `setValue`, thus the two stay equal and this does nothing for it.
87
+ $effect(() => {
88
+ const next = value ?? '';
89
+ untrack(() => {
90
+ if (next !== currentValue) currentValue = next;
91
+ });
92
+ });
93
+
94
+ const isEmpty = $derived(currentValue === '');
95
+ const resolvedInvalid = $derived(Boolean(invalid || isAriaInvalidValue(ariaInvalid)));
96
+ const clearAriaLabel = $derived(resolveLocalizedString($localeStore, 'searchField.clear'));
97
+
98
+ $effect(() => {
99
+ if (!disabled) return;
100
+ focused = false;
101
+ focusVisible = false;
102
+ focusWithin = false;
103
+ });
104
+
105
+ $effect(() => {
106
+ const form = inputRef?.form;
107
+ if (!form) return;
108
+
109
+ // The browser puts `defaultValue` back in the input, but it sends no `input` event.
110
+ const handleFormReset = () => setValue(initialDefaultValue, 'form-reset');
111
+
112
+ form.addEventListener('reset', handleFormReset);
113
+ return () => form.removeEventListener('reset', handleFormReset);
114
+ });
115
+
116
+ function setValue(next: string, reason: SearchFieldChangeReason) {
117
+ if (reason !== 'form-reset' && (disabled || readonly)) return;
118
+ if (next === currentValue) return;
119
+
120
+ currentValue = next;
121
+ value = next;
122
+ onChange?.(next);
123
+ }
124
+
125
+ function clear(reason: 'escape-key' | 'clear-press'): boolean {
126
+ if (disabled || readonly) return false;
127
+ // The text can be in the input and not in the state, when a script wrote `input.value`.
128
+ if (currentValue === '' && (!inputRef || inputRef.value === '')) return false;
129
+
130
+ setValue('', reason);
131
+ if (inputRef && inputRef.value !== '') inputRef.value = '';
132
+ onClear?.();
133
+ return true;
134
+ }
135
+
136
+ function submit() {
137
+ if (disabled || readonly) return;
138
+ onSubmit?.(currentValue);
139
+ }
140
+
141
+ function handleFocusIn() {
142
+ if (disabled) return;
143
+ focusWithin = true;
144
+ }
145
+
146
+ function handleFocusOut(event: FocusEvent) {
147
+ const root = event.currentTarget as HTMLElement;
148
+ const next = event.relatedTarget as Node | null;
149
+ if (next && root.contains(next)) return;
150
+ focusWithin = false;
151
+ }
152
+
153
+ const context: SearchFieldContext = {
154
+ get id() {
155
+ return rootId;
156
+ },
157
+ get inputId() {
158
+ return inputId;
159
+ },
160
+ get value() {
161
+ return currentValue;
162
+ },
163
+ get defaultValue() {
164
+ return initialDefaultValue;
165
+ },
166
+ get name() {
167
+ return name;
168
+ },
169
+ get isEmpty() {
170
+ return isEmpty;
171
+ },
172
+ get isDisabled() {
173
+ return disabled;
174
+ },
175
+ get isReadOnly() {
176
+ return readonly;
177
+ },
178
+ get isRequired() {
179
+ return required;
180
+ },
181
+ get isInvalid() {
182
+ return resolvedInvalid;
183
+ },
184
+ get clearAriaLabel() {
185
+ return clearAriaLabel;
186
+ },
187
+ get focused() {
188
+ return focused;
189
+ },
190
+ get focusVisible() {
191
+ return focusVisible;
192
+ },
193
+ get inputRef() {
194
+ return inputRef;
195
+ },
196
+ setInputRef(element) {
197
+ inputRef = element;
198
+ },
199
+ setFocused(next) {
200
+ focused = next;
201
+ if (!next) focusVisible = false;
202
+ },
203
+ setFocusVisible(next) {
204
+ focusVisible = next;
205
+ },
206
+ setValue,
207
+ clear,
208
+ submit
209
+ };
210
+
211
+ setSearchFieldContext(context);
212
+ </script>
213
+
214
+ <div
215
+ {...restProps}
216
+ id={rootId}
217
+ class={className}
218
+ data-search-field-root="true"
219
+ data-empty={isEmpty || undefined}
220
+ data-disabled={disabled || undefined}
221
+ data-readonly={readonly || undefined}
222
+ data-required={required || undefined}
223
+ data-invalid={resolvedInvalid || undefined}
224
+ data-focus-within={focusWithin || undefined}
225
+ data-focus-visible={(focusWithin && focusVisible) || undefined}
226
+ onfocusin={composeEventHandlers(handleFocusIn, onFocusInExternal ?? undefined)}
227
+ onfocusout={composeEventHandlers(handleFocusOut, onFocusOutExternal ?? undefined)}
228
+ >
229
+ {@render children?.()}
230
+ </div>
@@ -0,0 +1,33 @@
1
+ import { type Snippet } from 'svelte';
2
+ import type { HTMLAttributes } from 'svelte/elements';
3
+ type SearchFieldRootProps = Omit<HTMLAttributes<HTMLDivElement>, 'children' | 'class' | 'id'> & {
4
+ children?: Snippet;
5
+ class?: string;
6
+ /** The id of the root element. If you give none, the component makes one. */
7
+ id?: string;
8
+ /** The search text. You can bind it with `bind:value`. */
9
+ value?: string;
10
+ /** The search text at the start, for when you give no `value`. A form reset goes back to it. */
11
+ defaultValue?: string;
12
+ /** The component calls it when the search text changes. */
13
+ onChange?: (value: string) => void;
14
+ /** The component calls it with the search text when the user presses `Enter` in the input. */
15
+ onSubmit?: (value: string) => void;
16
+ /** The component calls it after the `Escape` key or the clear button empties the input. */
17
+ onClear?: () => void;
18
+ /** Disables the input and the clear button. */
19
+ disabled?: boolean;
20
+ /** The user cannot change the text. The `Escape` key and the clear button do nothing. */
21
+ readonly?: boolean;
22
+ /** Tells a form that it must have a value. The input gets the native `required`. */
23
+ required?: boolean;
24
+ /** Marks the value as invalid. The input gets `aria-invalid`. */
25
+ invalid?: boolean;
26
+ /** Marks the value as invalid, the same as `invalid`. */
27
+ 'aria-invalid'?: HTMLAttributes<HTMLDivElement>['aria-invalid'];
28
+ /** The name of the input in a form. The form submits the search text with this name. */
29
+ name?: string;
30
+ };
31
+ declare const SearchFieldRoot: import("svelte").Component<SearchFieldRootProps, {}, "value">;
32
+ type SearchFieldRoot = ReturnType<typeof SearchFieldRoot>;
33
+ export default SearchFieldRoot;
@@ -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,4 @@
1
+ import type { ToastActionProps } from '../types.js';
2
+ declare const ToastAction: import("svelte").Component<ToastActionProps, {}, "element">;
3
+ type ToastAction = ReturnType<typeof ToastAction>;
4
+ export default ToastAction;
@@ -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,4 @@
1
+ import type { ToastCloseProps } from '../types.js';
2
+ declare const ToastClose: import("svelte").Component<ToastCloseProps, {}, "element">;
3
+ type ToastClose = ReturnType<typeof ToastClose>;
4
+ export default ToastClose;
@@ -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,4 @@
1
+ import type { ToastContentProps } from '../types.js';
2
+ declare const ToastContent: import("svelte").Component<ToastContentProps, {}, "">;
3
+ type ToastContent = ReturnType<typeof ToastContent>;
4
+ export default ToastContent;
@@ -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,4 @@
1
+ import type { ToastDescriptionProps } from '../types.js';
2
+ declare const ToastDescription: import("svelte").Component<ToastDescriptionProps, {}, "">;
3
+ type ToastDescription = ReturnType<typeof ToastDescription>;
4
+ export default ToastDescription;
@@ -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,4 @@
1
+ import type { ToastPositionerProps } from '../types.js';
2
+ declare const ToastPositioner: import("svelte").Component<ToastPositionerProps, {}, "element">;
3
+ type ToastPositioner = ReturnType<typeof ToastPositioner>;
4
+ export default ToastPositioner;