@mdbase-dev/ui 0.1.0 → 0.1.3

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/css/motion.css CHANGED
@@ -13,34 +13,64 @@
13
13
  --motion-enter: 160ms;
14
14
  --motion-exit: 120ms;
15
15
  --motion-confirm: 1s;
16
+ /* A calm spring (0.36s, bounce 0.22, about 2% overshoot) for what a hand just moved: a
17
+ released button, a switch thumb, a tick. --motion-press is the quick way in. */
18
+ --ease-spring: linear(0, 0.035, 0.121, 0.233, 0.356, 0.476, 0.587, 0.686, 0.769, 0.838, 0.892, 0.934, 0.966, 0.988, 1.003, 1.012, 1.017, 1.02, 1.02, 1.019, 1.017, 1.014, 1.012, 1.009, 1.007, 1.005, 1.004, 1.003, 1.002, 1);
19
+ --motion-spring: 475ms;
20
+ --motion-press: 90ms;
16
21
  }
17
22
 
23
+ /* Overlays (menus, lists, dialogs) fade in while they grow a little, from the corner beside
24
+ their trigger when popover.ts knows it. They leave faster than they arrive. */
18
25
  .mdbase-overlay {
19
26
  opacity: 1;
20
- transform: scale(1);
21
- transition: opacity var(--motion-enter) var(--ease-out), transform var(--motion-enter) var(--ease-out);
27
+ transform: none;
28
+ transform-origin: var(--overlay-origin, center);
29
+ transition:
30
+ opacity var(--motion-enter) var(--ease-out),
31
+ transform var(--motion-spring) var(--ease-spring);
22
32
  }
23
- .mdbase-overlay::backdrop { opacity: 1; transition: opacity var(--motion-enter) var(--ease-out); }
33
+
34
+ .mdbase-overlay::backdrop {
35
+ opacity: 1;
36
+ transition: opacity var(--motion-enter) var(--ease-out);
37
+ }
38
+
24
39
  .mdbase-dialog.mdbase-overlay {
25
40
  transition-property: opacity, transform, display, overlay;
26
41
  transition-behavior: normal, normal, allow-discrete, allow-discrete;
27
42
  }
43
+
28
44
  .mdbase-dialog.mdbase-overlay::backdrop {
29
45
  transition-property: opacity, display, overlay;
30
46
  transition-behavior: normal, allow-discrete, allow-discrete;
31
47
  }
32
- .mdbase-overlay[data-open="false"]::backdrop { opacity: 0; transition-duration: var(--motion-exit); transition-timing-function: var(--ease-exit); }
48
+
33
49
  .mdbase-overlay[data-open="false"] {
34
50
  opacity: 0;
35
- transform: scale(.98);
51
+ transform: var(--overlay-from, scale(0.96));
36
52
  pointer-events: none;
37
53
  transition-duration: var(--motion-exit);
38
54
  transition-timing-function: var(--ease-exit);
39
55
  }
56
+
57
+ .mdbase-overlay[data-open="false"]::backdrop {
58
+ opacity: 0;
59
+ transition-duration: var(--motion-exit);
60
+ transition-timing-function: var(--ease-exit);
61
+ }
62
+
40
63
  @starting-style {
41
- .mdbase-overlay[data-open="true"] { opacity: 0; transform: scale(.98); }
42
- .mdbase-overlay[data-open="true"]::backdrop { opacity: 0; }
64
+ .mdbase-overlay[data-open="true"] {
65
+ opacity: 0;
66
+ transform: var(--overlay-from, scale(0.96));
67
+ }
68
+
69
+ .mdbase-overlay[data-open="true"]::backdrop {
70
+ opacity: 0;
71
+ }
43
72
  }
73
+
44
74
  @media (prefers-reduced-motion: reduce) {
45
75
  :root {
46
76
  --motion-enter: 0ms;
@@ -52,6 +82,8 @@
52
82
  --motion-slow: 0ms;
53
83
  --motion-spin: 0ms;
54
84
  --motion-pulse: 0ms;
85
+ --motion-spring: 0ms;
86
+ --motion-press: 0ms;
55
87
  }
56
88
  .mdbase-overlay, .mdbase-overlay::backdrop { transition: none; transform: none; }
57
89
  }
package/css/toast.css CHANGED
@@ -1,11 +1,135 @@
1
- .mdbase-toaster { position: fixed; bottom: 20px; right: 20px; z-index: 1000; max-width: min(460px, calc(100vw - 40px)); }
2
- .mdbase-toast-slot { display: grid; grid-template-rows: 1fr; opacity: 1; transform: translateY(0); transition: grid-template-rows var(--motion-enter) var(--ease-out), opacity var(--motion-enter) var(--ease-out), transform var(--motion-enter) var(--ease-out); }
3
- .mdbase-toast-slot[data-open="false"] { grid-template-rows: 0fr; opacity: 0; transform: translateY(8px); pointer-events: none; transition-duration: var(--motion-exit); }
4
- @starting-style { .mdbase-toast-slot[data-open="true"] { grid-template-rows: 0fr; opacity: 0; transform: translateY(8px); } }
5
- .mdbase-toast-clip { min-height: 0; overflow: hidden; }
6
- .mdbase-toast { display: flex; align-items: center; gap: 9px; padding: 8px 8px 8px 13px; border: 1px solid var(--color-border-strong); border-radius: var(--radius-sm); background: var(--color-surface-overlay); color: var(--color-text-soft); box-shadow: var(--elevation-high); font-size: var(--font-size-ui); }
7
- .mdbase-toast-clip > .mdbase-toast { margin-top: 8px; }
8
- .mdbase-toast span { flex: 1; min-width: 0; overflow-wrap: anywhere; }
9
- .mdbase-toast button { flex: none; font-size: inherit; }
10
- @media (max-width: 640px) { .mdbase-toaster { bottom: max(16px, env(safe-area-inset-bottom)); left: 16px; right: 16px; max-width: none; } }
11
- @media (prefers-reduced-motion: reduce) { .mdbase-toast-slot { transition: none; transform: none; } }
1
+ /*
2
+ * Toasts (./toast). Their buttons and motion tokens come from controls.css, so load that
3
+ * first. A slot's row grows from nothing as its toast arrives, which slides the stack along
4
+ * instead of jumping it.
5
+ */
6
+ .mdbase-toaster {
7
+ position: fixed;
8
+ right: 20px;
9
+ bottom: 20px;
10
+ z-index: 1000;
11
+ max-width: min(460px, calc(100vw - 40px));
12
+ }
13
+
14
+ .mdbase-toast-slot {
15
+ display: grid;
16
+ grid-template-rows: 1fr;
17
+ opacity: 1;
18
+ transform: none;
19
+ transition:
20
+ grid-template-rows var(--motion-enter) var(--ease-out),
21
+ opacity var(--motion-enter) var(--ease-out),
22
+ transform var(--motion-spring) var(--ease-spring);
23
+ }
24
+
25
+ .mdbase-toast-slot[data-open="false"] {
26
+ grid-template-rows: 0fr;
27
+ opacity: 0;
28
+ transform: translateY(8px) scale(0.98);
29
+ pointer-events: none;
30
+ transition-duration: var(--motion-exit);
31
+ transition-timing-function: var(--ease-exit);
32
+ }
33
+
34
+ @starting-style {
35
+ .mdbase-toast-slot[data-open="true"] {
36
+ grid-template-rows: 0fr;
37
+ opacity: 0;
38
+ transform: translateY(8px) scale(0.98);
39
+ }
40
+ }
41
+
42
+ .mdbase-toast-clip {
43
+ min-height: 0;
44
+ overflow: hidden;
45
+ }
46
+
47
+ .mdbase-toast {
48
+ display: flex;
49
+ align-items: center;
50
+ gap: 9px;
51
+ margin-top: 8px;
52
+ padding: 6px 6px 6px 13px;
53
+ border: 1px solid var(--color-border-strong);
54
+ border-radius: var(--radius-md);
55
+ color: var(--color-text-soft);
56
+ background: var(--color-surface-overlay);
57
+ box-shadow: var(--elevation-high);
58
+ font-size: var(--font-size-ui);
59
+ }
60
+
61
+ .mdbase-toast-message {
62
+ min-width: 0;
63
+ flex: 1;
64
+ padding-block: 2px;
65
+ overflow-wrap: anywhere;
66
+ }
67
+
68
+ .mdbase-toast button {
69
+ flex: none;
70
+ font-size: inherit;
71
+ }
72
+
73
+ .mdbase-toast .mdbase-icon-button {
74
+ width: 28px;
75
+ height: 28px;
76
+ color: var(--color-text-muted);
77
+ }
78
+
79
+ .mdbase-toast .mdbase-icon-button svg {
80
+ width: 14px;
81
+ height: 14px;
82
+ }
83
+
84
+ /* Tone: a small mark before the message, drawn as the toast arrives. */
85
+ .mdbase-toast-tone {
86
+ width: 15px;
87
+ height: 15px;
88
+ flex: none;
89
+ fill: none;
90
+ stroke: currentColor;
91
+ stroke-width: 1.6;
92
+ stroke-linecap: round;
93
+ stroke-linejoin: round;
94
+ }
95
+
96
+ .mdbase-toast[data-tone="success"] .mdbase-toast-tone {
97
+ color: var(--color-success);
98
+ }
99
+
100
+ .mdbase-toast[data-tone="error"] .mdbase-toast-tone {
101
+ color: var(--color-danger);
102
+ }
103
+
104
+ .mdbase-toast[data-tone="error"] {
105
+ border-color: color-mix(in oklch, var(--color-danger) 35%, var(--color-border-strong));
106
+ color: var(--color-text);
107
+ }
108
+
109
+ .mdbase-toast-tone path {
110
+ stroke-dasharray: 1;
111
+ animation: mdbase-draw var(--motion-spring) var(--ease-out) var(--motion-fast) backwards;
112
+ }
113
+
114
+ @media (max-width: 640px) {
115
+ .mdbase-toaster {
116
+ right: 16px;
117
+ bottom: max(16px, env(safe-area-inset-bottom));
118
+ left: 16px;
119
+ max-width: none;
120
+ }
121
+ }
122
+
123
+ @media (pointer: coarse) {
124
+ .mdbase-toast .mdbase-icon-button {
125
+ width: 40px;
126
+ height: 40px;
127
+ }
128
+ }
129
+
130
+ @media (prefers-reduced-motion: reduce) {
131
+ .mdbase-toast-slot {
132
+ transition: none;
133
+ transform: none;
134
+ }
135
+ }
package/css/tokens.css CHANGED
@@ -4,50 +4,51 @@
4
4
  * app-local and must map onto these roles.
5
5
  */
6
6
  :root {
7
+ /* One used scheme at a time, so light-dark() below and scripts reading color-scheme agree. */
7
8
  color-scheme: light;
8
- --color-canvas: oklch(100% 0 0);
9
- --color-sidebar: oklch(98% 0.002 286);
10
- --color-surface: oklch(100% 0 0);
11
- --color-surface-overlay: oklch(100% 0 0);
12
- --color-surface-subtle: oklch(97% 0.001 286);
13
- --color-text: oklch(21% 0.006 286);
14
- --color-text-soft: oklch(37% 0.012 286);
15
- --color-text-muted: oklch(52% 0.012 286);
16
- --color-text-faint: oklch(70% 0.008 286);
17
- --color-border: oklch(94.5% 0.002 286);
18
- --color-border-strong: oklch(90% 0.004 286);
19
- --color-selected: oklch(95.8% 0.001 286);
20
- --color-current: oklch(67% 0.001 286);
9
+ --color-canvas: light-dark(oklch(100% 0 0), oklch(18.7% 0.002 286));
10
+ --color-sidebar: light-dark(oklch(98% 0.002 286), oklch(16% 0.002 286));
11
+ --color-surface: light-dark(oklch(100% 0 0), oklch(18.7% 0.002 286));
12
+ --color-surface-overlay: light-dark(oklch(100% 0 0), oklch(21.4% 0.003 286));
13
+ --color-surface-subtle: light-dark(oklch(97% 0.001 286), oklch(23.6% 0.004 286));
14
+ --color-text: light-dark(oklch(21% 0.006 286), oklch(94.7% 0.003 286));
15
+ --color-text-soft: light-dark(oklch(37% 0.012 286), oklch(82% 0.007 286));
16
+ --color-text-muted: light-dark(oklch(52% 0.012 286), oklch(68% 0.01 286));
17
+ --color-text-faint: light-dark(oklch(70% 0.008 286), oklch(56% 0.01 286));
18
+ --color-border: light-dark(oklch(94.5% 0.002 286), oklch(25.7% 0.004 286));
19
+ --color-border-strong: light-dark(oklch(90% 0.004 286), oklch(32% 0.005 286));
20
+ --color-selected: light-dark(oklch(95.8% 0.001 286), oklch(25.7% 0.004 286));
21
+ --color-current: light-dark(oklch(67% 0.001 286), oklch(50% 0.004 286));
21
22
  --color-on-current: var(--color-text);
22
- --color-accent: #005c88;
23
- --color-accent-strong: oklch(40% 0.1 239);
24
- --color-accent-soft: oklch(93.5% 0.035 238);
25
- --color-on-accent: oklch(98% 0.004 245);
26
- --color-success: oklch(44% 0.085 153);
27
- --color-danger: oklch(50% 0.11 28);
28
- --color-danger-soft: oklch(97% 0.018 28);
29
- --color-warning: oklch(60% 0.09 75);
30
- --color-warning-border: oklch(83% 0.055 82);
31
- --color-warning-surface: oklch(98% 0.018 86);
32
- --color-warning-text: oklch(43% 0.055 69);
33
- --color-warning-strong: oklch(36% 0.052 67);
34
- --color-editor-text: oklch(28% 0.014 255);
35
- --color-placeholder: oklch(78% 0.007 255);
36
- --color-skeleton: var(--color-border);
37
- --color-shadow: oklch(20% 0.02 255 / 0.06);
38
- --color-scrim: oklch(21% 0.018 255 / 0.14);
39
- --scrollbar-thumb-rest: oklch(51% 0.014 255 / 0.07);
40
- --scrollbar-thumb: oklch(51% 0.014 255 / 0.22);
41
- --scrollbar-thumb-hover: oklch(42% 0.016 255 / 0.4);
42
- --syntax-comment: oklch(58% 0.012 255);
43
- --syntax-keyword: oklch(42% 0.09 294);
44
- --syntax-string: oklch(39% 0.075 153);
45
- --syntax-number: oklch(43% 0.1 45);
46
- --syntax-link: oklch(43% 0.1 238);
23
+ --color-accent: light-dark(#005c88, #63b1e2);
24
+ --color-accent-strong: light-dark(oklch(40% 0.1 239), oklch(65% 0.105 238));
25
+ --color-accent-soft: light-dark(oklch(93.5% 0.035 238), oklch(31% 0.045 238));
26
+ --color-on-accent: light-dark(oklch(98% 0.004 245), oklch(17% 0.012 255));
27
+ --color-success: light-dark(oklch(44% 0.085 153), oklch(72% 0.09 153));
28
+ --color-danger: light-dark(oklch(50% 0.11 28), oklch(72% 0.115 28));
29
+ --color-danger-soft: light-dark(oklch(97% 0.018 28), oklch(25% 0.03 28));
30
+ --color-warning: light-dark(oklch(60% 0.09 75), oklch(78% 0.09 75));
31
+ --color-warning-border: light-dark(oklch(83% 0.055 82), oklch(48% 0.06 82));
32
+ --color-warning-surface: light-dark(oklch(98% 0.018 86), oklch(25% 0.026 82));
33
+ --color-warning-text: light-dark(oklch(43% 0.055 69), oklch(78% 0.07 82));
34
+ --color-warning-strong: light-dark(oklch(36% 0.052 67), oklch(86% 0.065 82));
35
+ --color-editor-text: light-dark(oklch(28% 0.014 255), oklch(88% 0.009 255));
36
+ --color-placeholder: light-dark(oklch(78% 0.007 255), oklch(56% 0.01 255));
37
+ --color-skeleton: light-dark(var(--color-border), oklch(25% 0.012 255));
38
+ --color-shadow: light-dark(oklch(20% 0.02 255 / 0.06), oklch(5% 0.01 255 / 0.22));
39
+ --color-scrim: light-dark(oklch(21% 0.018 255 / 0.14), oklch(5% 0.01 255 / 0.46));
40
+ --scrollbar-thumb-rest: light-dark(oklch(51% 0.014 255 / 0.07), oklch(72% 0.012 255 / 0.06));
41
+ --scrollbar-thumb: light-dark(oklch(51% 0.014 255 / 0.22), oklch(72% 0.012 255 / 0.2));
42
+ --scrollbar-thumb-hover: light-dark(oklch(42% 0.016 255 / 0.4), oklch(78% 0.012 255 / 0.38));
43
+ --syntax-comment: light-dark(oklch(58% 0.012 255), oklch(62% 0.012 255));
44
+ --syntax-keyword: light-dark(oklch(42% 0.09 294), oklch(76% 0.09 294));
45
+ --syntax-string: light-dark(oklch(39% 0.075 153), oklch(75% 0.08 153));
46
+ --syntax-number: light-dark(oklch(43% 0.1 45), oklch(78% 0.1 65));
47
+ --syntax-link: light-dark(oklch(43% 0.1 238), oklch(77% 0.1 238));
47
48
  --syntax-heading: var(--color-text);
48
- --app-editor: #005c88;
49
- --app-reader: #a4467f;
50
- --app-writer: #d55e00;
49
+ --app-editor: light-dark(#005c88, #63b1e2);
50
+ --app-reader: light-dark(#a4467f, #e08cc0);
51
+ --app-writer: light-dark(#d55e00, #f58d4a);
51
52
  --sans: "IBM Plex Sans", system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
52
53
  --mono: "Azeret Mono", ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
53
54
  /* Six steps: captions, UI chrome, section titles, prose, headings, document titles. */
@@ -65,8 +66,10 @@
65
66
  --font-weight-medium: 500;
66
67
  --font-weight-semibold: 600;
67
68
  --font-weight-bold: 700;
69
+ /* Controls and rows; content blocks and toasts; menus and dialogs. A surface that pads its
70
+ rows takes the next step up, so its corners stay concentric with theirs. */
68
71
  --radius-sm: 6px;
69
- --radius-md: 6px;
72
+ --radius-md: 8px;
70
73
  --radius-lg: 10px;
71
74
  --elevation-low: 0 4px 12px var(--color-shadow);
72
75
  --elevation-high: 0 12px 32px var(--color-shadow);
@@ -83,92 +86,10 @@
83
86
 
84
87
  :root[data-theme="dark"] {
85
88
  color-scheme: dark;
86
- --color-canvas: oklch(18.7% 0.002 286);
87
- --color-sidebar: oklch(16% 0.002 286);
88
- --color-surface: oklch(18.7% 0.002 286);
89
- --color-surface-overlay: oklch(21.4% 0.003 286);
90
- --color-surface-subtle: oklch(23.6% 0.004 286);
91
- --color-text: oklch(94.7% 0.003 286);
92
- --color-text-soft: oklch(82% 0.007 286);
93
- --color-text-muted: oklch(68% 0.01 286);
94
- --color-text-faint: oklch(56% 0.01 286);
95
- --color-border: oklch(25.7% 0.004 286);
96
- --color-border-strong: oklch(32% 0.005 286);
97
- --color-selected: oklch(25.7% 0.004 286);
98
- --color-current: oklch(50% 0.004 286);
99
- --color-accent: #63b1e2;
100
- --color-accent-strong: oklch(65% 0.105 238);
101
- --color-accent-soft: oklch(31% 0.045 238);
102
- --color-on-accent: oklch(17% 0.012 255);
103
- --color-success: oklch(72% 0.09 153);
104
- --color-danger: oklch(72% 0.115 28);
105
- --color-danger-soft: oklch(25% 0.03 28);
106
- --color-warning: oklch(78% 0.09 75);
107
- --color-warning-border: oklch(48% 0.06 82);
108
- --color-warning-surface: oklch(25% 0.026 82);
109
- --color-warning-text: oklch(78% 0.07 82);
110
- --color-warning-strong: oklch(86% 0.065 82);
111
- --color-editor-text: oklch(88% 0.009 255);
112
- --color-placeholder: oklch(56% 0.01 255);
113
- --color-skeleton: oklch(25% 0.012 255);
114
- --color-shadow: oklch(5% 0.01 255 / 0.22);
115
- --color-scrim: oklch(5% 0.01 255 / 0.46);
116
- --scrollbar-thumb-rest: oklch(72% 0.012 255 / 0.06);
117
- --scrollbar-thumb: oklch(72% 0.012 255 / 0.2);
118
- --scrollbar-thumb-hover: oklch(78% 0.012 255 / 0.38);
119
- --syntax-comment: oklch(62% 0.012 255);
120
- --syntax-keyword: oklch(76% 0.09 294);
121
- --syntax-string: oklch(75% 0.08 153);
122
- --syntax-number: oklch(78% 0.1 65);
123
- --syntax-link: oklch(77% 0.1 238);
124
- --app-editor: #63b1e2;
125
- --app-reader: #e08cc0;
126
- --app-writer: #f58d4a;
127
89
  }
128
90
 
129
91
  @media (prefers-color-scheme: dark) {
130
92
  :root:not([data-theme]) {
131
93
  color-scheme: dark;
132
- --color-canvas: oklch(18.7% 0.002 286);
133
- --color-sidebar: oklch(16% 0.002 286);
134
- --color-surface: oklch(18.7% 0.002 286);
135
- --color-surface-overlay: oklch(21.4% 0.003 286);
136
- --color-surface-subtle: oklch(23.6% 0.004 286);
137
- --color-text: oklch(94.7% 0.003 286);
138
- --color-text-soft: oklch(82% 0.007 286);
139
- --color-text-muted: oklch(68% 0.01 286);
140
- --color-text-faint: oklch(56% 0.01 286);
141
- --color-border: oklch(25.7% 0.004 286);
142
- --color-border-strong: oklch(32% 0.005 286);
143
- --color-selected: oklch(25.7% 0.004 286);
144
- --color-current: oklch(50% 0.004 286);
145
- --color-accent: #63b1e2;
146
- --color-accent-strong: oklch(65% 0.105 238);
147
- --color-accent-soft: oklch(31% 0.045 238);
148
- --color-on-accent: oklch(17% 0.012 255);
149
- --color-success: oklch(72% 0.09 153);
150
- --color-danger: oklch(72% 0.115 28);
151
- --color-danger-soft: oklch(25% 0.03 28);
152
- --color-warning: oklch(78% 0.09 75);
153
- --color-warning-border: oklch(48% 0.06 82);
154
- --color-warning-surface: oklch(25% 0.026 82);
155
- --color-warning-text: oklch(78% 0.07 82);
156
- --color-warning-strong: oklch(86% 0.065 82);
157
- --color-editor-text: oklch(88% 0.009 255);
158
- --color-placeholder: oklch(56% 0.01 255);
159
- --color-skeleton: oklch(25% 0.012 255);
160
- --color-shadow: oklch(5% 0.01 255 / 0.22);
161
- --color-scrim: oklch(5% 0.01 255 / 0.46);
162
- --scrollbar-thumb-rest: oklch(72% 0.012 255 / 0.06);
163
- --scrollbar-thumb: oklch(72% 0.012 255 / 0.2);
164
- --scrollbar-thumb-hover: oklch(78% 0.012 255 / 0.38);
165
- --syntax-comment: oklch(62% 0.012 255);
166
- --syntax-keyword: oklch(76% 0.09 294);
167
- --syntax-string: oklch(75% 0.08 153);
168
- --syntax-number: oklch(78% 0.1 65);
169
- --syntax-link: oklch(77% 0.1 238);
170
- --app-editor: #63b1e2;
171
- --app-reader: #e08cc0;
172
- --app-writer: #f58d4a;
173
94
  }
174
95
  }
@@ -1,5 +1,10 @@
1
- import { type ReactNode, type RefObject } from "react";
2
- export declare function ConfirmDialog({ open, title, consequence, confirmLabel, pendingLabel, busy, disabled, onConfirm, onClose, returnFocusRef, children, cancelLabel, tone, initialFocus, role }: {
1
+ import { type JSX, type ReactNode, type RefObject } from "react";
2
+ /**
3
+ * Asks before an action that is hard to take back. Cancel has the initial focus unless
4
+ * `initialFocus` says otherwise; while the action runs the dialog stays open and cannot be
5
+ * dismissed, and the confirm button says what is happening.
6
+ */
7
+ export declare function ConfirmDialog({ open, title, consequence, confirmLabel, pendingLabel, busy, disabled, onConfirm, onClose, returnFocusRef, children, cancelLabel, tone, initialFocus, role, }: {
3
8
  readonly open: boolean;
4
9
  readonly title: string;
5
10
  readonly consequence?: string;
@@ -15,4 +20,4 @@ export declare function ConfirmDialog({ open, title, consequence, confirmLabel,
15
20
  readonly tone?: "default" | "danger" | undefined;
16
21
  readonly initialFocus?: "confirm" | "cancel" | undefined;
17
22
  readonly role?: "dialog" | "alertdialog";
18
- }): import("react").JSX.Element;
23
+ }): JSX.Element | null;
@@ -3,6 +3,11 @@ import { i18n as _i18n } from "@lingui/core";
3
3
  import { useId, useState } from "react";
4
4
  import { Dialog } from "./Dialog.js";
5
5
  import { useLocaleRevision } from "./i18n.js";
6
+ /**
7
+ * Asks before an action that is hard to take back. Cancel has the initial focus unless
8
+ * `initialFocus` says otherwise; while the action runs the dialog stays open and cannot be
9
+ * dismissed, and the confirm button says what is happening.
10
+ */
6
11
  export function ConfirmDialog({
7
12
  open,
8
13
  title,
@@ -57,6 +62,7 @@ export function ConfirmDialog({
57
62
  "aria-busy": working || undefined,
58
63
  disabled: working || disabled,
59
64
  onClick: async event => {
65
+ // The button is about to be disabled; the dialog keeps the focus meanwhile.
60
66
  event.currentTarget.closest("dialog")?.focus({
61
67
  preventScroll: true
62
68
  });
@@ -67,7 +73,11 @@ export function ConfirmDialog({
67
73
  setPending(false);
68
74
  }
69
75
  },
70
- children: working ? pendingLabel : confirmLabel
76
+ children: _jsx("span", {
77
+ className: "mdbase-button-label",
78
+ "data-other": working ? confirmLabel : pendingLabel,
79
+ children: working ? pendingLabel : confirmLabel
80
+ })
71
81
  })]
72
82
  })]
73
83
  });
package/dist/Dialog.d.ts CHANGED
@@ -1,13 +1,22 @@
1
1
  import { type JSX, type ReactNode, type RefObject } from "react";
2
- export declare function Dialog({ open, onClose, title, descriptionId, className, closeDisabled, returnFocusRef, initialFocusRef, role, children }: {
2
+ export interface DialogProps {
3
3
  readonly open: boolean;
4
4
  readonly onClose: () => void;
5
5
  readonly title: ReactNode;
6
6
  readonly descriptionId?: string | undefined;
7
7
  readonly className?: string | undefined;
8
+ /** While true, Escape, the backdrop and the close button do nothing (an action is running). */
8
9
  readonly closeDisabled?: boolean;
10
+ /** Where focus goes on close; the element focused when the dialog opened otherwise. */
9
11
  readonly returnFocusRef?: RefObject<HTMLElement | null> | undefined;
12
+ /** A region whose first control takes focus on open, in place of `[data-autofocus]`. */
10
13
  readonly initialFocusRef?: RefObject<HTMLElement | null> | undefined;
11
14
  readonly role?: "dialog" | "alertdialog";
12
15
  readonly children: ReactNode;
13
- }): JSX.Element | null;
16
+ }
17
+ /**
18
+ * A modal dialog: a native `<dialog>` in the top layer, with a title and a close button. It
19
+ * closes on Escape, a press on the backdrop or the close button, and hands focus back to
20
+ * where it came from.
21
+ */
22
+ export declare function Dialog({ open, onClose, title, descriptionId, className, closeDisabled, returnFocusRef, initialFocusRef, role, children, }: DialogProps): JSX.Element | null;
package/dist/Dialog.js CHANGED
@@ -5,6 +5,12 @@ import { createPortal } from "react-dom";
5
5
  import { useLocaleRevision } from "./i18n.js";
6
6
  import { useOverlay } from "./overlay.js";
7
7
  import { Tooltip } from "./Tooltip.js";
8
+ const FOCUSABLE = 'button:not(:disabled), input:not(:disabled), select:not(:disabled), textarea:not(:disabled), [tabindex="0"]';
9
+ /**
10
+ * A modal dialog: a native `<dialog>` in the top layer, with a title and a close button. It
11
+ * closes on Escape, a press on the backdrop or the close button, and hands focus back to
12
+ * where it came from.
13
+ */
8
14
  export function Dialog({
9
15
  open,
10
16
  onClose,
@@ -24,16 +30,20 @@ export function Dialog({
24
30
  } = useOverlay(open);
25
31
  const invoker = useRef(null);
26
32
  const titleId = useId();
33
+ const returnFocus = () => {
34
+ (returnFocusRef?.current ?? invoker.current)?.focus({
35
+ preventScroll: true
36
+ });
37
+ };
38
+ // Open: show the dialog and focus its first control. A region named by initialFocusRef may
39
+ // render its controls late, so the dialog holds focus until one arrives.
27
40
  useLayoutEffect(() => {
28
41
  const dialog = ref.current;
29
42
  if (!dialog || !open || dialog.open) return;
30
43
  invoker.current = document.activeElement instanceof HTMLElement ? document.activeElement : null;
31
44
  dialog.showModal();
32
- const focus = () => {
33
- const scope = initialFocusRef?.current ?? dialog;
34
- return scope.querySelector(initialFocusRef ? 'button:not([disabled]), input:not([disabled]), select:not([disabled]), textarea:not([disabled]), [tabindex="0"]' : "[data-autofocus]");
35
- };
36
- const first = focus();
45
+ const firstControl = () => initialFocusRef ? (initialFocusRef.current ?? dialog).querySelector(FOCUSABLE) : dialog.querySelector("[data-autofocus]");
46
+ const first = firstControl();
37
47
  if (first) first.focus({
38
48
  preventScroll: true
39
49
  });else if (initialFocusRef) dialog.focus({
@@ -42,7 +52,7 @@ export function Dialog({
42
52
  if (!initialFocusRef || first) return;
43
53
  const observer = new MutationObserver(() => {
44
54
  if (document.activeElement !== dialog) return;
45
- const control = focus();
55
+ const control = firstControl();
46
56
  if (control) {
47
57
  control.focus({
48
58
  preventScroll: true
@@ -56,36 +66,60 @@ export function Dialog({
56
66
  });
57
67
  return () => observer.disconnect();
58
68
  }, [open, ref, initialFocusRef]);
69
+ // A running action disables the control that had focus; the dialog holds it until the
70
+ // action ends, then gives it back to the autofocus control.
59
71
  useLayoutEffect(() => {
60
72
  const dialog = ref.current;
61
73
  if (!dialog || !open) return;
62
- if (closeDisabled && (!dialog.contains(document.activeElement) || document.activeElement?.matches(":disabled"))) dialog.focus({
63
- preventScroll: true
64
- });
65
- if (!closeDisabled && document.activeElement === dialog) dialog.querySelector("[data-autofocus]:not(:disabled)")?.focus({
66
- preventScroll: true
67
- });
74
+ const active = document.activeElement;
75
+ if (closeDisabled && (!dialog.contains(active) || active?.matches(":disabled"))) {
76
+ dialog.focus({
77
+ preventScroll: true
78
+ });
79
+ }
80
+ if (!closeDisabled && active === dialog) {
81
+ dialog.querySelector("[data-autofocus]:not(:disabled)")?.focus({
82
+ preventScroll: true
83
+ });
84
+ }
68
85
  }, [closeDisabled, open, ref]);
86
+ // Close: the dialog stays mounted while it leaves, but gives focus back at once.
69
87
  useLayoutEffect(() => {
70
88
  const dialog = ref.current;
71
89
  if (open || !dialog) return;
72
90
  const ownedFocus = dialog.contains(document.activeElement) || document.activeElement === document.body;
73
91
  if (dialog.open) dialog.close();
74
- if (ownedFocus) (returnFocusRef?.current ?? invoker.current)?.focus({
75
- preventScroll: true
76
- });
92
+ if (ownedFocus) returnFocus();
77
93
  }, [open, ref, returnFocusRef]);
94
+ // Unmounted while open (its owner went away): close and return focus all the same.
78
95
  useEffect(() => {
79
96
  const dialog = ref.current;
80
97
  return () => {
81
98
  const wasOpen = dialog?.open;
82
99
  dialog?.close();
83
- if (wasOpen) (returnFocusRef?.current ?? invoker.current)?.focus({
84
- preventScroll: true
85
- });
100
+ if (wasOpen) returnFocus();
86
101
  };
87
102
  }, [present, ref, returnFocusRef]);
88
103
  if (!present) return null;
104
+ const onKeyDown = event => {
105
+ // An exiting dialog no longer owns the keyboard, so app shortcuts still work.
106
+ if (open) event.stopPropagation();
107
+ if (event.key === "Escape") {
108
+ event.preventDefault();
109
+ if (!closeDisabled) onClose();
110
+ }
111
+ // While closing is disabled, Tab cycles through the controls that are still enabled.
112
+ if (event.key === "Tab" && closeDisabled) {
113
+ const controls = [...event.currentTarget.querySelectorAll(`${FOCUSABLE}, a[href]`)].filter(control => control.getClientRects().length > 0);
114
+ const current = controls.indexOf(document.activeElement);
115
+ const step = event.shiftKey ? -1 : 1;
116
+ const next = current < 0 ? event.shiftKey ? controls.length - 1 : 0 : (current + step + controls.length) % controls.length;
117
+ event.preventDefault();
118
+ (controls[next] ?? event.currentTarget).focus({
119
+ preventScroll: true
120
+ });
121
+ }
122
+ };
89
123
  return createPortal(_jsx("dialog", {
90
124
  ref: ref,
91
125
  className: ["mdbase-dialog mdbase-overlay", className].filter(Boolean).join(" "),
@@ -97,27 +131,13 @@ export function Dialog({
97
131
  "aria-labelledby": titleId,
98
132
  "aria-describedby": descriptionId,
99
133
  tabIndex: -1,
100
- onKeyDown: event => {
101
- // An exiting dialog no longer owns the keyboard, so app shortcuts still work.
102
- if (open) event.stopPropagation();
103
- if (event.key === "Escape") {
104
- event.preventDefault();
105
- if (!closeDisabled) onClose();
106
- }
107
- if (event.key === "Tab" && closeDisabled) {
108
- const controls = [...event.currentTarget.querySelectorAll('button:not(:disabled), a[href], input:not(:disabled), select:not(:disabled), textarea:not(:disabled), [tabindex="0"]')].filter(control => control.getClientRects().length > 0);
109
- const current = controls.indexOf(document.activeElement);
110
- const next = current < 0 ? event.shiftKey ? controls.length - 1 : 0 : (current + (event.shiftKey ? -1 : 1) + controls.length) % controls.length;
111
- event.preventDefault();
112
- (controls.length ? controls[next] : event.currentTarget)?.focus({
113
- preventScroll: true
114
- });
115
- }
116
- },
134
+ onKeyDown: onKeyDown,
117
135
  onCancel: event => {
118
136
  event.preventDefault();
119
137
  if (!closeDisabled) onClose();
120
138
  },
139
+ // A press on the backdrop lands on the dialog element itself; it closes the dialog
140
+ // without moving focus first.
121
141
  onMouseDown: event => {
122
142
  if (event.target === event.currentTarget) event.preventDefault();
123
143
  },
@@ -83,7 +83,8 @@ export function SaveNotice({
83
83
  viewBox: "0 0 16 16",
84
84
  "aria-hidden": "true",
85
85
  children: _jsx("path", {
86
- d: "m3.5 8.5 3 3 6-7"
86
+ d: "m3.5 8.5 3 3 6-7",
87
+ pathLength: 1
87
88
  })
88
89
  }), label]
89
90
  });