@oaiz/app-kit 0.1.4246 → 0.1.4254

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oaiz/app-kit",
3
- "version": "0.1.4246",
3
+ "version": "0.1.4254",
4
4
  "description": "Source components, hooks and theme for apps built on OAIZ, to copy and change",
5
5
  "keywords": [
6
6
  "oaiz",
@@ -63,7 +63,7 @@ export type ListViewProps<Row> = {
63
63
  onFocusedChange?: (row: Row | null) => void;
64
64
  /** Adds a row to a group; each group header shows a + button. */
65
65
  onAddToGroup?: (groupKey: string) => void;
66
- /** Menu items for a right-click on a row, such as `DropdownMenuItem`s. */
66
+ /** Menu items for a right-click or Shift+F10 on a row, such as `DropdownMenuItem`s. */
67
67
  rowMenu?: (row: Row) => ReactNode;
68
68
  /** Shown when no group has rows. */
69
69
  empty: ReactNode;
@@ -88,7 +88,7 @@ function typingOrOverlay(target: EventTarget | null): boolean {
88
88
 
89
89
  /**
90
90
  * A dense, grouped list in the style of Linear. J and K (or the arrow keys) move the focus, X
91
- * selects the focused row, Enter opens it, and a right-click opens `rowMenu`. Group headers
91
+ * selects the focused row, Enter opens it, and a right-click or Shift+F10 opens `rowMenu`. Group headers
92
92
  * collapse and show their count.
93
93
  */
94
94
  export function ListView<Row>({
@@ -13,6 +13,11 @@ import {
13
13
  const LONG_PRESS_MS = 500;
14
14
  /** A touch that drifts further than this is a scroll or a drag, not a press. */
15
15
  const LONG_PRESS_SLOP_PX = 10;
16
+ /**
17
+ * Windows and Linux browsers follow a Shift+F10 keydown with their own
18
+ * contextmenu event; one arriving this soon after belongs to that keypress.
19
+ */
20
+ const KEYBOARD_CONTEXTMENU_WINDOW_MS = 1000;
16
21
 
17
22
  interface ContextMenuContextValue {
18
23
  openAt: (x: number, y: number) => void;
@@ -128,6 +133,7 @@ const ContextMenuTrigger = React.forwardRef<
128
133
  onPointerUp,
129
134
  onPointerCancel,
130
135
  onClickCapture,
136
+ onKeyDown,
131
137
  ...props
132
138
  },
133
139
  ref,
@@ -139,6 +145,7 @@ const ContextMenuTrigger = React.forwardRef<
139
145
  timer: ReturnType<typeof setTimeout>;
140
146
  } | null>(null);
141
147
  const openedByPress = React.useRef(false);
148
+ const openedByKeyAt = React.useRef<number | null>(null);
142
149
 
143
150
  const cancelPress = () => {
144
151
  if (press.current) clearTimeout(press.current.timer);
@@ -156,6 +163,13 @@ const ContextMenuTrigger = React.forwardRef<
156
163
  if (disabled || event.defaultPrevented || !isInside(event)) return;
157
164
  event.preventDefault();
158
165
  cancelPress();
166
+ const keyOpenedAt = openedByKeyAt.current;
167
+ openedByKeyAt.current = null;
168
+ if (
169
+ keyOpenedAt !== null &&
170
+ event.timeStamp - keyOpenedAt < KEYBOARD_CONTEXTMENU_WINDOW_MS
171
+ )
172
+ return;
159
173
  // The Menu key and Shift+F10 report no pointer position.
160
174
  if (event.clientX === 0 && event.clientY === 0) {
161
175
  const rect = event.currentTarget.getBoundingClientRect();
@@ -164,6 +178,28 @@ const ContextMenuTrigger = React.forwardRef<
164
178
  openAt(event.clientX, event.clientY);
165
179
  }
166
180
  }}
181
+ // macOS browsers send no contextmenu event for Shift+F10, so the
182
+ // standard keyboard shortcut opens the menu here on every platform.
183
+ onKeyDown={(event: React.KeyboardEvent<HTMLElement>) => {
184
+ onKeyDown?.(event);
185
+ if (
186
+ disabled ||
187
+ event.defaultPrevented ||
188
+ event.key !== 'F10' ||
189
+ !event.shiftKey ||
190
+ !isInside(event)
191
+ )
192
+ return;
193
+ event.preventDefault();
194
+ cancelPress();
195
+ const target =
196
+ event.target instanceof HTMLElement
197
+ ? event.target
198
+ : event.currentTarget;
199
+ const rect = target.getBoundingClientRect();
200
+ openedByKeyAt.current = event.timeStamp;
201
+ openAt(rect.left + 8, rect.top + 8);
202
+ }}
167
203
  onPointerDown={(event: React.PointerEvent<HTMLElement>) => {
168
204
  onPointerDown?.(event);
169
205
  openedByPress.current = false;
@@ -72,6 +72,7 @@ export function RadioGroup({
72
72
  disabled={disabled === true || option.disabled === true}
73
73
  required={required}
74
74
  aria-labelledby={`${id}-${index}-label`}
75
+ // oxlint-disable-next-line jsx-a11y/role-supports-aria-props -- ARIA 1.2 makes aria-invalid global, and screen readers announce it on native radios.
75
76
  aria-invalid={error ? true : undefined}
76
77
  aria-describedby={
77
78
  [
@@ -80,9 +80,9 @@ const ToastClose = React.forwardRef<
80
80
  className,
81
81
  )}
82
82
  toast-close=""
83
- // The icon is decorative, so the button needs its own name.
84
- aria-label="Dismiss"
85
83
  {...props}
84
+ // The icon is decorative, so the button needs its own name.
85
+ aria-label={props['aria-label'] ?? 'Dismiss'}
86
86
  >
87
87
  <X className="h-4 w-4" />
88
88
  </ToastPrimitives.Close>
@@ -34,8 +34,19 @@ function ToastText({
34
34
  );
35
35
  }
36
36
 
37
- export function Toaster() {
37
+ export interface ToasterLabels {
38
+ /** Name of each toast's close button. */
39
+ close: string;
40
+ /** Announced with each toast, such as "Notification". */
41
+ toast: string;
42
+ /** Name of the toast region; `{hotkey}` is replaced with its shortcut (F8). */
43
+ viewport: string;
44
+ }
45
+
46
+ /** Pass translated `labels`; without them the labels are English. */
47
+ export function Toaster({ labels }: { labels?: ToasterLabels } = {}) {
38
48
  const { toasts, dismiss } = useToast();
49
+ const closeLabel = labels?.close;
39
50
 
40
51
  // Cmd+Z or Ctrl+Z undoes the newest open toast that offers Undo. Text
41
52
  // fields keep their own undo.
@@ -62,7 +73,7 @@ export function Toaster() {
62
73
  }, [dismiss, undoable]);
63
74
 
64
75
  return (
65
- <ToastProvider>
76
+ <ToastProvider label={labels?.toast}>
66
77
  {toasts.map(function ({
67
78
  id,
68
79
  title,
@@ -94,7 +105,10 @@ export function Toaster() {
94
105
  </div>
95
106
  ) : null}
96
107
  </div>
97
- <ToastClose className="top-1/2 -translate-y-1/2" />
108
+ <ToastClose
109
+ className="top-1/2 -translate-y-1/2"
110
+ aria-label={closeLabel}
111
+ />
98
112
  </Toast>
99
113
  );
100
114
  }
@@ -108,11 +122,11 @@ export function Toaster() {
108
122
  >
109
123
  <ToastText title={title} description={description} />
110
124
  {action}
111
- <ToastClose />
125
+ <ToastClose aria-label={closeLabel} />
112
126
  </Toast>
113
127
  );
114
128
  })}
115
- <ToastViewport />
129
+ <ToastViewport label={labels?.viewport} />
116
130
  </ToastProvider>
117
131
  );
118
132
  }
@@ -24,6 +24,11 @@ export interface TreeViewProps {
24
24
  onSelect?: (node: TreeNode) => void;
25
25
  /** The ids of nodes open at first. */
26
26
  defaultExpanded?: string[];
27
+ /** The ids of open nodes, when the caller owns them, such as to reveal a node. */
28
+ expanded?: string[];
29
+ onExpandedChange?: (expanded: string[]) => void;
30
+ /** Nodes with children open and close instead of being chosen, as in a file tree. */
31
+ selectLeavesOnly?: boolean;
27
32
  className?: string;
28
33
  }
29
34
 
@@ -63,11 +68,17 @@ function TreeView({
63
68
  selected = null,
64
69
  onSelect,
65
70
  defaultExpanded = [],
71
+ expanded: controlledExpanded,
72
+ onExpandedChange,
73
+ selectLeavesOnly = false,
66
74
  className,
67
75
  }: TreeViewProps) {
68
- const [expanded, setExpanded] = React.useState(
76
+ const [ownExpanded, setOwnExpanded] = React.useState(
69
77
  () => new Set(defaultExpanded),
70
78
  );
79
+ const expanded = controlledExpanded
80
+ ? new Set(controlledExpanded)
81
+ : ownExpanded;
71
82
  const [focused, setFocused] = React.useState<string | null>(null);
72
83
  const refs = React.useRef(new Map<string, HTMLDivElement>());
73
84
  const rows = visibleRows(nodes, expanded);
@@ -78,13 +89,20 @@ function TreeView({
78
89
  setFocused(id);
79
90
  refs.current.get(id)?.focus();
80
91
  };
81
- const toggle = (id: string, open: boolean) =>
82
- setExpanded((now) => {
83
- const next = new Set(now);
84
- if (open) next.add(id);
85
- else next.delete(id);
86
- return next;
87
- });
92
+ const toggle = (id: string, open: boolean) => {
93
+ const next = new Set(expanded);
94
+ if (open) next.add(id);
95
+ else next.delete(id);
96
+ if (!controlledExpanded) setOwnExpanded(next);
97
+ onExpandedChange?.([...next]);
98
+ };
99
+
100
+ const choosable = (node: TreeNode) =>
101
+ onSelect !== undefined && !(selectLeavesOnly && node.children?.length);
102
+ const activate = (row: Row) => {
103
+ if (choosable(row.node)) onSelect?.(row.node);
104
+ else if (row.node.children?.length) toggle(row.node.id, !row.open);
105
+ };
88
106
 
89
107
  function onKeyDown(event: React.KeyboardEvent, row: Row, index: number) {
90
108
  const hasChildren = Boolean(row.node.children?.length);
@@ -111,8 +129,7 @@ function TreeView({
111
129
  break;
112
130
  case 'Enter':
113
131
  case ' ':
114
- onSelect?.(row.node);
115
- if (hasChildren && !onSelect) toggle(row.node.id, !row.open);
132
+ activate(row);
116
133
  break;
117
134
  default:
118
135
  if (event.key.length === 1 && /\S/.test(event.key)) {
@@ -149,15 +166,14 @@ function TreeView({
149
166
  role="treeitem"
150
167
  aria-level={row.level}
151
168
  aria-expanded={hasChildren ? row.open : undefined}
152
- aria-selected={onSelect ? isSelected : undefined}
169
+ aria-selected={choosable(row.node) ? isSelected : undefined}
153
170
  aria-label={textOf(row.node)}
154
171
  tabIndex={row.node.id === current ? 0 : -1}
155
172
  onFocus={() => setFocused(row.node.id)}
156
173
  onKeyDown={(event) => onKeyDown(event, row, index)}
157
174
  onClick={() => {
158
175
  setFocused(row.node.id);
159
- if (onSelect) onSelect(row.node);
160
- else if (hasChildren) toggle(row.node.id, !row.open);
176
+ activate(row);
161
177
  }}
162
178
  // Each level indents by one stack step from the inline gutter.
163
179
  style={{