@xds/react 0.0.0-stage → 0.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 (190) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +19 -2
  3. package/dist/Accordion.d.ts +20 -0
  4. package/dist/Accordion.js +197 -0
  5. package/dist/Avatar.d.ts +9 -0
  6. package/dist/Avatar.js +69 -0
  7. package/dist/AvatarStack.d.ts +8 -0
  8. package/dist/AvatarStack.js +105 -0
  9. package/dist/Badge.d.ts +13 -0
  10. package/dist/Badge.js +66 -0
  11. package/dist/Banner.d.ts +23 -0
  12. package/dist/Banner.js +166 -0
  13. package/dist/Button.d.ts +14 -0
  14. package/dist/Button.js +159 -0
  15. package/dist/Card.d.ts +18 -0
  16. package/dist/Card.js +173 -0
  17. package/dist/Checkbox.d.ts +10 -0
  18. package/dist/Checkbox.js +140 -0
  19. package/dist/Chip.d.ts +12 -0
  20. package/dist/Chip.js +74 -0
  21. package/dist/ChipBar.d.ts +12 -0
  22. package/dist/ChipBar.js +38 -0
  23. package/dist/CountBadge.d.ts +12 -0
  24. package/dist/CountBadge.js +64 -0
  25. package/dist/DatePicker.d.ts +20 -0
  26. package/dist/DatePicker.js +429 -0
  27. package/dist/DateRangePicker.d.ts +24 -0
  28. package/dist/DateRangePicker.js +370 -0
  29. package/dist/Defaults.d.ts +17 -0
  30. package/dist/Defaults.js +48 -0
  31. package/dist/Dialog.d.ts +20 -0
  32. package/dist/Dialog.js +137 -0
  33. package/dist/Divider.d.ts +4 -0
  34. package/dist/Divider.js +31 -0
  35. package/dist/EmptyState.d.ts +12 -0
  36. package/dist/EmptyState.js +86 -0
  37. package/dist/Icon.d.ts +9 -0
  38. package/dist/Icon.js +79 -0
  39. package/dist/IconButton.d.ts +12 -0
  40. package/dist/IconButton.js +119 -0
  41. package/dist/ListRow.d.ts +21 -0
  42. package/dist/ListRow.js +185 -0
  43. package/dist/MediaCard.d.ts +20 -0
  44. package/dist/MediaCard.js +279 -0
  45. package/dist/Menu.d.ts +18 -0
  46. package/dist/Menu.js +244 -0
  47. package/dist/ProgressBar.d.ts +8 -0
  48. package/dist/ProgressBar.js +65 -0
  49. package/dist/RadioGroup.d.ts +15 -0
  50. package/dist/RadioGroup.js +176 -0
  51. package/dist/SearchField.d.ts +15 -0
  52. package/dist/SearchField.js +152 -0
  53. package/dist/SectionHeader.d.ts +14 -0
  54. package/dist/SectionHeader.js +124 -0
  55. package/dist/SegmentedControl.d.ts +12 -0
  56. package/dist/SegmentedControl.js +106 -0
  57. package/dist/Select.d.ts +18 -0
  58. package/dist/Select.js +318 -0
  59. package/dist/Sidebar.d.ts +21 -0
  60. package/dist/Sidebar.js +220 -0
  61. package/dist/Skeleton.d.ts +11 -0
  62. package/dist/Skeleton.js +64 -0
  63. package/dist/Slider.d.ts +13 -0
  64. package/dist/Slider.js +151 -0
  65. package/dist/Spinner.d.ts +9 -0
  66. package/dist/Spinner.js +97 -0
  67. package/dist/Switch.d.ts +13 -0
  68. package/dist/Switch.js +116 -0
  69. package/dist/Tabs.d.ts +14 -0
  70. package/dist/Tabs.js +137 -0
  71. package/dist/Text.d.ts +10 -0
  72. package/dist/Text.js +46 -0
  73. package/dist/TextField.d.ts +22 -0
  74. package/dist/TextField.js +213 -0
  75. package/dist/Thumbnail.d.ts +9 -0
  76. package/dist/Thumbnail.js +152 -0
  77. package/dist/Toast.d.ts +14 -0
  78. package/dist/Toast.js +121 -0
  79. package/dist/Tooltip.d.ts +26 -0
  80. package/dist/Tooltip.js +166 -0
  81. package/dist/base.d.ts +11 -0
  82. package/dist/base.js +10 -0
  83. package/dist/index.d.ts +39 -0
  84. package/dist/index.js +39 -0
  85. package/dist/modal-popover.d.ts +7 -0
  86. package/dist/modal-popover.js +37 -0
  87. package/dist/picture.d.ts +13 -0
  88. package/dist/picture.js +76 -0
  89. package/dist/portal.d.ts +9 -0
  90. package/dist/portal.js +27 -0
  91. package/dist/presence.d.ts +6 -0
  92. package/dist/presence.js +34 -0
  93. package/dist/preview.d.ts +17 -0
  94. package/dist/preview.js +126 -0
  95. package/dist/root.d.ts +12 -0
  96. package/dist/root.js +1 -0
  97. package/dist/trigger.d.ts +24 -0
  98. package/dist/trigger.js +47 -0
  99. package/eject/files/Accordion.tsx +157 -0
  100. package/eject/files/Avatar.tsx +41 -0
  101. package/eject/files/AvatarStack.tsx +47 -0
  102. package/eject/files/Badge.tsx +33 -0
  103. package/eject/files/Banner.tsx +70 -0
  104. package/eject/files/Button.tsx +88 -0
  105. package/eject/files/Card.tsx +85 -0
  106. package/eject/files/Checkbox.tsx +59 -0
  107. package/eject/files/Chip.tsx +43 -0
  108. package/eject/files/ChipBar.tsx +31 -0
  109. package/eject/files/CountBadge.tsx +50 -0
  110. package/eject/files/DatePicker.tsx +192 -0
  111. package/eject/files/DateRangePicker.tsx +271 -0
  112. package/eject/files/Dialog.tsx +108 -0
  113. package/eject/files/Divider.tsx +13 -0
  114. package/eject/files/EmptyState.tsx +39 -0
  115. package/eject/files/Icon.tsx +38 -0
  116. package/eject/files/IconButton.tsx +62 -0
  117. package/eject/files/ListRow.tsx +86 -0
  118. package/eject/files/MediaCard.tsx +111 -0
  119. package/eject/files/Menu.tsx +168 -0
  120. package/eject/files/ProgressBar.tsx +34 -0
  121. package/eject/files/RadioGroup.tsx +112 -0
  122. package/eject/files/SearchField.tsx +88 -0
  123. package/eject/files/SectionHeader.tsx +56 -0
  124. package/eject/files/SegmentedControl.tsx +57 -0
  125. package/eject/files/Select.tsx +228 -0
  126. package/eject/files/Sidebar.tsx +149 -0
  127. package/eject/files/Skeleton.tsx +28 -0
  128. package/eject/files/Slider.tsx +109 -0
  129. package/eject/files/Spinner.tsx +36 -0
  130. package/eject/files/Switch.tsx +53 -0
  131. package/eject/files/Tabs.tsx +71 -0
  132. package/eject/files/Text.tsx +31 -0
  133. package/eject/files/TextField.tsx +122 -0
  134. package/eject/files/Thumbnail.tsx +66 -0
  135. package/eject/files/Toast.tsx +88 -0
  136. package/eject/files/Tooltip.tsx +120 -0
  137. package/eject/files/accordion.css +89 -0
  138. package/eject/files/accordion.ts +67 -0
  139. package/eject/files/avatar-stack.ts +57 -0
  140. package/eject/files/avatar.ts +42 -0
  141. package/eject/files/badge.ts +30 -0
  142. package/eject/files/banner.ts +68 -0
  143. package/eject/files/base.css +38 -0
  144. package/eject/files/button.css +241 -0
  145. package/eject/files/button.ts +106 -0
  146. package/eject/files/card.ts +58 -0
  147. package/eject/files/checkbox.ts +32 -0
  148. package/eject/files/chip-bar.ts +16 -0
  149. package/eject/files/chip.ts +35 -0
  150. package/eject/files/count-badge.ts +32 -0
  151. package/eject/files/date-picker.css +352 -0
  152. package/eject/files/date-picker.ts +250 -0
  153. package/eject/files/date-range-picker.ts +323 -0
  154. package/eject/files/dialog.css +139 -0
  155. package/eject/files/dialog.ts +15 -0
  156. package/eject/files/divider.ts +5 -0
  157. package/eject/files/empty-state.ts +22 -0
  158. package/eject/files/icon.ts +36 -0
  159. package/eject/files/list-row.ts +66 -0
  160. package/eject/files/media-card.ts +66 -0
  161. package/eject/files/menu.css +176 -0
  162. package/eject/files/menu.ts +84 -0
  163. package/eject/files/parts.ts +17 -0
  164. package/eject/files/progress-bar.ts +23 -0
  165. package/eject/files/radio-group.css +160 -0
  166. package/eject/files/radio-group.ts +24 -0
  167. package/eject/files/section-header.ts +32 -0
  168. package/eject/files/segmented-control.ts +30 -0
  169. package/eject/files/select.css +285 -0
  170. package/eject/files/select.ts +36 -0
  171. package/eject/files/sidebar.ts +75 -0
  172. package/eject/files/skeleton.ts +51 -0
  173. package/eject/files/slider.css +136 -0
  174. package/eject/files/slider.ts +65 -0
  175. package/eject/files/spinner.ts +22 -0
  176. package/eject/files/switch.ts +34 -0
  177. package/eject/files/tabs.css +112 -0
  178. package/eject/files/tabs.ts +35 -0
  179. package/eject/files/text-field.css +229 -0
  180. package/eject/files/text-field.ts +48 -0
  181. package/eject/files/text.ts +38 -0
  182. package/eject/files/thumbnail.ts +51 -0
  183. package/eject/files/toast.ts +31 -0
  184. package/eject/files/tooltip.css +104 -0
  185. package/eject/files/tooltip.ts +21 -0
  186. package/eject/manifest.json +814 -0
  187. package/package.json +66 -4
  188. package/page.css +20 -0
  189. package/styles.css +3252 -0
  190. package/tailwind.css +2665 -0
@@ -0,0 +1,38 @@
1
+ // Ejected from `@xds/react` 0.2.0 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 { icons, type RootProps, useDefaults } from "@xds/react/base";
5
+ import { type IconBaseProps, iconClass } from "./icon.ts";
6
+ import { classes, rootPart } from "./parts.ts";
7
+
8
+ export type { IconName, IconSize } from "./icon.ts";
9
+
10
+ export type IconProps = IconBaseProps & RootProps;
11
+
12
+ /**
13
+ * An icon from design/icons. It takes the color of the text around it. An arrow or a chevron
14
+ * mirrors itself in a right-to-left layout when given `rtl:-scale-x-100` in `className`.
15
+ */
16
+ export function Icon(props: IconProps) {
17
+ const defaults = useDefaults("Icon");
18
+ const { name, filled = false, label, className, style } = props;
19
+ const size = props.size ?? defaults.size;
20
+ return (
21
+ // No <title>: the icon is named through aria-label when `label` is given, and hidden otherwise.
22
+ <svg
23
+ viewBox="0 0 24 24"
24
+ fill={filled ? "currentColor" : "none"}
25
+ stroke="currentColor"
26
+ strokeWidth="2"
27
+ strokeLinecap="round"
28
+ strokeLinejoin="round"
29
+ className={classes(rootPart("Icon"), iconClass(size), className)}
30
+ style={style}
31
+ {...(label ? { role: "img", "aria-label": label } : { "aria-hidden": true })}
32
+ >
33
+ {icons[name].map((path) => (
34
+ <path key={path} d={path} />
35
+ ))}
36
+ </svg>
37
+ );
38
+ }
@@ -0,0 +1,62 @@
1
+ // Ejected from `@xds/react` 0.2.0 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, useDefaults, useTrigger } from "@xds/react/base";
5
+ import { buttonClass, ICON_BUTTON_BOX, type IconButtonBaseProps, isHeld } from "./button.ts";
6
+ import { Icon } from "./Icon.tsx";
7
+ import { classes } from "./parts.ts";
8
+ import { Spinner } from "./Spinner.tsx";
9
+
10
+ export type IconButtonProps = IconButtonBaseProps &
11
+ RootProps & {
12
+ onPress?: () => void;
13
+ };
14
+
15
+ /**
16
+ * A button that holds only an icon, in Button's variants, sizes and states. It is a square as
17
+ * tall as a Button of its size, and its pressable area is never smaller than the minimum
18
+ * target, even when the square is. Like Button, it opens a Menu or a Tooltip it is the trigger
19
+ * of.
20
+ */
21
+ export function IconButton(props: IconButtonProps) {
22
+ const defaults = useDefaults("IconButton");
23
+ const {
24
+ icon,
25
+ label,
26
+ selected,
27
+ disabled = false,
28
+ loading = false,
29
+ onPress,
30
+ className,
31
+ style,
32
+ } = props;
33
+ const variant = props.variant ?? defaults.variant ?? "ghost";
34
+ const size = props.size ?? defaults.size ?? "md";
35
+ const attributes = useTrigger(
36
+ {
37
+ // The recipe gives the root part, `{{prefix}}-icon-button`.
38
+ className: classes(buttonClass({ variant, size, iconButton: true }), className),
39
+ style,
40
+ disabled,
41
+ "aria-disabled": loading || undefined,
42
+ "data-pending": loading ? "" : undefined,
43
+ "aria-pressed": selected,
44
+ "aria-label": label,
45
+ onClick: () => {
46
+ if (!loading) onPress?.();
47
+ },
48
+ },
49
+ loading,
50
+ );
51
+ return (
52
+ <button {...attributes} type="button">
53
+ {loading ? (
54
+ <span className={ICON_BUTTON_BOX[size]}>
55
+ <Spinner size="sm" />
56
+ </span>
57
+ ) : (
58
+ <Icon name={icon} size={size} filled={isHeld({ variant, selected, disabled })} />
59
+ )}
60
+ </button>
61
+ );
62
+ }
@@ -0,0 +1,86 @@
1
+ // Ejected from `@xds/react` 0.2.0 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 type { ReactNode } from "react";
6
+ import { Icon } from "./Icon.tsx";
7
+ import {
8
+ LIST_ROW_PARTS,
9
+ type ListRowBaseProps,
10
+ listRowClass,
11
+ listRowDescriptionClass,
12
+ listRowTitleClass,
13
+ } from "./list-row.ts";
14
+ import { classes, rootPart } from "./parts.ts";
15
+
16
+ export type ListRowProps = ListRowBaseProps &
17
+ RootProps & {
18
+ /** In front of the title: an avatar or an icon. */
19
+ leading?: ReactNode;
20
+ /** At the far end: a value, a badge or a switch. */
21
+ trailing?: ReactNode;
22
+ /** Makes the whole row a button. Do not put another control in `trailing` then. */
23
+ onPress?: () => void;
24
+ };
25
+
26
+ /** One line of a list or a settings screen. Separate rows with a Divider. */
27
+ export function ListRow(props: ListRowProps) {
28
+ return <PlaceRow {...props} current="true" />;
29
+ }
30
+
31
+ /**
32
+ * A ListRow that says what kind of current item its selected state is. A Sidebar's rows are the
33
+ * places of an app, so its selected row is the current page. Only for this package's own
34
+ * components: the index does not export it.
35
+ */
36
+ export function PlaceRow({
37
+ title,
38
+ description,
39
+ leading,
40
+ trailing,
41
+ chevron = false,
42
+ selected = false,
43
+ compact = false,
44
+ onPress,
45
+ disabled = false,
46
+ current,
47
+ className,
48
+ style,
49
+ }: ListRowProps & { current: "true" | "page" }) {
50
+ const row = classes(
51
+ rootPart("ListRow"),
52
+ listRowClass({ compact, selected, pressable: onPress !== undefined }),
53
+ className,
54
+ );
55
+ const currentProps = selected ? ({ "aria-current": current } as const) : {};
56
+ const content = (
57
+ <>
58
+ {leading}
59
+ <span className={LIST_ROW_PARTS.text}>
60
+ <span className={listRowTitleClass({ compact, selected, disabled })}>{title}</span>
61
+ {description && <span className={listRowDescriptionClass(compact)}>{description}</span>}
62
+ </span>
63
+ {trailing}
64
+ {chevron && <Icon name="chevron-right" size="sm" className={LIST_ROW_PARTS.chevron} />}
65
+ </>
66
+ );
67
+ if (!onPress) {
68
+ return (
69
+ <div {...currentProps} className={row} style={style}>
70
+ {content}
71
+ </div>
72
+ );
73
+ }
74
+ return (
75
+ <button
76
+ {...currentProps}
77
+ type="button"
78
+ disabled={disabled}
79
+ onClick={onPress}
80
+ className={row}
81
+ style={style}
82
+ >
83
+ {content}
84
+ </button>
85
+ );
86
+ }
@@ -0,0 +1,111 @@
1
+ // Ejected from `@xds/react` 0.2.0 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, useDefaults } from "@xds/react/base";
5
+ import { type ReactNode, useId } from "react";
6
+ import {
7
+ MEDIA_CARD_PARTS,
8
+ type MediaCardBaseProps,
9
+ mediaCardContentClass,
10
+ mediaCardIds,
11
+ mediaCardThumbnailClass,
12
+ } from "./media-card.ts";
13
+ import { classes, rootPart } from "./parts.ts";
14
+ import { Thumbnail } from "./Thumbnail.tsx";
15
+
16
+ export type MediaCardProps = MediaCardBaseProps &
17
+ RootProps & {
18
+ /** Before the text in the vertical layout, such as an Avatar. */
19
+ leading?: ReactNode;
20
+ /** At the end of the text: an IconButton or a Menu. It stays a control of its own. */
21
+ action?: ReactNode;
22
+ /** Makes the whole card one button. */
23
+ onPress?: () => void;
24
+ };
25
+
26
+ /**
27
+ * A thumbnail with its title and details: a video in a feed, an article in a list.
28
+ *
29
+ * With `onPress` the whole card is one button with no surface at rest: under a pointer and
30
+ * while pressed it takes the card's fills, and it has a focus ring. The button is named by the
31
+ * title followed by the details. `action` is a separate control beside it, so pressing it does
32
+ * not press the card.
33
+ */
34
+ export function MediaCard(props: MediaCardProps) {
35
+ const defaults = useDefaults("MediaCard");
36
+ const {
37
+ src,
38
+ alt = "",
39
+ // The card's own, so that a Thumbnail default the app sets does not reach its picture.
40
+ ratio = "16:9",
41
+ badge,
42
+ progress,
43
+ title,
44
+ meta = [],
45
+ leading,
46
+ action,
47
+ onPress,
48
+ className,
49
+ style,
50
+ } = props;
51
+ const layout = props.layout ?? defaults.layout ?? "vertical";
52
+ const ids = mediaCardIds(useId(), meta);
53
+ // On a pressable card the button says all of this, so the parts are hidden from assistive
54
+ // technology and only the action is left beside it.
55
+ const part = onPress ? ({ "aria-hidden": true } as const) : {};
56
+ const horizontal = layout === "horizontal";
57
+ const content = (
58
+ <div className={mediaCardContentClass({ horizontal, pressable: onPress !== undefined })}>
59
+ <span {...part} className={mediaCardThumbnailClass(horizontal)}>
60
+ <Thumbnail src={src} alt={alt} ratio={ratio} badge={badge} progress={progress} />
61
+ </span>
62
+ <div className={MEDIA_CARD_PARTS.body}>
63
+ {!horizontal && leading != null && (
64
+ <span {...part} className={MEDIA_CARD_PARTS.leading}>
65
+ {leading}
66
+ </span>
67
+ )}
68
+ <span {...part} className={MEDIA_CARD_PARTS.text}>
69
+ <span id={ids.title} className={MEDIA_CARD_PARTS.title}>
70
+ {title}
71
+ </span>
72
+ {meta.map((line, index) => (
73
+ // Two details may read the same, so they are told apart by their position.
74
+ <span key={index} id={ids.details[index]} className={MEDIA_CARD_PARTS.meta}>
75
+ {line}
76
+ </span>
77
+ ))}
78
+ </span>
79
+ {action != null && <div className={MEDIA_CARD_PARTS.action}>{action}</div>}
80
+ </div>
81
+ </div>
82
+ );
83
+ if (!onPress) {
84
+ return (
85
+ // A group, not a fieldset: a fieldset groups form controls, and sizes itself to its
86
+ // content, which would undo the ellipses.
87
+ <div
88
+ role="group"
89
+ aria-labelledby={ids.names}
90
+ className={classes(rootPart("MediaCard"), MEDIA_CARD_PARTS.group, className)}
91
+ style={style}
92
+ >
93
+ {content}
94
+ </div>
95
+ );
96
+ }
97
+ return (
98
+ <div
99
+ className={classes(rootPart("MediaCard"), MEDIA_CARD_PARTS.pressable, className)}
100
+ style={style}
101
+ >
102
+ <button
103
+ type="button"
104
+ onClick={onPress}
105
+ aria-labelledby={ids.names}
106
+ className={MEDIA_CARD_PARTS.button}
107
+ />
108
+ {content}
109
+ </div>
110
+ );
111
+ }
@@ -0,0 +1,168 @@
1
+ // Ejected from `@xds/react` 0.2.0 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
+ leaveEscapeToFields,
7
+ Portal,
8
+ type RootProps,
9
+ space,
10
+ TriggerContext,
11
+ type TriggerProps,
12
+ useMergedProps,
13
+ useModalPopover,
14
+ usePresence,
15
+ useProvideTrigger,
16
+ } from "@xds/react/base";
17
+ import * as menu from "@zag-js/menu";
18
+ import { normalizeProps, useMachine } from "@zag-js/react";
19
+ import { type MouseEvent, type PointerEvent, type ReactNode, useId, useRef } from "react";
20
+ import { Icon } from "./Icon.tsx";
21
+ import {
22
+ firstMenuValue,
23
+ layoutRect,
24
+ type MenuBaseProps,
25
+ menuItemClass,
26
+ opensOnPress,
27
+ } from "./menu.ts";
28
+ import { classes } from "./parts.ts";
29
+
30
+ export type { MenuItem } from "./menu.ts";
31
+
32
+ /** `className` and `style` are for the menu itself, the list that opens, not for its trigger. */
33
+ export type MenuProps = MenuBaseProps &
34
+ RootProps & {
35
+ /** The control that opens the menu: a Button or an IconButton. */
36
+ trigger: ReactNode;
37
+ };
38
+
39
+ /**
40
+ * A list of actions that opens from a control. Choosing an item calls its `onSelect` and
41
+ * closes the menu; a press outside or Escape closes it too, and focus goes back to the
42
+ * trigger.
43
+ *
44
+ * The trigger reports that it opens a menu and whether the menu is open. The arrow keys move
45
+ * the highlight and Enter chooses.
46
+ */
47
+ export function Menu({ trigger, label, items, align = "start", className, style }: MenuProps) {
48
+ const service = useMachine(menu.machine, {
49
+ id: useId(),
50
+ "aria-label": label,
51
+ positioning: {
52
+ placement: `bottom-${align}`,
53
+ gutter: space[1],
54
+ getAnchorRect: (anchor) => (anchor instanceof HTMLElement ? layoutRect(anchor) : null),
55
+ },
56
+ onSelect: (details) => {
57
+ // The values are positions, because two items may have the same label.
58
+ const item = items[Number(details.value)];
59
+ if (item && item !== "separator") item.onSelect();
60
+ },
61
+ onEscapeKeyDown: leaveEscapeToFields,
62
+ // The arrow keys go round from the last item to the first, as in a native menu.
63
+ loopFocus: true,
64
+ });
65
+ const api = menu.connect(service, normalizeProps);
66
+ // Zag's handler for a press is left out: it keeps the press from focusing the trigger, so
67
+ // the focus would reach the menu, and come back to the trigger as the menu closes, only from
68
+ // a script, which the browser marks with the keyboard ring.
69
+ const { onPointerDown: _onPointerDown, ...triggerProps } = api.getTriggerProps();
70
+ // Whether the last press on the trigger opened the menu, which its click must then not do.
71
+ const pressOpened = useRef(false);
72
+ const provided = useProvideTrigger(
73
+ useMergedProps<TriggerProps>(triggerProps, {
74
+ // Added to the control's own class; the trigger looks pressed while its menu is open.
75
+ className: "{{prefix}}-menu__trigger",
76
+ // A mouse opens the menu as its button goes down; touch and pen open it on the click,
77
+ // as Zag does.
78
+ onPointerDown: (event: PointerEvent<HTMLButtonElement>) => {
79
+ pressOpened.current = opensOnPress(event) && !api.open;
80
+ if (pressOpened.current) api.setOpen(true);
81
+ },
82
+ onClick: (event: MouseEvent<HTMLButtonElement>) => {
83
+ // A click with no pointer behind it comes from a screen reader or a script.
84
+ const pointer = event.detail !== 0;
85
+ // The press opened the menu already. This handler runs before Zag's, which skips a
86
+ // handled click. Not told by the click's pointer type: a browser may send the click
87
+ // as a plain mouse event.
88
+ if (pressOpened.current && pointer) {
89
+ pressOpened.current = false;
90
+ event.preventDefault();
91
+ return;
92
+ }
93
+ if (pointer) return;
94
+ // After the machine has opened the menu.
95
+ requestAnimationFrame(() => {
96
+ const now = menu.connect(service, normalizeProps);
97
+ if (!now.open) return;
98
+ const first = firstMenuValue(items);
99
+ if (first !== undefined) now.setHighlightedValue(first);
100
+ });
101
+ },
102
+ }),
103
+ );
104
+ const positioner = useRef<HTMLDivElement>(null);
105
+ const surface = useRef<HTMLDivElement>(null);
106
+ useModalPopover(api.open, () => positioner.current);
107
+ const present = usePresence(api.open, () => [surface.current]);
108
+ const content = useMergedProps(api.getContentProps(), {
109
+ className: classes("{{prefix}}-menu", className),
110
+ style,
111
+ // This handler runs before Zag's.
112
+ onKeyDown: (event: { key: string; preventDefault(): void }) =>
113
+ keepListKeys(event, surface.current, ".{{prefix}}-menu__item:not([data-disabled])", (value) =>
114
+ api.setHighlightedValue(value),
115
+ ),
116
+ });
117
+ return (
118
+ <>
119
+ <TriggerContext value={provided}>{trigger}</TriggerContext>
120
+ <Portal>
121
+ {/* Nothing inside opens an overlay from a control outside it. */}
122
+ <TriggerContext value={null}>
123
+ {api.open && (
124
+ // Takes a press outside, which then only closes the menu. The focus stays in the
125
+ // menu until the menu hands it back to the trigger.
126
+ // oxlint-disable-next-line jsx-a11y/no-static-element-interactions -- not a control: the handler only keeps the focus where it is
127
+ <div
128
+ className="{{prefix}}-menu__underlay"
129
+ onMouseDown={(event) => event.preventDefault()}
130
+ />
131
+ )}
132
+ <div
133
+ {...api.getPositionerProps()}
134
+ ref={positioner}
135
+ className="{{prefix}}-menu__positioner"
136
+ >
137
+ {/* Shown until its exit animation ends; Zag would hide it as it closes. */}
138
+ <div {...content} ref={surface} hidden={!present}>
139
+ {items.map((item, index) =>
140
+ item === "separator" ? (
141
+ // A separator has nothing but its position to tell it apart.
142
+ <div
143
+ key={index}
144
+ {...api.getSeparatorProps()}
145
+ className="{{prefix}}-menu__separator"
146
+ />
147
+ ) : (
148
+ <div
149
+ key={index}
150
+ {...api.getItemProps({ value: String(index), disabled: item.disabled })}
151
+ className={menuItemClass(item.danger)}
152
+ >
153
+ {item.icon && (
154
+ <Icon name={item.icon} size="sm" className="{{prefix}}-menu__item-icon" />
155
+ )}
156
+ <span className="flex-1">{item.label}</span>
157
+ {/* Named, so a screen reader hears the state that the check shows. */}
158
+ {item.selected && <Icon name="check" size="sm" label="Selected" />}
159
+ </div>
160
+ ),
161
+ )}
162
+ </div>
163
+ </div>
164
+ </TriggerContext>
165
+ </Portal>
166
+ </>
167
+ );
168
+ }
@@ -0,0 +1,34 @@
1
+ // Ejected from `@xds/react` 0.2.0 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 { classes, rootPart } from "./parts.ts";
6
+ import {
7
+ type ProgressBarBaseProps,
8
+ progressBarClass,
9
+ progressBarFillClass,
10
+ progressPercent,
11
+ } from "./progress-bar.ts";
12
+
13
+ export type ProgressBarProps = ProgressBarBaseProps & RootProps;
14
+
15
+ /**
16
+ * How far along something is, from 0 to 1. For work of unknown length use a Spinner.
17
+ * `label` says what is progressing; a screen reader reads it with the percentage.
18
+ */
19
+ export function ProgressBar({ value, label, className, style }: ProgressBarProps) {
20
+ const percent = progressPercent(value);
21
+ return (
22
+ <div
23
+ role="progressbar"
24
+ aria-label={label}
25
+ aria-valuemin={0}
26
+ aria-valuemax={100}
27
+ aria-valuenow={percent}
28
+ className={classes(rootPart("ProgressBar"), progressBarClass, className)}
29
+ style={style}
30
+ >
31
+ <div className={progressBarFillClass} style={{ width: `${percent}%` }} />
32
+ </div>
33
+ );
34
+ }
@@ -0,0 +1,112 @@
1
+ // Ejected from `@xds/react` 0.2.0 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;