@xds/react 0.1.0 → 1.0.0-rc.1

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 (140) hide show
  1. package/dist/Accordion.js +191 -33
  2. package/dist/Avatar.js +65 -6
  3. package/dist/AvatarStack.js +101 -6
  4. package/dist/Badge.js +61 -4
  5. package/dist/Banner.js +161 -5
  6. package/dist/Button.js +152 -24
  7. package/dist/Card.js +171 -12
  8. package/dist/Checkbox.js +138 -3
  9. package/dist/Chip.js +71 -4
  10. package/dist/ChipBar.js +36 -3
  11. package/dist/CountBadge.js +62 -9
  12. package/dist/DatePicker.js +421 -25
  13. package/dist/DateRangePicker.js +362 -40
  14. package/dist/Defaults.js +42 -7
  15. package/dist/Dialog.js +128 -24
  16. package/dist/Divider.js +29 -3
  17. package/dist/EmptyState.js +83 -3
  18. package/dist/Icon.js +74 -4
  19. package/dist/IconButton.js +113 -18
  20. package/dist/ListRow.js +180 -10
  21. package/dist/MediaCard.js +273 -11
  22. package/dist/Menu.js +234 -58
  23. package/dist/ProgressBar.js +63 -4
  24. package/dist/RadioGroup.js +173 -12
  25. package/dist/SearchField.js +148 -16
  26. package/dist/SectionHeader.js +121 -5
  27. package/dist/SegmentedControl.js +104 -5
  28. package/dist/Select.js +307 -55
  29. package/dist/Sidebar.js +212 -19
  30. package/dist/Skeleton.js +61 -5
  31. package/dist/Slider.js +142 -35
  32. package/dist/Spinner.js +93 -4
  33. package/dist/Switch.js +114 -3
  34. package/dist/Tabs.js +130 -14
  35. package/dist/Text.js +42 -4
  36. package/dist/TextField.js +209 -26
  37. package/dist/Thumbnail.js +146 -7
  38. package/dist/Toast.js +118 -30
  39. package/dist/Tooltip.js +154 -20
  40. package/dist/base.d.ts +11 -0
  41. package/dist/base.js +10 -0
  42. package/dist/modal-popover.js +32 -10
  43. package/dist/picture.d.ts +6 -4
  44. package/dist/picture.js +72 -17
  45. package/dist/portal.js +24 -4
  46. package/dist/presence.js +30 -9
  47. package/dist/preview.d.ts +14 -3
  48. package/dist/preview.js +118 -74
  49. package/dist/root.js +1 -0
  50. package/dist/trigger.d.ts +7 -0
  51. package/dist/trigger.js +41 -8
  52. package/eject/files/Accordion.tsx +157 -0
  53. package/eject/files/Avatar.tsx +41 -0
  54. package/eject/files/AvatarStack.tsx +47 -0
  55. package/eject/files/Badge.tsx +33 -0
  56. package/eject/files/Banner.tsx +70 -0
  57. package/eject/files/Button.tsx +88 -0
  58. package/eject/files/Card.tsx +85 -0
  59. package/eject/files/Checkbox.tsx +59 -0
  60. package/eject/files/Chip.tsx +43 -0
  61. package/eject/files/ChipBar.tsx +31 -0
  62. package/eject/files/CountBadge.tsx +50 -0
  63. package/eject/files/DatePicker.tsx +192 -0
  64. package/eject/files/DateRangePicker.tsx +271 -0
  65. package/eject/files/Dialog.tsx +108 -0
  66. package/eject/files/Divider.tsx +13 -0
  67. package/eject/files/EmptyState.tsx +39 -0
  68. package/eject/files/Icon.tsx +38 -0
  69. package/eject/files/IconButton.tsx +62 -0
  70. package/eject/files/ListRow.tsx +86 -0
  71. package/eject/files/MediaCard.tsx +111 -0
  72. package/eject/files/Menu.tsx +168 -0
  73. package/eject/files/ProgressBar.tsx +34 -0
  74. package/eject/files/RadioGroup.tsx +112 -0
  75. package/eject/files/SearchField.tsx +88 -0
  76. package/eject/files/SectionHeader.tsx +56 -0
  77. package/eject/files/SegmentedControl.tsx +57 -0
  78. package/eject/files/Select.tsx +228 -0
  79. package/eject/files/Sidebar.tsx +149 -0
  80. package/eject/files/Skeleton.tsx +28 -0
  81. package/eject/files/Slider.tsx +109 -0
  82. package/eject/files/Spinner.tsx +36 -0
  83. package/eject/files/Switch.tsx +53 -0
  84. package/eject/files/Tabs.tsx +71 -0
  85. package/eject/files/Text.tsx +31 -0
  86. package/eject/files/TextField.tsx +122 -0
  87. package/eject/files/Thumbnail.tsx +66 -0
  88. package/eject/files/Toast.tsx +88 -0
  89. package/eject/files/Tooltip.tsx +120 -0
  90. package/eject/files/accordion.css +89 -0
  91. package/eject/files/accordion.ts +67 -0
  92. package/eject/files/avatar-stack.ts +57 -0
  93. package/eject/files/avatar.ts +42 -0
  94. package/eject/files/badge.ts +30 -0
  95. package/eject/files/banner.ts +68 -0
  96. package/eject/files/base.css +38 -0
  97. package/eject/files/button.css +241 -0
  98. package/eject/files/button.ts +106 -0
  99. package/eject/files/card.ts +58 -0
  100. package/eject/files/checkbox.ts +32 -0
  101. package/eject/files/chip-bar.ts +16 -0
  102. package/eject/files/chip.ts +35 -0
  103. package/eject/files/count-badge.ts +32 -0
  104. package/eject/files/date-picker.css +352 -0
  105. package/eject/files/date-picker.ts +250 -0
  106. package/eject/files/date-range-picker.ts +323 -0
  107. package/eject/files/dialog.css +139 -0
  108. package/eject/files/dialog.ts +15 -0
  109. package/eject/files/divider.ts +5 -0
  110. package/eject/files/empty-state.ts +22 -0
  111. package/eject/files/icon.ts +36 -0
  112. package/eject/files/list-row.ts +66 -0
  113. package/eject/files/media-card.ts +66 -0
  114. package/eject/files/menu.css +176 -0
  115. package/eject/files/menu.ts +84 -0
  116. package/eject/files/parts.ts +17 -0
  117. package/eject/files/progress-bar.ts +23 -0
  118. package/eject/files/radio-group.css +160 -0
  119. package/eject/files/radio-group.ts +24 -0
  120. package/eject/files/section-header.ts +32 -0
  121. package/eject/files/segmented-control.ts +30 -0
  122. package/eject/files/select.css +285 -0
  123. package/eject/files/select.ts +36 -0
  124. package/eject/files/sidebar.ts +75 -0
  125. package/eject/files/skeleton.ts +51 -0
  126. package/eject/files/slider.css +136 -0
  127. package/eject/files/slider.ts +65 -0
  128. package/eject/files/spinner.ts +22 -0
  129. package/eject/files/switch.ts +34 -0
  130. package/eject/files/tabs.css +112 -0
  131. package/eject/files/tabs.ts +35 -0
  132. package/eject/files/text-field.css +229 -0
  133. package/eject/files/text-field.ts +48 -0
  134. package/eject/files/text.ts +38 -0
  135. package/eject/files/thumbnail.ts +51 -0
  136. package/eject/files/toast.ts +31 -0
  137. package/eject/files/tooltip.css +104 -0
  138. package/eject/files/tooltip.ts +21 -0
  139. package/eject/manifest.json +814 -0
  140. package/package.json +9 -4
@@ -0,0 +1,112 @@
1
+ // Ejected from `@xds/react` 1.0.0-rc.1 by `xds eject`. This copy is the app's own: change it
2
+ // as you need. `xds eject --list` says which ejected files differ from what was copied.
3
+
4
+ import type { RootProps } from "@xds/react/base";
5
+ import { useId } from "react";
6
+ import { classes } from "./parts.ts";
7
+ import type { RadioGroupBaseProps } from "./radio-group.ts";
8
+
9
+ export type { RadioOption } from "./radio-group.ts";
10
+
11
+ export type RadioGroupProps<Value extends string = string> = RadioGroupBaseProps<Value> &
12
+ RootProps & {
13
+ onChange: (value: Value) => void;
14
+ };
15
+
16
+ /**
17
+ * One choice among a few, applied later with the form it belongs to. For a choice that takes
18
+ * effect at once among two to four options, a SegmentedControl is the usual pick.
19
+ *
20
+ * The chosen option has a dot and a thicker ring, so the choice does not depend on color. The
21
+ * options are native radio buttons, hidden and drawn beside, in a group named by the legend:
22
+ * the arrow keys move between them, and the error is tied to the group and announced.
23
+ */
24
+ export function RadioGroup<Value extends string = string>({
25
+ label,
26
+ options,
27
+ value,
28
+ onChange,
29
+ disabled = false,
30
+ error,
31
+ name,
32
+ className,
33
+ style,
34
+ }: RadioGroupProps<Value>) {
35
+ const id = useId();
36
+ const labelId = `${id}-label`;
37
+ const errorId = `${id}-error`;
38
+ const invalid = error !== undefined;
39
+ return (
40
+ <div
41
+ role="radiogroup"
42
+ aria-labelledby={labelId}
43
+ aria-describedby={invalid ? errorId : undefined}
44
+ aria-invalid={invalid || undefined}
45
+ aria-disabled={disabled || undefined}
46
+ aria-orientation="vertical"
47
+ className={classes("{{prefix}}-radio-group", className)}
48
+ style={style}
49
+ >
50
+ <span id={labelId} className="{{prefix}}-radio-group__label">
51
+ {label}
52
+ </span>
53
+ <div className="{{prefix}}-radio-group__options">
54
+ {options.map((option, index) => (
55
+ <label key={option.value} className="{{prefix}}-radio-group__option">
56
+ <input
57
+ type="radio"
58
+ className="{{prefix}}-radio-group__input sr-only"
59
+ // Radios group by name, which is what makes the arrow keys move between them.
60
+ name={name ?? id}
61
+ value={option.value}
62
+ checked={option.value === value}
63
+ disabled={disabled}
64
+ // The description is inside the pressable row, so the row's text would name the
65
+ // radio together with it. The label alone names it; the description describes it.
66
+ aria-labelledby={`${id}-${index}`}
67
+ aria-describedby={ids(
68
+ option.description ? `${id}-${index}-description` : undefined,
69
+ invalid ? errorId : undefined,
70
+ )}
71
+ // oxlint-disable-next-line jsx-a11y/role-supports-aria-props -- on each radio as well as the group, so it is heard on the radio that has focus
72
+ aria-invalid={invalid || undefined}
73
+ onChange={() => onChange(option.value)}
74
+ />
75
+ <span className="{{prefix}}-radio-group__row">
76
+ <span className="{{prefix}}-radio-group__circle-line">
77
+ <span className="{{prefix}}-radio-group__circle">
78
+ <span className="{{prefix}}-radio-group__dot" />
79
+ </span>
80
+ </span>
81
+ <span className="{{prefix}}-radio-group__words">
82
+ <span id={`${id}-${index}`} className="{{prefix}}-radio-group__text">
83
+ {option.label}
84
+ </span>
85
+ {option.description && (
86
+ <span
87
+ id={`${id}-${index}-description`}
88
+ className="{{prefix}}-radio-group__description"
89
+ >
90
+ {option.description}
91
+ </span>
92
+ )}
93
+ </span>
94
+ </span>
95
+ </label>
96
+ ))}
97
+ </div>
98
+ {/* The region is there before the error is, so that the error is announced when it
99
+ appears. Empty, it takes no room. */}
100
+ <div aria-live="polite">
101
+ {invalid && (
102
+ <span id={errorId} className="{{prefix}}-radio-group__error">
103
+ {error}
104
+ </span>
105
+ )}
106
+ </div>
107
+ </div>
108
+ );
109
+ }
110
+
111
+ /** The ids that are there, as `aria-describedby` takes them, or nothing when none is. */
112
+ const ids = (...all: (string | undefined)[]) => all.filter(Boolean).join(" ") || undefined;
@@ -0,0 +1,88 @@
1
+ // Ejected from `@xds/react` 1.0.0-rc.1 by `xds eject`. This copy is the app's own: change it
2
+ // as you need. `xds eject --list` says which ejected files differ from what was copied.
3
+
4
+ import type { RootProps } from "@xds/react/base";
5
+ import { useRef } from "react";
6
+ import { Icon } from "./Icon.tsx";
7
+ import { classes, rootPart } from "./parts.ts";
8
+ import type { SearchFieldBaseProps } from "./text-field.ts";
9
+
10
+ export type SearchFieldProps = SearchFieldBaseProps &
11
+ RootProps & {
12
+ onChange: (value: string) => void;
13
+ /** Called with the query on Enter, or on the search key of a software keyboard. */
14
+ onSubmit?: (value: string) => void;
15
+ };
16
+
17
+ /**
18
+ * A text field for a query: TextField's box as a pill, with a search icon in front and, while
19
+ * there is a value, a control that clears it. Escape clears too.
20
+ *
21
+ * Use it to filter or search what is on the screen. For a value that is saved with a form,
22
+ * use a TextField, which has a visible label.
23
+ */
24
+ export function SearchField({
25
+ value,
26
+ onChange,
27
+ onSubmit,
28
+ label,
29
+ placeholder,
30
+ disabled = false,
31
+ clearLabel = "Clear",
32
+ className,
33
+ style,
34
+ }: SearchFieldProps) {
35
+ const input = useRef<HTMLInputElement>(null);
36
+ return (
37
+ // A TextField's root, drawn by its stylesheet, with a root part of its own.
38
+ <div
39
+ className={classes("{{prefix}}-text-field", rootPart("SearchField"), className)}
40
+ style={style}
41
+ >
42
+ <div className="{{prefix}}-text-field__control {{prefix}}-text-field__control--pill {{prefix}}-text-field__group">
43
+ <span className="{{prefix}}-text-field__slot {{prefix}}-text-field__slot--leading">
44
+ <Icon name="search" size="sm" />
45
+ </span>
46
+ <input
47
+ ref={input}
48
+ type="text"
49
+ className="{{prefix}}-text-field__input"
50
+ aria-label={label}
51
+ enterKeyHint="search"
52
+ placeholder={placeholder}
53
+ value={value}
54
+ disabled={disabled}
55
+ // Escape clears only while there is something to clear; an empty field lets Escape
56
+ // through to whatever it sits in, such as a dialog.
57
+ data-uses-escape={value !== "" ? "" : undefined}
58
+ onChange={(event) => onChange(event.currentTarget.value)}
59
+ onKeyDown={(event) => {
60
+ // While a composition is open, Enter and Escape belong to the input method.
61
+ if (event.nativeEvent.isComposing) return;
62
+ if (event.key === "Enter") onSubmit?.(value);
63
+ if (event.key === "Escape" && value !== "") {
64
+ event.stopPropagation();
65
+ onChange("");
66
+ }
67
+ }}
68
+ />
69
+ {value !== "" && !disabled && (
70
+ <span className="{{prefix}}-text-field__slot {{prefix}}-text-field__slot--trailing">
71
+ <button
72
+ type="button"
73
+ aria-label={clearLabel}
74
+ onClick={() => {
75
+ onChange("");
76
+ // The control is about to disappear, and focus would be lost with it.
77
+ input.current?.focus();
78
+ }}
79
+ className="-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"
80
+ >
81
+ <Icon name="circle-x" size="sm" />
82
+ </button>
83
+ </span>
84
+ )}
85
+ </div>
86
+ </div>
87
+ );
88
+ }
@@ -0,0 +1,56 @@
1
+ // Ejected from `@xds/react` 1.0.0-rc.1 by `xds eject`. This copy is the app's own: change it
2
+ // as you need. `xds eject --list` says which ejected files differ from what was copied.
3
+
4
+ import type { RootProps } from "@xds/react/base";
5
+ import { Button } from "./Button.tsx";
6
+ import { Icon } from "./Icon.tsx";
7
+ import { classes, rootPart } from "./parts.ts";
8
+ import { SECTION_HEADER_PARTS, type SectionHeaderBaseProps } from "./section-header.ts";
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
+
16
+ /**
17
+ * The title row of a group of content. The title is a heading at `level`; the action is a quiet
18
+ * button with a chevron at its end edge, which points the other way in a right-to-left layout.
19
+ */
20
+ export function SectionHeader({
21
+ title,
22
+ description,
23
+ icon,
24
+ action,
25
+ level = 2,
26
+ className,
27
+ style,
28
+ }: SectionHeaderProps) {
29
+ const Heading = `h${level}` as const;
30
+ return (
31
+ <div
32
+ className={classes(rootPart("SectionHeader"), SECTION_HEADER_PARTS.root, className)}
33
+ style={style}
34
+ >
35
+ <div className={SECTION_HEADER_PARTS.lead}>
36
+ {icon && (
37
+ <span className={SECTION_HEADER_PARTS.icon}>
38
+ <Icon name={icon} size="md" />
39
+ </span>
40
+ )}
41
+ <div className={SECTION_HEADER_PARTS.text}>
42
+ <Heading className={SECTION_HEADER_PARTS.title}>{title}</Heading>
43
+ {description && <p className={SECTION_HEADER_PARTS.description}>{description}</p>}
44
+ </div>
45
+ </div>
46
+ {action && (
47
+ <span className={SECTION_HEADER_PARTS.action}>
48
+ <Button variant="ghost" size="sm" onPress={action.onPress}>
49
+ {action.label}
50
+ <Icon name="chevron-right" size="sm" className={SECTION_HEADER_PARTS.chevron} />
51
+ </Button>
52
+ </span>
53
+ )}
54
+ </div>
55
+ );
56
+ }
@@ -0,0 +1,57 @@
1
+ // Ejected from `@xds/react` 1.0.0-rc.1 by `xds eject`. This copy is the app's own: change it
2
+ // as you need. `xds eject --list` says which ejected files differ from what was copied.
3
+
4
+ import type { RootProps } from "@xds/react/base";
5
+ import { useId } from "react";
6
+ import { classes, rootPart } from "./parts.ts";
7
+ import {
8
+ type SegmentedControlBaseProps,
9
+ segmentedControlClass,
10
+ segmentedControlOptionClass,
11
+ segmentedControlSegmentClass,
12
+ } from "./segmented-control.ts";
13
+
14
+ export type SegmentedControlProps<Value extends string> = SegmentedControlBaseProps<Value> &
15
+ RootProps & {
16
+ onChange: (value: Value) => void;
17
+ };
18
+
19
+ /**
20
+ * A choice between a few views or modes, all visible at once. The selected segment is outlined
21
+ * as well as filled, so the choice does not depend on telling two greys apart.
22
+ *
23
+ * It is a group of native radio buttons, so the arrow keys move between segments.
24
+ */
25
+ export function SegmentedControl<Value extends string>({
26
+ options,
27
+ value,
28
+ onChange,
29
+ fullWidth = false,
30
+ className,
31
+ style,
32
+ ...aria
33
+ }: SegmentedControlProps<Value>) {
34
+ const name = useId();
35
+ return (
36
+ <div
37
+ {...aria}
38
+ role="radiogroup"
39
+ className={classes(rootPart("SegmentedControl"), segmentedControlClass(fullWidth), className)}
40
+ style={style}
41
+ >
42
+ {options.map((option) => (
43
+ <label key={option.value} className={segmentedControlOptionClass(fullWidth)}>
44
+ <input
45
+ type="radio"
46
+ name={name}
47
+ value={option.value}
48
+ className="peer sr-only"
49
+ checked={option.value === value}
50
+ onChange={() => onChange(option.value)}
51
+ />
52
+ <span className={segmentedControlSegmentClass}>{option.label}</span>
53
+ </label>
54
+ ))}
55
+ </div>
56
+ );
57
+ }
@@ -0,0 +1,228 @@
1
+ // Ejected from `@xds/react` 1.0.0-rc.1 by `xds eject`. This copy is the app's own: change it
2
+ // as you need. `xds eject --list` says which ejected files differ from what was copied.
3
+
4
+ import {
5
+ keepListKeys,
6
+ Portal,
7
+ type RootProps,
8
+ space,
9
+ useModalPopover,
10
+ usePresence,
11
+ } from "@xds/react/base";
12
+ import { mergeProps, normalizeProps, useMachine } from "@zag-js/react";
13
+ import * as select from "@zag-js/select";
14
+ import { type KeyboardEvent, useId, useMemo, useRef } from "react";
15
+ import { Icon } from "./Icon.tsx";
16
+ import { classes } from "./parts.ts";
17
+ import { type SelectBaseProps, type SelectOption, selectOptionIds } from "./select.ts";
18
+
19
+ export type { SelectOption } from "./select.ts";
20
+
21
+ /** `className` and `style` are for the field, not for the list that opens from it. */
22
+ export type SelectProps<Value extends string = string> = SelectBaseProps<Value> &
23
+ RootProps & {
24
+ onChange: (value: Value) => void;
25
+ };
26
+
27
+ type KeyHandler = (event: KeyboardEvent) => void;
28
+
29
+ /**
30
+ * A field for choosing one option from a list too long to show at once. Closed, it looks like a
31
+ * TextField; pressed, it opens the list under itself with a Menu's surface, as wide as the
32
+ * field. Choosing an option calls `onChange` and closes the list; a press outside or Escape
33
+ * closes it too.
34
+ *
35
+ * The field is a button that says it opens a list box and whether the list is open. In the list
36
+ * the arrow keys move, typing jumps to an option, and Enter chooses. The chosen option is
37
+ * selected for assistive technology and marked with a check.
38
+ */
39
+ export function Select<Value extends string = string>({
40
+ label,
41
+ options,
42
+ value,
43
+ onChange,
44
+ placeholder,
45
+ description,
46
+ error,
47
+ name,
48
+ disabled = false,
49
+ className,
50
+ style,
51
+ }: SelectProps<Value>) {
52
+ const id = useId();
53
+ const valueId = `${id}-value`;
54
+ const descriptionId = `${id}-description`;
55
+ const errorId = `${id}-error`;
56
+ const invalid = error !== undefined;
57
+ const collection = useMemo(
58
+ () =>
59
+ select.collection<SelectOption<Value>>({
60
+ items: [...options],
61
+ itemToValue: (option) => option.value,
62
+ itemToString: (option) => option.label,
63
+ }),
64
+ [options],
65
+ );
66
+ const service = useMachine(select.machine, {
67
+ id,
68
+ collection,
69
+ value: value === undefined ? [] : [value],
70
+ onValueChange: (details) => {
71
+ const next = details.value[0];
72
+ if (next !== undefined) onChange(next as Value);
73
+ },
74
+ disabled,
75
+ invalid,
76
+ name,
77
+ positioning: {
78
+ placement: "bottom-start",
79
+ gutter: space[1],
80
+ sameWidth: true,
81
+ // The list keeps this far from the window's edges.
82
+ overflowPadding: space[3],
83
+ },
84
+ });
85
+ const api = select.connect(service, normalizeProps);
86
+ // Zag's key handlers read whether a search by typing is under way when the field renders,
87
+ // and a search ends on a timer, which renders nothing: long after a search, Space would
88
+ // still search for a space instead of opening the list or choosing. A handler taken from
89
+ // the machine when the key comes reads it then.
90
+ const current = () => select.connect(service, normalizeProps);
91
+ // A span: the field is a button, which a <label> cannot point at. A press on it still
92
+ // focuses the field.
93
+ const { htmlFor: _for, ...labelProps } = api.getLabelProps();
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 {
98
+ role: _role,
99
+ "aria-invalid": _invalid,
100
+ "aria-required": _required,
101
+ onKeyDown: _onTriggerKeyDown,
102
+ ...triggerProps
103
+ } = api.getTriggerProps();
104
+ const positioner = useRef<HTMLDivElement>(null);
105
+ const surface = useRef<HTMLDivElement>(null);
106
+ const { onKeyDown: _onContentKeyDown, ...contentProps } = api.getContentProps();
107
+ const content = {
108
+ ...contentProps,
109
+ onKeyDown: (event: KeyboardEvent) => {
110
+ // Ahead of Zag's handler, which skips a key that was already handled.
111
+ keepListKeys(event, surface.current, ".{{prefix}}-select__item:not([data-disabled])", (row) =>
112
+ api.setHighlightedValue(row),
113
+ );
114
+ (current().getContentProps().onKeyDown as KeyHandler | undefined)?.(event);
115
+ },
116
+ };
117
+ useModalPopover(api.open, () => positioner.current);
118
+ const present = usePresence(api.open, () => [surface.current]);
119
+ return (
120
+ <div
121
+ {...mergeProps(api.getRootProps(), {
122
+ className: classes("{{prefix}}-select", className),
123
+ style,
124
+ })}
125
+ >
126
+ <span {...labelProps} className="{{prefix}}-select__label">
127
+ {label}
128
+ </span>
129
+ <button
130
+ {...triggerProps}
131
+ aria-labelledby={`${valueId} ${labelProps.id}`}
132
+ aria-describedby={invalid ? errorId : description ? descriptionId : undefined}
133
+ onKeyDown={(event) => {
134
+ const now = current();
135
+ // Home and End leave a closed field to the page; Zag would choose the first or the
136
+ // last option.
137
+ if (!now.open && (event.key === "Home" || event.key === "End")) return;
138
+ (now.getTriggerProps().onKeyDown as KeyHandler | undefined)?.(event);
139
+ }}
140
+ type="button"
141
+ className="{{prefix}}-select__trigger"
142
+ >
143
+ {/* The chosen option's label alone: the list's rows also hold a description and a
144
+ check, which do not belong in the field. */}
145
+ <span {...api.getValueTextProps()} id={valueId} className="{{prefix}}-select__value">
146
+ {api.valueAsString || placeholder}
147
+ </span>
148
+ <Icon name="chevrons-up-down" size="sm" className="text-xds-field-placeholder" />
149
+ </button>
150
+ {/* One line under the box: the help, or the error in its place. */}
151
+ {(invalid || description) && (
152
+ <div className="{{prefix}}-select__footer">
153
+ {invalid ? (
154
+ <span id={errorId} className="{{prefix}}-select__error">
155
+ {error}
156
+ </span>
157
+ ) : (
158
+ <span id={descriptionId} className="{{prefix}}-select__description">
159
+ {description}
160
+ </span>
161
+ )}
162
+ </div>
163
+ )}
164
+ {/* What a form submits; the first option stands for no choice. */}
165
+ <select {...api.getHiddenSelectProps()}>
166
+ {/* oxlint-disable-next-line jsx-a11y/control-has-associated-label -- Zag's props hide the select from assistive technology, which uses the trigger */}
167
+ <option value="" />
168
+ {options.map((option) => (
169
+ <option key={option.value} value={option.value}>
170
+ {option.label}
171
+ </option>
172
+ ))}
173
+ </select>
174
+ <Portal>
175
+ {api.open && (
176
+ // Takes a press outside, which then only closes the list. The focus stays in the
177
+ // list until the list hands it back to the field.
178
+ // oxlint-disable-next-line jsx-a11y/no-static-element-interactions -- not a control: the handler only keeps the focus where it is
179
+ <div
180
+ className="{{prefix}}-select__underlay"
181
+ onMouseDown={(event) => event.preventDefault()}
182
+ />
183
+ )}
184
+ <div
185
+ {...api.getPositionerProps()}
186
+ ref={positioner}
187
+ className="{{prefix}}-select__positioner"
188
+ >
189
+ {/* Shown until its exit animation ends; Zag would hide it as it closes. */}
190
+ <div {...content} ref={surface} hidden={!present} className="{{prefix}}-select__content">
191
+ <div {...api.getListProps()} className="{{prefix}}-select__list">
192
+ {collection.items.map((option, index) => {
193
+ const ids = selectOptionIds(id, index);
194
+ return (
195
+ // Named by its label alone, as the field shows it; the help line describes it.
196
+ <div
197
+ key={option.value}
198
+ {...api.getItemProps({ item: option })}
199
+ aria-labelledby={ids.label}
200
+ aria-describedby={option.description ? ids.description : undefined}
201
+ className="{{prefix}}-select__item"
202
+ >
203
+ <span className="{{prefix}}-select__item-words">
204
+ <span
205
+ {...api.getItemTextProps({ item: option })}
206
+ id={ids.label}
207
+ className="{{prefix}}-select__item-label"
208
+ >
209
+ {option.label}
210
+ </span>
211
+ {option.description && (
212
+ <span id={ids.description} className="{{prefix}}-select__item-description">
213
+ {option.description}
214
+ </span>
215
+ )}
216
+ </span>
217
+ {/* Unnamed, unlike a Menu's: the option itself reports that it is selected. */}
218
+ {api.getItemState({ item: option }).selected && <Icon name="check" size="sm" />}
219
+ </div>
220
+ );
221
+ })}
222
+ </div>
223
+ </div>
224
+ </div>
225
+ </Portal>
226
+ </div>
227
+ );
228
+ }
@@ -0,0 +1,149 @@
1
+ // Ejected from `@xds/react` 1.0.0-rc.1 by `xds eject`. This copy is the app's own: change it
2
+ // as you need. `xds eject --list` says which ejected files differ from what was copied.
3
+
4
+ import { type IconName, type RootProps, useTrigger } from "@xds/react/base";
5
+ import { type ReactNode, useId } from "react";
6
+ import { Icon } from "./Icon.tsx";
7
+ import { PlaceRow } from "./ListRow.tsx";
8
+ import { classes, rootPart } from "./parts.ts";
9
+ import {
10
+ SIDEBAR_PARTS,
11
+ type SidebarBaseProps,
12
+ sidebarClass,
13
+ sidebarCount,
14
+ sidebarIconClass,
15
+ sidebarRailButtonClass,
16
+ sidebarSectionClass,
17
+ sidebarTitleClass,
18
+ } from "./sidebar.ts";
19
+ import { TooltipBeside } from "./Tooltip.tsx";
20
+
21
+ export type { SidebarItem, SidebarSection } from "./sidebar.ts";
22
+
23
+ export type SidebarProps<Value extends string = string> = SidebarBaseProps<Value> &
24
+ RootProps & {
25
+ onChange: (value: Value) => void;
26
+ /** At the top, such as the app's name. It has to fit the rail itself while collapsed. */
27
+ header?: ReactNode;
28
+ /** At the bottom, such as an account row. It has to fit the rail itself while collapsed. */
29
+ footer?: ReactNode;
30
+ };
31
+
32
+ /**
33
+ * The top-level places of an app used with a pointer: the counterpart of TabBar on touch. Each
34
+ * row is a compact list row; the current one is selected, its title heavier and its icon
35
+ * filled, so the place does not depend on the tint alone.
36
+ *
37
+ * It is navigation named by `label`, and the current row says it is the current page.
38
+ * Collapsed, the rows keep their names, and show them in a Tooltip under a pointer and on
39
+ * keyboard focus.
40
+ */
41
+ export function Sidebar<Value extends string = string>({
42
+ sections,
43
+ value,
44
+ onChange,
45
+ label,
46
+ collapsed = false,
47
+ header,
48
+ footer,
49
+ className,
50
+ style,
51
+ }: SidebarProps<Value>) {
52
+ const id = useId();
53
+ return (
54
+ <nav
55
+ aria-label={label}
56
+ className={classes(rootPart("Sidebar"), sidebarClass(collapsed), className)}
57
+ style={style}
58
+ >
59
+ {header != null && <div className={SIDEBAR_PARTS.edge}>{header}</div>}
60
+ {/* The places scroll when there are more than fit; the header and footer stay. */}
61
+ <div className={SIDEBAR_PARTS.places}>
62
+ {sections.map((section, index) => {
63
+ const title = `${id}-${index}`;
64
+ return (
65
+ // A section has nothing but its position to tell it apart: titles are optional.
66
+ <div key={index} className={sidebarSectionClass(index, collapsed)}>
67
+ {section.title && (
68
+ <p id={title} className={sidebarTitleClass(collapsed)}>
69
+ {section.title}
70
+ </p>
71
+ )}
72
+ {collapsed && index > 0 && <div aria-hidden="true" className={SIDEBAR_PARTS.rule} />}
73
+ <ul
74
+ {...(section.title ? { "aria-labelledby": title } : {})}
75
+ className={SIDEBAR_PARTS.list}
76
+ >
77
+ {section.items.map((item) => {
78
+ const selected = item.value === value;
79
+ const count = sidebarCount(item.count);
80
+ return (
81
+ <li key={item.value} className={SIDEBAR_PARTS.item}>
82
+ {collapsed ? (
83
+ <TooltipBeside label={item.label}>
84
+ <RailButton
85
+ label={item.label}
86
+ icon={item.icon}
87
+ selected={selected}
88
+ onPress={() => onChange(item.value)}
89
+ />
90
+ </TooltipBeside>
91
+ ) : (
92
+ <PlaceRow
93
+ compact
94
+ current="page"
95
+ selected={selected}
96
+ onPress={() => onChange(item.value)}
97
+ title={item.label}
98
+ leading={
99
+ <Icon
100
+ name={item.icon}
101
+ size="md"
102
+ filled={selected}
103
+ className={sidebarIconClass(selected)}
104
+ />
105
+ }
106
+ trailing={
107
+ count !== undefined && (
108
+ <span className={SIDEBAR_PARTS.count}>{count}</span>
109
+ )
110
+ }
111
+ />
112
+ )}
113
+ </li>
114
+ );
115
+ })}
116
+ </ul>
117
+ </div>
118
+ );
119
+ })}
120
+ </div>
121
+ {footer != null && <div className={SIDEBAR_PARTS.edge}>{footer}</div>}
122
+ </nav>
123
+ );
124
+ }
125
+
126
+ /** A row of the collapsed rail. It is the trigger of the Tooltip that names it. */
127
+ function RailButton({
128
+ label,
129
+ icon,
130
+ selected,
131
+ onPress,
132
+ }: {
133
+ label: string;
134
+ icon: IconName;
135
+ selected: boolean;
136
+ onPress: () => void;
137
+ }) {
138
+ const attributes = useTrigger({
139
+ className: sidebarRailButtonClass(selected),
140
+ "aria-label": label,
141
+ "aria-current": selected ? ("page" as const) : undefined,
142
+ onClick: onPress,
143
+ });
144
+ return (
145
+ <button {...attributes} type="button">
146
+ <Icon name={icon} size="md" filled={selected} />
147
+ </button>
148
+ );
149
+ }