@domicile-desktop/component-library 0.0.0-alpha-47cc305304ed → 0.0.0-alpha-df0645efcaa6

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.
package/README.md CHANGED
@@ -29,7 +29,7 @@ Each export is `@domicile-desktop/component-library/<name>`.
29
29
  | `Kbd` | Keyboard key cap. |
30
30
  | `list-walk` | Arrow-key movement of a highlight through a list while focus stays in an input. |
31
31
  | `ModalDialog` | Modal dialog with `title`, `footer` and `trigger` props. |
32
- | `ContextMenu` | Menu opened at a point, such as a right click's. Parts: `Item`, `CheckboxItem`, `RadioGroup` and `RadioItem`, `Submenu`, `Separator`. |
32
+ | `ContextMenu` | Menu opened at a point, such as a right click's. Parts: `Item`, `CheckboxItem`, `RadioGroup` and `RadioItem`, `Submenu`, `Separator`. Items take an `icon` and a `mnemonic`, whose letter chooses them. |
33
33
  | `PortalDialogs` | Every dialog applications ask for through the desktop portal. Mount one per shell with `host={domicile}`; it refuses kinds it cannot draw. `omitScreenCasts` leaves screen casts out of its indicator. See [PORTALS.md](/docs/PORTALS.md). |
34
34
  | `source-name` | `sourceName`: how a screen cast's source is named. |
35
35
  | `Popover` | Non-modal panel anchored to the control that opened it. |
package/package.json CHANGED
@@ -2,8 +2,8 @@
2
2
  "dependencies": {
3
3
  "@base-ui/react": "^1.8.0",
4
4
  "@cprussin/option-result": "^2.0.1",
5
- "@domicile-desktop/sdk": "0.0.0-alpha-47cc305304ed",
6
- "@domicile-desktop/system-apps": "0.0.0-alpha-47cc305304ed",
5
+ "@domicile-desktop/sdk": "0.0.0-alpha-df0645efcaa6",
6
+ "@domicile-desktop/system-apps": "0.0.0-alpha-df0645efcaa6",
7
7
  "@phosphor-icons/react": "^2.1.10"
8
8
  },
9
9
  "description": "React components and the Panda CSS design system that Domicile's shells are built from.",
@@ -104,5 +104,5 @@
104
104
  "test:unit": "bun test"
105
105
  },
106
106
  "type": "module",
107
- "version": "0.0.0-alpha-47cc305304ed"
107
+ "version": "0.0.0-alpha-df0645efcaa6"
108
108
  }
@@ -1,9 +1,10 @@
1
1
  import { ContextMenu as BaseContextMenu } from "@base-ui/react/context-menu";
2
2
  import { Menu as BaseMenu } from "@base-ui/react/menu";
3
+ import type { BaseUIEvent } from "@base-ui/react/types";
3
4
  import { CaretRightIcon } from "@phosphor-icons/react/dist/ssr/CaretRight";
4
5
  import { CheckIcon } from "@phosphor-icons/react/dist/ssr/Check";
5
6
  import { DotIcon } from "@phosphor-icons/react/dist/ssr/Dot";
6
- import type { ReactNode } from "react";
7
+ import type { KeyboardEvent, ReactNode } from "react";
7
8
  import { useMemo } from "react";
8
9
  import { css } from "../../styled-system/css";
9
10
  import type { ExtendProps } from "../extend-props";
@@ -29,6 +30,10 @@ type Props = ExtendProps<
29
30
  * outside press) through `onOpenChange`. The press may not be in this
30
31
  * document, as with a click in a `<webview>`. The menu takes the keyboard while
31
32
  * open.
33
+ *
34
+ * A letter chooses the first enabled item in the open menu or submenu whose
35
+ * mnemonic it is, and opens a submenu. Other letters go to base-ui's
36
+ * type-ahead, which highlights the next item whose label starts with them.
32
37
  */
33
38
  const ContextMenuComponent = ({ at, children, label, ...rootProps }: Props) => {
34
39
  // A zero-size box at the point, for the positioner to align the menu's
@@ -52,7 +57,11 @@ const ContextMenuComponent = ({ at, children, label, ...rootProps }: Props) => {
52
57
  side="bottom"
53
58
  sideOffset={0}
54
59
  >
55
- <BaseMenu.Popup aria-label={label} className={popupStyles}>
60
+ <BaseMenu.Popup
61
+ aria-label={label}
62
+ className={popupStyles}
63
+ onKeyDown={chooseByMnemonic}
64
+ >
56
65
  {children}
57
66
  </BaseMenu.Popup>
58
67
  </BaseMenu.Positioner>
@@ -61,34 +70,72 @@ const ContextMenuComponent = ({ at, children, label, ...rootProps }: Props) => {
61
70
  );
62
71
  };
63
72
 
73
+ /**
74
+ * An item's label and the icon before it. In a menu where any item has an
75
+ * icon, every label leaves room for one, so labels line up.
76
+ */
77
+ type Content = {
78
+ /** Drawn before the label, at the size of a line of text. */
79
+ icon?: ReactNode | undefined;
80
+ } & (
81
+ | { children: ReactNode; mnemonic?: undefined }
82
+ | {
83
+ children: string;
84
+ /**
85
+ * Where in the label the access key is. It is underlined, and its key
86
+ * chooses the item.
87
+ */
88
+ mnemonic?: number | undefined;
89
+ }
90
+ );
91
+
64
92
  type ItemProps = ExtendProps<
65
93
  typeof BaseMenu.Item,
66
- {
67
- children: ReactNode;
94
+ Content & {
68
95
  /** The keys that do the same, shown at the item's end. */
69
96
  shortcut?: string | undefined;
70
97
  }
71
98
  >;
72
99
 
73
100
  /** One thing the menu can do. Closes the menu when chosen. */
74
- const Item = ({ children, shortcut, ...itemProps }: ItemProps) => (
75
- <BaseMenu.Item className={itemStyles} {...itemProps}>
76
- <span className={labelStyles}>{children}</span>
101
+ const Item = ({
102
+ children,
103
+ icon,
104
+ mnemonic,
105
+ shortcut,
106
+ ...itemProps
107
+ }: ItemProps) => (
108
+ <BaseMenu.Item
109
+ className={itemStyles}
110
+ data-mnemonic={mnemonicKey(children, mnemonic)}
111
+ {...itemProps}
112
+ >
113
+ <Label icon={icon} mnemonic={mnemonic}>
114
+ {children}
115
+ </Label>
77
116
  {shortcut !== undefined && (
78
117
  <span className={shortcutStyles}>{shortcut}</span>
79
118
  )}
80
119
  </BaseMenu.Item>
81
120
  );
82
121
 
83
- type CheckboxItemProps = ExtendProps<
84
- typeof BaseMenu.CheckboxItem,
85
- { children: ReactNode }
86
- >;
122
+ type CheckboxItemProps = ExtendProps<typeof BaseMenu.CheckboxItem, Content>;
87
123
 
88
124
  /** An item with a check mark that shows whether it is on. */
89
- const CheckboxItem = ({ children, ...itemProps }: CheckboxItemProps) => (
90
- <BaseMenu.CheckboxItem className={itemStyles} {...itemProps}>
91
- <span className={labelStyles}>{children}</span>
125
+ const CheckboxItem = ({
126
+ children,
127
+ icon,
128
+ mnemonic,
129
+ ...itemProps
130
+ }: CheckboxItemProps) => (
131
+ <BaseMenu.CheckboxItem
132
+ className={itemStyles}
133
+ data-mnemonic={mnemonicKey(children, mnemonic)}
134
+ {...itemProps}
135
+ >
136
+ <Label icon={icon} mnemonic={mnemonic}>
137
+ {children}
138
+ </Label>
92
139
  <BaseMenu.CheckboxItemIndicator className={indicatorStyles}>
93
140
  <CheckIcon />
94
141
  </BaseMenu.CheckboxItemIndicator>
@@ -98,15 +145,23 @@ const CheckboxItem = ({ children, ...itemProps }: CheckboxItemProps) => (
98
145
  /** Radio items, of which the one whose `value` is the group's is chosen. */
99
146
  const RadioGroup = BaseMenu.RadioGroup;
100
147
 
101
- type RadioItemProps = ExtendProps<
102
- typeof BaseMenu.RadioItem,
103
- { children: ReactNode }
104
- >;
148
+ type RadioItemProps = ExtendProps<typeof BaseMenu.RadioItem, Content>;
105
149
 
106
150
  /** One choice in a `RadioGroup`, with a dot when chosen. */
107
- const RadioItem = ({ children, ...itemProps }: RadioItemProps) => (
108
- <BaseMenu.RadioItem className={itemStyles} {...itemProps}>
109
- <span className={labelStyles}>{children}</span>
151
+ const RadioItem = ({
152
+ children,
153
+ icon,
154
+ mnemonic,
155
+ ...itemProps
156
+ }: RadioItemProps) => (
157
+ <BaseMenu.RadioItem
158
+ className={itemStyles}
159
+ data-mnemonic={mnemonicKey(children, mnemonic)}
160
+ {...itemProps}
161
+ >
162
+ <Label icon={icon} mnemonic={mnemonic}>
163
+ {children}
164
+ </Label>
110
165
  <BaseMenu.RadioItemIndicator className={indicatorStyles}>
111
166
  <DotIcon weight="bold" />
112
167
  </BaseMenu.RadioItemIndicator>
@@ -118,21 +173,42 @@ type SubmenuProps = ExtendProps<
118
173
  {
119
174
  children: ReactNode;
120
175
  disabled?: boolean | undefined;
176
+ /** Drawn before the label, as on {@link Item}. */
177
+ icon?: ReactNode | undefined;
121
178
  /** The text of the item that opens it, and the submenu's name. */
122
179
  label: string;
180
+ /** Where in `label` the access key is, as on {@link Item}. */
181
+ mnemonic?: number | undefined;
123
182
  }
124
183
  >;
125
184
 
126
185
  /** An item that opens a menu of `children` beside it. */
127
- const Submenu = ({ children, disabled, label, ...rootProps }: SubmenuProps) => (
186
+ const Submenu = ({
187
+ children,
188
+ disabled,
189
+ icon,
190
+ label,
191
+ mnemonic,
192
+ ...rootProps
193
+ }: SubmenuProps) => (
128
194
  <BaseMenu.SubmenuRoot {...rootProps}>
129
- <BaseMenu.SubmenuTrigger className={itemStyles} disabled={disabled}>
130
- <span className={labelStyles}>{label}</span>
195
+ <BaseMenu.SubmenuTrigger
196
+ className={itemStyles}
197
+ data-mnemonic={mnemonicKey(label, mnemonic)}
198
+ disabled={disabled}
199
+ >
200
+ <Label icon={icon} mnemonic={mnemonic}>
201
+ {label}
202
+ </Label>
131
203
  <CaretRightIcon className={caretStyles} />
132
204
  </BaseMenu.SubmenuTrigger>
133
205
  <BaseMenu.Portal>
134
206
  <BaseMenu.Positioner className={positionerStyles}>
135
- <BaseMenu.Popup aria-label={label} className={popupStyles}>
207
+ <BaseMenu.Popup
208
+ aria-label={label}
209
+ className={popupStyles}
210
+ onKeyDown={chooseByMnemonic}
211
+ >
136
212
  {children}
137
213
  </BaseMenu.Popup>
138
214
  </BaseMenu.Positioner>
@@ -160,6 +236,9 @@ const positionerStyles = css({
160
236
  // `&[data-starting-style]` rather than Panda's `_starting`: `@starting-style`
161
237
  // does not reliably fire for a portaled popup. See `Popover`.
162
238
  const popupStyles = css({
239
+ "&:has([data-item-icon]:not(:empty)) [data-item-icon]": {
240
+ display: "inline-flex",
241
+ },
163
242
  "&[data-ending-style]": {
164
243
  opacity: 0,
165
244
  transition: "opacity {durations.fast} {easings.in}",
@@ -206,11 +285,31 @@ const itemStyles = css({
206
285
  });
207
286
 
208
287
  const labelStyles = css({
288
+ alignItems: "center",
289
+ display: "flex",
290
+ gap: 2,
291
+ minInlineSize: 0,
292
+ });
293
+
294
+ // Hidden unless the popup has an icon; see `popupStyles`.
295
+ const iconStyles = css({
296
+ "& > *": { blockSize: "100%", inlineSize: "100%" },
297
+ alignItems: "center",
298
+ blockSize: 4,
299
+ display: "none",
300
+ flexShrink: 0,
301
+ inlineSize: 4,
302
+ justifyContent: "center",
303
+ });
304
+
305
+ const textStyles = css({
209
306
  overflow: "hidden",
210
307
  textOverflow: "ellipsis",
211
308
  whiteSpace: "nowrap",
212
309
  });
213
310
 
311
+ const mnemonicStyles = css({ textDecoration: "underline" });
312
+
214
313
  const shortcutStyles = css({
215
314
  color: "muted",
216
315
  fontSize: "xs",
@@ -231,3 +330,79 @@ const separatorStyles = css({
231
330
  blockSize: "1px",
232
331
  marginBlock: 1,
233
332
  });
333
+
334
+ type LabelProps = {
335
+ children: ReactNode;
336
+ icon: ReactNode;
337
+ mnemonic: number | undefined;
338
+ };
339
+
340
+ /**
341
+ * An item's icon slot, then its label with the mnemonic underlined. `Content`
342
+ * allows a mnemonic only on a text label.
343
+ */
344
+ const Label = ({ children, icon, mnemonic }: LabelProps) => (
345
+ <span className={labelStyles}>
346
+ <span className={iconStyles} data-item-icon="">
347
+ {icon}
348
+ </span>
349
+ <span className={textStyles}>
350
+ {typeof children === "string" && mnemonic !== undefined ? (
351
+ <>
352
+ {children.slice(0, mnemonic)}
353
+ <span className={mnemonicStyles}>{children.charAt(mnemonic)}</span>
354
+ {children.slice(mnemonic + 1)}
355
+ </>
356
+ ) : (
357
+ children
358
+ )}
359
+ </span>
360
+ </span>
361
+ );
362
+
363
+ /** The key, in lower case, that chooses an item labeled `children`. */
364
+ const mnemonicKey = (
365
+ children: ReactNode,
366
+ mnemonic: number | undefined,
367
+ ): string | undefined =>
368
+ typeof children === "string" && mnemonic !== undefined
369
+ ? children.charAt(mnemonic).toLocaleLowerCase()
370
+ : undefined;
371
+
372
+ /**
373
+ * Chooses the first enabled item in this popup whose mnemonic was pressed,
374
+ * instead of base-ui's type-ahead. A key from a submenu bubbles here through
375
+ * React's tree; that submenu's popup is not inside this one, so it is skipped.
376
+ */
377
+ const chooseByMnemonic = (
378
+ event: BaseUIEvent<KeyboardEvent<HTMLDivElement>>,
379
+ ) => {
380
+ const item = mnemonicItem(event);
381
+ if (item !== undefined) {
382
+ event.preventBaseUIHandler();
383
+ event.preventDefault();
384
+ item.click();
385
+ }
386
+ };
387
+
388
+ /** The first enabled item in `event`'s popup whose mnemonic it pressed. */
389
+ const mnemonicItem = (
390
+ event: KeyboardEvent<HTMLDivElement>,
391
+ ): HTMLElement | undefined => {
392
+ const popup = event.currentTarget;
393
+ if (
394
+ event.ctrlKey ||
395
+ event.metaKey ||
396
+ event.altKey ||
397
+ !(event.target instanceof Node) ||
398
+ !popup.contains(event.target)
399
+ ) {
400
+ return undefined;
401
+ } else {
402
+ return [...popup.querySelectorAll<HTMLElement>("[data-mnemonic]")].find(
403
+ (candidate) =>
404
+ candidate.dataset.mnemonic === event.key.toLocaleLowerCase() &&
405
+ !candidate.hasAttribute("data-disabled"),
406
+ );
407
+ }
408
+ };