@mdbase-dev/ui 0.1.0-beta.156 → 0.1.0-beta.158

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
@@ -20,6 +20,8 @@ applyThemePreference(loadThemePreference());
20
20
  `--color-text-soft`, `--color-accent`, ...) for light, dark and system
21
21
  themes. Style components with roles, not palette values. Short aliases such
22
22
  as `--ink` belong to each app.
23
+ `--color-current` and `--color-on-current` mark the keyboard/pointer current
24
+ choice in palettes, completion lists and diagnostics; `--color-selected` stays subtle for persistent selections.
23
25
  - `theme` stores the System, Light or Dark choice as `mdbase:theme`.
24
26
  - `brand` draws the Frontmatter mark, each app's inverted mark, and the
25
27
  `mdbase <app>` wordmark. Add `motion.css` to animate the mark: loops
@@ -36,6 +38,21 @@ applyThemePreference(loadThemePreference());
36
38
  collection in another app. Lab and local builds pass their own URLs to
37
39
  `withAppUrls`.
38
40
 
41
+ ## Icon-button tooltips
42
+
43
+ Import `Tooltip` from `@mdbase-dev/ui/tooltip` and include `controls.css`.
44
+ Wrap one native icon button in `<Tooltip label="Search" shortcut="mod+k">…</Tooltip>`.
45
+ Localize the label in the caller. The optional shortcut uses the same notation and
46
+ platform labels as the command palette and sets `aria-keyshortcuts` on the button.
47
+ The button keeps its ref and handlers; a native manual popover adds no layout box
48
+ or focus stop. Escape dismisses a visible tooltip before the parent dialog;
49
+ the bubble remains visible while hovered, with 120 ms to cross the gap.
50
+ Only the supplemental shortcut is linked as a description, preserving existing
51
+ button names and descriptions. Hover waits 400 ms, keyboard
52
+ focus is immediate, and a recently closed tooltip keeps the next one immediate
53
+ for 800 ms. Touch presses do not show a tooltip. Scroll, resize, window blur,
54
+ press, or an unavailable trigger cancels pending and visible hints.
55
+
39
56
  ## Toasts
40
57
 
41
58
  Mount one `Toaster` from `@mdbase-dev/ui/toast` inside the app's locale provider
@@ -191,7 +208,6 @@ built-in value.
191
208
  | `border` | hairlines | `--color-border-strong` | light grey |
192
209
  | `danger` | errors and destructive actions | `--color-danger` | red |
193
210
  | `warning` | warning text | `--color-warning-text` | amber |
194
- | `warning-edge` | the warning notice's edge | `--color-warning` | amber |
195
211
  | `scrim` | behind dialogs and sheets | `--color-scrim` | `rgb(0 0 0 / 0.3)` |
196
212
  | `current-bg` | the open collection's row | (none) | accent 9% into surface, mixed in oklab |
197
213
  | `font` | font family | `--sans` | `system-ui, sans-serif` |
@@ -22,7 +22,6 @@
22
22
  --_border: var(--mdbase-collections-border, var(--color-border-strong, oklch(82% 0.008 255)));
23
23
  --_danger: var(--mdbase-collections-danger, var(--color-danger, oklch(50% 0.11 28)));
24
24
  --_warning: var(--mdbase-collections-warning, var(--color-warning-text, oklch(43% 0.055 69)));
25
- --_warning-edge: var(--mdbase-collections-warning-edge, var(--color-warning, oklch(60% 0.09 75)));
26
25
  --_scrim: var(--mdbase-collections-scrim, var(--color-scrim, rgb(0 0 0 / 0.3)));
27
26
  /*
28
27
  * Mixes are in oklab, which has no hue: in oklch an achromatic surface
@@ -258,21 +257,20 @@
258
257
  gap: 10px;
259
258
  padding: 12px 14px;
260
259
  border: 1px solid var(--_border);
261
- border-left-width: 3px;
262
260
  border-radius: var(--_radius);
263
261
  background: var(--_surface);
264
262
  }
265
263
 
266
264
  .mdbase-collections-notice[data-tone="warning"] {
267
- border-left-color: var(--_warning-edge);
265
+ background: color-mix(in oklab, var(--_warning) 8%, var(--_surface));
268
266
  }
269
267
 
270
268
  .mdbase-collections-notice[data-tone="error"] {
271
- border-left-color: var(--_danger);
269
+ background: color-mix(in oklab, var(--_danger) 8%, var(--_surface));
272
270
  }
273
271
 
274
272
  .mdbase-collections-notice[data-tone="info"] {
275
- border-left-color: var(--_accent);
273
+ background: color-mix(in oklab, var(--_accent) 8%, var(--_surface));
276
274
  }
277
275
 
278
276
  .mdbase-collections-notice.is-inline {
package/css/controls.css CHANGED
@@ -26,6 +26,33 @@
26
26
  white-space: nowrap;
27
27
  }
28
28
 
29
+ .mdbase-tooltip {
30
+ position: fixed;
31
+ inset: auto;
32
+ margin: 0;
33
+ display: flex;
34
+ align-items: center;
35
+ gap: 12px;
36
+ max-width: calc(100vw - 16px);
37
+ max-height: calc(100dvh - 16px);
38
+ box-sizing: border-box;
39
+ overflow: hidden;
40
+ overflow-wrap: anywhere;
41
+ padding: 6px 8px;
42
+ border: 1px solid var(--color-border-strong);
43
+ border-radius: var(--radius-sm);
44
+ background: var(--color-surface-overlay);
45
+ color: var(--color-text);
46
+ box-shadow: var(--elevation-low);
47
+ font: var(--font-size-caption)/1.4 var(--sans);
48
+ pointer-events: auto;
49
+ opacity: 1;
50
+ transition: opacity var(--motion-fast) var(--ease-out);
51
+ }
52
+ .mdbase-tooltip kbd { flex-shrink: 0; color: var(--color-text-muted); font: inherit; }
53
+ @starting-style { .mdbase-tooltip:popover-open { opacity: 0; } }
54
+ @media (prefers-reduced-motion: reduce) { .mdbase-tooltip { transition: none; } }
55
+
29
56
  .mdbase-message {
30
57
  margin: 1.2rem 0 0;
31
58
  padding: 0.7rem 0;
@@ -262,7 +289,7 @@ textarea.mdbase-field {
262
289
  border-radius: var(--control-radius);
263
290
  outline: 0;
264
291
  color: var(--color-text-soft);
265
- background: var(--color-surface);
292
+ background: var(--color-surface-overlay);
266
293
  font-size: var(--control-font-size);
267
294
  overscroll-behavior: contain;
268
295
  }
@@ -715,8 +742,8 @@ textarea.mdbase-field {
715
742
  border: 1px solid var(--color-border-strong);
716
743
  border-radius: var(--control-radius);
717
744
  color: var(--color-text);
718
- background: var(--color-surface);
719
- box-shadow: none;
745
+ background: var(--color-surface-overlay);
746
+ box-shadow: var(--elevation-high);
720
747
  }
721
748
 
722
749
  .mdbase-dialog::backdrop {
package/css/motion.css CHANGED
@@ -2,6 +2,7 @@
2
2
  /* Transitions share durations and easing; settled confirmations hold then fade. */
3
3
  --ease-out: cubic-bezier(0.22, 1, 0.36, 1);
4
4
  --ease-standard: cubic-bezier(0.4, 0, 0.2, 1);
5
+ --ease-exit: cubic-bezier(0.4, 0, 1, 1);
5
6
  --motion-fast: 120ms;
6
7
  --motion-quick: 150ms;
7
8
  --motion-base: 180ms;
@@ -11,7 +12,6 @@
11
12
  --motion-settle: 2.4s;
12
13
  --motion-enter: 160ms;
13
14
  --motion-exit: 120ms;
14
- --motion-grant: 300ms;
15
15
  --motion-confirm: 1s;
16
16
  }
17
17
 
@@ -21,12 +21,13 @@
21
21
  transition: opacity var(--motion-enter) var(--ease-out), transform var(--motion-enter) var(--ease-out);
22
22
  }
23
23
  .mdbase-overlay::backdrop { opacity: 1; transition: opacity var(--motion-enter) var(--ease-out); }
24
- .mdbase-overlay[data-open="false"]::backdrop { opacity: 0; transition-duration: var(--motion-exit); }
24
+ .mdbase-overlay[data-open="false"]::backdrop { opacity: 0; transition-duration: var(--motion-exit); transition-timing-function: var(--ease-exit); }
25
25
  .mdbase-overlay[data-open="false"] {
26
26
  opacity: 0;
27
27
  transform: scale(.98);
28
28
  pointer-events: none;
29
29
  transition-duration: var(--motion-exit);
30
+ transition-timing-function: var(--ease-exit);
30
31
  }
31
32
  @starting-style {
32
33
  .mdbase-overlay[data-open="true"] { opacity: 0; transform: scale(.98); }
@@ -36,7 +37,6 @@
36
37
  :root {
37
38
  --motion-enter: 0ms;
38
39
  --motion-exit: 0ms;
39
- --motion-grant: 0ms;
40
40
  --motion-confirm: 0ms;
41
41
  --motion-fast: 0ms;
42
42
  --motion-quick: 0ms;
package/css/palette.css CHANGED
@@ -1,5 +1,6 @@
1
1
  /* Shared by the portal palette and Editor quick open after its npm repin. */
2
2
  .mdbase-dialog.mdbase-command-palette {
3
+ --accent-soft: var(--color-accent-soft);
3
4
  width: min(var(--palette-width), calc(100vw - 16px));
4
5
  margin: var(--palette-top) auto auto;
5
6
  max-height: calc(100dvh - var(--palette-top) - 16px);
@@ -41,13 +42,20 @@
41
42
 
42
43
  .mdbase-command-results {
43
44
  min-height: 0;
45
+ height: 380px;
46
+ max-height: 100%;
44
47
  overflow: auto;
45
48
  padding: 4px 6px 8px;
46
49
  scroll-padding: 28px 0 8px;
47
- transition: height var(--motion-enter) var(--ease-out);
48
50
  }
49
51
 
52
+ .mdbase-command-results:has(> .mdbase-command-empty) { height: auto; }
53
+
50
54
  .mdbase-command-group {
55
+ position: sticky;
56
+ top: -4px;
57
+ z-index: 1;
58
+ background: var(--color-surface-overlay);
51
59
  padding: 10px 10px 4px;
52
60
  color: var(--color-text-muted);
53
61
  font-size: var(--font-size-caption);
@@ -71,13 +79,18 @@
71
79
  cursor: pointer;
72
80
  }
73
81
 
74
- .mdbase-command-results > button[aria-selected="true"],
75
- .mdbase-command-results > button:hover {
82
+ .mdbase-command-results > button[aria-selected="true"] {
76
83
  color: var(--color-text);
77
- background: var(--color-selected);
84
+ background: light-dark(oklch(94% 0.001 286), oklch(29% 0.004 286));
85
+ box-shadow: inset 0 0 0 1px light-dark(oklch(60% 0.004 286), oklch(63% 0.004 286));
78
86
  }
79
87
 
80
- .mdbase-command-results > button:active { background: var(--color-border-strong); }
88
+ @media (forced-colors: active) {
89
+ .mdbase-command-results > button[aria-selected="true"] {
90
+ outline: 1px solid Highlight;
91
+ outline-offset: -1px;
92
+ }
93
+ }
81
94
 
82
95
  .mdbase-command-results .mdbase-command-copy {
83
96
  min-width: 0;
@@ -100,6 +113,11 @@
100
113
  font-weight: var(--font-weight-medium);
101
114
  }
102
115
 
116
+ .mdbase-command-results mark {
117
+ color: inherit;
118
+ background: var(--accent-soft);
119
+ }
120
+
103
121
  .mdbase-command-results small {
104
122
  flex: 1 1 auto;
105
123
  color: var(--color-text-muted);
package/css/toast.css CHANGED
@@ -3,9 +3,9 @@
3
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
4
  @starting-style { .mdbase-toast-slot[data-open="true"] { grid-template-rows: 0fr; opacity: 0; transform: translateY(8px); } }
5
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); color: var(--color-text-soft); box-shadow: var(--elevation-high); font-size: var(--font-size-ui); }
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
7
  .mdbase-toast-clip > .mdbase-toast { margin-top: 8px; }
8
8
  .mdbase-toast span { flex: 1; min-width: 0; overflow-wrap: anywhere; }
9
9
  .mdbase-toast button { flex: none; font-size: inherit; }
10
- @media (max-width: 640px) { .mdbase-toaster { top: max(16px, env(safe-area-inset-top)); bottom: auto; left: 16px; right: 16px; max-width: none; } }
10
+ @media (max-width: 640px) { .mdbase-toaster { bottom: max(16px, env(safe-area-inset-bottom)); left: 16px; right: 16px; max-width: none; } }
11
11
  @media (prefers-reduced-motion: reduce) { .mdbase-toast-slot { transition: none; transform: none; } }
package/css/tokens.css CHANGED
@@ -8,6 +8,7 @@
8
8
  --color-canvas: oklch(100% 0 0);
9
9
  --color-sidebar: oklch(98% 0.002 286);
10
10
  --color-surface: oklch(100% 0 0);
11
+ --color-surface-overlay: oklch(100% 0 0);
11
12
  --color-surface-subtle: oklch(97% 0.001 286);
12
13
  --color-text: oklch(21% 0.006 286);
13
14
  --color-text-soft: oklch(37% 0.012 286);
@@ -16,6 +17,8 @@
16
17
  --color-border: oklch(94.5% 0.002 286);
17
18
  --color-border-strong: oklch(90% 0.004 286);
18
19
  --color-selected: oklch(95.8% 0.001 286);
20
+ --color-current: oklch(67% 0.001 286);
21
+ --color-on-current: var(--color-text);
19
22
  --color-accent: #005c88;
20
23
  --color-accent-strong: oklch(40% 0.1 239);
21
24
  --color-accent-soft: oklch(93.5% 0.035 238);
@@ -83,6 +86,7 @@
83
86
  --color-canvas: oklch(18.7% 0.002 286);
84
87
  --color-sidebar: oklch(16% 0.002 286);
85
88
  --color-surface: oklch(18.7% 0.002 286);
89
+ --color-surface-overlay: oklch(21.4% 0.003 286);
86
90
  --color-surface-subtle: oklch(23.6% 0.004 286);
87
91
  --color-text: oklch(94.7% 0.003 286);
88
92
  --color-text-soft: oklch(82% 0.007 286);
@@ -91,6 +95,7 @@
91
95
  --color-border: oklch(25.7% 0.004 286);
92
96
  --color-border-strong: oklch(32% 0.005 286);
93
97
  --color-selected: oklch(25.7% 0.004 286);
98
+ --color-current: oklch(50% 0.004 286);
94
99
  --color-accent: #63b1e2;
95
100
  --color-accent-strong: oklch(65% 0.105 238);
96
101
  --color-accent-soft: oklch(31% 0.045 238);
@@ -127,6 +132,7 @@
127
132
  --color-canvas: oklch(18.7% 0.002 286);
128
133
  --color-sidebar: oklch(16% 0.002 286);
129
134
  --color-surface: oklch(18.7% 0.002 286);
135
+ --color-surface-overlay: oklch(21.4% 0.003 286);
130
136
  --color-surface-subtle: oklch(23.6% 0.004 286);
131
137
  --color-text: oklch(94.7% 0.003 286);
132
138
  --color-text-soft: oklch(82% 0.007 286);
@@ -135,6 +141,7 @@
135
141
  --color-border: oklch(25.7% 0.004 286);
136
142
  --color-border-strong: oklch(32% 0.005 286);
137
143
  --color-selected: oklch(25.7% 0.004 286);
144
+ --color-current: oklch(50% 0.004 286);
138
145
  --color-accent: #63b1e2;
139
146
  --color-accent-strong: oklch(65% 0.105 238);
140
147
  --color-accent-soft: oklch(31% 0.045 238);
@@ -1,7 +1,9 @@
1
+ export { isApplePlatform, shortcutLabel } from "./shortcuts.js";
1
2
  import { type JSX, type RefObject, type ReactNode } from "react";
2
3
  export interface Command {
3
4
  readonly id: string;
4
5
  readonly label: string;
6
+ readonly content?: ReactNode;
5
7
  readonly detail?: string | undefined;
6
8
  readonly icon?: ReactNode;
7
9
  /** Stable group ID; headings follow first appearance and may be presented by groupLabel. */
@@ -9,6 +11,7 @@ export interface Command {
9
11
  readonly keywords?: string | undefined;
10
12
  /** For example "mod+shift+f"; see shortcutLabel. */
11
13
  readonly shortcut?: string | undefined;
14
+ readonly context?: string | undefined;
12
15
  readonly run: () => void;
13
16
  /** A variant run with Shift+Enter or Shift+click, such as opening beside the document. */
14
17
  readonly alternate?: {
@@ -16,9 +19,6 @@ export interface Command {
16
19
  readonly run: () => void;
17
20
  } | undefined;
18
21
  }
19
- export declare function isApplePlatform(): boolean;
20
- /** "mod+shift+f" as the platform writes it: ⌘⇧F on Apple systems, Ctrl+Shift+F elsewhere. */
21
- export declare function shortcutLabel(shortcut: string): string;
22
22
  /**
23
23
  * Without a query, show a few of each group (every command of the groups in `showAll`); with
24
24
  * one, show every command matching all its terms, grouped in their original order.
@@ -28,7 +28,7 @@ export declare function matchingCommands(commands: readonly Command[], query: st
28
28
  * Find or run anything: every mdbase app opens it with Mod+K. Typing filters commands; the
29
29
  * arrows move, Enter runs, Shift+Enter runs a command's alternate, Escape closes.
30
30
  */
31
- export declare function CommandPalette({ open, commands, onClose, label, placeholder, showAll, groupLabel, returnFocusRef, emptyState }: {
31
+ export declare function CommandPalette({ open, commands, onClose, label, placeholder, showAll, groupLabel, returnFocusRef, emptyState, initialQuery, getResults, inputLabel }: {
32
32
  readonly open: boolean;
33
33
  readonly commands: readonly Command[];
34
34
  readonly onClose: () => void;
@@ -39,4 +39,7 @@ export declare function CommandPalette({ open, commands, onClose, label, placeho
39
39
  readonly groupLabel?: ((group: string) => string) | undefined;
40
40
  readonly returnFocusRef?: RefObject<HTMLElement | null> | undefined;
41
41
  readonly emptyState?: ReactNode;
42
+ readonly initialQuery?: string;
43
+ readonly getResults?: (query: string) => readonly Command[];
44
+ readonly inputLabel?: string;
42
45
  }): JSX.Element | null;
@@ -2,43 +2,30 @@ import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-run
2
2
  import { i18n as _i18n } from "@lingui/core";
3
3
  import { useLocaleRevision } from "./i18n.js";
4
4
  import { Dialog } from "./Dialog.js";
5
- import { Fragment, useEffect, useId, useMemo, useRef, useState } from "react";
6
- export function isApplePlatform() {
7
- return typeof navigator !== "undefined" && /Mac|iPhone|iPad/u.test(navigator.platform || navigator.userAgent);
8
- }
9
- /** "mod+shift+f" as the platform writes it: ⌘⇧F on Apple systems, Ctrl+Shift+F elsewhere. */
10
- export function shortcutLabel(shortcut) {
11
- if (/^[a-z] [a-z]$/iu.test(shortcut)) return shortcut.toUpperCase();
12
- const apple = isApplePlatform();
13
- return shortcut.split("+").map(part => {
14
- switch (part.toLowerCase()) {
15
- case "mod":
16
- return apple ? "⌘" : "Ctrl";
17
- case "ctrl":
18
- return apple ? "⌃" : "Ctrl";
19
- case "shift":
20
- return apple ? "⇧" : "Shift";
21
- case "alt":
22
- return apple ? "⌥" : "Alt";
23
- default:
24
- return part.length === 1 ? part.toUpperCase() : part;
25
- }
26
- }).join(apple ? "" : "+");
27
- }
5
+ import { shortcutLabel } from "./shortcuts.js";
6
+ export { isApplePlatform, shortcutLabel } from "./shortcuts.js";
7
+ import { Fragment, useEffect, useLayoutEffect, useId, useMemo, useRef, useState } from "react";
28
8
  const browseLimit = 6;
29
9
  const searchLimit = 40;
10
+ function contextualCommands(commands) {
11
+ return [...commands.filter(command => command.context).map(command => ({
12
+ ...command,
13
+ group: "context"
14
+ })), ...commands.filter(command => !command.context)];
15
+ }
30
16
  /**
31
17
  * Without a query, show a few of each group (every command of the groups in `showAll`); with
32
18
  * one, show every command matching all its terms, grouped in their original order.
33
19
  */
34
20
  export function matchingCommands(commands, query, showAll = []) {
21
+ commands = contextualCommands(commands);
35
22
  const terms = query.trim().toLocaleLowerCase().split(/\s+/u).filter(Boolean);
36
23
  if (terms.length === 0) {
37
24
  const counts = new Map();
38
25
  return commands.filter(command => {
39
26
  const count = counts.get(command.group) ?? 0;
40
27
  counts.set(command.group, count + 1);
41
- return showAll.includes(command.group) || count < browseLimit;
28
+ return command.context !== undefined || showAll.includes(command.group) || count < browseLimit;
42
29
  });
43
30
  }
44
31
  const matches = commands.filter(command => {
@@ -47,7 +34,7 @@ export function matchingCommands(commands, query, showAll = []) {
47
34
  });
48
35
  const names = matches.filter(command => terms.every(term => command.label.toLocaleLowerCase().includes(term)));
49
36
  const selected = [...names, ...matches.filter(command => !names.includes(command))].slice(0, searchLimit);
50
- const groups = [...new Set(selected.map(({
37
+ const groups = [...new Set([...selected.filter(command => command.context), ...selected.filter(command => !command.context)].map(({
51
38
  group
52
39
  }) => group))];
53
40
  return groups.flatMap(group => selected.filter(command => command.group === group));
@@ -75,7 +62,10 @@ export function CommandPalette({
75
62
  showAll,
76
63
  groupLabel,
77
64
  returnFocusRef,
78
- emptyState
65
+ emptyState,
66
+ initialQuery = "",
67
+ getResults,
68
+ inputLabel = placeholder
79
69
  }) {
80
70
  useLocaleRevision();
81
71
  return _jsx(Dialog, {
@@ -93,7 +83,10 @@ export function CommandPalette({
93
83
  showAll: showAll,
94
84
  groupLabel: groupLabel,
95
85
  returnFocusRef: returnFocusRef,
96
- emptyState: emptyState
86
+ emptyState: emptyState,
87
+ initialQuery: initialQuery,
88
+ getResults: getResults,
89
+ inputLabel: inputLabel
97
90
  })
98
91
  });
99
92
  }
@@ -106,20 +99,28 @@ function OpenCommandPalette({
106
99
  showAll,
107
100
  groupLabel,
108
101
  returnFocusRef,
109
- emptyState
102
+ emptyState,
103
+ initialQuery,
104
+ getResults,
105
+ inputLabel
110
106
  }) {
111
107
  useLocaleRevision();
112
- const [query, setQuery] = useState("");
108
+ const [query, setQuery] = useState(initialQuery);
113
109
  const [active, setActive] = useState(0);
114
110
  const inputRef = useRef(null);
115
- const matches = useMemo(() => matchingCommands(commands, query, showAll), [commands, query, showAll]);
116
- const resultHeight = Math.min(380, Math.max(152, Math.ceil((matches.length * 38 + new Set(matches.map(command => command.group)).size * 30 + 12) / 76) * 76));
111
+ const resultsRef = useRef(null);
112
+ const matches = useMemo(() => getResults ? contextualCommands(getResults(query)) : matchingCommands(commands, query, showAll), [commands, query, showAll, getResults]);
113
+ useLayoutEffect(() => {
114
+ resultsRef.current.querySelector('[aria-selected="true"]')?.scrollIntoView({
115
+ block: "nearest"
116
+ });
117
+ }, [active, matches]);
117
118
  useEffect(() => {
118
119
  if (open) {
119
- setQuery("");
120
+ setQuery(initialQuery);
120
121
  setActive(0);
121
122
  }
122
- }, [open]);
123
+ }, [open, initialQuery]);
123
124
  const run = (command, alternate = false) => {
124
125
  inputRef.current?.closest("dialog")?.close();
125
126
  returnFocusRef?.current?.focus({
@@ -147,7 +148,7 @@ function OpenCommandPalette({
147
148
  })]
148
149
  }), _jsx("span", {
149
150
  className: "mdbase-visually-hidden",
150
- children: placeholder
151
+ children: inputLabel
151
152
  }), _jsx("input", {
152
153
  ref: inputRef,
153
154
  "data-autofocus": true,
@@ -167,11 +168,9 @@ function OpenCommandPalette({
167
168
  }
168
169
  })]
169
170
  }), _jsx("div", {
171
+ ref: resultsRef,
170
172
  id: `${hintId}-results`,
171
173
  className: "mdbase-command-results",
172
- style: {
173
- height: resultHeight
174
- },
175
174
  role: "listbox",
176
175
  "aria-label": _i18n._(
177
176
  /** i18n */
@@ -181,14 +180,16 @@ function OpenCommandPalette({
181
180
  }),
182
181
  "aria-describedby": hintId,
183
182
  children: matches.length > 0 ? matches.map((command, index) => _jsxs(Fragment, {
184
- children: [command.group !== matches[index - 1]?.group ? _jsx("div", {
183
+ children: [command.group !== matches[index - 1]?.group || command.context !== matches[index - 1]?.context ? _jsx("div", {
185
184
  className: "mdbase-command-group",
186
185
  role: "presentation",
187
- children: groupLabel ? groupLabel(command.group) : command.group
186
+ children: command.context ?? (groupLabel ? groupLabel(command.group) : command.group)
188
187
  }) : null, _jsx(CommandRow, {
189
188
  id: `${hintId}-${command.id}`,
190
189
  command: command,
190
+ query: query,
191
191
  active: index === active,
192
+ onActivate: () => setActive(index),
192
193
  onRun: run
193
194
  })]
194
195
  }, command.id)) : _jsx("div", {
@@ -221,8 +222,8 @@ function OpenCommandPalette({
221
222
  }), " ", _i18n._(
222
223
  /** i18n */
223
224
  {
224
- id: "2cqK5k",
225
- message: "to open"
225
+ id: "yq3Myn",
226
+ message: "to run"
226
227
  })]
227
228
  }), matches[active]?.alternate ? _jsxs("span", {
228
229
  children: [_jsx("kbd", {
@@ -251,36 +252,40 @@ function OpenCommandPalette({
251
252
  function CommandRow({
252
253
  id,
253
254
  command,
255
+ query,
254
256
  active,
257
+ onActivate,
255
258
  onRun
256
259
  }) {
257
260
  useLocaleRevision();
258
- const ref = useRef(null);
259
- useEffect(() => {
260
- if (active) ref.current?.scrollIntoView({
261
- block: "nearest"
262
- });
263
- }, [active]);
261
+ const pattern = query.trim().split(/\s+/u).filter(Boolean).map(term => term.replace(/[.*+?^${}()|[\]\\]/gu, "\\$&")).join("|");
262
+ const label = pattern ? command.label.split(new RegExp(`(${pattern})`, "giu")).map((part, index) => index % 2 ? _jsx("mark", {
263
+ children: part
264
+ }, index) : part) : command.label;
264
265
  return _jsxs("button", {
265
266
  id: id,
266
- ref: ref,
267
267
  tabIndex: -1,
268
268
  type: "button",
269
269
  role: "option",
270
270
  "aria-selected": active,
271
+ onPointerMove: event => {
272
+ if (event.movementX || event.movementY) onActivate();
273
+ },
271
274
  onMouseDown: event => event.preventDefault(),
272
275
  onClick: event => onRun(command, event.shiftKey),
273
276
  children: [command.icon && _jsx("span", {
274
277
  className: "mdbase-command-icon",
275
278
  "aria-hidden": "true",
276
279
  children: command.icon
277
- }), _jsxs("span", {
280
+ }), _jsx("span", {
278
281
  className: "mdbase-command-copy",
279
- children: [_jsx("strong", {
280
- children: command.label
281
- }), command.detail ? _jsx("small", {
282
- children: command.detail
283
- }) : null]
282
+ children: command.content ?? _jsxs(_Fragment, {
283
+ children: [_jsx("strong", {
284
+ children: label
285
+ }), command.detail ? _jsx("small", {
286
+ children: command.detail
287
+ }) : null]
288
+ })
284
289
  }), command.alternate && active ? _jsxs("span", {
285
290
  className: "mdbase-command-alternate",
286
291
  children: [shortcutLabel("shift"), "\u21B5 ", command.alternate.label]
@@ -297,6 +302,9 @@ function handleKeys(event, matches, active, setActive, run, close) {
297
302
  event.preventDefault();
298
303
  const direction = event.key === "ArrowDown" ? 1 : -1;
299
304
  setActive((active + direction + matches.length) % Math.max(1, matches.length));
305
+ } else if (event.key === "Home" || event.key === "End") {
306
+ event.preventDefault();
307
+ setActive(event.key === "Home" ? 0 : Math.max(0, matches.length - 1));
300
308
  } else if (event.key === "Enter") {
301
309
  const command = matches[active];
302
310
  if (command) {
@@ -1,5 +1,5 @@
1
1
  import { type ReactNode, type RefObject } from "react";
2
- export declare function ConfirmDialog({ open, title, consequence, confirmLabel, pendingLabel, busy, disabled, onConfirm, onClose, returnFocusRef, children }: {
2
+ export declare function ConfirmDialog({ open, title, consequence, confirmLabel, pendingLabel, busy, disabled, onConfirm, onClose, returnFocusRef, children, cancelLabel, tone, initialFocus, role }: {
3
3
  readonly open: boolean;
4
4
  readonly title: string;
5
5
  readonly consequence?: string;
@@ -11,4 +11,8 @@ export declare function ConfirmDialog({ open, title, consequence, confirmLabel,
11
11
  readonly onClose: () => void;
12
12
  readonly returnFocusRef?: RefObject<HTMLElement | null> | undefined;
13
13
  readonly children?: ReactNode;
14
+ readonly cancelLabel?: string | undefined;
15
+ readonly tone?: "default" | "danger" | undefined;
16
+ readonly initialFocus?: "confirm" | "cancel" | undefined;
17
+ readonly role?: "dialog" | "alertdialog";
14
18
  }): import("react").JSX.Element;
@@ -14,7 +14,16 @@ export function ConfirmDialog({
14
14
  onConfirm,
15
15
  onClose,
16
16
  returnFocusRef,
17
- children
17
+ children,
18
+ cancelLabel = _i18n._(
19
+ /** i18n */
20
+ {
21
+ id: "dEgA5A",
22
+ message: "Cancel"
23
+ }),
24
+ tone = "danger",
25
+ initialFocus = "cancel",
26
+ role = "dialog"
18
27
  }) {
19
28
  useLocaleRevision();
20
29
  const [pending, setPending] = useState(false);
@@ -22,6 +31,7 @@ export function ConfirmDialog({
22
31
  const working = busy || pending;
23
32
  return _jsxs(Dialog, {
24
33
  open: open,
34
+ role: role,
25
35
  title: title,
26
36
  descriptionId: consequence ? descriptionId : undefined,
27
37
  onClose: onClose,
@@ -36,21 +46,20 @@ export function ConfirmDialog({
36
46
  children: [_jsx("button", {
37
47
  type: "button",
38
48
  className: "mdbase-button is-tertiary",
39
- "data-autofocus": true,
49
+ "data-autofocus": initialFocus === "cancel" || undefined,
40
50
  disabled: working,
41
51
  onClick: onClose,
42
- children: _i18n._(
43
- /** i18n */
44
- {
45
- id: "dEgA5A",
46
- message: "Cancel"
47
- })
52
+ children: cancelLabel
48
53
  }), _jsx("button", {
49
54
  type: "button",
50
- className: "mdbase-button is-danger",
55
+ className: `mdbase-button ${tone === "danger" ? "is-danger" : "is-primary"}`,
56
+ "data-autofocus": initialFocus === "confirm" || undefined,
51
57
  "aria-busy": working || undefined,
52
58
  disabled: working || disabled,
53
- onClick: async () => {
59
+ onClick: async event => {
60
+ event.currentTarget.closest("dialog")?.focus({
61
+ preventScroll: true
62
+ });
54
63
  setPending(true);
55
64
  try {
56
65
  await onConfirm();
package/dist/Dialog.d.ts CHANGED
@@ -1,11 +1,13 @@
1
1
  import { type JSX, type ReactNode, type RefObject } from "react";
2
- export declare function Dialog({ open, onClose, title, descriptionId, className, closeDisabled, returnFocusRef, children }: {
2
+ export declare function Dialog({ open, onClose, title, descriptionId, className, closeDisabled, returnFocusRef, initialFocusRef, role, children }: {
3
3
  readonly open: boolean;
4
4
  readonly onClose: () => void;
5
- readonly title: string;
5
+ readonly title: ReactNode;
6
6
  readonly descriptionId?: string | undefined;
7
7
  readonly className?: string | undefined;
8
8
  readonly closeDisabled?: boolean;
9
9
  readonly returnFocusRef?: RefObject<HTMLElement | null> | undefined;
10
+ readonly initialFocusRef?: RefObject<HTMLElement | null> | undefined;
11
+ readonly role?: "dialog" | "alertdialog";
10
12
  readonly children: ReactNode;
11
13
  }): JSX.Element | null;