@human-kit/ui 1.0.0-beta.7 → 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/FOCUS_STATE_CONTRACT.md +15 -4
- package/dist/FOCUS_STATE_REVIEW_TEMPLATE.md +8 -8
- package/dist/avatar/README.md +52 -0
- package/dist/avatar/count/avatar-count.svelte +56 -0
- package/dist/avatar/count/avatar-count.svelte.d.ts +4 -0
- package/dist/avatar/fallback/avatar-fallback.svelte +67 -0
- package/dist/avatar/fallback/avatar-fallback.svelte.d.ts +4 -0
- package/dist/avatar/group/avatar-group.svelte +65 -0
- package/dist/avatar/group/avatar-group.svelte.d.ts +4 -0
- package/dist/avatar/group/context.d.ts +15 -0
- package/dist/avatar/group/context.js +15 -0
- package/dist/avatar/image/avatar-image.svelte +104 -0
- package/dist/avatar/image/avatar-image.svelte.d.ts +4 -0
- package/dist/avatar/index.d.ts +22 -0
- package/dist/avatar/index.js +10 -0
- package/dist/avatar/index.parts.d.ts +5 -0
- package/dist/avatar/index.parts.js +5 -0
- package/dist/avatar/root/avatar-root.svelte +79 -0
- package/dist/avatar/root/avatar-root.svelte.d.ts +4 -0
- package/dist/avatar/root/context.d.ts +14 -0
- package/dist/avatar/root/context.js +15 -0
- package/dist/avatar/types.d.ts +84 -0
- package/dist/avatar/types.js +1 -0
- package/dist/breadcrumbs/README.md +65 -0
- package/dist/breadcrumbs/ellipsis/breadcrumbs-ellipsis.svelte +62 -0
- package/dist/breadcrumbs/ellipsis/breadcrumbs-ellipsis.svelte.d.ts +4 -0
- package/dist/breadcrumbs/index.d.ts +23 -0
- package/dist/breadcrumbs/index.js +10 -0
- package/dist/breadcrumbs/index.parts.d.ts +6 -0
- package/dist/breadcrumbs/index.parts.js +6 -0
- package/dist/breadcrumbs/item/breadcrumbs-item.svelte +42 -0
- package/dist/breadcrumbs/item/breadcrumbs-item.svelte.d.ts +4 -0
- package/dist/breadcrumbs/link/breadcrumbs-link.svelte +47 -0
- package/dist/breadcrumbs/link/breadcrumbs-link.svelte.d.ts +4 -0
- package/dist/breadcrumbs/list/breadcrumbs-list.svelte +31 -0
- package/dist/breadcrumbs/list/breadcrumbs-list.svelte.d.ts +4 -0
- package/dist/breadcrumbs/root/breadcrumbs-root.svelte +112 -0
- package/dist/breadcrumbs/root/breadcrumbs-root.svelte.d.ts +4 -0
- package/dist/breadcrumbs/root/context.d.ts +19 -0
- package/dist/breadcrumbs/root/context.js +15 -0
- package/dist/breadcrumbs/separator/breadcrumbs-separator.svelte +35 -0
- package/dist/breadcrumbs/separator/breadcrumbs-separator.svelte.d.ts +4 -0
- package/dist/breadcrumbs/types.d.ts +80 -0
- package/dist/breadcrumbs/types.js +1 -0
- package/dist/combobox/popover/combobox-popover.svelte +4 -56
- package/dist/dialog/root/dialog-root.svelte +15 -2
- package/dist/drawer/root/drawer-root.svelte +15 -2
- package/dist/index.d.ts +16 -0
- package/dist/index.js +15 -0
- package/dist/internal/localized-strings.d.ts +72 -0
- package/dist/internal/localized-strings.js +72 -0
- package/dist/internal/number-precision.d.ts +9 -0
- package/dist/internal/number-precision.js +27 -0
- package/dist/menu/root/focus-state.d.ts +2 -2
- package/dist/menu/root/focus-state.js +8 -32
- package/dist/menu/root/menu-state.svelte.js +5 -1
- package/dist/numberfield/root/number-utils.js +1 -20
- package/dist/popover/root/focus-state.d.ts +2 -2
- package/dist/popover/root/focus-state.js +7 -34
- package/dist/primitives/aria-hide-outside.d.ts +13 -0
- package/dist/primitives/aria-hide-outside.js +7 -1
- package/dist/primitives/contain-wheel.d.ts +9 -0
- package/dist/primitives/contain-wheel.js +41 -0
- package/dist/primitives/floating.d.ts +6 -0
- package/dist/primitives/floating.js +26 -5
- package/dist/primitives/focus-trap.d.ts +2 -0
- package/dist/primitives/focus-trap.js +7 -1
- package/dist/primitives/keyboard-navigation.d.ts +6 -0
- package/dist/primitives/keyboard-navigation.js +2 -1
- package/dist/primitives/presence.svelte.js +6 -1
- package/dist/primitives/trigger-focus-state.d.ts +29 -0
- package/dist/primitives/trigger-focus-state.js +57 -0
- package/dist/progress/README.md +54 -0
- package/dist/progress/index.d.ts +21 -0
- package/dist/progress/index.js +9 -0
- package/dist/progress/index.parts.d.ts +5 -0
- package/dist/progress/index.parts.js +5 -0
- package/dist/progress/indicator/progress-indicator.svelte +37 -0
- package/dist/progress/indicator/progress-indicator.svelte.d.ts +4 -0
- package/dist/progress/label/progress-label.svelte +34 -0
- package/dist/progress/label/progress-label.svelte.d.ts +4 -0
- package/dist/progress/root/context.d.ts +26 -0
- package/dist/progress/root/context.js +15 -0
- package/dist/progress/root/progress-root.svelte +136 -0
- package/dist/progress/root/progress-root.svelte.d.ts +4 -0
- package/dist/progress/root/progress-utils.d.ts +12 -0
- package/dist/progress/root/progress-utils.js +41 -0
- package/dist/progress/track/progress-track.svelte +27 -0
- package/dist/progress/track/progress-track.svelte.d.ts +4 -0
- package/dist/progress/types.d.ts +81 -0
- package/dist/progress/types.js +1 -0
- package/dist/progress/value/progress-value.svelte +36 -0
- package/dist/progress/value/progress-value.svelte.d.ts +4 -0
- package/dist/select/README.md +129 -0
- package/dist/select/index.d.ts +30 -0
- package/dist/select/index.js +16 -0
- package/dist/select/index.parts.d.ts +8 -0
- package/dist/select/index.parts.js +9 -0
- package/dist/select/item/select-item.svelte +68 -0
- package/dist/select/item/select-item.svelte.d.ts +10 -0
- package/dist/select/item-indicator/select-item-indicator.svelte +55 -0
- package/dist/select/item-indicator/select-item-indicator.svelte.d.ts +4 -0
- package/dist/select/label/select-label.svelte +46 -0
- package/dist/select/label/select-label.svelte.d.ts +4 -0
- package/dist/select/list/select-list.svelte +241 -0
- package/dist/select/list/select-list.svelte.d.ts +62 -0
- package/dist/select/popover/select-popover.svelte +80 -0
- package/dist/select/popover/select-popover.svelte.d.ts +9 -0
- package/dist/select/root/context.d.ts +106 -0
- package/dist/select/root/context.js +22 -0
- package/dist/select/root/select-root.svelte +544 -0
- package/dist/select/root/select-root.svelte.d.ts +25 -0
- package/dist/select/trigger/select-trigger.svelte +192 -0
- package/dist/select/trigger/select-trigger.svelte.d.ts +4 -0
- package/dist/select/types.d.ts +133 -0
- package/dist/select/types.js +1 -0
- package/dist/select/value/select-value.svelte +61 -0
- package/dist/select/value/select-value.svelte.d.ts +4 -0
- package/dist/separator/README.md +35 -0
- package/dist/separator/index.d.ts +7 -0
- package/dist/separator/index.js +3 -0
- package/dist/separator/separator.svelte +254 -0
- package/dist/separator/separator.svelte.d.ts +45 -0
- package/dist/separator/types.d.ts +1 -0
- package/dist/separator/types.js +1 -0
- package/dist/slider/README.md +61 -0
- package/dist/slider/fill/slider-fill.svelte +40 -0
- package/dist/slider/fill/slider-fill.svelte.d.ts +4 -0
- package/dist/slider/index.d.ts +9 -0
- package/dist/slider/index.js +8 -0
- package/dist/slider/index.parts.d.ts +6 -0
- package/dist/slider/index.parts.js +6 -0
- package/dist/slider/label/slider-label.svelte +44 -0
- package/dist/slider/label/slider-label.svelte.d.ts +4 -0
- package/dist/slider/output/slider-output.svelte +46 -0
- package/dist/slider/output/slider-output.svelte.d.ts +4 -0
- package/dist/slider/root/context.d.ts +106 -0
- package/dist/slider/root/context.js +15 -0
- package/dist/slider/root/slider-root.svelte +545 -0
- package/dist/slider/root/slider-root.svelte.d.ts +4 -0
- package/dist/slider/root/slider-utils.d.ts +57 -0
- package/dist/slider/root/slider-utils.js +88 -0
- package/dist/slider/thumb/slider-thumb.svelte +276 -0
- package/dist/slider/thumb/slider-thumb.svelte.d.ts +4 -0
- package/dist/slider/track/slider-track.svelte +60 -0
- package/dist/slider/track/slider-track.svelte.d.ts +4 -0
- package/dist/slider/types.d.ts +144 -0
- package/dist/slider/types.js +1 -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/README.md +53 -0
- package/dist/tooltip/arrow/tooltip-arrow.svelte +36 -0
- package/dist/tooltip/arrow/tooltip-arrow.svelte.d.ts +4 -0
- package/dist/tooltip/content/tooltip-content.svelte +148 -0
- package/dist/tooltip/content/tooltip-content.svelte.d.ts +4 -0
- package/dist/tooltip/index.d.ts +8 -0
- package/dist/tooltip/index.js +7 -0
- package/dist/tooltip/index.parts.d.ts +5 -0
- package/dist/tooltip/index.parts.js +5 -0
- package/dist/tooltip/provider/tooltip-provider.svelte +36 -0
- package/dist/tooltip/provider/tooltip-provider.svelte.d.ts +4 -0
- package/dist/tooltip/root/context.d.ts +46 -0
- package/dist/tooltip/root/context.js +22 -0
- package/dist/tooltip/root/safe-polygon.d.ts +26 -0
- package/dist/tooltip/root/safe-polygon.js +66 -0
- package/dist/tooltip/root/tooltip-group.d.ts +26 -0
- package/dist/tooltip/root/tooltip-group.js +41 -0
- package/dist/tooltip/root/tooltip-root.svelte +454 -0
- package/dist/tooltip/root/tooltip-root.svelte.d.ts +4 -0
- package/dist/tooltip/trigger/tooltip-trigger.svelte +52 -0
- package/dist/tooltip/trigger/tooltip-trigger.svelte.d.ts +4 -0
- package/dist/tooltip/types.d.ts +105 -0
- package/dist/tooltip/types.js +1 -0
- package/package.json +41 -1
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { untrack, type Snippet } from 'svelte';
|
|
3
|
+
import { readable } from 'svelte/store';
|
|
4
|
+
import { resolveLocalizedString } from '../../internal/localized-strings';
|
|
5
|
+
import { isRtl } from '../../internal/rtl';
|
|
6
|
+
import { getLocaleContext } from '../../locale-provider/context';
|
|
7
|
+
import { watchFocusVisible } from '../../primitives/focus-visible.svelte';
|
|
8
|
+
import {
|
|
9
|
+
shouldShowFocusVisible,
|
|
10
|
+
trackInteractionModality
|
|
11
|
+
} from '../../primitives/input-modality';
|
|
12
|
+
import { useSliderContext, type SliderChangeDetails } from '../root/context';
|
|
13
|
+
import type { SliderThumbProps, SliderThumbRenderState } from '../types';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Slider.Thumb — the handle of one value.
|
|
17
|
+
*
|
|
18
|
+
* The element the user sees is a `<div>` positioned along the track. Inside it, a native
|
|
19
|
+
* `<input type="range">` fills the thumb, invisible, and it is what has the focus and the ARIA
|
|
20
|
+
* state. The native input gives the role and the value to a screen reader, and it receives
|
|
21
|
+
* the swipe of a mobile screen reader as an `input` event. The keyboard is handled here and
|
|
22
|
+
* not by the input, so that the steps, the large steps and the text direction are the same in
|
|
23
|
+
* every browser.
|
|
24
|
+
*/
|
|
25
|
+
let {
|
|
26
|
+
index: indexProp,
|
|
27
|
+
name,
|
|
28
|
+
'aria-label': ariaLabel,
|
|
29
|
+
'aria-labelledby': ariaLabelledBy,
|
|
30
|
+
children,
|
|
31
|
+
class: className = '',
|
|
32
|
+
element = $bindable<HTMLDivElement | null>(null),
|
|
33
|
+
style,
|
|
34
|
+
onpointerdown,
|
|
35
|
+
...restProps
|
|
36
|
+
}: SliderThumbProps = $props();
|
|
37
|
+
|
|
38
|
+
const ctx = useSliderContext('Slider.Thumb');
|
|
39
|
+
const localeStore = getLocaleContext()?.locale ?? readable<string | undefined>(undefined);
|
|
40
|
+
|
|
41
|
+
let thumbRef: HTMLDivElement | null = $state(null);
|
|
42
|
+
let inputRef: HTMLInputElement | null = $state(null);
|
|
43
|
+
|
|
44
|
+
const registration = ctx.registerThumb({
|
|
45
|
+
index: untrack(() => indexProp),
|
|
46
|
+
inputRef: () => inputRef
|
|
47
|
+
});
|
|
48
|
+
const index = registration.index;
|
|
49
|
+
const inputId = ctx.getThumbInputId(index);
|
|
50
|
+
|
|
51
|
+
$effect(() => {
|
|
52
|
+
element = thumbRef;
|
|
53
|
+
return () => {
|
|
54
|
+
element = null;
|
|
55
|
+
};
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
$effect(() => registration.unregister);
|
|
59
|
+
|
|
60
|
+
const value = $derived(ctx.values[index] ?? ctx.min);
|
|
61
|
+
const percent = $derived(ctx.getPercent(value));
|
|
62
|
+
const text = $derived(ctx.formatValue(value));
|
|
63
|
+
const valueText = $derived(ctx.getValueText(value, index));
|
|
64
|
+
const dragging = $derived(ctx.draggingIndex === index);
|
|
65
|
+
const focused = $derived(ctx.focusedIndex === index);
|
|
66
|
+
const focusVisible = $derived(focused && ctx.isFocusVisible);
|
|
67
|
+
const active = $derived(ctx.activeIndex === index);
|
|
68
|
+
|
|
69
|
+
// Two thumbs with the same name are two sliders the screen reader cannot tell apart. A range
|
|
70
|
+
// of two gets "Minimum" and "Maximum" when the consumer gives no name of its own.
|
|
71
|
+
const defaultLabel = $derived.by(() => {
|
|
72
|
+
if (ctx.values.length !== 2) return undefined;
|
|
73
|
+
return resolveLocalizedString($localeStore, index === 0 ? 'slider.minimum' : 'slider.maximum');
|
|
74
|
+
});
|
|
75
|
+
const ownLabel = $derived(ariaLabel ?? (ariaLabelledBy ? undefined : defaultLabel));
|
|
76
|
+
const ownLabelSource = $derived(ariaLabelledBy ?? (ownLabel ? inputId : null));
|
|
77
|
+
// The name of the group: the label, the ids the consumer gave, or the root element itself
|
|
78
|
+
// when it carries an `aria-label`.
|
|
79
|
+
const groupLabelSource = $derived(
|
|
80
|
+
ctx.ariaLabelledBy ?? ctx.labelId ?? (ctx.ariaLabel ? ctx.rootId : null)
|
|
81
|
+
);
|
|
82
|
+
const inputAriaLabel = $derived(
|
|
83
|
+
ownLabel ?? (groupLabelSource === ctx.rootId ? ctx.ariaLabel : undefined)
|
|
84
|
+
);
|
|
85
|
+
const inputLabelledBy = $derived.by(() => {
|
|
86
|
+
if (!ownLabelSource) {
|
|
87
|
+
return groupLabelSource && groupLabelSource !== ctx.rootId ? groupLabelSource : undefined;
|
|
88
|
+
}
|
|
89
|
+
return groupLabelSource ? `${ownLabelSource} ${groupLabelSource}` : ownLabelSource;
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
// The native input keeps the value: a screen reader reads it from there, and a form sends it.
|
|
93
|
+
$effect(() => {
|
|
94
|
+
if (!inputRef) return;
|
|
95
|
+
const nextValue = String(value);
|
|
96
|
+
if (inputRef.value !== nextValue) inputRef.value = nextValue;
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
// The focus ring must appear when a pointer press is followed by a key press, and the
|
|
100
|
+
// modality alone changes there: no focus event fires to re-read it.
|
|
101
|
+
watchFocusVisible({
|
|
102
|
+
isFocused: () => focused,
|
|
103
|
+
element: () => inputRef,
|
|
104
|
+
set: (visible) => ctx.setFocusVisible(visible)
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
const positionStyle = $derived.by(() => {
|
|
108
|
+
if (ctx.orientation === 'vertical') {
|
|
109
|
+
return `position: absolute; bottom: ${percent}%; left: 50%; translate: -50% 50%;`;
|
|
110
|
+
}
|
|
111
|
+
// A physical `left`, because the `translate` that centers the thumb is physical too.
|
|
112
|
+
const left = ctx.isRtl ? 100 - percent : percent;
|
|
113
|
+
return `position: absolute; left: ${left}%; top: 50%; translate: -50% -50%;`;
|
|
114
|
+
});
|
|
115
|
+
// The thumb the user touched last draws above the others: two thumbs at the same value
|
|
116
|
+
// would hide the one that just moved.
|
|
117
|
+
const resolvedStyle = $derived(
|
|
118
|
+
`${positionStyle} touch-action: none;${active ? ' z-index: 1;' : ''}${style ? ` ${style}` : ''}`
|
|
119
|
+
);
|
|
120
|
+
|
|
121
|
+
// The keyboard reports `onChange` on each key press, and `onChangeEnd` one time at the
|
|
122
|
+
// release, thus a held key does not report an end on each repeat.
|
|
123
|
+
let keyboardChanged = false;
|
|
124
|
+
|
|
125
|
+
function commitKeyboard(event: Event) {
|
|
126
|
+
if (!keyboardChanged) return;
|
|
127
|
+
keyboardChanged = false;
|
|
128
|
+
ctx.commitValue({ reason: 'keyboard', index, event });
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function handleKeyDown(event: KeyboardEvent) {
|
|
132
|
+
if (event.defaultPrevented) return;
|
|
133
|
+
if (event.ctrlKey || event.metaKey || event.altKey) return;
|
|
134
|
+
|
|
135
|
+
const horizontal = ctx.orientation === 'horizontal';
|
|
136
|
+
const rtl = horizontal && isRtl(thumbRef);
|
|
137
|
+
let direction: -1 | 1 | null = null;
|
|
138
|
+
let large = event.shiftKey;
|
|
139
|
+
let absolute: number | null = null;
|
|
140
|
+
|
|
141
|
+
switch (event.key) {
|
|
142
|
+
case 'ArrowRight':
|
|
143
|
+
direction = rtl ? -1 : 1;
|
|
144
|
+
break;
|
|
145
|
+
case 'ArrowLeft':
|
|
146
|
+
direction = rtl ? 1 : -1;
|
|
147
|
+
break;
|
|
148
|
+
case 'ArrowUp':
|
|
149
|
+
direction = 1;
|
|
150
|
+
break;
|
|
151
|
+
case 'ArrowDown':
|
|
152
|
+
direction = -1;
|
|
153
|
+
break;
|
|
154
|
+
case 'PageUp':
|
|
155
|
+
direction = 1;
|
|
156
|
+
large = true;
|
|
157
|
+
break;
|
|
158
|
+
case 'PageDown':
|
|
159
|
+
direction = -1;
|
|
160
|
+
large = true;
|
|
161
|
+
break;
|
|
162
|
+
case 'Home':
|
|
163
|
+
absolute = ctx.min;
|
|
164
|
+
break;
|
|
165
|
+
case 'End':
|
|
166
|
+
absolute = ctx.max;
|
|
167
|
+
break;
|
|
168
|
+
default:
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// The native input would step on its own, in its own amounts.
|
|
173
|
+
event.preventDefault();
|
|
174
|
+
trackInteractionModality(event, inputRef);
|
|
175
|
+
if (focused) ctx.setFocusVisible(true);
|
|
176
|
+
if (ctx.isReadOnly) return;
|
|
177
|
+
|
|
178
|
+
if (absolute !== null) {
|
|
179
|
+
if (ctx.setValueAt(index, absolute, { reason: 'keyboard', index, event })) {
|
|
180
|
+
keyboardChanged = true;
|
|
181
|
+
}
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
if (direction !== null && ctx.stepValueAt(index, direction, { large, event })) {
|
|
185
|
+
keyboardChanged = true;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
function handleKeyUp(event: KeyboardEvent) {
|
|
190
|
+
commitKeyboard(event);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
// A change that did not come through the keyboard handler: a mobile screen reader swipe, or
|
|
194
|
+
// a browser feature that writes the native input.
|
|
195
|
+
function handleInput(event: Event) {
|
|
196
|
+
if (!inputRef) return;
|
|
197
|
+
const nextValue = Number(inputRef.value);
|
|
198
|
+
if (Number.isFinite(nextValue) && nextValue !== value && !ctx.isReadOnly) {
|
|
199
|
+
const details: SliderChangeDetails = { reason: 'input', index, event };
|
|
200
|
+
if (ctx.setValueAt(index, nextValue, details)) ctx.commitValue(details);
|
|
201
|
+
}
|
|
202
|
+
// Re-sync when the change was refused, or clamped by a neighbor.
|
|
203
|
+
inputRef.value = String(ctx.values[index] ?? ctx.min);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
function handleFocus() {
|
|
207
|
+
ctx.setThumbFocus(index, true);
|
|
208
|
+
ctx.setFocusVisible(shouldShowFocusVisible(inputRef));
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
function handleBlur(event: FocusEvent) {
|
|
212
|
+
// A key still held when the focus leaves has no release to wait for.
|
|
213
|
+
commitKeyboard(event);
|
|
214
|
+
ctx.setThumbFocus(index, false);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
function handlePointerDown(
|
|
218
|
+
event: PointerEvent & { currentTarget: EventTarget & HTMLDivElement }
|
|
219
|
+
) {
|
|
220
|
+
onpointerdown?.(event);
|
|
221
|
+
if (event.defaultPrevented) return;
|
|
222
|
+
// The thumb keeps its value at the press; only a move changes it.
|
|
223
|
+
ctx.startDrag(event, index);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
const renderState = $derived<SliderThumbRenderState>({ value, text, index, dragging, focused });
|
|
227
|
+
</script>
|
|
228
|
+
|
|
229
|
+
<div
|
|
230
|
+
{...restProps}
|
|
231
|
+
bind:this={thumbRef}
|
|
232
|
+
class={className}
|
|
233
|
+
style={resolvedStyle}
|
|
234
|
+
data-slider-thumb="true"
|
|
235
|
+
data-index={index}
|
|
236
|
+
data-orientation={ctx.orientation}
|
|
237
|
+
data-disabled={ctx.isDisabled || undefined}
|
|
238
|
+
data-readonly={ctx.isReadOnly || undefined}
|
|
239
|
+
data-invalid={ctx.isInvalid || undefined}
|
|
240
|
+
data-dragging={dragging || undefined}
|
|
241
|
+
data-focused={focused || undefined}
|
|
242
|
+
data-focus-visible={focusVisible || undefined}
|
|
243
|
+
onpointerdown={handlePointerDown}
|
|
244
|
+
>
|
|
245
|
+
<input
|
|
246
|
+
bind:this={inputRef}
|
|
247
|
+
type="range"
|
|
248
|
+
id={inputId}
|
|
249
|
+
name={name ?? ctx.name}
|
|
250
|
+
form={ctx.form}
|
|
251
|
+
min={ctx.min}
|
|
252
|
+
max={ctx.max}
|
|
253
|
+
step={ctx.step}
|
|
254
|
+
value={String(value)}
|
|
255
|
+
disabled={ctx.isDisabled}
|
|
256
|
+
aria-orientation={ctx.orientation}
|
|
257
|
+
aria-valuenow={value}
|
|
258
|
+
aria-valuetext={valueText}
|
|
259
|
+
aria-label={inputAriaLabel}
|
|
260
|
+
aria-labelledby={inputLabelledBy}
|
|
261
|
+
aria-describedby={ctx.ariaDescribedBy}
|
|
262
|
+
aria-readonly={ctx.isReadOnly || undefined}
|
|
263
|
+
aria-invalid={ctx.isInvalid || undefined}
|
|
264
|
+
data-slider-input="true"
|
|
265
|
+
data-index={index}
|
|
266
|
+
style="position:absolute;inset:0;width:100%;height:100%;margin:0;padding:0;border:0;opacity:0;cursor:inherit;pointer-events:none;"
|
|
267
|
+
onkeydown={handleKeyDown}
|
|
268
|
+
onkeyup={handleKeyUp}
|
|
269
|
+
oninput={handleInput}
|
|
270
|
+
onfocus={handleFocus}
|
|
271
|
+
onblur={handleBlur}
|
|
272
|
+
/>
|
|
273
|
+
{#if children}
|
|
274
|
+
{@render (children as Snippet<[SliderThumbRenderState]>)(renderState)}
|
|
275
|
+
{/if}
|
|
276
|
+
</div>
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { useSliderContext } from '../root/context';
|
|
3
|
+
import type { SliderTrackProps } from '../types';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Slider.Track — the rail, and the surface of the pointer.
|
|
7
|
+
*
|
|
8
|
+
* A press on it moves the nearest thumb to the pointer and starts a drag. The thumbs sit in
|
|
9
|
+
* it, positioned along it in percent of the value. It is `position: relative` for that, and
|
|
10
|
+
* `touch-action: none` so that a touch drag moves the thumb and not the page.
|
|
11
|
+
*/
|
|
12
|
+
let {
|
|
13
|
+
children,
|
|
14
|
+
class: className = '',
|
|
15
|
+
element = $bindable<HTMLDivElement | null>(null),
|
|
16
|
+
style,
|
|
17
|
+
onpointerdown,
|
|
18
|
+
...restProps
|
|
19
|
+
}: SliderTrackProps = $props();
|
|
20
|
+
|
|
21
|
+
const ctx = useSliderContext('Slider.Track');
|
|
22
|
+
let trackRef: HTMLDivElement | null = $state(null);
|
|
23
|
+
|
|
24
|
+
$effect(() => {
|
|
25
|
+
element = trackRef;
|
|
26
|
+
ctx.setTrackRef(trackRef);
|
|
27
|
+
return () => {
|
|
28
|
+
ctx.setTrackRef(null);
|
|
29
|
+
};
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
// The consumer style comes last, thus it can replace the two defaults.
|
|
33
|
+
const resolvedStyle = $derived(
|
|
34
|
+
`position: relative; touch-action: none;${style ? ` ${style}` : ''}`
|
|
35
|
+
);
|
|
36
|
+
|
|
37
|
+
function handlePointerDown(
|
|
38
|
+
event: PointerEvent & { currentTarget: EventTarget & HTMLDivElement }
|
|
39
|
+
) {
|
|
40
|
+
onpointerdown?.(event);
|
|
41
|
+
if (event.defaultPrevented) return;
|
|
42
|
+
ctx.startDrag(event);
|
|
43
|
+
}
|
|
44
|
+
</script>
|
|
45
|
+
|
|
46
|
+
<div
|
|
47
|
+
{...restProps}
|
|
48
|
+
bind:this={trackRef}
|
|
49
|
+
class={className}
|
|
50
|
+
style={resolvedStyle}
|
|
51
|
+
data-slider-track="true"
|
|
52
|
+
data-orientation={ctx.orientation}
|
|
53
|
+
data-disabled={ctx.isDisabled || undefined}
|
|
54
|
+
data-readonly={ctx.isReadOnly || undefined}
|
|
55
|
+
data-invalid={ctx.isInvalid || undefined}
|
|
56
|
+
data-dragging={ctx.draggingIndex !== null || undefined}
|
|
57
|
+
onpointerdown={handlePointerDown}
|
|
58
|
+
>
|
|
59
|
+
{@render children?.()}
|
|
60
|
+
</div>
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import type { Snippet } from 'svelte';
|
|
2
|
+
import type { HTMLAttributes } from 'svelte/elements';
|
|
3
|
+
import type { SliderChangeDetails, SliderContext, SliderOrientation } from './root/context';
|
|
4
|
+
export type { SliderChangeDetails, SliderOrientation };
|
|
5
|
+
export type SliderValue = number | number[];
|
|
6
|
+
export type SliderRootProps = {
|
|
7
|
+
/** A stable id, from which the component makes its internal ARIA ids. Give one on a server. */
|
|
8
|
+
id?: string;
|
|
9
|
+
/**
|
|
10
|
+
* The value: one number for one thumb, and an array in ascending order for a range with more
|
|
11
|
+
* thumbs. You can bind it with `bind:value`.
|
|
12
|
+
*/
|
|
13
|
+
value?: SliderValue;
|
|
14
|
+
/** The value at the start, for when you give no `value`. The default is `min`. */
|
|
15
|
+
defaultValue?: SliderValue;
|
|
16
|
+
/**
|
|
17
|
+
* Give your own code full control of the value. The component stops to write back to `value`,
|
|
18
|
+
* and it reports only through `onChange`. Thus the parent can refuse a change: the parent does
|
|
19
|
+
* not send the new value down. The default is off, because `bind:value` is the usual case and
|
|
20
|
+
* it needs the write-back.
|
|
21
|
+
*/
|
|
22
|
+
controlledValue?: boolean;
|
|
23
|
+
/** The component calls it on each change, also on each move of a drag. */
|
|
24
|
+
onChange?: (value: SliderValue, details: SliderChangeDetails) => void;
|
|
25
|
+
/**
|
|
26
|
+
* The component calls it when a sequence of changes ends: at the release of a key, and at the
|
|
27
|
+
* end of a drag. A change from assistive technology ends at once. Use it for work that must
|
|
28
|
+
* not run on each move.
|
|
29
|
+
*/
|
|
30
|
+
onChangeEnd?: (value: SliderValue, details: SliderChangeDetails) => void;
|
|
31
|
+
/** The lowest value. The default is 0. */
|
|
32
|
+
min?: number;
|
|
33
|
+
/** The highest value. The default is 100. */
|
|
34
|
+
max?: number;
|
|
35
|
+
/** The distance between two values. The default is 1. */
|
|
36
|
+
step?: number;
|
|
37
|
+
/**
|
|
38
|
+
* The distance of `PageUp`, `PageDown` and `Shift+Arrow`. The default is one tenth of the
|
|
39
|
+
* range, and never less than one step.
|
|
40
|
+
*/
|
|
41
|
+
largeStep?: number;
|
|
42
|
+
/** The steps that two thumbs of a range keep between them. The default is 0: they can meet. */
|
|
43
|
+
minStepsBetweenThumbs?: number;
|
|
44
|
+
/** The direction of the track. The default is horizontal. */
|
|
45
|
+
orientation?: SliderOrientation;
|
|
46
|
+
/** Disables the slider. The native inputs are disabled, and the thumbs leave the tab order. */
|
|
47
|
+
disabled?: boolean;
|
|
48
|
+
/** Keeps the thumbs in the tab order, but the value does not change. */
|
|
49
|
+
readonly?: boolean;
|
|
50
|
+
/** Marks the value as invalid. The thumbs get `aria-invalid`. */
|
|
51
|
+
invalid?: boolean;
|
|
52
|
+
/** The name of the native inputs. A `Slider.Thumb` can give its own. */
|
|
53
|
+
name?: string;
|
|
54
|
+
/** The id of the form that the native inputs belong to, when the slider is outside it. */
|
|
55
|
+
form?: string;
|
|
56
|
+
/** The format of the value in `Slider.Output` and in the text that a screen reader reads. */
|
|
57
|
+
formatOptions?: Intl.NumberFormatOptions;
|
|
58
|
+
/**
|
|
59
|
+
* The text that a screen reader reads for a value, in place of the formatted number. The
|
|
60
|
+
* second argument is the index of the thumb.
|
|
61
|
+
*/
|
|
62
|
+
getValueText?: (value: number, index: number) => string;
|
|
63
|
+
/** The accessible name of the slider, for when there is no `Slider.Label`. */
|
|
64
|
+
'aria-label'?: string;
|
|
65
|
+
/** The id of the element that gives the slider its name, in place of `Slider.Label`. */
|
|
66
|
+
'aria-labelledby'?: string;
|
|
67
|
+
/** The id of the element that describes the slider, for example an error message. */
|
|
68
|
+
'aria-describedby'?: string;
|
|
69
|
+
/** The content: the label, the output and the track. */
|
|
70
|
+
children?: Snippet;
|
|
71
|
+
/** The CSS class names of the root element. */
|
|
72
|
+
class?: string;
|
|
73
|
+
/** A bindable reference to the root element. */
|
|
74
|
+
element?: HTMLDivElement | null;
|
|
75
|
+
/** A bindable reference to the context, for a composition of your own. */
|
|
76
|
+
context?: SliderContext;
|
|
77
|
+
} & Omit<HTMLAttributes<HTMLDivElement>, 'class' | 'children' | 'id' | 'role' | 'aria-label' | 'aria-labelledby' | 'aria-describedby' | 'onchange'>;
|
|
78
|
+
export type SliderLabelProps = Omit<HTMLAttributes<HTMLSpanElement>, 'class'> & {
|
|
79
|
+
/** The CSS class names of the label. */
|
|
80
|
+
class?: string;
|
|
81
|
+
};
|
|
82
|
+
export type SliderOutputRenderState = {
|
|
83
|
+
/** The values, one per thumb. */
|
|
84
|
+
values: number[];
|
|
85
|
+
/** The text of each value, in the locale and the `formatOptions`. */
|
|
86
|
+
texts: string[];
|
|
87
|
+
/** The text of the whole value: one number, or a range in the locale. */
|
|
88
|
+
text: string;
|
|
89
|
+
};
|
|
90
|
+
export type SliderOutputProps = Omit<HTMLAttributes<HTMLOutputElement>, 'class' | 'children'> & {
|
|
91
|
+
/**
|
|
92
|
+
* The content. As a snippet with one argument, it receives the render state: `values`, `texts`
|
|
93
|
+
* and `text`. Without it, the component shows `text`.
|
|
94
|
+
*/
|
|
95
|
+
children?: Snippet<[SliderOutputRenderState]>;
|
|
96
|
+
/** The CSS class names of the element. */
|
|
97
|
+
class?: string;
|
|
98
|
+
};
|
|
99
|
+
export type SliderTrackProps = Omit<HTMLAttributes<HTMLDivElement>, 'class'> & {
|
|
100
|
+
/** The CSS class names of the track. */
|
|
101
|
+
class?: string;
|
|
102
|
+
/** A bindable reference to the track element. */
|
|
103
|
+
element?: HTMLDivElement | null;
|
|
104
|
+
};
|
|
105
|
+
export type SliderFillProps = Omit<HTMLAttributes<HTMLDivElement>, 'class'> & {
|
|
106
|
+
/** The CSS class names of the fill. */
|
|
107
|
+
class?: string;
|
|
108
|
+
};
|
|
109
|
+
export type SliderThumbRenderState = {
|
|
110
|
+
/** The value of the thumb. */
|
|
111
|
+
value: number;
|
|
112
|
+
/** The text of the value. */
|
|
113
|
+
text: string;
|
|
114
|
+
/** The index of the thumb. */
|
|
115
|
+
index: number;
|
|
116
|
+
/** Whether the thumb is under a pointer drag. */
|
|
117
|
+
dragging: boolean;
|
|
118
|
+
/** Whether the native input of the thumb has the focus. */
|
|
119
|
+
focused: boolean;
|
|
120
|
+
};
|
|
121
|
+
export type SliderThumbProps = Omit<HTMLAttributes<HTMLDivElement>, 'class' | 'children' | 'aria-label' | 'aria-labelledby'> & {
|
|
122
|
+
/**
|
|
123
|
+
* The index of the thumb, for a range. Without it, the thumbs take the indexes in mount order.
|
|
124
|
+
*/
|
|
125
|
+
index?: number;
|
|
126
|
+
/** The `name` of the native input of this thumb. It replaces the `name` of the root. */
|
|
127
|
+
name?: string;
|
|
128
|
+
/**
|
|
129
|
+
* The name of this thumb, before the name of the slider. A range of two thumbs without one
|
|
130
|
+
* gets "Minimum" and "Maximum" in the locale.
|
|
131
|
+
*/
|
|
132
|
+
'aria-label'?: string;
|
|
133
|
+
/** The id of the element that names this thumb, in place of `aria-label`. */
|
|
134
|
+
'aria-labelledby'?: string;
|
|
135
|
+
/**
|
|
136
|
+
* The content. As a snippet with one argument, it receives the render state: `value`, `text`,
|
|
137
|
+
* `index`, `dragging` and `focused`.
|
|
138
|
+
*/
|
|
139
|
+
children?: Snippet<[SliderThumbRenderState]> | Snippet;
|
|
140
|
+
/** The CSS class names of the thumb. */
|
|
141
|
+
class?: string;
|
|
142
|
+
/** A bindable reference to the thumb element. */
|
|
143
|
+
element?: HTMLDivElement | null;
|
|
144
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -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>
|