@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.
Files changed (207) hide show
  1. .diffend-unextracted-0 +2 -1
  2. package/dist/Accordion.svelte +7 -2
  3. package/dist/Avatar.svelte +10 -2
  4. package/dist/AvatarStack.svelte +11 -1
  5. package/dist/Badge.svelte +14 -3
  6. package/dist/Banner.svelte +6 -1
  7. package/dist/Breadcrumb.svelte +53 -0
  8. package/dist/Breadcrumb.svelte.d.ts +11 -0
  9. package/dist/Button.svelte +30 -2
  10. package/dist/Button.svelte.d.ts +10 -1
  11. package/dist/Card.svelte +16 -2
  12. package/dist/Checkbox.svelte +62 -10
  13. package/dist/Checkbox.svelte.d.ts +14 -3
  14. package/dist/Chip.svelte +3 -1
  15. package/dist/ChipBar.svelte +3 -2
  16. package/dist/Combobox.svelte +194 -0
  17. package/dist/Combobox.svelte.d.ts +37 -0
  18. package/dist/CountBadge.svelte +4 -2
  19. package/dist/DatePicker.svelte +16 -5
  20. package/dist/DateRangePicker.svelte +6 -1
  21. package/dist/Dialog.svelte +9 -2
  22. package/dist/Divider.svelte +7 -3
  23. package/dist/EmptyState.svelte +15 -3
  24. package/dist/Icon.svelte +11 -2
  25. package/dist/IconButton.svelte +30 -2
  26. package/dist/IconButton.svelte.d.ts +10 -1
  27. package/dist/Labels.svelte +29 -0
  28. package/dist/Labels.svelte.d.ts +17 -0
  29. package/dist/Link.svelte +86 -0
  30. package/dist/Link.svelte.d.ts +26 -0
  31. package/dist/MediaCard.svelte +4 -1
  32. package/dist/Menu.svelte +21 -2
  33. package/dist/Pagination.svelte +108 -0
  34. package/dist/Pagination.svelte.d.ts +14 -0
  35. package/dist/PlaceRow.svelte +6 -1
  36. package/dist/Popover.svelte +91 -0
  37. package/dist/Popover.svelte.d.ts +21 -0
  38. package/dist/PopoverSurface.svelte +77 -0
  39. package/dist/PopoverSurface.svelte.d.ts +21 -0
  40. package/dist/ProgressBar.svelte +3 -1
  41. package/dist/RadioGroup.svelte +23 -8
  42. package/dist/RadioGroup.svelte.d.ts +4 -2
  43. package/dist/SearchField.svelte +65 -18
  44. package/dist/SearchField.svelte.d.ts +14 -4
  45. package/dist/SectionHeader.svelte +7 -2
  46. package/dist/SegmentedControl.svelte +21 -7
  47. package/dist/SegmentedControl.svelte.d.ts +3 -2
  48. package/dist/Select.svelte +64 -12
  49. package/dist/Select.svelte.d.ts +9 -2
  50. package/dist/Sidebar.svelte +3 -0
  51. package/dist/Skeleton.svelte +10 -2
  52. package/dist/Slider.svelte +23 -7
  53. package/dist/Slider.svelte.d.ts +2 -2
  54. package/dist/Spinner.svelte +3 -2
  55. package/dist/Switch.svelte +53 -9
  56. package/dist/Switch.svelte.d.ts +13 -3
  57. package/dist/Table.svelte +92 -0
  58. package/dist/Table.svelte.d.ts +17 -0
  59. package/dist/Tabs.svelte +21 -6
  60. package/dist/Tabs.svelte.d.ts +2 -1
  61. package/dist/Text.svelte +3 -1
  62. package/dist/TextField.svelte +59 -14
  63. package/dist/TextField.svelte.d.ts +13 -3
  64. package/dist/Thumbnail.svelte +7 -1
  65. package/dist/TimePicker.svelte +222 -0
  66. package/dist/TimePicker.svelte.d.ts +20 -0
  67. package/dist/Toast.svelte +12 -2
  68. package/dist/ToastQueue.svelte +57 -0
  69. package/dist/ToastQueue.svelte.d.ts +20 -0
  70. package/dist/Tooltip.svelte +9 -2
  71. package/dist/TooltipOverlay.svelte +9 -2
  72. package/dist/base.d.ts +14 -0
  73. package/dist/base.js +27 -0
  74. package/dist/controllable.svelte.d.ts +28 -0
  75. package/dist/controllable.svelte.js +41 -0
  76. package/dist/index.d.ts +12 -2
  77. package/dist/index.js +12 -0
  78. package/dist/labels.svelte.d.ts +13 -0
  79. package/dist/labels.svelte.js +24 -0
  80. package/dist/modal-popover.svelte.js +5 -1
  81. package/dist/presence.svelte.js +5 -2
  82. package/dist/preview/Component.svelte +8 -3
  83. package/dist/preview/Preview.svelte +8 -2
  84. package/dist/preview/Preview.svelte.d.ts +2 -0
  85. package/dist/preview/components.d.ts +9 -2
  86. package/dist/preview/components.js +18 -1
  87. package/dist/preview/index.d.ts +1 -0
  88. package/dist/root.d.ts +4 -2
  89. package/eject/files/Accordion.svelte +100 -0
  90. package/eject/files/AccordionPanel.svelte +48 -0
  91. package/eject/files/Avatar.svelte +51 -0
  92. package/eject/files/AvatarStack.svelte +61 -0
  93. package/eject/files/Badge.svelte +52 -0
  94. package/eject/files/Banner.svelte +94 -0
  95. package/eject/files/Breadcrumb.svelte +52 -0
  96. package/eject/files/Button.svelte +126 -0
  97. package/eject/files/Card.svelte +112 -0
  98. package/eject/files/Checkbox.svelte +121 -0
  99. package/eject/files/Chip.svelte +51 -0
  100. package/eject/files/ChipBar.svelte +39 -0
  101. package/eject/files/Combobox.svelte +198 -0
  102. package/eject/files/CountBadge.svelte +51 -0
  103. package/eject/files/DatePicker.svelte +213 -0
  104. package/eject/files/DateRangePicker.svelte +294 -0
  105. package/eject/files/Dialog.svelte +119 -0
  106. package/eject/files/Divider.svelte +27 -0
  107. package/eject/files/EmptyState.svelte +52 -0
  108. package/eject/files/Icon.svelte +51 -0
  109. package/eject/files/IconButton.svelte +99 -0
  110. package/eject/files/Link.svelte +85 -0
  111. package/eject/files/ListRow.svelte +33 -0
  112. package/eject/files/MediaCard.svelte +117 -0
  113. package/eject/files/Menu.svelte +183 -0
  114. package/eject/files/Pagination.svelte +109 -0
  115. package/eject/files/PlaceRow.svelte +75 -0
  116. package/eject/files/Popover.svelte +100 -0
  117. package/eject/files/PopoverSurface.svelte +85 -0
  118. package/eject/files/ProgressBar.svelte +38 -0
  119. package/eject/files/RadioGroup.svelte +133 -0
  120. package/eject/files/SearchField.svelte +154 -0
  121. package/eject/files/SectionHeader.svelte +67 -0
  122. package/eject/files/SegmentedControl.svelte +85 -0
  123. package/eject/files/Select.svelte +276 -0
  124. package/eject/files/Sidebar.svelte +130 -0
  125. package/eject/files/SidebarRailButton.svelte +32 -0
  126. package/eject/files/Skeleton.svelte +55 -0
  127. package/eject/files/Slider.svelte +136 -0
  128. package/eject/files/Spinner.svelte +41 -0
  129. package/eject/files/Switch.svelte +105 -0
  130. package/eject/files/Table.svelte +90 -0
  131. package/eject/files/Tabs.svelte +85 -0
  132. package/eject/files/Text.svelte +43 -0
  133. package/eject/files/TextField.svelte +222 -0
  134. package/eject/files/Thumbnail.svelte +88 -0
  135. package/eject/files/TimePicker.svelte +237 -0
  136. package/eject/files/Toast.svelte +98 -0
  137. package/eject/files/ToastQueue.svelte +61 -0
  138. package/eject/files/Tooltip.svelte +51 -0
  139. package/eject/files/TooltipOverlay.svelte +88 -0
  140. package/eject/files/accordion.css +89 -0
  141. package/eject/files/accordion.ts +67 -0
  142. package/eject/files/avatar-stack.ts +57 -0
  143. package/eject/files/avatar.ts +42 -0
  144. package/eject/files/badge.ts +30 -0
  145. package/eject/files/banner.ts +71 -0
  146. package/eject/files/base.css +38 -0
  147. package/eject/files/breadcrumb.css +51 -0
  148. package/eject/files/breadcrumb.ts +42 -0
  149. package/eject/files/button.css +241 -0
  150. package/eject/files/button.ts +106 -0
  151. package/eject/files/card.ts +58 -0
  152. package/eject/files/checkbox.ts +52 -0
  153. package/eject/files/chip-bar.ts +16 -0
  154. package/eject/files/chip.ts +35 -0
  155. package/eject/files/combobox.css +269 -0
  156. package/eject/files/combobox.ts +85 -0
  157. package/eject/files/count-badge.ts +32 -0
  158. package/eject/files/date-picker.css +375 -0
  159. package/eject/files/date-picker.ts +280 -0
  160. package/eject/files/date-range-picker.ts +338 -0
  161. package/eject/files/dialog.css +139 -0
  162. package/eject/files/dialog.ts +15 -0
  163. package/eject/files/divider.ts +5 -0
  164. package/eject/files/empty-state.ts +22 -0
  165. package/eject/files/icon.ts +36 -0
  166. package/eject/files/link.css +64 -0
  167. package/eject/files/link.ts +63 -0
  168. package/eject/files/list-row.ts +66 -0
  169. package/eject/files/media-card.ts +66 -0
  170. package/eject/files/menu.css +192 -0
  171. package/eject/files/menu.ts +84 -0
  172. package/eject/files/overlay.css +40 -0
  173. package/eject/files/pagination.css +106 -0
  174. package/eject/files/pagination.ts +68 -0
  175. package/eject/files/parts.ts +17 -0
  176. package/eject/files/popover.css +101 -0
  177. package/eject/files/popover.ts +37 -0
  178. package/eject/files/progress-bar.ts +26 -0
  179. package/eject/files/radio-group.css +184 -0
  180. package/eject/files/radio-group.ts +30 -0
  181. package/eject/files/section-header.ts +32 -0
  182. package/eject/files/segmented-control.ts +33 -0
  183. package/eject/files/select.css +347 -0
  184. package/eject/files/select.ts +77 -0
  185. package/eject/files/sidebar.ts +75 -0
  186. package/eject/files/skeleton.ts +51 -0
  187. package/eject/files/slider.css +160 -0
  188. package/eject/files/slider.ts +80 -0
  189. package/eject/files/spinner.ts +22 -0
  190. package/eject/files/switch.ts +45 -0
  191. package/eject/files/table.css +107 -0
  192. package/eject/files/table.ts +84 -0
  193. package/eject/files/tabs.css +120 -0
  194. package/eject/files/tabs.ts +37 -0
  195. package/eject/files/text-field.css +225 -0
  196. package/eject/files/text-field.ts +64 -0
  197. package/eject/files/text.ts +38 -0
  198. package/eject/files/thumbnail.ts +51 -0
  199. package/eject/files/time-picker.ts +113 -0
  200. package/eject/files/toast-queue.ts +64 -0
  201. package/eject/files/toast.ts +31 -0
  202. package/eject/files/tooltip.css +111 -0
  203. package/eject/files/tooltip.ts +21 -0
  204. package/eject/manifest.json +1053 -0
  205. package/package.json +12 -4
  206. package/styles.css +1064 -169
  207. package/tailwind.css +1295 -218
@@ -0,0 +1,105 @@
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
+ Turns a setting on or off, and takes effect at once. For a choice that is applied later, with a
8
+ form, use a Checkbox.
9
+
10
+ It is a native checkbox with the switch role, so the keyboard, forms and screen readers treat
11
+ it as one. The input is hidden and the track is drawn beside it. Without `checked` it keeps its
12
+ own state, from `defaultChecked`, and a reset of its form puts that back.
13
+
14
+ `focus()`, `blur()` and `element()` on the instance reach the native checkbox.
15
+ -->
16
+ <script module lang="ts">
17
+ import { type RootProps, rootAttributes } from "@xds/svelte/base";
18
+ import type { FocusEventHandler } from "svelte/elements";
19
+ import { classes, rootPart } from "./parts.ts";
20
+ import {
21
+ type SwitchBaseProps,
22
+ switchClass,
23
+ switchLabelClass,
24
+ switchThumbClass,
25
+ switchTrackClass,
26
+ } from "./switch.ts";
27
+
28
+ export type SwitchProps = SwitchBaseProps &
29
+ RootProps & {
30
+ onChange?: (checked: boolean) => void;
31
+ onFocus?: FocusEventHandler<HTMLInputElement>;
32
+ onBlur?: FocusEventHandler<HTMLInputElement>;
33
+ };
34
+ </script>
35
+
36
+ <script lang="ts">
37
+ import { controllable, followFormReset, keepChecked } from "@xds/svelte/base";
38
+
39
+ // `checked` stays in the rest, where whether it is given at all can be asked.
40
+ let {
41
+ defaultChecked = false,
42
+ label,
43
+ "aria-label": ariaLabel,
44
+ disabled,
45
+ onChange,
46
+ onFocus,
47
+ onBlur,
48
+ class: className,
49
+ style,
50
+ ...rest
51
+ }: SwitchProps = $props();
52
+
53
+ const control = controllable(
54
+ () => rest,
55
+ "checked",
56
+ () => rest.checked ?? false,
57
+ () => defaultChecked,
58
+ () => onChange,
59
+ );
60
+ let input = $state<HTMLInputElement>();
61
+ followFormReset(() => input, control);
62
+
63
+ /** Moves the focus to the native checkbox. */
64
+ export function focus(options?: FocusOptions) {
65
+ input?.focus(options);
66
+ }
67
+
68
+ /** Takes the focus from the native checkbox. */
69
+ export function blur() {
70
+ input?.blur();
71
+ }
72
+
73
+ /** The native checkbox, once it is on the page. */
74
+ export function element(): HTMLInputElement | undefined {
75
+ return input;
76
+ }
77
+ </script>
78
+
79
+ <label
80
+ {...rootAttributes(rest)}
81
+ class={classes(rootPart("Switch"), switchClass, className)}
82
+ {style}
83
+ >
84
+ <input
85
+ bind:this={input}
86
+ type="checkbox"
87
+ role="switch"
88
+ aria-checked={control.current ? "true" : "false"}
89
+ aria-label={ariaLabel}
90
+ class="peer sr-only"
91
+ checked={control.current}
92
+ defaultChecked={control.controlled ? control.current : defaultChecked}
93
+ {disabled}
94
+ onchange={(event) => {
95
+ control.set(event.currentTarget.checked);
96
+ keepChecked(event.currentTarget, () => control.current);
97
+ }}
98
+ onfocus={onFocus}
99
+ onblur={onBlur}
100
+ />
101
+ <span class={switchTrackClass}><span class={switchThumbClass}></span></span>
102
+ {#if label}
103
+ <span class={switchLabelClass}>{label}</span>
104
+ {/if}
105
+ </label>
@@ -0,0 +1,90 @@
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
+ Rows of data under a row of column headers, to read and compare across. The header of a
8
+ sortable column is a button that asks for the rows sorted by it (`onSortChange`), ascending
9
+ first and then the other way, and the header they are sorted by says so with `aria-sort` and an
10
+ arrow. The rows are shown in the order given. Where the columns do not fit, the table scrolls
11
+ sideways.
12
+ -->
13
+ <script module lang="ts">
14
+ import type { RootProps } from "@xds/svelte/base";
15
+ import type { TableBaseProps, TableSort } from "./table.ts";
16
+
17
+ export type { SortDirection, TableColumn, TableRow, TableSort } from "./table.ts";
18
+
19
+ export type TableProps = TableBaseProps &
20
+ RootProps & {
21
+ /** The sort a press on a sortable column's header asks for; the app sorts the rows. */
22
+ onSortChange?: (sort: TableSort) => void;
23
+ };
24
+ </script>
25
+
26
+ <script lang="ts">
27
+ import { rootAttributes } from "@xds/svelte/base";
28
+ import Icon from "./Icon.svelte";
29
+ import { classes, rootPart } from "./parts.ts";
30
+ import { columnSort, nextSort, sortIcon, TABLE_PARTS } from "./table.ts";
31
+
32
+ let {
33
+ label,
34
+ columns,
35
+ rows,
36
+ sort,
37
+ empty,
38
+ onSortChange,
39
+ class: className,
40
+ style,
41
+ ...rest
42
+ }: TableProps = $props();
43
+
44
+ const end = (align: string | undefined) => align === "end" && TABLE_PARTS.end;
45
+ </script>
46
+
47
+ <div {...rootAttributes(rest)} class={classes(rootPart("Table"), className)} {style}>
48
+ <table class={TABLE_PARTS.table} aria-label={label}>
49
+ <thead>
50
+ <tr class={TABLE_PARTS.row}>
51
+ {#each columns as column (column.key)}
52
+ {@const direction = columnSort(sort, column.key)}
53
+ <th
54
+ scope="col"
55
+ aria-sort={direction}
56
+ class={classes(TABLE_PARTS.header, end(column.align))}
57
+ >
58
+ {#if column.sortable}
59
+ <button
60
+ type="button"
61
+ class={TABLE_PARTS.sort}
62
+ onclick={() => onSortChange?.(nextSort(sort, column.key))}
63
+ >
64
+ {column.label}
65
+ <Icon name={sortIcon(direction)} size="xs" />
66
+ </button>
67
+ {:else}
68
+ {column.label}
69
+ {/if}
70
+ </th>
71
+ {/each}
72
+ </tr>
73
+ </thead>
74
+ <tbody>
75
+ {#if rows.length === 0 && empty !== undefined}
76
+ <tr class={TABLE_PARTS.row}>
77
+ <td class={TABLE_PARTS.empty} colspan={columns.length}>{empty}</td>
78
+ </tr>
79
+ {:else}
80
+ {#each rows as row (row.id)}
81
+ <tr class={TABLE_PARTS.row}>
82
+ {#each columns as column, index (column.key)}
83
+ <td class={classes(TABLE_PARTS.cell, end(column.align))}>{row.cells[index]}</td>
84
+ {/each}
85
+ </tr>
86
+ {/each}
87
+ {/if}
88
+ </tbody>
89
+ </table>
90
+ </div>
@@ -0,0 +1,85 @@
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
+ Switches between views inside one screen. For two to four short modes a SegmentedControl is
8
+ lighter; tabs are for more items, longer labels, or a row that may have to scroll.
9
+
10
+ The selected tab is underlined as well as darker, so the choice does not depend on color. This
11
+ is only the row: the app shows the view that matches `value`. The arrow keys move between tabs.
12
+ Without `value` the tabs keep their own, from `defaultValue` or else the first.
13
+ -->
14
+ <script module lang="ts">
15
+ import type { RootProps } from "@xds/svelte/base";
16
+ import type { TabsBaseProps } from "./tabs.ts";
17
+
18
+ export type TabsProps<Value extends string> = TabsBaseProps<Value> &
19
+ RootProps & {
20
+ onChange?: (value: Value) => void;
21
+ };
22
+ </script>
23
+
24
+ <script lang="ts" generics="Value extends string">
25
+ import { controllable, rootAttributes } from "@xds/svelte/base";
26
+ import { mergeProps, normalizeProps, useMachine } from "@zag-js/svelte";
27
+ import * as tabs from "@zag-js/tabs";
28
+ import Icon from "./Icon.svelte";
29
+ import { classes } from "./parts.ts";
30
+ import { tabStopIndex, tabsClass } from "./tabs.ts";
31
+
32
+ // `value` stays in the rest, where whether it is given at all can be asked.
33
+ let {
34
+ items,
35
+ defaultValue,
36
+ label,
37
+ fullWidth,
38
+ onChange,
39
+ class: className,
40
+ style,
41
+ ...rest
42
+ }: TabsProps<Value> = $props();
43
+
44
+ const control = controllable<Value | undefined, Value>(
45
+ () => rest,
46
+ "value",
47
+ () => rest.value,
48
+ () => defaultValue ?? items[0]?.value,
49
+ () => onChange,
50
+ );
51
+ const id = $props.id();
52
+ const service = useMachine(tabs.machine, () => ({
53
+ id,
54
+ value: control.current ?? null,
55
+ onValueChange: (details: tabs.ValueChangeDetails) => control.set(details.value as Value),
56
+ }));
57
+ const api = $derived(tabs.connect(service, normalizeProps));
58
+ // Merged, so that the app's style joins any the machine writes on the root. The recipe gives
59
+ // the root part, `{{prefix}}-tabs`.
60
+ const root = $derived(
61
+ mergeProps(api.getRootProps(), {
62
+ ...rootAttributes(rest),
63
+ class: classes(tabsClass(fullWidth), className),
64
+ style,
65
+ }),
66
+ );
67
+ </script>
68
+
69
+ <div {...root}>
70
+ <div {...api.getListProps()} aria-label={label} class="{{prefix}}-tabs__list">
71
+ {#each items as item (item.value)}
72
+ {@const trigger = api.getTriggerProps({ value: item.value })}
73
+ <!-- Zag points the selected tab at its panel, which this row never renders: the app shows
74
+ the view. A reference to an element that does not exist is invalid. -->
75
+ <button
76
+ {...trigger}
77
+ tabindex={tabStopIndex(api.focusedValue, item.value, trigger.tabindex)}
78
+ aria-controls={undefined}
79
+ class="{{prefix}}-tabs__tab"
80
+ >{#if item.icon}<Icon name={item.icon} size="sm" />{/if}{item.label}</button
81
+ >
82
+ {/each}
83
+ <div {...api.getIndicatorProps()} class="{{prefix}}-tabs__indicator"></div>
84
+ </div>
85
+ </div>
@@ -0,0 +1,43 @@
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
+ <script module lang="ts">
6
+ import type { RootProps } from "@xds/svelte/base";
7
+ import type { Snippet } from "svelte";
8
+ import type { TextBaseProps } from "./text.ts";
9
+
10
+ export type TextProps = TextBaseProps &
11
+ RootProps & {
12
+ /** The element to render. Pick it for the document outline (`h1`, `h2`, `p`), not for looks. */
13
+ as?: keyof HTMLElementTagNameMap;
14
+ children: Snippet;
15
+ };
16
+ </script>
17
+
18
+ <script lang="ts">
19
+ import { rootAttributes, useDefaults } from "@xds/svelte/base";
20
+ import { classes, rootPart } from "./parts.ts";
21
+ import { textClass } from "./text.ts";
22
+
23
+ let {
24
+ variant: ownVariant,
25
+ tone: ownTone,
26
+ as = "p",
27
+ class: className,
28
+ style,
29
+ children,
30
+ ...rest
31
+ }: TextProps = $props();
32
+
33
+ const defaults = useDefaults("Text");
34
+ const variant = $derived(ownVariant ?? defaults().variant);
35
+ const tone = $derived(ownTone ?? defaults().tone);
36
+ </script>
37
+
38
+ <svelte:element
39
+ this={as}
40
+ {...rootAttributes(rest)}
41
+ class={classes(rootPart("Text"), textClass({ variant, tone }), className)}
42
+ {style}>{@render children()}</svelte:element
43
+ >
@@ -0,0 +1,222 @@
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 labelled text field, of one line or several. The label, the description and the error message
8
+ are tied to the input for assistive technology, and the error is announced when it appears.
9
+
10
+ An invalid field blocks its form from being submitted, so `error` has to be cleared as soon as
11
+ the person edits the value. Without `value` it keeps its own text, from `defaultValue`, and a
12
+ reset of its form puts that back.
13
+
14
+ `focus()`, `blur()` and `element()` on the instance reach the native input or text area.
15
+ -->
16
+ <script module lang="ts">
17
+ import type { RootProps } from "@xds/svelte/base";
18
+ import type { Snippet } from "svelte";
19
+ import type { FocusEventHandler } from "svelte/elements";
20
+ import type { TextFieldBaseProps } from "./text-field.ts";
21
+
22
+ export type TextFieldProps = TextFieldBaseProps &
23
+ RootProps & {
24
+ onChange?: (value: string) => void;
25
+ onFocus?: FocusEventHandler<HTMLInputElement | HTMLTextAreaElement>;
26
+ onBlur?: FocusEventHandler<HTMLInputElement | HTMLTextAreaElement>;
27
+ /**
28
+ * Inside the box, in front of the text: an icon, a unit. It takes the placeholder's color
29
+ * unless it sets its own.
30
+ */
31
+ leading?: Snippet;
32
+ /** Inside the box, after the text: an icon, a unit, a small button. */
33
+ trailing?: Snippet;
34
+ };
35
+ </script>
36
+
37
+ <script lang="ts">
38
+ import { controllable, followFormReset, keepValue, rootAttributes } from "@xds/svelte/base";
39
+ import { untrack } from "svelte";
40
+ import type { Attachment } from "svelte/attachments";
41
+ import { classes } from "./parts.ts";
42
+
43
+ // `value` stays in the rest, where whether it is given at all can be asked.
44
+ let {
45
+ label,
46
+ defaultValue = "",
47
+ readOnly,
48
+ description,
49
+ error,
50
+ placeholder,
51
+ multiline,
52
+ rows = 3,
53
+ maxLength,
54
+ name,
55
+ type = "text",
56
+ inputMode,
57
+ autoComplete,
58
+ autoFocus,
59
+ required,
60
+ disabled,
61
+ onChange,
62
+ onFocus,
63
+ onBlur,
64
+ leading,
65
+ trailing,
66
+ class: className,
67
+ style,
68
+ ...rest
69
+ }: TextFieldProps = $props();
70
+
71
+ const control = controllable(
72
+ () => rest,
73
+ "value",
74
+ () => rest.value ?? "",
75
+ () => defaultValue,
76
+ () => onChange,
77
+ );
78
+ let input = $state<HTMLInputElement | HTMLTextAreaElement>();
79
+ followFormReset(() => input, control);
80
+
81
+ /** Moves the focus to the native input or text area. */
82
+ export function focus(options?: FocusOptions) {
83
+ input?.focus(options);
84
+ }
85
+
86
+ /** Takes the focus from the native input or text area. */
87
+ export function blur() {
88
+ input?.blur();
89
+ }
90
+
91
+ /** The native input or text area, once it is on the page. */
92
+ export function element(): HTMLInputElement | HTMLTextAreaElement | undefined {
93
+ return input;
94
+ }
95
+
96
+ const id = $props.id();
97
+ const descriptionId = `${id}-description`;
98
+ const errorId = `${id}-error`;
99
+ const invalid = $derived(error !== undefined);
100
+ const describedBy = $derived(invalid ? errorId : description ? descriptionId : undefined);
101
+
102
+ // Once the field is on the page.
103
+ const settle: Attachment<HTMLInputElement | HTMLTextAreaElement> = (input) => {
104
+ // The value is set as a property, which leaves a text area's caret after the last line,
105
+ // and keyboard focus would then scroll to it. React renders the first value as the text
106
+ // area's own text, with the caret before the first line.
107
+ if (input instanceof HTMLTextAreaElement) input.setSelectionRange(0, 0);
108
+ if (untrack(() => autoFocus)) input.focus();
109
+ };
110
+
111
+ const syncValidity: Attachment<HTMLInputElement | HTMLTextAreaElement> = (input) => {
112
+ input.setCustomValidity(error ?? "");
113
+ };
114
+
115
+ function oninput(event: Event & { currentTarget: HTMLInputElement | HTMLTextAreaElement }) {
116
+ control.set(event.currentTarget.value);
117
+ // While a composition is open the text belongs to the input method; it ends with
118
+ // compositionend.
119
+ if (!(event instanceof InputEvent && event.isComposing))
120
+ keepValue(event.currentTarget, () => control.current);
121
+ }
122
+
123
+ function oncompositionend(
124
+ event: CompositionEvent & { currentTarget: HTMLInputElement | HTMLTextAreaElement },
125
+ ) {
126
+ keepValue(event.currentTarget, () => control.current);
127
+ }
128
+ </script>
129
+
130
+ <!-- The input draws the box itself until something sits inside the box beside the text. -->
131
+ {#snippet field(box: boolean)}
132
+ {#if multiline}
133
+ <textarea
134
+ {@attach settle}
135
+ {@attach syncValidity}
136
+ {rows}
137
+ {id}
138
+ class={box
139
+ ? "{{prefix}}-text-field__input {{prefix}}-text-field__control"
140
+ : "{{prefix}}-text-field__input"}
141
+ bind:this={input}
142
+ value={control.current}
143
+ defaultValue={control.controlled ? control.current : defaultValue}
144
+ {placeholder}
145
+ {name}
146
+ maxlength={maxLength}
147
+ inputmode={inputMode}
148
+ autocomplete={autoComplete as HTMLTextAreaElement["autocomplete"]}
149
+ readonly={readOnly}
150
+ {required}
151
+ {disabled}
152
+ aria-invalid={invalid ? "true" : undefined}
153
+ aria-errormessage={invalid ? errorId : undefined}
154
+ aria-describedby={describedBy}
155
+ {oninput}
156
+ {oncompositionend}
157
+ onfocus={onFocus}
158
+ onblur={onBlur}></textarea>
159
+ {:else}
160
+ <input
161
+ {@attach settle}
162
+ {@attach syncValidity}
163
+ {type}
164
+ {id}
165
+ class={box
166
+ ? "{{prefix}}-text-field__input {{prefix}}-text-field__control"
167
+ : "{{prefix}}-text-field__input"}
168
+ bind:this={input}
169
+ value={control.current}
170
+ defaultValue={control.controlled ? control.current : defaultValue}
171
+ {placeholder}
172
+ {name}
173
+ maxlength={maxLength}
174
+ inputmode={inputMode}
175
+ autocomplete={autoComplete as HTMLInputElement["autocomplete"]}
176
+ readonly={readOnly}
177
+ {required}
178
+ {disabled}
179
+ aria-invalid={invalid ? "true" : undefined}
180
+ aria-errormessage={invalid ? errorId : undefined}
181
+ aria-describedby={describedBy}
182
+ {oninput}
183
+ {oncompositionend}
184
+ onfocus={onFocus}
185
+ onblur={onBlur}
186
+ />
187
+ {/if}
188
+ {/snippet}
189
+
190
+ <div {...rootAttributes(rest)} class={classes("{{prefix}}-text-field", className)} {style}>
191
+ <label for={id} class="{{prefix}}-text-field__label">{label}</label>
192
+ {#if leading || trailing}
193
+ <div class="{{prefix}}-text-field__control {{prefix}}-text-field__group">
194
+ {#if leading}
195
+ <span class="{{prefix}}-text-field__slot {{prefix}}-text-field__slot--leading"
196
+ >{@render leading()}</span
197
+ >
198
+ {/if}
199
+ {@render field(false)}
200
+ {#if trailing}
201
+ <span class="{{prefix}}-text-field__slot {{prefix}}-text-field__slot--trailing"
202
+ >{@render trailing()}</span
203
+ >
204
+ {/if}
205
+ </div>
206
+ {:else}
207
+ {@render field(true)}
208
+ {/if}
209
+ <!-- One line under the box: the help or the error at the start edge, the count at the end.
210
+ The error is a live region, so it is announced when it appears. -->
211
+ <div class="{{prefix}}-text-field__footer">
212
+ {#if !invalid && description}
213
+ <span id={descriptionId} class="{{prefix}}-text-field__description">{description}</span>
214
+ {/if}
215
+ <span id={errorId} class="{{prefix}}-text-field__error" aria-live="polite">{error}</span>
216
+ {#if maxLength !== undefined}
217
+ <span class="ms-auto shrink-0 text-xds-footnote text-xds-text-secondary tabular-nums"
218
+ >{control.current.length}/{maxLength}</span
219
+ >
220
+ {/if}
221
+ </div>
222
+ </div>
@@ -0,0 +1,88 @@
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 picture in a fixed shape, for a video, an article or a product. It fills the width it is
8
+ given and takes its height from `ratio`, and crops the picture to fill it.
9
+ -->
10
+ <script module lang="ts">
11
+ import type { RootProps } from "@xds/svelte/base";
12
+ import type { ThumbnailBaseProps } from "./thumbnail.ts";
13
+
14
+ export type { ThumbnailRatio } from "./thumbnail.ts";
15
+
16
+ export type ThumbnailProps = ThumbnailBaseProps & RootProps;
17
+ </script>
18
+
19
+ <script lang="ts">
20
+ import { createPicture, pictureClass, rootAttributes, useDefaults } from "@xds/svelte/base";
21
+ import Badge from "./Badge.svelte";
22
+ import Icon from "./Icon.svelte";
23
+ import { classes, rootPart } from "./parts.ts";
24
+ import {
25
+ THUMBNAIL_PARTS,
26
+ thumbnailBadgeClass,
27
+ thumbnailClass,
28
+ thumbnailWatched,
29
+ } from "./thumbnail.ts";
30
+
31
+ let {
32
+ src,
33
+ alt,
34
+ ratio: ownRatio,
35
+ badge,
36
+ progress,
37
+ class: className,
38
+ style,
39
+ ...rest
40
+ }: ThumbnailProps = $props();
41
+
42
+ const defaults = useDefaults("Thumbnail");
43
+ const ratio = $derived(ownRatio ?? defaults().ratio);
44
+
45
+ const picture = createPicture(() => src);
46
+ const watched = $derived(thumbnailWatched(progress));
47
+ </script>
48
+
49
+ <span
50
+ {...rootAttributes(rest)}
51
+ class={classes(rootPart("Thumbnail"), thumbnailClass(ratio), className)}
52
+ {style}
53
+ >
54
+ <!-- The placeholder is the picture's stand-in for assistive technology too, so the description
55
+ is on the box that always shows, not on the picture that may not. -->
56
+ <span
57
+ class={THUMBNAIL_PARTS.placeholder}
58
+ role={alt ? "img" : undefined}
59
+ aria-label={alt || undefined}
60
+ aria-hidden={alt ? undefined : "true"}
61
+ >
62
+ {#if picture.loaded() !== true}
63
+ <Icon name="image" size="lg" />
64
+ {/if}
65
+ {#if picture.loaded() !== false && src}
66
+ {@const address = src}
67
+ <img
68
+ {@attach picture.sync(address)}
69
+ src={address}
70
+ alt=""
71
+ aria-hidden="true"
72
+ class={pictureClass(picture.loaded() === true)}
73
+ onload={() => picture.onLoad(address)}
74
+ onerror={() => picture.onError(address)}
75
+ />
76
+ {/if}
77
+ </span>
78
+ {#if badge}
79
+ <span class={thumbnailBadgeClass(watched)}><Badge tone="overlay">{badge}</Badge></span>
80
+ {/if}
81
+ {#if watched > 0}
82
+ <!-- Decoration: what was watched is for the eye, and a list that needs it said says it in
83
+ words. -->
84
+ <span aria-hidden="true" class={THUMBNAIL_PARTS.track}>
85
+ <span class={THUMBNAIL_PARTS.fill} style:width="{watched * 100}%"></span>
86
+ </span>
87
+ {/if}
88
+ </span>