@xds/svelte 0.0.0-stage → 0.1.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 (122) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +19 -2
  3. package/dist/Accordion.svelte +89 -0
  4. package/dist/Accordion.svelte.d.ts +43 -0
  5. package/dist/AccordionPanel.svelte +40 -0
  6. package/dist/AccordionPanel.svelte.d.ts +15 -0
  7. package/dist/Avatar.svelte +45 -0
  8. package/dist/Avatar.svelte.d.ts +11 -0
  9. package/dist/AvatarStack.svelte +49 -0
  10. package/dist/AvatarStack.svelte.d.ts +10 -0
  11. package/dist/Badge.svelte +36 -0
  12. package/dist/Badge.svelte.d.ts +15 -0
  13. package/dist/Banner.svelte +88 -0
  14. package/dist/Banner.svelte.d.ts +25 -0
  15. package/dist/Button.svelte +90 -0
  16. package/dist/Button.svelte.d.ts +16 -0
  17. package/dist/Card.svelte +99 -0
  18. package/dist/Card.svelte.d.ts +20 -0
  19. package/dist/Checkbox.svelte +68 -0
  20. package/dist/Checkbox.svelte.d.ts +12 -0
  21. package/dist/Chip.svelte +43 -0
  22. package/dist/Chip.svelte.d.ts +14 -0
  23. package/dist/ChipBar.svelte +32 -0
  24. package/dist/ChipBar.svelte.d.ts +14 -0
  25. package/dist/CountBadge.svelte +50 -0
  26. package/dist/CountBadge.svelte.d.ts +14 -0
  27. package/dist/DatePicker.svelte +178 -0
  28. package/dist/DatePicker.svelte.d.ts +22 -0
  29. package/dist/DateRangePicker.svelte +264 -0
  30. package/dist/DateRangePicker.svelte.d.ts +25 -0
  31. package/dist/Defaults.svelte +29 -0
  32. package/dist/Defaults.svelte.d.ts +17 -0
  33. package/dist/Dialog.svelte +104 -0
  34. package/dist/Dialog.svelte.d.ts +22 -0
  35. package/dist/Divider.svelte +17 -0
  36. package/dist/Divider.svelte.d.ts +6 -0
  37. package/dist/EmptyState.svelte +34 -0
  38. package/dist/EmptyState.svelte.d.ts +14 -0
  39. package/dist/Icon.svelte +38 -0
  40. package/dist/Icon.svelte.d.ts +10 -0
  41. package/dist/IconButton.svelte +67 -0
  42. package/dist/IconButton.svelte.d.ts +13 -0
  43. package/dist/ListRow.svelte +29 -0
  44. package/dist/ListRow.svelte.d.ts +15 -0
  45. package/dist/MediaCard.svelte +112 -0
  46. package/dist/MediaCard.svelte.d.ts +22 -0
  47. package/dist/Menu.svelte +156 -0
  48. package/dist/Menu.svelte.d.ts +19 -0
  49. package/dist/PlaceRow.svelte +66 -0
  50. package/dist/PlaceRow.svelte.d.ts +12 -0
  51. package/dist/Portal.svelte +27 -0
  52. package/dist/Portal.svelte.d.ts +15 -0
  53. package/dist/ProgressBar.svelte +37 -0
  54. package/dist/ProgressBar.svelte.d.ts +10 -0
  55. package/dist/RadioGroup.svelte +114 -0
  56. package/dist/RadioGroup.svelte.d.ts +38 -0
  57. package/dist/SearchField.svelte +93 -0
  58. package/dist/SearchField.svelte.d.ts +17 -0
  59. package/dist/SectionHeader.svelte +56 -0
  60. package/dist/SectionHeader.svelte.d.ts +16 -0
  61. package/dist/SegmentedControl.svelte +70 -0
  62. package/dist/SegmentedControl.svelte.d.ts +35 -0
  63. package/dist/Select.svelte +214 -0
  64. package/dist/Select.svelte.d.ts +40 -0
  65. package/dist/Sidebar.svelte +125 -0
  66. package/dist/Sidebar.svelte.d.ts +43 -0
  67. package/dist/SidebarRailButton.svelte +29 -0
  68. package/dist/SidebarRailButton.svelte.d.ts +14 -0
  69. package/dist/Skeleton.svelte +34 -0
  70. package/dist/Skeleton.svelte.d.ts +13 -0
  71. package/dist/Slider.svelte +111 -0
  72. package/dist/Slider.svelte.d.ts +15 -0
  73. package/dist/Spinner.svelte +35 -0
  74. package/dist/Spinner.svelte.d.ts +11 -0
  75. package/dist/Switch.svelte +60 -0
  76. package/dist/Switch.svelte.d.ts +15 -0
  77. package/dist/Tabs.svelte +65 -0
  78. package/dist/Tabs.svelte.d.ts +36 -0
  79. package/dist/Text.svelte +36 -0
  80. package/dist/Text.svelte.d.ts +11 -0
  81. package/dist/TextField.svelte +163 -0
  82. package/dist/TextField.svelte.d.ts +23 -0
  83. package/dist/Thumbnail.svelte +81 -0
  84. package/dist/Thumbnail.svelte.d.ts +11 -0
  85. package/dist/Toast.svelte +83 -0
  86. package/dist/Toast.svelte.d.ts +17 -0
  87. package/dist/Tooltip.svelte +40 -0
  88. package/dist/Tooltip.svelte.d.ts +19 -0
  89. package/dist/TooltipOverlay.svelte +66 -0
  90. package/dist/TooltipOverlay.svelte.d.ts +17 -0
  91. package/dist/controlled.d.ts +4 -0
  92. package/dist/controlled.js +23 -0
  93. package/dist/defaults.svelte.d.ts +12 -0
  94. package/dist/defaults.svelte.js +25 -0
  95. package/dist/index.d.ts +41 -0
  96. package/dist/index.js +47 -0
  97. package/dist/modal-popover.svelte.d.ts +8 -0
  98. package/dist/modal-popover.svelte.js +22 -0
  99. package/dist/picture.svelte.d.ts +19 -0
  100. package/dist/picture.svelte.js +18 -0
  101. package/dist/presence.svelte.d.ts +6 -0
  102. package/dist/presence.svelte.js +19 -0
  103. package/dist/preview/Component.svelte +56 -0
  104. package/dist/preview/Component.svelte.d.ts +15 -0
  105. package/dist/preview/Draw.svelte +65 -0
  106. package/dist/preview/Draw.svelte.d.ts +15 -0
  107. package/dist/preview/Preview.svelte +41 -0
  108. package/dist/preview/Preview.svelte.d.ts +13 -0
  109. package/dist/preview/Snippets.svelte +36 -0
  110. package/dist/preview/Snippets.svelte.d.ts +19 -0
  111. package/dist/preview/components.js +48 -0
  112. package/dist/preview/index.d.ts +2 -0
  113. package/dist/preview/index.js +4 -0
  114. package/dist/root.d.ts +11 -0
  115. package/dist/root.js +1 -0
  116. package/dist/styles/index.css +5 -0
  117. package/dist/trigger.svelte.d.ts +21 -0
  118. package/dist/trigger.svelte.js +48 -0
  119. package/package.json +62 -4
  120. package/page.css +20 -0
  121. package/styles.css +3354 -0
  122. package/tailwind.css +2665 -0
@@ -0,0 +1,38 @@
1
+ import type { RadioGroupBaseProps } from "@xds/core";
2
+ import type { RootProps } from "./root.js";
3
+ export type { RadioOption } from "@xds/core";
4
+ export type RadioGroupProps<Value extends string = string> = RadioGroupBaseProps<Value> & RootProps & {
5
+ onChange: (value: Value) => void;
6
+ };
7
+ declare function $$render<Value extends string = string>(): {
8
+ props: RadioGroupProps<Value>;
9
+ exports: {};
10
+ bindings: "";
11
+ slots: {};
12
+ events: {};
13
+ };
14
+ declare class __sveltets_Render<Value extends string = string> {
15
+ props(): ReturnType<typeof $$render<Value>>['props'];
16
+ events(): ReturnType<typeof $$render<Value>>['events'];
17
+ slots(): ReturnType<typeof $$render<Value>>['slots'];
18
+ bindings(): "";
19
+ exports(): {};
20
+ }
21
+ interface $$IsomorphicComponent {
22
+ new <Value extends string = string>(options: import('svelte').ComponentConstructorOptions<ReturnType<__sveltets_Render<Value>['props']>>): import('svelte').SvelteComponent<ReturnType<__sveltets_Render<Value>['props']>, ReturnType<__sveltets_Render<Value>['events']>, ReturnType<__sveltets_Render<Value>['slots']>> & {
23
+ $$bindings?: ReturnType<__sveltets_Render<Value>['bindings']>;
24
+ } & ReturnType<__sveltets_Render<Value>['exports']>;
25
+ <Value extends string = string>(internal: unknown, props: ReturnType<__sveltets_Render<Value>['props']> & {}): ReturnType<__sveltets_Render<Value>['exports']>;
26
+ z_$$bindings?: ReturnType<__sveltets_Render<any>['bindings']>;
27
+ }
28
+ /**
29
+ * One choice among a few, applied later with the form it belongs to. For a choice that takes
30
+ * effect at once among two to four options, a SegmentedControl is the usual pick.
31
+ *
32
+ * The chosen option has a dot and a thicker ring, so the choice does not depend on color. The
33
+ * options are native radio buttons, hidden and drawn beside, in a group named by the legend: the
34
+ * arrow keys move between them, and the error is tied to the group and announced.
35
+ */
36
+ declare const RadioGroup: $$IsomorphicComponent;
37
+ type RadioGroup<Value extends string = string> = InstanceType<typeof RadioGroup<Value>>;
38
+ export default RadioGroup;
@@ -0,0 +1,93 @@
1
+ <!--
2
+ @component
3
+ A text field for a query: TextField's box as a pill, with a search icon in front and, while
4
+ there is a value, a control that clears it. Escape clears too.
5
+
6
+ Use it to filter or search what is on the screen. For a value that is saved with a form, use a
7
+ TextField, which has a visible label.
8
+ -->
9
+ <script module lang="ts">
10
+ import type { SearchFieldBaseProps } from "@xds/core";
11
+ import type { RootProps } from "./root.js";
12
+
13
+ export type SearchFieldProps = SearchFieldBaseProps &
14
+ RootProps & {
15
+ onChange: (value: string) => void;
16
+ /** Called with the query on Enter, or on the search key of a software keyboard. */
17
+ onSubmit?: (value: string) => void;
18
+ };
19
+ </script>
20
+
21
+ <script lang="ts">
22
+ import { classes, rootPart } from "@xds/core";
23
+ import { keepValue } from "./controlled.js";
24
+ import Icon from "./Icon.svelte";
25
+
26
+ let {
27
+ value,
28
+ label,
29
+ placeholder,
30
+ disabled,
31
+ clearLabel,
32
+ onChange,
33
+ onSubmit,
34
+ class: className,
35
+ style,
36
+ }: SearchFieldProps = $props();
37
+
38
+ let input = $state<HTMLInputElement>();
39
+ </script>
40
+
41
+ <!-- TextField's box, so its part as well as its own. -->
42
+ <div class={classes("xds-text-field", rootPart("SearchField"), className)} {style}>
43
+ <div class="xds-text-field__control xds-text-field__control--pill xds-text-field__group">
44
+ <span class="xds-text-field__slot xds-text-field__slot--leading"
45
+ ><Icon name="search" size="sm" /></span
46
+ >
47
+ <!-- Escape clears only while there is something to clear; an empty field lets Escape
48
+ through to whatever it sits in, such as a dialog. -->
49
+ <input
50
+ bind:this={input}
51
+ type="text"
52
+ class="xds-text-field__input"
53
+ aria-label={label}
54
+ enterkeyhint="search"
55
+ {placeholder}
56
+ {value}
57
+ defaultValue={value}
58
+ {disabled}
59
+ data-uses-escape={value !== "" ? "" : undefined}
60
+ oninput={(event) => {
61
+ onChange(event.currentTarget.value);
62
+ if (!(event instanceof InputEvent && event.isComposing))
63
+ keepValue(event.currentTarget, () => value);
64
+ }}
65
+ oncompositionend={(event) => keepValue(event.currentTarget, () => value)}
66
+ onkeydown={(event) => {
67
+ // While a composition is open, Enter and Escape belong to the input method.
68
+ if (event.isComposing) return;
69
+ if (event.key === "Enter") onSubmit?.(value);
70
+ if (event.key === "Escape" && value !== "") {
71
+ event.stopPropagation();
72
+ onChange("");
73
+ }
74
+ }}
75
+ />
76
+ {#if value !== "" && !disabled}
77
+ <span class="xds-text-field__slot xds-text-field__slot--trailing">
78
+ <button
79
+ type="button"
80
+ aria-label={clearLabel ?? "Clear"}
81
+ onclick={() => {
82
+ onChange("");
83
+ // The control is about to disappear, and focus would be lost with it.
84
+ input?.focus();
85
+ }}
86
+ class="-me-1 flex size-xds-target-min cursor-pointer items-center justify-center rounded-xds-full hover:text-xds-field-text focus-visible:outline-(length:--xds-focus-width) focus-visible:outline-xds-border-focus focus-visible:outline-solid"
87
+ >
88
+ <Icon name="circle-x" size="sm" />
89
+ </button>
90
+ </span>
91
+ {/if}
92
+ </div>
93
+ </div>
@@ -0,0 +1,17 @@
1
+ import type { SearchFieldBaseProps } from "@xds/core";
2
+ import type { RootProps } from "./root.js";
3
+ export type SearchFieldProps = SearchFieldBaseProps & RootProps & {
4
+ onChange: (value: string) => void;
5
+ /** Called with the query on Enter, or on the search key of a software keyboard. */
6
+ onSubmit?: (value: string) => void;
7
+ };
8
+ /**
9
+ * A text field for a query: TextField's box as a pill, with a search icon in front and, while
10
+ * there is a value, a control that clears it. Escape clears too.
11
+ *
12
+ * Use it to filter or search what is on the screen. For a value that is saved with a form, use a
13
+ * TextField, which has a visible label.
14
+ */
15
+ declare const SearchField: import("svelte").Component<SearchFieldProps, {}, "">;
16
+ type SearchField = ReturnType<typeof SearchField>;
17
+ export default SearchField;
@@ -0,0 +1,56 @@
1
+ <!--
2
+ @component
3
+ The title row of a group of content. The title is a heading at `level`; the action is a quiet
4
+ button with a chevron at its end edge, which points the other way in a right-to-left layout.
5
+ -->
6
+ <script module lang="ts">
7
+ import type { SectionHeaderBaseProps } from "@xds/core";
8
+ import type { RootProps } from "./root.js";
9
+
10
+ export type SectionHeaderProps = SectionHeaderBaseProps &
11
+ RootProps & {
12
+ /** A link-like action at the end, such as "See all". */
13
+ action?: { label: string; onPress: () => void };
14
+ };
15
+ </script>
16
+
17
+ <script lang="ts">
18
+ import { classes, rootPart, SECTION_HEADER_PARTS } from "@xds/core";
19
+ import Button from "./Button.svelte";
20
+ import Icon from "./Icon.svelte";
21
+
22
+ let {
23
+ title,
24
+ description,
25
+ icon,
26
+ level = 2,
27
+ action,
28
+ class: className,
29
+ style,
30
+ }: SectionHeaderProps = $props();
31
+ </script>
32
+
33
+ <div class={classes(rootPart("SectionHeader"), SECTION_HEADER_PARTS.root, className)} {style}>
34
+ <div class={SECTION_HEADER_PARTS.lead}>
35
+ {#if icon}
36
+ <span class={SECTION_HEADER_PARTS.icon}><Icon name={icon} size="md" /></span>
37
+ {/if}
38
+ <div class={SECTION_HEADER_PARTS.text}>
39
+ <svelte:element this={`h${level}`} class={SECTION_HEADER_PARTS.title}>{title}</svelte:element>
40
+ {#if description}
41
+ <p class={SECTION_HEADER_PARTS.description}>{description}</p>
42
+ {/if}
43
+ </div>
44
+ </div>
45
+ {#if action}
46
+ <span class={SECTION_HEADER_PARTS.action}>
47
+ <Button variant="ghost" size="sm" onPress={() => action.onPress()}
48
+ >{action.label}<Icon
49
+ name="chevron-right"
50
+ size="sm"
51
+ class={SECTION_HEADER_PARTS.chevron}
52
+ /></Button
53
+ >
54
+ </span>
55
+ {/if}
56
+ </div>
@@ -0,0 +1,16 @@
1
+ import type { SectionHeaderBaseProps } from "@xds/core";
2
+ import type { RootProps } from "./root.js";
3
+ export type SectionHeaderProps = SectionHeaderBaseProps & RootProps & {
4
+ /** A link-like action at the end, such as "See all". */
5
+ action?: {
6
+ label: string;
7
+ onPress: () => void;
8
+ };
9
+ };
10
+ /**
11
+ * The title row of a group of content. The title is a heading at `level`; the action is a quiet
12
+ * button with a chevron at its end edge, which points the other way in a right-to-left layout.
13
+ */
14
+ declare const SectionHeader: import("svelte").Component<SectionHeaderProps, {}, "">;
15
+ type SectionHeader = ReturnType<typeof SectionHeader>;
16
+ export default SectionHeader;
@@ -0,0 +1,70 @@
1
+ <!--
2
+ @component
3
+ A choice between a few views or modes, all visible at once. The selected segment is outlined as
4
+ well as filled, so the choice does not depend on telling two greys apart.
5
+
6
+ It is a group of native radio buttons, so the arrow keys move between segments.
7
+ -->
8
+ <script module lang="ts">
9
+ import type { SegmentedControlBaseProps } from "@xds/core";
10
+ import type { RootProps } from "./root.js";
11
+
12
+ export type SegmentedControlProps<Value extends string> = SegmentedControlBaseProps<Value> &
13
+ RootProps & {
14
+ onChange: (value: Value) => void;
15
+ };
16
+ </script>
17
+
18
+ <script lang="ts" generics="Value extends string">
19
+ import {
20
+ classes,
21
+ rootPart,
22
+ segmentedControlClass,
23
+ segmentedControlOptionClass,
24
+ segmentedControlSegmentClass,
25
+ } from "@xds/core";
26
+ import { keepChecked } from "./controlled.js";
27
+
28
+ let {
29
+ options,
30
+ value,
31
+ "aria-label": ariaLabel,
32
+ fullWidth,
33
+ onChange,
34
+ class: className,
35
+ style,
36
+ }: SegmentedControlProps<Value> = $props();
37
+
38
+ const name = $props.id();
39
+ let group = $state<HTMLDivElement>();
40
+ </script>
41
+
42
+ <div
43
+ bind:this={group}
44
+ aria-label={ariaLabel}
45
+ role="radiogroup"
46
+ class={classes(rootPart("SegmentedControl"), segmentedControlClass(fullWidth), className)}
47
+ {style}
48
+ >
49
+ {#each options as option (option.value)}
50
+ <label class={segmentedControlOptionClass(fullWidth)}>
51
+ <input
52
+ type="radio"
53
+ {name}
54
+ value={option.value}
55
+ class="peer sr-only"
56
+ checked={option.value === value}
57
+ defaultChecked={option.value === value}
58
+ onchange={() => {
59
+ onChange(option.value);
60
+ // The browser moved the dot already; where the owner kept the old value, it goes
61
+ // back, on every radio of the group.
62
+ for (const input of group?.querySelectorAll("input") ?? []) {
63
+ keepChecked(input, () => input.value === value);
64
+ }
65
+ }}
66
+ />
67
+ <span class={segmentedControlSegmentClass}>{option.label}</span>
68
+ </label>
69
+ {/each}
70
+ </div>
@@ -0,0 +1,35 @@
1
+ import type { SegmentedControlBaseProps } from "@xds/core";
2
+ import type { RootProps } from "./root.js";
3
+ export type SegmentedControlProps<Value extends string> = SegmentedControlBaseProps<Value> & RootProps & {
4
+ onChange: (value: Value) => void;
5
+ };
6
+ declare function $$render<Value extends string>(): {
7
+ props: SegmentedControlProps<Value>;
8
+ exports: {};
9
+ bindings: "";
10
+ slots: {};
11
+ events: {};
12
+ };
13
+ declare class __sveltets_Render<Value extends string> {
14
+ props(): ReturnType<typeof $$render<Value>>['props'];
15
+ events(): ReturnType<typeof $$render<Value>>['events'];
16
+ slots(): ReturnType<typeof $$render<Value>>['slots'];
17
+ bindings(): "";
18
+ exports(): {};
19
+ }
20
+ interface $$IsomorphicComponent {
21
+ new <Value extends string>(options: import('svelte').ComponentConstructorOptions<ReturnType<__sveltets_Render<Value>['props']>>): import('svelte').SvelteComponent<ReturnType<__sveltets_Render<Value>['props']>, ReturnType<__sveltets_Render<Value>['events']>, ReturnType<__sveltets_Render<Value>['slots']>> & {
22
+ $$bindings?: ReturnType<__sveltets_Render<Value>['bindings']>;
23
+ } & ReturnType<__sveltets_Render<Value>['exports']>;
24
+ <Value extends string>(internal: unknown, props: ReturnType<__sveltets_Render<Value>['props']> & {}): ReturnType<__sveltets_Render<Value>['exports']>;
25
+ z_$$bindings?: ReturnType<__sveltets_Render<any>['bindings']>;
26
+ }
27
+ /**
28
+ * A choice between a few views or modes, all visible at once. The selected segment is outlined as
29
+ * well as filled, so the choice does not depend on telling two greys apart.
30
+ *
31
+ * It is a group of native radio buttons, so the arrow keys move between segments.
32
+ */
33
+ declare const SegmentedControl: $$IsomorphicComponent;
34
+ type SegmentedControl<Value extends string> = InstanceType<typeof SegmentedControl<Value>>;
35
+ export default SegmentedControl;
@@ -0,0 +1,214 @@
1
+ <!--
2
+ @component
3
+ A field for choosing one option from a list too long to show at once. Closed, it looks like a
4
+ TextField; pressed, it opens the list under itself with a Menu's surface, as wide as the field.
5
+ Choosing an option calls `onChange` and closes the list; a press outside or Escape closes it
6
+ too.
7
+
8
+ The field is a button that says it opens a list box and whether the list is open. In the list
9
+ the arrow keys move, typing jumps to an option, and Enter chooses. The chosen option is
10
+ selected for assistive technology and marked with a check.
11
+ -->
12
+ <script module lang="ts">
13
+ import type { SelectBaseProps } from "@xds/core";
14
+ import type { RootProps } from "./root.js";
15
+
16
+ export type { SelectOption } from "@xds/core";
17
+
18
+ export type SelectProps<Value extends string = string> = SelectBaseProps<Value> &
19
+ RootProps & {
20
+ onChange: (value: Value) => void;
21
+ };
22
+ </script>
23
+
24
+ <script lang="ts" generics="Value extends string = string">
25
+ import { space } from "@xds/tokens";
26
+ import { classes, keepListKeys, type SelectOption, selectOptionIds } from "@xds/core";
27
+ import * as select from "@zag-js/select";
28
+ import { mergeProps, normalizeProps, useMachine } from "@zag-js/svelte";
29
+ import type { HTMLAttributes, HTMLLabelAttributes } from "svelte/elements";
30
+ import Icon from "./Icon.svelte";
31
+ import { modalPopover } from "./modal-popover.svelte.js";
32
+ import Portal from "./Portal.svelte";
33
+ import { createPresence } from "./presence.svelte.js";
34
+
35
+ type KeyHandler = (event: KeyboardEvent) => void;
36
+
37
+ let {
38
+ label,
39
+ options,
40
+ value,
41
+ placeholder,
42
+ description,
43
+ error,
44
+ name,
45
+ disabled,
46
+ onChange,
47
+ class: className,
48
+ style,
49
+ }: SelectProps<Value> = $props();
50
+
51
+ const id = $props.id();
52
+ const valueId = `${id}-value`;
53
+ const descriptionId = `${id}-description`;
54
+ const errorId = `${id}-error`;
55
+ const invalid = $derived(error !== undefined);
56
+ const collection = $derived(
57
+ select.collection<SelectOption<Value>>({
58
+ items: [...options],
59
+ itemToValue: (option) => option.value,
60
+ itemToString: (option) => option.label,
61
+ }),
62
+ );
63
+ const service = useMachine(select.machine, () => ({
64
+ id,
65
+ collection,
66
+ value: value === undefined ? [] : [value],
67
+ onValueChange: (details: select.ValueChangeDetails<SelectOption<Value>>) => {
68
+ const next = details.value[0];
69
+ if (next !== undefined) onChange(next as Value);
70
+ },
71
+ disabled,
72
+ invalid,
73
+ name,
74
+ positioning: {
75
+ placement: "bottom-start" as const,
76
+ gutter: space[1],
77
+ sameWidth: true,
78
+ // The list keeps this far from the window's edges.
79
+ overflowPadding: space[3],
80
+ },
81
+ }));
82
+ const api = $derived(select.connect(service, normalizeProps));
83
+ // Zag's key handlers read whether a search by typing is under way when the field renders, and
84
+ // a search ends on a timer, which renders nothing: long after a search, Space would still
85
+ // search for a space instead of opening the list or choosing. A handler taken from the
86
+ // machine when the key comes reads it then.
87
+ const current = () => select.connect(service, normalizeProps);
88
+ // A span: the field is a button, which a <label> cannot point at. A press on it still focuses
89
+ // the field.
90
+ const labelProps = $derived.by(() => {
91
+ const { for: _, ...rest } = api.getLabelProps() as HTMLLabelAttributes;
92
+ return rest as HTMLAttributes<HTMLSpanElement>;
93
+ });
94
+ // A button that opens a list box, rather than Zag's combobox: named by the chosen option and
95
+ // then the label, and described by the help or the error. The error is said there, so the
96
+ // button itself does not claim to be invalid or required.
97
+ const triggerProps = $derived.by(() => {
98
+ const {
99
+ role: _role,
100
+ "aria-invalid": _invalid,
101
+ "aria-required": _required,
102
+ onkeydown: _onKeyDown,
103
+ ...rest
104
+ } = api.getTriggerProps();
105
+ return {
106
+ ...rest,
107
+ // Home and End leave a closed field to the page; Zag would choose the first or the last
108
+ // option.
109
+ onkeydown: (event: KeyboardEvent) => {
110
+ if (!api.open && (event.key === "Home" || event.key === "End")) return;
111
+ (current().getTriggerProps().onkeydown as KeyHandler | undefined)?.(event);
112
+ },
113
+ };
114
+ });
115
+ let positioner = $state<HTMLDivElement>();
116
+ let surface = $state<HTMLDivElement>();
117
+ modalPopover(
118
+ () => api.open,
119
+ () => positioner,
120
+ );
121
+ const present = createPresence(
122
+ () => api.open,
123
+ () => [surface],
124
+ );
125
+ const content = $derived.by(() => {
126
+ const { onkeydown: _onKeyDown, ...rest } = api.getContentProps();
127
+ return mergeProps(rest, {
128
+ onkeydown: (event: KeyboardEvent) => {
129
+ keepListKeys(event, surface, ".xds-select__item:not([data-disabled])", (value) =>
130
+ api.setHighlightedValue(value),
131
+ );
132
+ (current().getContentProps().onkeydown as KeyHandler | undefined)?.(event);
133
+ },
134
+ });
135
+ });
136
+ </script>
137
+
138
+ <!-- Merged, so that the app's style joins any the machine writes on the root. -->
139
+ <div {...mergeProps(api.getRootProps(), { class: classes("xds-select", className), style })}>
140
+ <span {...labelProps} class="xds-select__label">{label}</span>
141
+ <button
142
+ {...triggerProps}
143
+ aria-labelledby={`${valueId} ${labelProps.id}`}
144
+ aria-describedby={invalid ? errorId : description ? descriptionId : undefined}
145
+ class="xds-select__trigger"
146
+ >
147
+ <!-- The chosen option's label alone: the list's rows also hold a description and a check,
148
+ which do not belong in the field. -->
149
+ <span {...api.getValueTextProps()} id={valueId} class="xds-select__value"
150
+ >{api.valueAsString || placeholder}</span
151
+ >
152
+ <Icon name="chevrons-up-down" size="sm" class="text-xds-field-placeholder" />
153
+ </button>
154
+ <!-- One line under the box: the help, or the error in its place. -->
155
+ {#if invalid || description}
156
+ <div class="xds-select__footer">
157
+ {#if invalid}
158
+ <span id={errorId} class="xds-select__error">{error}</span>
159
+ {:else}
160
+ <span id={descriptionId} class="xds-select__description">{description}</span>
161
+ {/if}
162
+ </div>
163
+ {/if}
164
+ <!-- What a form submits; the first option stands for no choice. Zag's props hide the select
165
+ from assistive technology, which uses the trigger. -->
166
+ <select {...api.getHiddenSelectProps()}>
167
+ <option value=""></option>
168
+ {#each options as option (option.value)}
169
+ <option value={option.value}>{option.label}</option>
170
+ {/each}
171
+ </select>
172
+ <Portal>
173
+ {#if api.open}
174
+ <!-- Takes a press outside, which then only closes the list. The focus stays in the list
175
+ until the list hands it back to the field. -->
176
+ <!-- svelte-ignore a11y_no_static_element_interactions -->
177
+ <div class="xds-select__underlay" onmousedown={(event) => event.preventDefault()}></div>
178
+ {/if}
179
+ <div {...api.getPositionerProps()} bind:this={positioner} class="xds-select__positioner">
180
+ <!-- Shown until its exit animation ends; Zag would hide it as it closes. -->
181
+ <div {...content} bind:this={surface} hidden={!present()} class="xds-select__content">
182
+ <div {...api.getListProps()} class="xds-select__list">
183
+ {#each collection.items as option, index (option.value)}
184
+ {@const ids = selectOptionIds(id, index)}
185
+ <!-- Named by its label alone, as the field shows it; the help line describes it. -->
186
+ <div
187
+ {...api.getItemProps({ item: option })}
188
+ aria-labelledby={ids.label}
189
+ aria-describedby={option.description ? ids.description : undefined}
190
+ class="xds-select__item"
191
+ >
192
+ <span class="xds-select__item-words">
193
+ <span
194
+ {...api.getItemTextProps({ item: option })}
195
+ id={ids.label}
196
+ class="xds-select__item-label">{option.label}</span
197
+ >
198
+ {#if option.description}
199
+ <span id={ids.description} class="xds-select__item-description"
200
+ >{option.description}</span
201
+ >
202
+ {/if}
203
+ </span>
204
+ <!-- Unnamed, unlike a Menu's: the option itself reports that it is selected. -->
205
+ {#if api.getItemState({ item: option }).selected}
206
+ <Icon name="check" size="sm" />
207
+ {/if}
208
+ </div>
209
+ {/each}
210
+ </div>
211
+ </div>
212
+ </div>
213
+ </Portal>
214
+ </div>
@@ -0,0 +1,40 @@
1
+ import type { SelectBaseProps } from "@xds/core";
2
+ import type { RootProps } from "./root.js";
3
+ export type { SelectOption } from "@xds/core";
4
+ export type SelectProps<Value extends string = string> = SelectBaseProps<Value> & RootProps & {
5
+ onChange: (value: Value) => void;
6
+ };
7
+ declare function $$render<Value extends string = string>(): {
8
+ props: SelectProps<Value>;
9
+ exports: {};
10
+ bindings: "";
11
+ slots: {};
12
+ events: {};
13
+ };
14
+ declare class __sveltets_Render<Value extends string = string> {
15
+ props(): ReturnType<typeof $$render<Value>>['props'];
16
+ events(): ReturnType<typeof $$render<Value>>['events'];
17
+ slots(): ReturnType<typeof $$render<Value>>['slots'];
18
+ bindings(): "";
19
+ exports(): {};
20
+ }
21
+ interface $$IsomorphicComponent {
22
+ new <Value extends string = string>(options: import('svelte').ComponentConstructorOptions<ReturnType<__sveltets_Render<Value>['props']>>): import('svelte').SvelteComponent<ReturnType<__sveltets_Render<Value>['props']>, ReturnType<__sveltets_Render<Value>['events']>, ReturnType<__sveltets_Render<Value>['slots']>> & {
23
+ $$bindings?: ReturnType<__sveltets_Render<Value>['bindings']>;
24
+ } & ReturnType<__sveltets_Render<Value>['exports']>;
25
+ <Value extends string = string>(internal: unknown, props: ReturnType<__sveltets_Render<Value>['props']> & {}): ReturnType<__sveltets_Render<Value>['exports']>;
26
+ z_$$bindings?: ReturnType<__sveltets_Render<any>['bindings']>;
27
+ }
28
+ /**
29
+ * A field for choosing one option from a list too long to show at once. Closed, it looks like a
30
+ * TextField; pressed, it opens the list under itself with a Menu's surface, as wide as the field.
31
+ * Choosing an option calls `onChange` and closes the list; a press outside or Escape closes it
32
+ * too.
33
+ *
34
+ * The field is a button that says it opens a list box and whether the list is open. In the list
35
+ * the arrow keys move, typing jumps to an option, and Enter chooses. The chosen option is
36
+ * selected for assistive technology and marked with a check.
37
+ */
38
+ declare const Select: $$IsomorphicComponent;
39
+ type Select<Value extends string = string> = InstanceType<typeof Select<Value>>;
40
+ export default Select;