@xds/svelte 1.0.1 → 1.2.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.
- .diffend-unextracted-0 +2 -1
- package/dist/Accordion.svelte +7 -2
- package/dist/Avatar.svelte +10 -2
- package/dist/AvatarStack.svelte +11 -1
- package/dist/Badge.svelte +14 -3
- package/dist/Banner.svelte +6 -1
- package/dist/Breadcrumb.svelte +53 -0
- package/dist/Breadcrumb.svelte.d.ts +11 -0
- package/dist/Button.svelte +30 -2
- package/dist/Button.svelte.d.ts +10 -1
- package/dist/Card.svelte +16 -2
- package/dist/Checkbox.svelte +62 -10
- package/dist/Checkbox.svelte.d.ts +14 -3
- package/dist/Chip.svelte +3 -1
- package/dist/ChipBar.svelte +3 -2
- package/dist/Combobox.svelte +194 -0
- package/dist/Combobox.svelte.d.ts +37 -0
- package/dist/CountBadge.svelte +4 -2
- package/dist/DatePicker.svelte +16 -5
- package/dist/DateRangePicker.svelte +6 -1
- package/dist/Dialog.svelte +9 -2
- package/dist/Divider.svelte +7 -3
- package/dist/EmptyState.svelte +15 -3
- package/dist/Icon.svelte +11 -2
- package/dist/IconButton.svelte +30 -2
- package/dist/IconButton.svelte.d.ts +10 -1
- package/dist/Labels.svelte +29 -0
- package/dist/Labels.svelte.d.ts +17 -0
- package/dist/Link.svelte +86 -0
- package/dist/Link.svelte.d.ts +26 -0
- package/dist/MediaCard.svelte +4 -1
- package/dist/Menu.svelte +21 -2
- package/dist/Pagination.svelte +108 -0
- package/dist/Pagination.svelte.d.ts +14 -0
- package/dist/PlaceRow.svelte +6 -1
- package/dist/Popover.svelte +91 -0
- package/dist/Popover.svelte.d.ts +21 -0
- package/dist/PopoverSurface.svelte +77 -0
- package/dist/PopoverSurface.svelte.d.ts +21 -0
- package/dist/ProgressBar.svelte +3 -1
- package/dist/RadioGroup.svelte +23 -8
- package/dist/RadioGroup.svelte.d.ts +4 -2
- package/dist/SearchField.svelte +65 -18
- package/dist/SearchField.svelte.d.ts +14 -4
- package/dist/SectionHeader.svelte +7 -2
- package/dist/SegmentedControl.svelte +21 -7
- package/dist/SegmentedControl.svelte.d.ts +3 -2
- package/dist/Select.svelte +64 -12
- package/dist/Select.svelte.d.ts +9 -2
- package/dist/Sidebar.svelte +3 -0
- package/dist/Skeleton.svelte +10 -2
- package/dist/Slider.svelte +23 -7
- package/dist/Slider.svelte.d.ts +2 -2
- package/dist/Spinner.svelte +3 -2
- package/dist/Switch.svelte +53 -9
- package/dist/Switch.svelte.d.ts +13 -3
- package/dist/Table.svelte +92 -0
- package/dist/Table.svelte.d.ts +17 -0
- package/dist/Tabs.svelte +21 -6
- package/dist/Tabs.svelte.d.ts +2 -1
- package/dist/Text.svelte +3 -1
- package/dist/TextField.svelte +59 -14
- package/dist/TextField.svelte.d.ts +13 -3
- package/dist/Thumbnail.svelte +7 -1
- package/dist/TimePicker.svelte +222 -0
- package/dist/TimePicker.svelte.d.ts +20 -0
- package/dist/Toast.svelte +12 -2
- package/dist/ToastQueue.svelte +57 -0
- package/dist/ToastQueue.svelte.d.ts +20 -0
- package/dist/Tooltip.svelte +9 -2
- package/dist/TooltipOverlay.svelte +9 -2
- package/dist/base.d.ts +14 -0
- package/dist/base.js +27 -0
- package/dist/controllable.svelte.d.ts +28 -0
- package/dist/controllable.svelte.js +41 -0
- package/dist/index.d.ts +12 -2
- package/dist/index.js +12 -0
- package/dist/labels.svelte.d.ts +13 -0
- package/dist/labels.svelte.js +24 -0
- package/dist/modal-popover.svelte.js +5 -1
- package/dist/presence.svelte.js +5 -2
- package/dist/preview/Component.svelte +8 -3
- package/dist/preview/Preview.svelte +8 -2
- package/dist/preview/Preview.svelte.d.ts +2 -0
- package/dist/preview/components.d.ts +9 -2
- package/dist/preview/components.js +18 -1
- package/dist/preview/index.d.ts +1 -0
- package/dist/root.d.ts +4 -2
- package/eject/files/Accordion.svelte +100 -0
- package/eject/files/AccordionPanel.svelte +48 -0
- package/eject/files/Avatar.svelte +51 -0
- package/eject/files/AvatarStack.svelte +61 -0
- package/eject/files/Badge.svelte +52 -0
- package/eject/files/Banner.svelte +94 -0
- package/eject/files/Breadcrumb.svelte +52 -0
- package/eject/files/Button.svelte +126 -0
- package/eject/files/Card.svelte +112 -0
- package/eject/files/Checkbox.svelte +121 -0
- package/eject/files/Chip.svelte +51 -0
- package/eject/files/ChipBar.svelte +39 -0
- package/eject/files/Combobox.svelte +198 -0
- package/eject/files/CountBadge.svelte +51 -0
- package/eject/files/DatePicker.svelte +213 -0
- package/eject/files/DateRangePicker.svelte +294 -0
- package/eject/files/Dialog.svelte +119 -0
- package/eject/files/Divider.svelte +27 -0
- package/eject/files/EmptyState.svelte +52 -0
- package/eject/files/Icon.svelte +51 -0
- package/eject/files/IconButton.svelte +99 -0
- package/eject/files/Link.svelte +85 -0
- package/eject/files/ListRow.svelte +33 -0
- package/eject/files/MediaCard.svelte +117 -0
- package/eject/files/Menu.svelte +183 -0
- package/eject/files/Pagination.svelte +109 -0
- package/eject/files/PlaceRow.svelte +75 -0
- package/eject/files/Popover.svelte +100 -0
- package/eject/files/PopoverSurface.svelte +85 -0
- package/eject/files/ProgressBar.svelte +38 -0
- package/eject/files/RadioGroup.svelte +133 -0
- package/eject/files/SearchField.svelte +154 -0
- package/eject/files/SectionHeader.svelte +67 -0
- package/eject/files/SegmentedControl.svelte +85 -0
- package/eject/files/Select.svelte +276 -0
- package/eject/files/Sidebar.svelte +130 -0
- package/eject/files/SidebarRailButton.svelte +32 -0
- package/eject/files/Skeleton.svelte +55 -0
- package/eject/files/Slider.svelte +136 -0
- package/eject/files/Spinner.svelte +41 -0
- package/eject/files/Switch.svelte +105 -0
- package/eject/files/Table.svelte +90 -0
- package/eject/files/Tabs.svelte +85 -0
- package/eject/files/Text.svelte +43 -0
- package/eject/files/TextField.svelte +222 -0
- package/eject/files/Thumbnail.svelte +88 -0
- package/eject/files/TimePicker.svelte +237 -0
- package/eject/files/Toast.svelte +98 -0
- package/eject/files/ToastQueue.svelte +61 -0
- package/eject/files/Tooltip.svelte +51 -0
- package/eject/files/TooltipOverlay.svelte +88 -0
- package/eject/files/accordion.css +89 -0
- package/eject/files/accordion.ts +67 -0
- package/eject/files/avatar-stack.ts +57 -0
- package/eject/files/avatar.ts +42 -0
- package/eject/files/badge.ts +30 -0
- package/eject/files/banner.ts +71 -0
- package/eject/files/base.css +38 -0
- package/eject/files/breadcrumb.css +51 -0
- package/eject/files/breadcrumb.ts +42 -0
- package/eject/files/button.css +241 -0
- package/eject/files/button.ts +106 -0
- package/eject/files/card.ts +58 -0
- package/eject/files/checkbox.ts +52 -0
- package/eject/files/chip-bar.ts +16 -0
- package/eject/files/chip.ts +35 -0
- package/eject/files/combobox.css +269 -0
- package/eject/files/combobox.ts +85 -0
- package/eject/files/count-badge.ts +32 -0
- package/eject/files/date-picker.css +375 -0
- package/eject/files/date-picker.ts +280 -0
- package/eject/files/date-range-picker.ts +338 -0
- package/eject/files/dialog.css +139 -0
- package/eject/files/dialog.ts +15 -0
- package/eject/files/divider.ts +5 -0
- package/eject/files/empty-state.ts +22 -0
- package/eject/files/icon.ts +36 -0
- package/eject/files/link.css +64 -0
- package/eject/files/link.ts +63 -0
- package/eject/files/list-row.ts +66 -0
- package/eject/files/media-card.ts +66 -0
- package/eject/files/menu.css +192 -0
- package/eject/files/menu.ts +84 -0
- package/eject/files/overlay.css +40 -0
- package/eject/files/pagination.css +106 -0
- package/eject/files/pagination.ts +68 -0
- package/eject/files/parts.ts +17 -0
- package/eject/files/popover.css +101 -0
- package/eject/files/popover.ts +37 -0
- package/eject/files/progress-bar.ts +26 -0
- package/eject/files/radio-group.css +184 -0
- package/eject/files/radio-group.ts +30 -0
- package/eject/files/section-header.ts +32 -0
- package/eject/files/segmented-control.ts +33 -0
- package/eject/files/select.css +347 -0
- package/eject/files/select.ts +77 -0
- package/eject/files/sidebar.ts +75 -0
- package/eject/files/skeleton.ts +51 -0
- package/eject/files/slider.css +160 -0
- package/eject/files/slider.ts +80 -0
- package/eject/files/spinner.ts +22 -0
- package/eject/files/switch.ts +45 -0
- package/eject/files/table.css +107 -0
- package/eject/files/table.ts +84 -0
- package/eject/files/tabs.css +120 -0
- package/eject/files/tabs.ts +37 -0
- package/eject/files/text-field.css +225 -0
- package/eject/files/text-field.ts +64 -0
- package/eject/files/text.ts +38 -0
- package/eject/files/thumbnail.ts +51 -0
- package/eject/files/time-picker.ts +113 -0
- package/eject/files/toast-queue.ts +64 -0
- package/eject/files/toast.ts +31 -0
- package/eject/files/tooltip.css +111 -0
- package/eject/files/tooltip.ts +21 -0
- package/eject/manifest.json +1053 -0
- package/package.json +12 -4
- package/styles.css +1064 -169
- package/tailwind.css +1295 -218
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
Ejected from `@xds/svelte` 1.2.0 by `xds eject`. This copy is the app's own: change it
|
|
3
|
+
as you need. `xds eject --list` says which ejected files differ from what was copied.
|
|
4
|
+
-->
|
|
5
|
+
<!--
|
|
6
|
+
@component
|
|
7
|
+
A field for choosing one option from a list by typing part of its name. As the person types, the
|
|
8
|
+
options whose label holds what was typed open under it, those that start with it first, and the
|
|
9
|
+
first is highlighted, so Enter chooses it. Choosing an option calls `onChange`, puts its label in
|
|
10
|
+
the field and closes the list. Text that names no option goes back to the chosen option's label
|
|
11
|
+
when the field is left. When nothing matches, the list says so. Without `value` it keeps its own
|
|
12
|
+
choice, from `defaultValue`.
|
|
13
|
+
-->
|
|
14
|
+
<script module lang="ts">
|
|
15
|
+
import type { RootProps } from "@xds/svelte/base";
|
|
16
|
+
import type { ComboboxBaseProps } from "./combobox.ts";
|
|
17
|
+
|
|
18
|
+
export type ComboboxProps<Value extends string = string> = ComboboxBaseProps<Value> &
|
|
19
|
+
RootProps & {
|
|
20
|
+
onChange?: (value: Value) => void;
|
|
21
|
+
};
|
|
22
|
+
</script>
|
|
23
|
+
|
|
24
|
+
<script lang="ts" generics="Value extends string = string">
|
|
25
|
+
import {
|
|
26
|
+
controllable,
|
|
27
|
+
createItemPropsCache,
|
|
28
|
+
followFormReset,
|
|
29
|
+
Portal,
|
|
30
|
+
readCssPx,
|
|
31
|
+
rootAttributes,
|
|
32
|
+
type SelectOption,
|
|
33
|
+
selectOptionIds,
|
|
34
|
+
space,
|
|
35
|
+
useLabels,
|
|
36
|
+
} from "@xds/svelte/base";
|
|
37
|
+
import * as combobox from "@zag-js/combobox";
|
|
38
|
+
import { mergeProps, normalizeProps, useMachine } from "@zag-js/svelte";
|
|
39
|
+
import { COMBOBOX_PARTS as PARTS, matchOptions } from "./combobox.ts";
|
|
40
|
+
import Icon from "./Icon.svelte";
|
|
41
|
+
import { classes } from "./parts.ts";
|
|
42
|
+
|
|
43
|
+
let {
|
|
44
|
+
label,
|
|
45
|
+
options,
|
|
46
|
+
defaultValue,
|
|
47
|
+
placeholder,
|
|
48
|
+
description,
|
|
49
|
+
error,
|
|
50
|
+
name,
|
|
51
|
+
disabled,
|
|
52
|
+
onChange,
|
|
53
|
+
emptyLabel,
|
|
54
|
+
class: className,
|
|
55
|
+
style,
|
|
56
|
+
...rest
|
|
57
|
+
}: ComboboxProps<Value> = $props();
|
|
58
|
+
|
|
59
|
+
const labels = useLabels();
|
|
60
|
+
const control = controllable<Value | undefined, Value>(
|
|
61
|
+
() => rest,
|
|
62
|
+
"value",
|
|
63
|
+
() => rest.value,
|
|
64
|
+
() => defaultValue,
|
|
65
|
+
() => onChange,
|
|
66
|
+
);
|
|
67
|
+
const value = $derived(control.current);
|
|
68
|
+
let root = $state<HTMLDivElement>();
|
|
69
|
+
followFormReset(() => root, control);
|
|
70
|
+
|
|
71
|
+
const id = $props.id();
|
|
72
|
+
const descriptionId = `${id}-description`;
|
|
73
|
+
const errorId = `${id}-error`;
|
|
74
|
+
const invalid = $derived(error !== undefined);
|
|
75
|
+
// What the person typed, which narrows the list. Choosing an option puts its label in the field
|
|
76
|
+
// without narrowing it, so the list opens on every option again.
|
|
77
|
+
let query = $state("");
|
|
78
|
+
const shown = $derived(matchOptions(options, query));
|
|
79
|
+
const collection = $derived(
|
|
80
|
+
combobox.collection<SelectOption<Value>>({
|
|
81
|
+
items: shown,
|
|
82
|
+
itemToValue: (option) => option.value,
|
|
83
|
+
itemToString: (option) => option.label,
|
|
84
|
+
}),
|
|
85
|
+
);
|
|
86
|
+
const service = useMachine(combobox.machine, () => ({
|
|
87
|
+
id,
|
|
88
|
+
collection,
|
|
89
|
+
value: value === undefined ? [] : [value],
|
|
90
|
+
onValueChange: (details: combobox.ValueChangeDetails<SelectOption<Value>>) => {
|
|
91
|
+
const next = details.value[0];
|
|
92
|
+
if (next !== undefined) control.set(next as Value);
|
|
93
|
+
},
|
|
94
|
+
onInputValueChange: (details: combobox.InputValueChangeDetails) => {
|
|
95
|
+
if (details.reason === "input-change") query = details.inputValue;
|
|
96
|
+
},
|
|
97
|
+
onOpenChange: (details: combobox.OpenChangeDetails) => {
|
|
98
|
+
if (!details.open) query = "";
|
|
99
|
+
},
|
|
100
|
+
// The first match is highlighted as the person types, so Enter chooses it.
|
|
101
|
+
inputBehavior: "autohighlight" as const,
|
|
102
|
+
selectionBehavior: "replace" as const,
|
|
103
|
+
openOnClick: true,
|
|
104
|
+
disabled,
|
|
105
|
+
invalid,
|
|
106
|
+
placeholder,
|
|
107
|
+
positioning: {
|
|
108
|
+
placement: "bottom-start" as const,
|
|
109
|
+
gutter: readCssPx(null, "--xds-space-1", space[1]),
|
|
110
|
+
sameWidth: true,
|
|
111
|
+
overflowPadding: readCssPx(null, "--xds-space-3", space[3]),
|
|
112
|
+
},
|
|
113
|
+
}));
|
|
114
|
+
const api = $derived(combobox.connect(service, normalizeProps));
|
|
115
|
+
// The rows' props as they last were while they draw the same, so that a step of the highlight
|
|
116
|
+
// sets the attributes and the handlers of the two rows it moves between, not of every row.
|
|
117
|
+
const keep = createItemPropsCache();
|
|
118
|
+
// Zag puts the chosen label back only when the list closes by a press outside; after Escape,
|
|
119
|
+
// text that names no option would stay in a field that has left it.
|
|
120
|
+
const input = $derived(
|
|
121
|
+
mergeProps(api.getInputProps(), {
|
|
122
|
+
onblur: () => {
|
|
123
|
+
const chosen = options.find((option) => option.value === value)?.label ?? "";
|
|
124
|
+
const now = combobox.connect(service, normalizeProps);
|
|
125
|
+
if (now.inputValue !== chosen) now.setInputValue(chosen, "script");
|
|
126
|
+
},
|
|
127
|
+
}),
|
|
128
|
+
);
|
|
129
|
+
</script>
|
|
130
|
+
|
|
131
|
+
<!-- Merged, so that the app's style joins any the machine writes on the root. -->
|
|
132
|
+
<div
|
|
133
|
+
bind:this={root}
|
|
134
|
+
{...mergeProps(api.getRootProps(), {
|
|
135
|
+
...rootAttributes(rest),
|
|
136
|
+
class: classes("{{prefix}}-combobox", className),
|
|
137
|
+
style,
|
|
138
|
+
})}
|
|
139
|
+
>
|
|
140
|
+
<label {...api.getLabelProps()} class={PARTS.label}>{label}</label>
|
|
141
|
+
<div {...api.getControlProps()} class={PARTS.control}>
|
|
142
|
+
<input
|
|
143
|
+
{...input}
|
|
144
|
+
aria-describedby={invalid ? errorId : description ? descriptionId : undefined}
|
|
145
|
+
class={PARTS.input}
|
|
146
|
+
/>
|
|
147
|
+
<Icon name="chevrons-up-down" size="sm" class={PARTS.icon} />
|
|
148
|
+
</div>
|
|
149
|
+
<div class={PARTS.footer}>
|
|
150
|
+
{#if !invalid && description}
|
|
151
|
+
<span id={descriptionId} class={PARTS.description}>{description}</span>
|
|
152
|
+
{/if}
|
|
153
|
+
<span id={errorId} class={PARTS.error} aria-live="polite">{error}</span>
|
|
154
|
+
</div>
|
|
155
|
+
<!-- What a form submits: the option's value, where the field holds its label. -->
|
|
156
|
+
{#if name !== undefined}
|
|
157
|
+
<input type="hidden" {name} value={value ?? ""} />
|
|
158
|
+
{/if}
|
|
159
|
+
<Portal>
|
|
160
|
+
<div {...api.getPositionerProps()} class={PARTS.positioner}>
|
|
161
|
+
<!-- Zag hides it while closed. It goes at once, with no animation out: it comes and goes as
|
|
162
|
+
the person types. -->
|
|
163
|
+
<div {...api.getContentProps()} class={PARTS.content}>
|
|
164
|
+
{#if shown.length === 0}
|
|
165
|
+
<div {...api.getEmptyProps()} class={PARTS.empty}>{emptyLabel ?? labels().noMatches}</div>
|
|
166
|
+
{:else}
|
|
167
|
+
<div {...api.getListProps()} class={PARTS.list}>
|
|
168
|
+
{#each shown as option, index (option.value)}
|
|
169
|
+
{@const ids = selectOptionIds(id, index)}
|
|
170
|
+
<div
|
|
171
|
+
{...keep(`item:${option.value}`, api.getItemProps({ item: option }))}
|
|
172
|
+
aria-labelledby={ids.label}
|
|
173
|
+
aria-describedby={option.description ? ids.description : undefined}
|
|
174
|
+
class={PARTS.item}
|
|
175
|
+
>
|
|
176
|
+
<span class={PARTS.itemWords}>
|
|
177
|
+
<span
|
|
178
|
+
{...keep(`text:${option.value}`, api.getItemTextProps({ item: option }))}
|
|
179
|
+
id={ids.label}
|
|
180
|
+
class={PARTS.itemLabel}>{option.label}</span
|
|
181
|
+
>
|
|
182
|
+
{#if option.description}
|
|
183
|
+
<span id={ids.description} class={PARTS.itemDescription}
|
|
184
|
+
>{option.description}</span
|
|
185
|
+
>
|
|
186
|
+
{/if}
|
|
187
|
+
</span>
|
|
188
|
+
{#if api.getItemState({ item: option }).selected}
|
|
189
|
+
<Icon name="check" size="sm" />
|
|
190
|
+
{/if}
|
|
191
|
+
</div>
|
|
192
|
+
{/each}
|
|
193
|
+
</div>
|
|
194
|
+
{/if}
|
|
195
|
+
</div>
|
|
196
|
+
</div>
|
|
197
|
+
</Portal>
|
|
198
|
+
</div>
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
Ejected from `@xds/svelte` 1.2.0 by `xds eject`. This copy is the app's own: change it
|
|
3
|
+
as you need. `xds eject --list` says which ejected files differ from what was copied.
|
|
4
|
+
-->
|
|
5
|
+
<!--
|
|
6
|
+
@component
|
|
7
|
+
A count of something waiting: unread messages, items in a cart. On its own it is laid out
|
|
8
|
+
inline; around a child it sits on the child's top end corner.
|
|
9
|
+
-->
|
|
10
|
+
<script module lang="ts">
|
|
11
|
+
import type { RootProps } from "@xds/svelte/base";
|
|
12
|
+
import type { Snippet } from "svelte";
|
|
13
|
+
import type { CountBadgeBaseProps } from "./count-badge.ts";
|
|
14
|
+
|
|
15
|
+
export type CountBadgeProps = CountBadgeBaseProps &
|
|
16
|
+
RootProps & {
|
|
17
|
+
/** What the count belongs to: an icon, an avatar, a button. The badge sits on its corner. */
|
|
18
|
+
children?: Snippet;
|
|
19
|
+
};
|
|
20
|
+
</script>
|
|
21
|
+
|
|
22
|
+
<script lang="ts">
|
|
23
|
+
import { rootAttributes } from "@xds/svelte/base";
|
|
24
|
+
import { countBadgeAnchorClass, countBadgeClass, countBadgeText } from "./count-badge.ts";
|
|
25
|
+
import { classes, rootPart } from "./parts.ts";
|
|
26
|
+
|
|
27
|
+
let { count, max, label, class: className, style, children, ...rest }: CountBadgeProps = $props();
|
|
28
|
+
|
|
29
|
+
// The root is the anchor around the child, or the badge itself when there is none.
|
|
30
|
+
const root = $derived(classes(rootPart("CountBadge"), className));
|
|
31
|
+
</script>
|
|
32
|
+
|
|
33
|
+
{#snippet badge(onChild: boolean)}
|
|
34
|
+
{#if count > 0}
|
|
35
|
+
<span
|
|
36
|
+
{...onChild ? {} : rootAttributes(rest)}
|
|
37
|
+
role={label ? "img" : undefined}
|
|
38
|
+
aria-label={label || undefined}
|
|
39
|
+
class={classes(countBadgeClass(onChild), !onChild && root)}
|
|
40
|
+
style={onChild ? undefined : style}>{countBadgeText(count, max)}</span
|
|
41
|
+
>
|
|
42
|
+
{/if}
|
|
43
|
+
{/snippet}
|
|
44
|
+
|
|
45
|
+
{#if children}
|
|
46
|
+
<span {...rootAttributes(rest)} class={classes(countBadgeAnchorClass, root)} {style}
|
|
47
|
+
>{@render children()}{@render badge(true)}</span
|
|
48
|
+
>
|
|
49
|
+
{:else}
|
|
50
|
+
{@render badge(false)}
|
|
51
|
+
{/if}
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
Ejected from `@xds/svelte` 1.2.0 by `xds eject`. This copy is the app's own: change it
|
|
3
|
+
as you need. `xds eject --list` says which ejected files differ from what was copied.
|
|
4
|
+
-->
|
|
5
|
+
<!--
|
|
6
|
+
@component
|
|
7
|
+
A field for choosing one day. Closed, it looks like a Select: the chosen day in the locale's
|
|
8
|
+
medium form, or the placeholder. Pressed, it opens a calendar of one month under itself, on the
|
|
9
|
+
chosen day, or on today while none is. A press on a day, or Enter on the focused one, calls
|
|
10
|
+
`onChange` with the day as `YYYY-MM-DD` and closes the calendar; Escape or a press outside
|
|
11
|
+
closes it without a choice.
|
|
12
|
+
|
|
13
|
+
The field is a button that says it opens a dialog and whether it is open. In the calendar the
|
|
14
|
+
arrow keys move a day or a week, Page Up and Page Down a month (a year with Shift), and Home and
|
|
15
|
+
End to the first and the last day of the month, never past `min` or `max`. Tab goes round the
|
|
16
|
+
month buttons and the focused day. The chosen day is selected for assistive technology, and
|
|
17
|
+
today is the current date.
|
|
18
|
+
-->
|
|
19
|
+
<script module lang="ts">
|
|
20
|
+
import type { RootProps } from "@xds/svelte/base";
|
|
21
|
+
import type { DatePickerBaseProps, IsoDate } from "./date-picker.ts";
|
|
22
|
+
|
|
23
|
+
export type { IsoDate } from "./date-picker.ts";
|
|
24
|
+
|
|
25
|
+
export type DatePickerProps = DatePickerBaseProps &
|
|
26
|
+
RootProps & {
|
|
27
|
+
onChange: (value: IsoDate) => void;
|
|
28
|
+
};
|
|
29
|
+
</script>
|
|
30
|
+
|
|
31
|
+
<script lang="ts">
|
|
32
|
+
import {
|
|
33
|
+
createPresence,
|
|
34
|
+
modalPopover,
|
|
35
|
+
Portal,
|
|
36
|
+
readCssPx,
|
|
37
|
+
rootAttributes,
|
|
38
|
+
space,
|
|
39
|
+
underlayLayerStyle,
|
|
40
|
+
useLabels,
|
|
41
|
+
} from "@xds/svelte/base";
|
|
42
|
+
import * as datePicker from "@zag-js/date-picker";
|
|
43
|
+
import { mergeProps, normalizeProps, useMachine } from "@zag-js/svelte";
|
|
44
|
+
import { untrack } from "svelte";
|
|
45
|
+
import {
|
|
46
|
+
DATE_PICKER_NAV_CLASS,
|
|
47
|
+
datePickerContentAttrs,
|
|
48
|
+
datePickerIds,
|
|
49
|
+
datePickerLocale,
|
|
50
|
+
datePickerMachineProps,
|
|
51
|
+
datePickerTriggerAttrs,
|
|
52
|
+
focusOpeningDay,
|
|
53
|
+
formatDay,
|
|
54
|
+
keepCalendarFocus,
|
|
55
|
+
keepDayFocused,
|
|
56
|
+
} from "./date-picker.ts";
|
|
57
|
+
import Icon from "./Icon.svelte";
|
|
58
|
+
import { classes } from "./parts.ts";
|
|
59
|
+
|
|
60
|
+
// The rest stays reactive: the machine's props read it each time it changes.
|
|
61
|
+
let { onChange, class: className, style, ...props }: DatePickerProps = $props();
|
|
62
|
+
|
|
63
|
+
const id = $props.id();
|
|
64
|
+
const ids = datePickerIds(id);
|
|
65
|
+
const locale = $derived(datePickerLocale(props.locale));
|
|
66
|
+
const labels = useLabels();
|
|
67
|
+
const service = useMachine(datePicker.machine, () =>
|
|
68
|
+
datePickerMachineProps(props, {
|
|
69
|
+
id,
|
|
70
|
+
locale,
|
|
71
|
+
onChange,
|
|
72
|
+
gutter: readCssPx(null, "--xds-space-1", space[1]),
|
|
73
|
+
labels: labels(),
|
|
74
|
+
}),
|
|
75
|
+
);
|
|
76
|
+
const api = $derived(datePicker.connect(service, normalizeProps));
|
|
77
|
+
const trigger = $derived(api.getTriggerProps());
|
|
78
|
+
const invalid = $derived(props.error !== undefined);
|
|
79
|
+
let positioner = $state<HTMLDivElement>();
|
|
80
|
+
let surface = $state<HTMLDivElement>();
|
|
81
|
+
modalPopover(
|
|
82
|
+
() => api.open,
|
|
83
|
+
() => positioner,
|
|
84
|
+
);
|
|
85
|
+
const present = createPresence(
|
|
86
|
+
() => api.open,
|
|
87
|
+
() => [surface],
|
|
88
|
+
);
|
|
89
|
+
// Derived, so that the effect below runs only when the calendar opens or closes or the focused
|
|
90
|
+
// day or the month changes, not on every change of the machine.
|
|
91
|
+
const open = $derived(api.open);
|
|
92
|
+
const focused = $derived(api.focusedValue.toString());
|
|
93
|
+
const month = $derived(api.visibleRange.start.toString());
|
|
94
|
+
$effect(() => {
|
|
95
|
+
const isOpen = open;
|
|
96
|
+
void focused;
|
|
97
|
+
void month;
|
|
98
|
+
untrack(() => keepDayFocused(isOpen, surface));
|
|
99
|
+
});
|
|
100
|
+
</script>
|
|
101
|
+
|
|
102
|
+
<!-- Merged, so that the app's style joins any the machine writes on the root. -->
|
|
103
|
+
<div
|
|
104
|
+
{...mergeProps(api.getRootProps(), {
|
|
105
|
+
...rootAttributes(props),
|
|
106
|
+
class: classes("{{prefix}}-date-picker", className),
|
|
107
|
+
style,
|
|
108
|
+
})}
|
|
109
|
+
>
|
|
110
|
+
<!-- A span: the field is a button, which a <label> cannot point at. -->
|
|
111
|
+
<span id={ids.label} class="{{prefix}}-date-picker__label">{props.label}</span>
|
|
112
|
+
<!-- What Zag places the calendar against. -->
|
|
113
|
+
<div {...api.getControlProps()} class="{{prefix}}-date-picker__control">
|
|
114
|
+
<button
|
|
115
|
+
{...trigger}
|
|
116
|
+
{...datePickerTriggerAttrs(props, ids)}
|
|
117
|
+
type="button"
|
|
118
|
+
onclick={(event) => {
|
|
119
|
+
focusOpeningDay(api, props);
|
|
120
|
+
trigger.onclick?.(event);
|
|
121
|
+
}}
|
|
122
|
+
class="{{prefix}}-date-picker__trigger"
|
|
123
|
+
>
|
|
124
|
+
<span id={ids.value} class="{{prefix}}-date-picker__value"
|
|
125
|
+
>{props.value === undefined ? props.placeholder : formatDay(props.value, locale)}</span
|
|
126
|
+
>
|
|
127
|
+
<Icon name="calendar" size="sm" class="text-xds-field-placeholder" />
|
|
128
|
+
</button>
|
|
129
|
+
</div>
|
|
130
|
+
<!-- One line under the box: the help, or the error in its place. The error stays mounted as
|
|
131
|
+
a live region so assistive technology announces it when it appears. -->
|
|
132
|
+
<div class="{{prefix}}-date-picker__footer">
|
|
133
|
+
{#if !invalid && props.description}
|
|
134
|
+
<span id={ids.description} class="{{prefix}}-date-picker__description"
|
|
135
|
+
>{props.description}</span
|
|
136
|
+
>
|
|
137
|
+
{/if}
|
|
138
|
+
<span id={ids.error} class="{{prefix}}-date-picker__error" aria-live="polite"
|
|
139
|
+
>{props.error}</span
|
|
140
|
+
>
|
|
141
|
+
</div>
|
|
142
|
+
<!-- What a form submits. Zag gives the name to its text input, which is not drawn. -->
|
|
143
|
+
{#if props.name !== undefined}
|
|
144
|
+
<input type="hidden" name={props.name} value={props.value ?? ""} />
|
|
145
|
+
{/if}
|
|
146
|
+
<Portal>
|
|
147
|
+
{#if api.open}
|
|
148
|
+
<!-- Takes a press outside, which then only closes the calendar. The focus stays in the
|
|
149
|
+
calendar until it hands it back to the field. -->
|
|
150
|
+
<!-- svelte-ignore a11y_no_static_element_interactions -->
|
|
151
|
+
<div
|
|
152
|
+
class="{{prefix}}-date-picker__underlay"
|
|
153
|
+
style:--layer-index={underlayLayerStyle(api.getPositionerProps().style)?.["--layer-index"]}
|
|
154
|
+
onmousedown={(event) => event.preventDefault()}
|
|
155
|
+
></div>
|
|
156
|
+
{/if}
|
|
157
|
+
<div
|
|
158
|
+
{...api.getPositionerProps()}
|
|
159
|
+
bind:this={positioner}
|
|
160
|
+
class="{{prefix}}-date-picker__positioner"
|
|
161
|
+
>
|
|
162
|
+
<!-- Shown until its exit animation ends; Zag would hide it as it closes. -->
|
|
163
|
+
<div
|
|
164
|
+
{...api.getContentProps()}
|
|
165
|
+
{...datePickerContentAttrs(ids)}
|
|
166
|
+
bind:this={surface}
|
|
167
|
+
hidden={!present()}
|
|
168
|
+
onkeydown={(event) => keepCalendarFocus(event, surface)}
|
|
169
|
+
class="{{prefix}}-date-picker__content"
|
|
170
|
+
>
|
|
171
|
+
<div class="{{prefix}}-date-picker__header">
|
|
172
|
+
<button {...api.getPreviousTriggerProps()} type="button" class={DATE_PICKER_NAV_CLASS}>
|
|
173
|
+
<Icon name="chevron-left" size="sm" />
|
|
174
|
+
</button>
|
|
175
|
+
<span {...api.getRangeTextProps()} class="{{prefix}}-date-picker__heading"
|
|
176
|
+
>{api.visibleRangeText.start}</span
|
|
177
|
+
>
|
|
178
|
+
<button {...api.getNextTriggerProps()} type="button" class={DATE_PICKER_NAV_CLASS}>
|
|
179
|
+
<Icon name="chevron-right" size="sm" />
|
|
180
|
+
</button>
|
|
181
|
+
</div>
|
|
182
|
+
<table {...api.getTableProps({ view: "day" })} class="{{prefix}}-date-picker__grid">
|
|
183
|
+
<thead {...api.getTableHeadProps({ view: "day" })}>
|
|
184
|
+
<tr {...api.getTableRowProps({ view: "day" })}>
|
|
185
|
+
{#each api.weekDays as weekDay (weekDay.long)}
|
|
186
|
+
<th scope="col" class="{{prefix}}-date-picker__weekday">{weekDay.narrow}</th>
|
|
187
|
+
{/each}
|
|
188
|
+
</tr>
|
|
189
|
+
</thead>
|
|
190
|
+
<tbody {...api.getTableBodyProps({ view: "day" })}>
|
|
191
|
+
<!-- Keyed by the day, not by the object: Zag hands out new dates on every change, and
|
|
192
|
+
a cell made anew would drop the focus it holds when a move is held at `min` or
|
|
193
|
+
`max`. -->
|
|
194
|
+
{#each api.weeks as week (week[0]?.toString())}
|
|
195
|
+
<tr {...api.getTableRowProps({ view: "day" })}>
|
|
196
|
+
{#each week as value (value.toString())}
|
|
197
|
+
<td {...api.getDayTableCellProps({ value })} class="{{prefix}}-date-picker__cell">
|
|
198
|
+
<div
|
|
199
|
+
{...api.getDayTableCellTriggerProps({ value })}
|
|
200
|
+
class="{{prefix}}-date-picker__day"
|
|
201
|
+
>
|
|
202
|
+
{value.day}
|
|
203
|
+
</div>
|
|
204
|
+
</td>
|
|
205
|
+
{/each}
|
|
206
|
+
</tr>
|
|
207
|
+
{/each}
|
|
208
|
+
</tbody>
|
|
209
|
+
</table>
|
|
210
|
+
</div>
|
|
211
|
+
</div>
|
|
212
|
+
</Portal>
|
|
213
|
+
</div>
|