@oaiz/app-kit 0.1.4201 → 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.4201",
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",
@@ -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>
@@ -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