@meldgrove/ui 0.1.0 → 0.1.2
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 +148 -3
- package/dist/components/app-frame.d.ts +82 -0
- package/dist/components/app-frame.js +129 -0
- package/dist/components/input-group.d.ts +1 -1
- package/dist/components/page.d.ts +14 -0
- package/dist/components/page.js +10 -0
- package/dist/components/settings-cards.d.ts +19 -0
- package/dist/components/settings-cards.js +62 -0
- package/dist/components/sidebar.js +3 -0
- package/dist/components/sign-in-page.d.ts +23 -0
- package/dist/components/sign-in-page.js +31 -0
- package/dist/frame/about-messages.d.ts +5 -0
- package/dist/frame/about-messages.js +12 -0
- package/dist/frame/crumbs.d.ts +18 -0
- package/dist/frame/crumbs.js +33 -0
- package/dist/frame/focus.d.ts +7 -0
- package/dist/frame/focus.js +22 -0
- package/dist/frame/keyboard.d.ts +20 -0
- package/dist/frame/keyboard.js +46 -0
- package/dist/frame/meldgrove-link.d.ts +6 -0
- package/dist/frame/meldgrove-link.js +7 -0
- package/dist/frame/messages.d.ts +38 -0
- package/dist/frame/messages.js +78 -0
- package/dist/frame/nav.d.ts +11 -0
- package/dist/frame/nav.js +24 -0
- package/dist/frame/part-of-meldgrove.d.ts +5 -0
- package/dist/frame/part-of-meldgrove.js +11 -0
- package/dist/frame/search.d.ts +47 -0
- package/dist/frame/search.js +104 -0
- package/dist/frame/shortcuts.d.ts +47 -0
- package/dist/frame/shortcuts.js +141 -0
- package/dist/lib/theme.d.ts +12 -0
- package/dist/lib/theme.js +60 -1
- package/dist/standalone/meldgrove-updates.js +3 -3
- package/dist/styles.css +1 -0
- package/dist/updates/controller.d.ts +41 -2
- package/dist/updates/controller.js +72 -7
- package/dist/updates/state.d.ts +10 -0
- package/dist/updates/state.js +15 -0
- package/package.json +4 -2
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
+
import { Fragment } from "react";
|
|
4
|
+
import { ChevronLeft } from "lucide-react";
|
|
5
|
+
import { Breadcrumb, BreadcrumbEllipsis, BreadcrumbItem, BreadcrumbLink, BreadcrumbList, BreadcrumbPage, BreadcrumbSeparator, } from "../components/breadcrumb.js";
|
|
6
|
+
import { Button } from "../components/button.js";
|
|
7
|
+
import { useT } from "../lib/i18n.js";
|
|
8
|
+
import { frameMessages } from "./messages.js";
|
|
9
|
+
/** At most three items: deeper paths keep the first step and the last two, with "…" between. */
|
|
10
|
+
export function visibleCrumbs(crumbs) {
|
|
11
|
+
return crumbs.length > 3 ? [crumbs[0], "more", ...crumbs.slice(-2)] : crumbs;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Where the page sits in the app, in the header: short, cut when long (the
|
|
15
|
+
* full label shows on hover), the current page as text. On phones only the
|
|
16
|
+
* way back: "‹ parent".
|
|
17
|
+
*/
|
|
18
|
+
export function FrameCrumbs(props) {
|
|
19
|
+
const t = useT(frameMessages);
|
|
20
|
+
const { crumbs, compact } = props;
|
|
21
|
+
const Link = props.link;
|
|
22
|
+
if (crumbs.length === 0)
|
|
23
|
+
return null;
|
|
24
|
+
const parent = crumbs.length > 1 ? crumbs.at(-2) : undefined;
|
|
25
|
+
if (compact && parent?.href) {
|
|
26
|
+
return (_jsx(Button, { variant: "ghost", size: "sm", asChild: true, className: "text-muted-foreground -ml-2 min-w-0 gap-1 px-2", children: _jsxs(Link, { href: parent.href, "aria-label": t("back", { page: parent.label }), children: [_jsx(ChevronLeft, { "aria-hidden": "true" }), _jsx("span", { className: "truncate", children: parent.label })] }) }));
|
|
27
|
+
}
|
|
28
|
+
const shown = visibleCrumbs(crumbs);
|
|
29
|
+
return (_jsx(Breadcrumb, { "aria-label": t("breadcrumbs"), className: "min-w-0", children: _jsx(BreadcrumbList, { className: "flex-nowrap", children: shown.map((crumb, index) => {
|
|
30
|
+
const last = index === shown.length - 1;
|
|
31
|
+
return (_jsxs(Fragment, { children: [index > 0 ? _jsx(BreadcrumbSeparator, {}) : null, _jsx(BreadcrumbItem, { className: "min-w-0", children: crumb === "more" ? (_jsx(BreadcrumbEllipsis, { className: "size-5" })) : last || !crumb.href ? (_jsx(BreadcrumbPage, { className: "block max-w-64 truncate", title: crumb.label, children: crumb.label })) : (_jsx(BreadcrumbLink, { asChild: true, children: _jsx(Link, { href: crumb.href, className: "block max-w-40 truncate", title: crumb.label, children: crumb.label }) })) })] }, crumb === "more" ? "more" : `${index}-${crumb.label}`));
|
|
32
|
+
}) }) }));
|
|
33
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Focus after a dialog closes. Radix gives it back only to a DialogTrigger;
|
|
3
|
+
* the search and the shortcuts open by key or from a menu, so this keeps the
|
|
4
|
+
* element that had focus when they opened and returns to it, or to the page
|
|
5
|
+
* (`#main`) when it is gone (a phone sheet or a menu that closed).
|
|
6
|
+
*/
|
|
7
|
+
export declare function useReturnFocus(open: boolean): (event: Event) => void;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { useLayoutEffect, useRef } from "react";
|
|
3
|
+
/**
|
|
4
|
+
* Focus after a dialog closes. Radix gives it back only to a DialogTrigger;
|
|
5
|
+
* the search and the shortcuts open by key or from a menu, so this keeps the
|
|
6
|
+
* element that had focus when they opened and returns to it, or to the page
|
|
7
|
+
* (`#main`) when it is gone (a phone sheet or a menu that closed).
|
|
8
|
+
*/
|
|
9
|
+
export function useReturnFocus(open) {
|
|
10
|
+
const opener = useRef(null);
|
|
11
|
+
useLayoutEffect(() => {
|
|
12
|
+
if (open)
|
|
13
|
+
opener.current = document.activeElement instanceof HTMLElement ? document.activeElement : null;
|
|
14
|
+
}, [open]);
|
|
15
|
+
return (event) => {
|
|
16
|
+
event.preventDefault();
|
|
17
|
+
// Nothing had focus (the body, on a fresh page) or it is gone: the page.
|
|
18
|
+
const opened = opener.current;
|
|
19
|
+
const target = opened?.isConnected && opened !== document.body ? opened : document.getElementById("main");
|
|
20
|
+
target?.focus();
|
|
21
|
+
};
|
|
22
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/** In fields and editors, letters are typing, not shortcuts. */
|
|
2
|
+
export declare function isTyping(target: EventTarget | null): boolean;
|
|
3
|
+
/** A dialog or menu is open: its keys win. */
|
|
4
|
+
export declare function layerOpen(): boolean;
|
|
5
|
+
/** ⌘K or Ctrl+K: the search, also while typing. */
|
|
6
|
+
export declare function isSearchKey(event: KeyboardEvent): boolean;
|
|
7
|
+
/** "/" outside a field: the search. A single-key shortcut. Shift is allowed (German keyboards: Shift+7). */
|
|
8
|
+
export declare function isSlashKey(event: KeyboardEvent): boolean;
|
|
9
|
+
/**
|
|
10
|
+
* Ctrl+/: the overview of the shortcuts. Ctrl on every system, also on a
|
|
11
|
+
* Mac, where ⌘ shortcuts belong to the browser.
|
|
12
|
+
*/
|
|
13
|
+
export declare function isOverviewKey(event: KeyboardEvent): boolean;
|
|
14
|
+
/** A letter without modifiers, outside fields: part of "G then a letter". */
|
|
15
|
+
export declare function letterOf(event: KeyboardEvent): string | null;
|
|
16
|
+
/** Letters that more than one page claims, with the pages; each letter may go to one page only. */
|
|
17
|
+
export declare function shortcutClashes(items: {
|
|
18
|
+
label: string;
|
|
19
|
+
shortcut?: string;
|
|
20
|
+
}[]): Map<string, string[]>;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// Key handling of the app frame, without React: which keys mean what, and where they do not count.
|
|
2
|
+
/** In fields and editors, letters are typing, not shortcuts. */
|
|
3
|
+
export function isTyping(target) {
|
|
4
|
+
return (target instanceof Element &&
|
|
5
|
+
((target instanceof HTMLElement && target.isContentEditable) ||
|
|
6
|
+
target.closest("input, textarea, select, [contenteditable=''], [contenteditable='true']") !== null));
|
|
7
|
+
}
|
|
8
|
+
/** A dialog or menu is open: its keys win. */
|
|
9
|
+
export function layerOpen() {
|
|
10
|
+
return (document.querySelector('[role="dialog"][data-state="open"], [role="alertdialog"][data-state="open"], [role="menu"][data-state="open"]') !== null);
|
|
11
|
+
}
|
|
12
|
+
function plain(event) {
|
|
13
|
+
return !event.defaultPrevented && !event.isComposing && !event.altKey;
|
|
14
|
+
}
|
|
15
|
+
/** ⌘K or Ctrl+K: the search, also while typing. */
|
|
16
|
+
export function isSearchKey(event) {
|
|
17
|
+
return (plain(event) && (event.metaKey || event.ctrlKey) && !event.shiftKey && event.key.toLowerCase() === "k");
|
|
18
|
+
}
|
|
19
|
+
/** "/" outside a field: the search. A single-key shortcut. Shift is allowed (German keyboards: Shift+7). */
|
|
20
|
+
export function isSlashKey(event) {
|
|
21
|
+
return plain(event) && !event.metaKey && !event.ctrlKey && event.key === "/" && !isTyping(event.target);
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Ctrl+/: the overview of the shortcuts. Ctrl on every system, also on a
|
|
25
|
+
* Mac, where ⌘ shortcuts belong to the browser.
|
|
26
|
+
*/
|
|
27
|
+
export function isOverviewKey(event) {
|
|
28
|
+
return plain(event) && event.ctrlKey && !event.metaKey && event.key === "/";
|
|
29
|
+
}
|
|
30
|
+
/** A letter without modifiers, outside fields: part of "G then a letter". */
|
|
31
|
+
export function letterOf(event) {
|
|
32
|
+
if (!plain(event) || event.ctrlKey || event.metaKey || event.repeat || isTyping(event.target))
|
|
33
|
+
return null;
|
|
34
|
+
return /^[a-z]$/i.test(event.key) ? event.key.toLowerCase() : null;
|
|
35
|
+
}
|
|
36
|
+
/** Letters that more than one page claims, with the pages; each letter may go to one page only. */
|
|
37
|
+
export function shortcutClashes(items) {
|
|
38
|
+
const pages = new Map();
|
|
39
|
+
for (const item of items) {
|
|
40
|
+
if (!item.shortcut)
|
|
41
|
+
continue;
|
|
42
|
+
const key = item.shortcut.toLowerCase();
|
|
43
|
+
pages.set(key, [...(pages.get(key) ?? []), item.label]);
|
|
44
|
+
}
|
|
45
|
+
return new Map([...pages].filter(([, labels]) => labels.length > 1));
|
|
46
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { ArrowUpRight } from "lucide-react";
|
|
3
|
+
import { cn } from "../lib/utils.js";
|
|
4
|
+
/** "Meldgrove" as a link to an app's repository, opening in a new tab. */
|
|
5
|
+
export function MeldgroveLink(props) {
|
|
6
|
+
return (_jsxs("a", { href: props.href, target: "_blank", rel: "noreferrer", className: cn("hover:text-foreground inline-flex items-center gap-0.5 underline underline-offset-4", props.className), children: ["Meldgrove", _jsx(ArrowUpRight, { className: "size-3", "aria-hidden": "true" }), _jsx("span", { className: "sr-only", children: props.newTabLabel })] }));
|
|
7
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/** Texts of the app frame: sidebar, header, search, user menu and shortcuts. */
|
|
2
|
+
export declare const frameMessages: import("../lib/i18n-core.ts").Messages<{
|
|
3
|
+
skip: string;
|
|
4
|
+
ctrl: string;
|
|
5
|
+
navigation: string;
|
|
6
|
+
breadcrumbs: string;
|
|
7
|
+
back: string;
|
|
8
|
+
toggleSidebar: string;
|
|
9
|
+
account: string;
|
|
10
|
+
appearance: string;
|
|
11
|
+
light: string;
|
|
12
|
+
dark: string;
|
|
13
|
+
system: string;
|
|
14
|
+
settings: string;
|
|
15
|
+
shortcuts: string;
|
|
16
|
+
signOut: string;
|
|
17
|
+
search: string;
|
|
18
|
+
searchPlaceholder: string;
|
|
19
|
+
searchDescription: string;
|
|
20
|
+
closeSearch: string;
|
|
21
|
+
pages: string;
|
|
22
|
+
actions: string;
|
|
23
|
+
results: string;
|
|
24
|
+
searching: string;
|
|
25
|
+
searchFailed: string;
|
|
26
|
+
nothingFound: string;
|
|
27
|
+
allResults: string;
|
|
28
|
+
navigateHint: string;
|
|
29
|
+
openHint: string;
|
|
30
|
+
shortcutsDescription: string;
|
|
31
|
+
general: string;
|
|
32
|
+
goTo: string;
|
|
33
|
+
then: string;
|
|
34
|
+
showShortcuts: string;
|
|
35
|
+
singleKeysOn: string;
|
|
36
|
+
singleKeysOff: string;
|
|
37
|
+
openSettings: string;
|
|
38
|
+
}>;
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { defineMessages } from "../lib/i18n-core.js";
|
|
2
|
+
/** Texts of the app frame: sidebar, header, search, user menu and shortcuts. */
|
|
3
|
+
export const frameMessages = defineMessages({
|
|
4
|
+
en: {
|
|
5
|
+
skip: "Skip to content",
|
|
6
|
+
ctrl: "Ctrl",
|
|
7
|
+
navigation: "Main",
|
|
8
|
+
breadcrumbs: "Breadcrumbs",
|
|
9
|
+
back: "Back to {page}",
|
|
10
|
+
toggleSidebar: "Show or hide the sidebar",
|
|
11
|
+
account: "Account of {name}",
|
|
12
|
+
appearance: "Appearance",
|
|
13
|
+
light: "Light",
|
|
14
|
+
dark: "Dark",
|
|
15
|
+
system: "Device setting",
|
|
16
|
+
settings: "Settings",
|
|
17
|
+
shortcuts: "Keyboard shortcuts",
|
|
18
|
+
signOut: "Sign out",
|
|
19
|
+
search: "Search",
|
|
20
|
+
searchPlaceholder: "Search {app}…",
|
|
21
|
+
searchDescription: "Find pages and records by name or number.",
|
|
22
|
+
closeSearch: "Close search",
|
|
23
|
+
pages: "Pages",
|
|
24
|
+
actions: "Actions",
|
|
25
|
+
results: "Results",
|
|
26
|
+
searching: "Searching…",
|
|
27
|
+
searchFailed: "The search is not available right now. Try again.",
|
|
28
|
+
nothingFound: "Nothing found.",
|
|
29
|
+
allResults: "Show all results",
|
|
30
|
+
navigateHint: "Navigate",
|
|
31
|
+
openHint: "Open",
|
|
32
|
+
shortcutsDescription: "They work on every page, except while typing in a field.",
|
|
33
|
+
general: "General",
|
|
34
|
+
goTo: "Go to",
|
|
35
|
+
then: "then",
|
|
36
|
+
showShortcuts: "Show these shortcuts",
|
|
37
|
+
singleKeysOn: "Shortcuts with single letters can be turned off in Settings.",
|
|
38
|
+
singleKeysOff: "Shortcuts with single letters are off. Turn them on in Settings.",
|
|
39
|
+
openSettings: "Open Settings",
|
|
40
|
+
},
|
|
41
|
+
de: {
|
|
42
|
+
skip: "Zum Inhalt springen",
|
|
43
|
+
ctrl: "Strg",
|
|
44
|
+
navigation: "Hauptnavigation",
|
|
45
|
+
breadcrumbs: "Navigationspfad",
|
|
46
|
+
back: "Zurück zu {page}",
|
|
47
|
+
toggleSidebar: "Seitenleiste ein- oder ausblenden",
|
|
48
|
+
account: "Konto von {name}",
|
|
49
|
+
appearance: "Darstellung",
|
|
50
|
+
light: "Hell",
|
|
51
|
+
dark: "Dunkel",
|
|
52
|
+
system: "Wie das Gerät",
|
|
53
|
+
settings: "Einstellungen",
|
|
54
|
+
shortcuts: "Tastenkürzel",
|
|
55
|
+
signOut: "Abmelden",
|
|
56
|
+
search: "Suchen",
|
|
57
|
+
searchPlaceholder: "{app} durchsuchen…",
|
|
58
|
+
searchDescription: "Seiten und Einträge nach Name oder Nummer finden.",
|
|
59
|
+
closeSearch: "Suche schließen",
|
|
60
|
+
pages: "Seiten",
|
|
61
|
+
actions: "Aktionen",
|
|
62
|
+
results: "Treffer",
|
|
63
|
+
searching: "Wird gesucht…",
|
|
64
|
+
searchFailed: "Die Suche ist gerade nicht verfügbar. Versuche es erneut.",
|
|
65
|
+
nothingFound: "Nichts gefunden.",
|
|
66
|
+
allResults: "Alle Treffer zeigen",
|
|
67
|
+
navigateHint: "Auswählen",
|
|
68
|
+
openHint: "Öffnen",
|
|
69
|
+
shortcutsDescription: "Sie gelten auf jeder Seite, außer beim Schreiben in einem Feld.",
|
|
70
|
+
general: "Allgemein",
|
|
71
|
+
goTo: "Gehe zu",
|
|
72
|
+
then: "dann",
|
|
73
|
+
showShortcuts: "Diese Tastenkürzel zeigen",
|
|
74
|
+
singleKeysOn: "Tastenkürzel mit einzelnen Buchstaben lassen sich in den Einstellungen ausschalten.",
|
|
75
|
+
singleKeysOff: "Tastenkürzel mit einzelnen Buchstaben sind aus. In den Einstellungen lassen sie sich einschalten.",
|
|
76
|
+
openSettings: "Einstellungen öffnen",
|
|
77
|
+
},
|
|
78
|
+
});
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
type Matchable = {
|
|
2
|
+
href: string;
|
|
3
|
+
exact?: boolean;
|
|
4
|
+
match?: string[];
|
|
5
|
+
};
|
|
6
|
+
/**
|
|
7
|
+
* The item that is active: the longest path the current one starts with
|
|
8
|
+
* ("/" and `exact` items only on themselves; `match` paths with their sub-pages).
|
|
9
|
+
*/
|
|
10
|
+
export declare function activeHref<T extends Matchable>(pathname: string, items: T[]): string | undefined;
|
|
11
|
+
export {};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// Which navigation item is current, without React.
|
|
2
|
+
function pathOf(href) {
|
|
3
|
+
return href.split(/[?#]/)[0] ?? href;
|
|
4
|
+
}
|
|
5
|
+
const within = (pathname, path) => pathname === path || pathname.startsWith(`${path}/`);
|
|
6
|
+
/**
|
|
7
|
+
* The item that is active: the longest path the current one starts with
|
|
8
|
+
* ("/" and `exact` items only on themselves; `match` paths with their sub-pages).
|
|
9
|
+
*/
|
|
10
|
+
export function activeHref(pathname, items) {
|
|
11
|
+
let best;
|
|
12
|
+
for (const item of items) {
|
|
13
|
+
const path = pathOf(item.href);
|
|
14
|
+
const own = path === "/" || item.exact ? pathname === path : within(pathname, path);
|
|
15
|
+
const lengths = [
|
|
16
|
+
own ? path.length : -1,
|
|
17
|
+
...(item.match ?? []).map((extra) => (within(pathname, extra) ? extra.length : -1)),
|
|
18
|
+
];
|
|
19
|
+
const length = Math.max(...lengths);
|
|
20
|
+
if (length >= 0 && (best === undefined || length > best.length))
|
|
21
|
+
best = { href: item.href, length };
|
|
22
|
+
}
|
|
23
|
+
return best?.href;
|
|
24
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
+
import { useT } from "../lib/i18n.js";
|
|
4
|
+
import { aboutMessages } from "./about-messages.js";
|
|
5
|
+
import { MeldgroveLink } from "./meldgrove-link.js";
|
|
6
|
+
/** "<App> is part of Meldgrove, …", with "Meldgrove" linking to the app's repository. */
|
|
7
|
+
export function PartOfMeldgrove(props) {
|
|
8
|
+
const t = useT(aboutMessages);
|
|
9
|
+
const [before, after] = t("partOf", { app: props.appName }).split("{meldgrove}");
|
|
10
|
+
return (_jsxs(_Fragment, { children: [before, _jsx(MeldgroveLink, { href: props.repositoryUrl, newTabLabel: t("newTab") }), after] }));
|
|
11
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/** A record the app found for a query. */
|
|
2
|
+
export type SearchResult = {
|
|
3
|
+
href: string;
|
|
4
|
+
title: string;
|
|
5
|
+
/** A short number or code, shown before the title, e.g. "REQ-142". */
|
|
6
|
+
reference?: string;
|
|
7
|
+
/** What kind of record, shown as a badge, e.g. "Request". */
|
|
8
|
+
kind?: string;
|
|
9
|
+
};
|
|
10
|
+
/** What the app adds to the search; without it, the search offers the app's pages. */
|
|
11
|
+
export type AppSearchOptions = {
|
|
12
|
+
/** Records for a query; called after a short pause in typing, with a signal that aborts outdated queries. */
|
|
13
|
+
find?: (query: string, signal: AbortSignal) => Promise<SearchResult[]>;
|
|
14
|
+
/** A page with every result, e.g. a filtered list: "Show all results". */
|
|
15
|
+
allResultsHref?: (query: string) => string;
|
|
16
|
+
};
|
|
17
|
+
type SearchPage = {
|
|
18
|
+
label: string;
|
|
19
|
+
href: string;
|
|
20
|
+
shortcut?: string;
|
|
21
|
+
};
|
|
22
|
+
/** Looks like a search field at the top of the sidebar; opens the search. */
|
|
23
|
+
export declare function SearchButton({ onClick }: {
|
|
24
|
+
onClick: () => void;
|
|
25
|
+
}): import("react").JSX.Element;
|
|
26
|
+
/** Opens the search with ⌘K or Ctrl+K anywhere, and with "/" outside fields when single keys are on. */
|
|
27
|
+
export declare function useSearchKeys(props: {
|
|
28
|
+
open: boolean;
|
|
29
|
+
slash: boolean;
|
|
30
|
+
onOpen: () => void;
|
|
31
|
+
}): void;
|
|
32
|
+
/**
|
|
33
|
+
* The search of the frame: the app's pages and, when the app passes `find`,
|
|
34
|
+
* its records. Near the top of the screen, so the field stays put while the
|
|
35
|
+
* results change.
|
|
36
|
+
*/
|
|
37
|
+
export declare function SearchDialog(props: {
|
|
38
|
+
appName: string;
|
|
39
|
+
open: boolean;
|
|
40
|
+
onOpenChange: (open: boolean) => void;
|
|
41
|
+
pages: SearchPage[];
|
|
42
|
+
options?: AppSearchOptions;
|
|
43
|
+
singleKeys: boolean;
|
|
44
|
+
navigate: (href: string) => void;
|
|
45
|
+
onShortcuts: () => void;
|
|
46
|
+
}): import("react").JSX.Element;
|
|
47
|
+
export {};
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
+
import { useEffect, useRef, useState } from "react";
|
|
4
|
+
import { ArrowDown, ArrowUp, CornerDownLeft, Keyboard, Search } from "lucide-react";
|
|
5
|
+
import { Badge } from "../components/badge.js";
|
|
6
|
+
import { Button } from "../components/button.js";
|
|
7
|
+
import { Command, CommandGroup, CommandInput, CommandItem, CommandList, CommandShortcut, } from "../components/command.js";
|
|
8
|
+
import { Dialog, DialogClose, DialogContent, DialogDescription, DialogHeader, DialogTitle, } from "../components/dialog.js";
|
|
9
|
+
import { Kbd, KbdGroup } from "../components/kbd.js";
|
|
10
|
+
import { SidebarMenuButton } from "../components/sidebar.js";
|
|
11
|
+
import { useT } from "../lib/i18n.js";
|
|
12
|
+
import { cn } from "../lib/utils.js";
|
|
13
|
+
import { isSearchKey, isSlashKey, layerOpen } from "./keyboard.js";
|
|
14
|
+
import { useReturnFocus } from "./focus.js";
|
|
15
|
+
import { frameMessages } from "./messages.js";
|
|
16
|
+
import { useModifierLabel } from "./shortcuts.js";
|
|
17
|
+
/** Looks like a search field at the top of the sidebar; opens the search. */
|
|
18
|
+
export function SearchButton({ onClick }) {
|
|
19
|
+
const t = useT(frameMessages);
|
|
20
|
+
const { mod } = useModifierLabel();
|
|
21
|
+
const shortcut = mod === "⌘" ? "⌘K" : `${mod} K`;
|
|
22
|
+
return (_jsxs(SidebarMenuButton, { "aria-haspopup": "dialog", "aria-keyshortcuts": "Meta+K Control+K /", onClick: onClick, className: "bg-background text-muted-foreground border shadow-xs", children: [_jsx(Search, { "aria-hidden": "true" }), _jsx("span", { children: t("search") }), _jsx(Kbd, { "aria-hidden": "true", className: "ml-auto", children: shortcut })] }));
|
|
23
|
+
}
|
|
24
|
+
/** Opens the search with ⌘K or Ctrl+K anywhere, and with "/" outside fields when single keys are on. */
|
|
25
|
+
export function useSearchKeys(props) {
|
|
26
|
+
const { open, slash, onOpen } = props;
|
|
27
|
+
useEffect(() => {
|
|
28
|
+
function onKeyDown(event) {
|
|
29
|
+
if (!isSearchKey(event) && !(slash && isSlashKey(event)))
|
|
30
|
+
return;
|
|
31
|
+
if (open || layerOpen())
|
|
32
|
+
return;
|
|
33
|
+
event.preventDefault();
|
|
34
|
+
onOpen();
|
|
35
|
+
}
|
|
36
|
+
document.addEventListener("keydown", onKeyDown);
|
|
37
|
+
return () => document.removeEventListener("keydown", onKeyDown);
|
|
38
|
+
}, [open, slash, onOpen]);
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* The search of the frame: the app's pages and, when the app passes `find`,
|
|
42
|
+
* its records. Near the top of the screen, so the field stays put while the
|
|
43
|
+
* results change.
|
|
44
|
+
*/
|
|
45
|
+
export function SearchDialog(props) {
|
|
46
|
+
const t = useT(frameMessages);
|
|
47
|
+
const returnFocus = useReturnFocus(props.open);
|
|
48
|
+
return (_jsx(Dialog, { open: props.open, onOpenChange: props.onOpenChange, children: _jsxs(DialogContent, { onCloseAutoFocus: returnFocus, showCloseButton: false, className: "top-[12vh] translate-y-0 gap-0 overflow-hidden p-0 sm:max-w-xl", children: [_jsxs(DialogHeader, { className: "sr-only", children: [_jsx(DialogTitle, { children: t("searchPlaceholder", { app: props.appName }) }), _jsx(DialogDescription, { children: t("searchDescription") })] }), _jsx(SearchPanel, { ...props }), _jsx(DialogClose, { asChild: true, children: _jsxs(Button, { variant: "ghost", size: "sm", className: "absolute top-2.5 right-3 h-7 px-1.5", children: [_jsx(Kbd, { "aria-hidden": "true", children: "Esc" }), _jsx("span", { className: "sr-only", children: t("closeSearch") })] }) })] }) }));
|
|
49
|
+
}
|
|
50
|
+
/** The field, the results and the footer. It unmounts with the dialog, so every search starts empty. */
|
|
51
|
+
function SearchPanel(props) {
|
|
52
|
+
const t = useT(frameMessages);
|
|
53
|
+
const { ctrl } = useModifierLabel();
|
|
54
|
+
const [query, setQuery] = useState("");
|
|
55
|
+
const [found, setFound] = useState();
|
|
56
|
+
const term = query.trim();
|
|
57
|
+
const find = useRef(props.options?.find);
|
|
58
|
+
useEffect(() => {
|
|
59
|
+
find.current = props.options?.find;
|
|
60
|
+
});
|
|
61
|
+
const searches = Boolean(props.options?.find);
|
|
62
|
+
useEffect(() => {
|
|
63
|
+
if (!term || !find.current)
|
|
64
|
+
return;
|
|
65
|
+
const controller = new AbortController();
|
|
66
|
+
const timer = window.setTimeout(() => {
|
|
67
|
+
find
|
|
68
|
+
.current?.(term, controller.signal)
|
|
69
|
+
.then((results) => {
|
|
70
|
+
// An app's find may ignore the signal: an outdated answer still arrives.
|
|
71
|
+
if (!controller.signal.aborted)
|
|
72
|
+
setFound({ term, results });
|
|
73
|
+
})
|
|
74
|
+
.catch(() => {
|
|
75
|
+
if (!controller.signal.aborted)
|
|
76
|
+
setFound({ term, results: null });
|
|
77
|
+
});
|
|
78
|
+
}, 200);
|
|
79
|
+
return () => {
|
|
80
|
+
window.clearTimeout(timer);
|
|
81
|
+
controller.abort();
|
|
82
|
+
};
|
|
83
|
+
}, [term]);
|
|
84
|
+
const go = (href) => {
|
|
85
|
+
props.onOpenChange(false);
|
|
86
|
+
props.navigate(href);
|
|
87
|
+
};
|
|
88
|
+
const lower = term.toLowerCase();
|
|
89
|
+
const pages = props.pages.filter((page) => page.label.toLowerCase().includes(lower));
|
|
90
|
+
const ready = found?.term === term;
|
|
91
|
+
const results = searches && term && ready ? found.results : undefined;
|
|
92
|
+
const allResults = term && props.options?.allResultsHref ? props.options.allResultsHref(term) : null;
|
|
93
|
+
let status = null;
|
|
94
|
+
if (searches && term && !ready)
|
|
95
|
+
status = t("searching");
|
|
96
|
+
else if (results === null)
|
|
97
|
+
status = t("searchFailed");
|
|
98
|
+
else if (pages.length === 0 && !results?.length)
|
|
99
|
+
status = t("nothingFound");
|
|
100
|
+
return (_jsxs(Command, { shouldFilter: false, loop: true, className: "rounded-none **:data-[slot=command-input-wrapper]:h-12", children: [_jsx(CommandInput, { value: query, onValueChange: setQuery, placeholder: t("searchPlaceholder", { app: props.appName }), className: "h-12 pr-12 text-base sm:text-sm" }), _jsx("p", { role: "status", className: cn("text-muted-foreground px-4 text-center text-sm", status ? "py-6" : "sr-only"), children: status }), _jsxs(CommandList, { className: "max-h-[min(24rem,50vh)]", children: [results && results.length > 0 ? (_jsx(CommandGroup, { heading: t("results"), children: results.map((result) => (_jsxs(CommandItem, { value: `result ${result.href}`, onSelect: () => go(result.href), className: "gap-3", children: [result.reference ? (_jsx("span", { className: "text-muted-foreground w-16 shrink-0 text-xs tabular-nums", children: result.reference })) : null, _jsx("span", { className: "min-w-0 flex-1 truncate", children: result.title }), result.kind ? (_jsx(Badge, { variant: "outline", className: "shrink-0 font-normal", children: result.kind })) : null] }, result.href))) })) : null, pages.length > 0 ? (_jsx(CommandGroup, { heading: t("pages"), children: pages.map((page) => (_jsxs(CommandItem, { value: `page ${page.href}`, onSelect: () => go(page.href), children: [page.label, props.singleKeys && page.shortcut ? (_jsxs(CommandShortcut, { children: ["G ", page.shortcut.toUpperCase()] })) : null] }, page.href))) })) : null, !term || t("shortcuts").toLowerCase().includes(lower) ? (_jsx(CommandGroup, { heading: t("actions"), children: _jsxs(CommandItem, { value: "action shortcuts", onSelect: () => {
|
|
101
|
+
props.onOpenChange(false);
|
|
102
|
+
props.onShortcuts();
|
|
103
|
+
}, children: [_jsx(Keyboard, {}), t("shortcuts"), _jsxs(CommandShortcut, { children: [ctrl, " /"] })] }) })) : null] }), _jsxs("div", { className: "text-muted-foreground flex items-center justify-between gap-3 border-t px-3 py-2 text-xs", children: [_jsxs("span", { className: "hidden items-center gap-3 sm:flex", "aria-hidden": "true", children: [_jsxs("span", { className: "flex items-center gap-1.5", children: [_jsxs(KbdGroup, { children: [_jsx(Kbd, { children: _jsx(ArrowUp, {}) }), _jsx(Kbd, { children: _jsx(ArrowDown, {}) })] }), t("navigateHint")] }), _jsxs("span", { className: "flex items-center gap-1.5", children: [_jsx(Kbd, { children: _jsx(CornerDownLeft, {}) }), t("openHint")] })] }), allResults ? (_jsx(Button, { type: "button", variant: "ghost", size: "sm", className: "ml-auto h-7", onClick: () => go(allResults), children: t("allResults") })) : null] })] }));
|
|
104
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { type ReactNode } from "react";
|
|
2
|
+
/** A page that "G then a letter" opens. */
|
|
3
|
+
export type GoTarget = {
|
|
4
|
+
key: string;
|
|
5
|
+
label: string;
|
|
6
|
+
href: string;
|
|
7
|
+
};
|
|
8
|
+
/** How long G waits for its second key. */
|
|
9
|
+
export declare const GO_WAIT_MS = 1500;
|
|
10
|
+
/**
|
|
11
|
+
* Whether the person uses shortcuts with single letters ("G then a letter",
|
|
12
|
+
* "/"), saved in this browser; on unless turned off. Ctrl and ⌘ shortcuts
|
|
13
|
+
* always work. Turning them off is what WCAG 2.1.4 asks for.
|
|
14
|
+
*/
|
|
15
|
+
export declare function useShortcutsPreference(): [boolean, (enabled: boolean) => void];
|
|
16
|
+
/**
|
|
17
|
+
* The key for Search (K) and the sidebar (B): "⌘" on Apple devices, else
|
|
18
|
+
* Ctrl ("Strg" on German keyboards). The overview (Ctrl+/) is Ctrl everywhere.
|
|
19
|
+
*/
|
|
20
|
+
export declare function useModifierLabel(): {
|
|
21
|
+
mod: string;
|
|
22
|
+
ctrl: string;
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* "G then a letter" opens a page, Ctrl+/ the overview. Single letters only
|
|
26
|
+
* when `enabled`; nothing while typing or while a dialog or menu is open.
|
|
27
|
+
* Returns the pages while G waits for its second key.
|
|
28
|
+
*/
|
|
29
|
+
export declare function useGoShortcuts(props: {
|
|
30
|
+
targets: GoTarget[];
|
|
31
|
+
enabled: boolean;
|
|
32
|
+
navigate: (href: string) => void;
|
|
33
|
+
onOverview: () => void;
|
|
34
|
+
}): GoTarget[] | null;
|
|
35
|
+
/** Shown while G waits for its second key: which letter opens which page. */
|
|
36
|
+
export declare function GoHint({ targets }: {
|
|
37
|
+
targets: GoTarget[];
|
|
38
|
+
}): import("react").JSX.Element;
|
|
39
|
+
/** The overview of every shortcut: Ctrl+/, the user menu or the search open it. */
|
|
40
|
+
export declare function ShortcutsDialog(props: {
|
|
41
|
+
targets: GoTarget[];
|
|
42
|
+
enabled: boolean;
|
|
43
|
+
open: boolean;
|
|
44
|
+
onOpenChange: (open: boolean) => void;
|
|
45
|
+
/** Where single-letter shortcuts are turned on and off. */
|
|
46
|
+
settings?: ReactNode;
|
|
47
|
+
}): import("react").JSX.Element;
|