@oaiz/app-kit 0.1.4199 → 0.1.4233

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.4199",
3
+ "version": "0.1.4233",
4
4
  "description": "Source components, hooks and theme for apps built on OAIZ, to copy and change",
5
5
  "keywords": [
6
6
  "oaiz",
@@ -105,6 +105,7 @@ const DropdownMenuContent = React.forwardRef<
105
105
  sideOffset = 4,
106
106
  onOpenAutoFocus,
107
107
  onPointerDownCapture,
108
+ onPointerUpCapture,
108
109
  onClickCapture,
109
110
  onKeyDownCapture,
110
111
  onEscapeKeyDown,
@@ -116,6 +117,7 @@ const DropdownMenuContent = React.forwardRef<
116
117
  ) => {
117
118
  const pointerCloseFocus = usePointerCloseFocus({
118
119
  onPointerDownCapture,
120
+ onPointerUpCapture,
119
121
  onClickCapture,
120
122
  onKeyDownCapture,
121
123
  onEscapeKeyDown,
@@ -19,6 +19,7 @@ const PopoverContent = React.forwardRef<
19
19
  align = 'center',
20
20
  sideOffset = 4,
21
21
  onPointerDownCapture,
22
+ onPointerUpCapture,
22
23
  onClickCapture,
23
24
  onKeyDownCapture,
24
25
  onEscapeKeyDown,
@@ -30,6 +31,7 @@ const PopoverContent = React.forwardRef<
30
31
  ) => {
31
32
  const pointerCloseFocus = usePointerCloseFocus({
32
33
  onPointerDownCapture,
34
+ onPointerUpCapture,
33
35
  onClickCapture,
34
36
  onKeyDownCapture,
35
37
  onEscapeKeyDown,
@@ -8,7 +8,32 @@ import { cspNonce } from '../../lib/csp-nonce';
8
8
  import { cn, usePointerCloseFocus } from '../../lib/utils';
9
9
  import { Text } from './text';
10
10
 
11
- const Select = SelectPrimitive.Root;
11
+ const SelectInputContext = React.createContext<{
12
+ pointerInput: boolean;
13
+ setPointerInput: (pointer: boolean) => void;
14
+ } | null>(null);
15
+
16
+ function useSelectInput() {
17
+ const input = React.useContext(SelectInputContext);
18
+ if (!input) throw new Error('Select parts must be rendered inside <Select>');
19
+ return input;
20
+ }
21
+
22
+ function Select(props: React.ComponentProps<typeof SelectPrimitive.Root>) {
23
+ const [pointerInput, setPointerInput] = React.useState(false);
24
+ const input = React.useMemo(
25
+ () => ({ pointerInput, setPointerInput }),
26
+ [pointerInput],
27
+ );
28
+
29
+ return (
30
+ <SelectInputContext.Provider value={input}>
31
+ <SelectPrimitive.Root {...props} />
32
+ </SelectInputContext.Provider>
33
+ );
34
+ }
35
+
36
+ Select.displayName = 'Select';
12
37
 
13
38
  const SelectGroup = SelectPrimitive.Group;
14
39
 
@@ -44,18 +69,35 @@ export interface SelectTriggerProps
44
69
  const SelectTrigger = React.forwardRef<
45
70
  React.ElementRef<typeof SelectPrimitive.Trigger>,
46
71
  SelectTriggerProps
47
- >(({ className, variant, size, children, ...props }, ref) => (
48
- <SelectPrimitive.Trigger
49
- ref={ref}
50
- className={cn(selectTriggerVariants({ variant, size, className }))}
51
- {...props}
52
- >
53
- {children}
54
- <SelectPrimitive.Icon asChild>
55
- <ChevronDown className="h-4 w-4 opacity-50" />
56
- </SelectPrimitive.Icon>
57
- </SelectPrimitive.Trigger>
58
- ));
72
+ >(
73
+ (
74
+ { className, variant, size, children, onPointerDown, onKeyDown, ...props },
75
+ ref,
76
+ ) => {
77
+ const input = useSelectInput();
78
+ return (
79
+ <SelectPrimitive.Trigger
80
+ ref={ref}
81
+ className={cn(selectTriggerVariants({ variant, size, className }))}
82
+ {...props}
83
+ onPointerDown={(event) => {
84
+ onPointerDown?.(event);
85
+ if (!event.defaultPrevented && event.button === 0 && !event.ctrlKey)
86
+ input.setPointerInput(true);
87
+ }}
88
+ onKeyDown={(event) => {
89
+ onKeyDown?.(event);
90
+ input.setPointerInput(false);
91
+ }}
92
+ >
93
+ {children}
94
+ <SelectPrimitive.Icon asChild>
95
+ <ChevronDown className="h-4 w-4 opacity-50" />
96
+ </SelectPrimitive.Icon>
97
+ </SelectPrimitive.Trigger>
98
+ );
99
+ },
100
+ );
59
101
  SelectTrigger.displayName = SelectPrimitive.Trigger.displayName;
60
102
 
61
103
  const SelectScrollUpButton = React.forwardRef<
@@ -102,7 +144,9 @@ const SelectContent = React.forwardRef<
102
144
  className,
103
145
  children,
104
146
  position = 'popper',
147
+ onPointerMove,
105
148
  onPointerDownCapture,
149
+ onPointerUpCapture,
106
150
  onClickCapture,
107
151
  onKeyDownCapture,
108
152
  onEscapeKeyDown,
@@ -112,13 +156,18 @@ const SelectContent = React.forwardRef<
112
156
  },
113
157
  ref,
114
158
  ) => {
159
+ const input = useSelectInput();
115
160
  const pointerCloseFocus = usePointerCloseFocus({
116
161
  onPointerDownCapture,
162
+ onPointerUpCapture,
117
163
  onClickCapture,
118
164
  onKeyDownCapture,
119
165
  onEscapeKeyDown,
120
166
  onPointerDownOutside,
121
- onCloseAutoFocus,
167
+ onCloseAutoFocus: (event) => {
168
+ input.setPointerInput(false);
169
+ onCloseAutoFocus?.(event);
170
+ },
122
171
  });
123
172
 
124
173
  return (
@@ -132,8 +181,16 @@ const SelectContent = React.forwardRef<
132
181
  className,
133
182
  )}
134
183
  position={position}
184
+ onPointerMove={(event) => {
185
+ input.setPointerInput(true);
186
+ onPointerMove?.(event);
187
+ }}
135
188
  {...pointerCloseFocus}
136
189
  {...props}
190
+ onKeyDownCapture={(event) => {
191
+ input.setPointerInput(false);
192
+ pointerCloseFocus.onKeyDownCapture(event);
193
+ }}
137
194
  >
138
195
  <SelectScrollUpButton />
139
196
  <SelectPrimitive.Viewport
@@ -178,29 +235,37 @@ export interface SelectItemProps extends React.ComponentPropsWithoutRef<
178
235
  const SelectItem = React.forwardRef<
179
236
  React.ElementRef<typeof SelectPrimitive.Item>,
180
237
  SelectItemProps
181
- >(({ className, children, selectionIndicator = 'check', ...props }, ref) => (
182
- <SelectPrimitive.Item
183
- ref={ref}
184
- className={cn(
185
- 'relative flex w-full cursor-default select-none items-center rounded-sm py-1.5 text-sm outline-none focus:bg-accent focus:text-foreground data-[disabled]:pointer-events-none data-[disabled]:opacity-50',
186
- selectionIndicator === 'check'
187
- ? 'pl-section pr-inline'
188
- : 'px-inset data-[state=checked]:bg-primary data-[state=checked]:text-primary-foreground focus:ring-2 focus:ring-inset focus:ring-ring data-[state=checked]:focus:ring-primary-foreground',
189
- className,
190
- )}
191
- {...props}
192
- >
193
- {selectionIndicator === 'check' && (
194
- <span className="absolute left-inline flex h-3.5 w-3.5 items-center justify-center">
195
- <SelectPrimitive.ItemIndicator>
196
- <Check className="h-4 w-4" />
197
- </SelectPrimitive.ItemIndicator>
198
- </span>
199
- )}
238
+ >(({ className, children, selectionIndicator = 'check', ...props }, ref) => {
239
+ const input = useSelectInput();
240
+ return (
241
+ <SelectPrimitive.Item
242
+ ref={ref}
243
+ className={cn(
244
+ 'relative flex w-full cursor-default select-none items-center rounded-sm py-1.5 text-sm outline-none data-[disabled]:pointer-events-none data-[disabled]:opacity-50',
245
+ selectionIndicator === 'check'
246
+ ? 'pl-section pr-inline focus:bg-accent focus:text-foreground'
247
+ : [
248
+ 'px-inset data-[state=checked]:bg-primary data-[state=checked]:text-primary-foreground',
249
+ input.pointerInput
250
+ ? 'data-[state=unchecked]:hover:bg-accent data-[state=unchecked]:hover:text-foreground forced-colors:hover:outline-solid forced-colors:hover:outline-2 forced-colors:hover:-outline-offset-2 forced-colors:hover:outline-[Highlight]'
251
+ : 'data-[state=unchecked]:focus:bg-accent data-[state=unchecked]:focus:text-foreground forced-colors:focus:outline-solid forced-colors:focus:outline-2 forced-colors:focus:-outline-offset-2 forced-colors:focus:outline-[Highlight] focus:ring-2 focus:ring-inset focus:ring-ring data-[state=checked]:focus:ring-primary-foreground',
252
+ ],
253
+ className,
254
+ )}
255
+ {...props}
256
+ >
257
+ {selectionIndicator === 'check' && (
258
+ <span className="absolute left-inline flex h-3.5 w-3.5 items-center justify-center">
259
+ <SelectPrimitive.ItemIndicator>
260
+ <Check className="h-4 w-4" />
261
+ </SelectPrimitive.ItemIndicator>
262
+ </span>
263
+ )}
200
264
 
201
- <SelectPrimitive.ItemText>{children}</SelectPrimitive.ItemText>
202
- </SelectPrimitive.Item>
203
- ));
265
+ <SelectPrimitive.ItemText>{children}</SelectPrimitive.ItemText>
266
+ </SelectPrimitive.Item>
267
+ );
268
+ });
204
269
  SelectItem.displayName = SelectPrimitive.Item.displayName;
205
270
 
206
271
  const SelectSeparator = React.forwardRef<
@@ -1,3 +1,4 @@
1
+ import * as React from 'react';
1
2
  import { useToast } from './use-toast';
2
3
  import {
3
4
  Toast,
@@ -8,8 +9,57 @@ import {
8
9
  ToastViewport,
9
10
  } from './toast';
10
11
 
12
+ function isTypingTarget(target: EventTarget | null) {
13
+ return (
14
+ target instanceof HTMLElement &&
15
+ (target.isContentEditable ||
16
+ target.closest('input, textarea, select') !== null)
17
+ );
18
+ }
19
+
20
+ const UNDO_KEYSHORTCUTS = 'Meta+Z Control+Z';
21
+
22
+ function ToastText({
23
+ title,
24
+ description,
25
+ }: {
26
+ title?: React.ReactNode;
27
+ description?: React.ReactNode;
28
+ }) {
29
+ return (
30
+ <div className="grid gap-1">
31
+ {title && <ToastTitle>{title}</ToastTitle>}
32
+ {description && <ToastDescription>{description}</ToastDescription>}
33
+ </div>
34
+ );
35
+ }
36
+
11
37
  export function Toaster() {
12
- const { toasts } = useToast();
38
+ const { toasts, dismiss } = useToast();
39
+
40
+ // Cmd+Z or Ctrl+Z undoes the newest open toast that offers Undo. Text
41
+ // fields keep their own undo.
42
+ const undoable = toasts.find((toast) => toast.open && toast.onUndo);
43
+ React.useEffect(() => {
44
+ const onUndo = undoable?.onUndo;
45
+ if (!undoable || !onUndo) return;
46
+ const onKeyDown = (event: KeyboardEvent) => {
47
+ if (
48
+ event.defaultPrevented ||
49
+ event.key.toLowerCase() !== 'z' ||
50
+ !(event.metaKey || event.ctrlKey) ||
51
+ event.shiftKey ||
52
+ event.altKey ||
53
+ isTypingTarget(event.target)
54
+ )
55
+ return;
56
+ event.preventDefault();
57
+ dismiss(undoable.id);
58
+ onUndo();
59
+ };
60
+ window.addEventListener('keydown', onKeyDown);
61
+ return () => window.removeEventListener('keydown', onKeyDown);
62
+ }, [dismiss, undoable]);
13
63
 
14
64
  return (
15
65
  <ToastProvider>
@@ -18,17 +68,45 @@ export function Toaster() {
18
68
  title,
19
69
  description,
20
70
  action,
71
+ layout = 'inline',
72
+ onUndo,
21
73
  testId,
22
74
  ...props
23
75
  }) {
76
+ const keyshortcuts = onUndo ? UNDO_KEYSHORTCUTS : undefined;
77
+
78
+ if (layout === 'stacked') {
79
+ return (
80
+ <Toast
81
+ key={id}
82
+ data-testid={testId}
83
+ aria-keyshortcuts={keyshortcuts}
84
+ {...props}
85
+ >
86
+ <div className="flex min-w-0 flex-col gap-2">
87
+ <ToastText title={title} description={description} />
88
+ {action ? (
89
+ <div
90
+ className="flex flex-wrap items-center gap-inline"
91
+ data-testid="toast-actions"
92
+ >
93
+ {action}
94
+ </div>
95
+ ) : null}
96
+ </div>
97
+ <ToastClose className="top-1/2 -translate-y-1/2" />
98
+ </Toast>
99
+ );
100
+ }
101
+
24
102
  return (
25
- <Toast key={id} data-testid={testId} {...props}>
26
- <div className="grid gap-1">
27
- {title && <ToastTitle>{title}</ToastTitle>}
28
- {description && (
29
- <ToastDescription>{description}</ToastDescription>
30
- )}
31
- </div>
103
+ <Toast
104
+ key={id}
105
+ data-testid={testId}
106
+ aria-keyshortcuts={keyshortcuts}
107
+ {...props}
108
+ >
109
+ <ToastText title={title} description={description} />
32
110
  {action}
33
111
  <ToastClose />
34
112
  </Toast>
@@ -8,7 +8,23 @@ const TooltipProvider = TooltipPrimitive.Provider;
8
8
 
9
9
  const Tooltip = TooltipPrimitive.Root;
10
10
 
11
- const TooltipTrigger = TooltipPrimitive.Trigger;
11
+ const TooltipTrigger = React.forwardRef<
12
+ React.ElementRef<typeof TooltipPrimitive.Trigger>,
13
+ React.ComponentPropsWithoutRef<typeof TooltipPrimitive.Trigger>
14
+ >(({ onFocus, ...props }, ref) => (
15
+ <TooltipPrimitive.Trigger
16
+ {...props}
17
+ ref={ref}
18
+ onFocus={(event) => {
19
+ onFocus?.(event);
20
+ // Radix skips its open handler when the composed handler prevents the event.
21
+ if (event.currentTarget.hasAttribute('data-pointer-close-focus')) {
22
+ event.preventDefault();
23
+ }
24
+ }}
25
+ />
26
+ ));
27
+ TooltipTrigger.displayName = TooltipPrimitive.Trigger.displayName;
12
28
 
13
29
  interface TooltipContentProps extends React.ComponentPropsWithoutRef<
14
30
  typeof TooltipPrimitive.Content
@@ -11,6 +11,13 @@ type ToasterToast = ToastProps & {
11
11
  title?: React.ReactNode;
12
12
  description?: React.ReactNode;
13
13
  action?: ToastActionElement;
14
+ /**
15
+ * `stacked` puts the actions in a row under the title, for toasts with more
16
+ * than one action.
17
+ */
18
+ layout?: 'inline' | 'stacked';
19
+ /** Runs when the person presses Cmd+Z or Ctrl+Z while the toast is open. */
20
+ onUndo?: () => void;
14
21
  testId?: string;
15
22
  };
16
23
 
package/src/lib/utils.ts CHANGED
@@ -13,11 +13,12 @@ export function cn(...inputs: ClassValue[]) {
13
13
  * and scoped shortcuts; the marker clears on the next keydown or blur.
14
14
  */
15
15
  export function suppressPointerAutoFocusRing(): void {
16
- const previousActive =
17
- typeof document !== 'undefined' ? document.activeElement : null;
18
- queueMicrotask(() => {
19
- const active =
20
- typeof document !== 'undefined' ? document.activeElement : null;
16
+ if (typeof document === 'undefined') return;
17
+ const previousActive = document.activeElement;
18
+ // React dispatches `onFocus` from the root's bubbling `focusin`, so this
19
+ // capture listener on the document marks the element first.
20
+ const markRestoredFocus = (event: FocusEvent) => {
21
+ const active = event.target;
21
22
  if (
22
23
  !(active instanceof HTMLElement) ||
23
24
  active === document.body ||
@@ -33,15 +34,27 @@ export function suppressPointerAutoFocusRing(): void {
33
34
  };
34
35
  active.addEventListener('blur', clear, { once: true });
35
36
  window.addEventListener('keydown', clear, { capture: true, once: true });
36
- });
37
+ };
38
+ // Radix restores focus synchronously after `onCloseAutoFocus`, so every
39
+ // element focused before the microtask runs is marked; each marker clears on
40
+ // that element's blur.
41
+ document.addEventListener('focusin', markRestoredFocus, true);
42
+ // Outside dismissal can finish without moving focus.
43
+ queueMicrotask(() =>
44
+ document.removeEventListener('focusin', markRestoredFocus, true),
45
+ );
37
46
  }
38
47
 
48
+ const SELECTABLE_ITEM_SELECTOR =
49
+ '[role="option"], [role="menuitem"], [role="menuitemcheckbox"], [role="menuitemradio"]';
50
+
39
51
  interface PointerCloseFocusHandlers<
40
52
  TEscape extends Event,
41
53
  TPointerOutside extends Event,
42
54
  TCloseAutoFocus extends Event,
43
55
  > {
44
56
  onPointerDownCapture?: React.PointerEventHandler | undefined;
57
+ onPointerUpCapture?: React.PointerEventHandler | undefined;
45
58
  onClickCapture?: React.MouseEventHandler | undefined;
46
59
  onKeyDownCapture?: React.KeyboardEventHandler | undefined;
47
60
  onEscapeKeyDown?: ((event: TEscape) => void) | undefined;
@@ -68,6 +81,7 @@ export function usePointerCloseFocus<
68
81
  ) {
69
82
  const {
70
83
  onPointerDownCapture,
84
+ onPointerUpCapture,
71
85
  onClickCapture,
72
86
  onKeyDownCapture,
73
87
  onEscapeKeyDown,
@@ -81,6 +95,16 @@ export function usePointerCloseFocus<
81
95
  closedByPointerRef.current = true;
82
96
  onPointerDownCapture?.(event);
83
97
  },
98
+ onPointerUpCapture: (event: React.PointerEvent) => {
99
+ // Releasing a drag over an item selects it without a click.
100
+ if (
101
+ event.target instanceof Element &&
102
+ event.target.closest(SELECTABLE_ITEM_SELECTOR)
103
+ ) {
104
+ closedByPointerRef.current = true;
105
+ }
106
+ onPointerUpCapture?.(event);
107
+ },
84
108
  onClickCapture: (event: React.MouseEvent) => {
85
109
  if (event.detail > 0) closedByPointerRef.current = true;
86
110
  onClickCapture?.(event);