@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.
Files changed (40) hide show
  1. package/README.md +148 -3
  2. package/dist/components/app-frame.d.ts +82 -0
  3. package/dist/components/app-frame.js +129 -0
  4. package/dist/components/input-group.d.ts +1 -1
  5. package/dist/components/page.d.ts +14 -0
  6. package/dist/components/page.js +10 -0
  7. package/dist/components/settings-cards.d.ts +19 -0
  8. package/dist/components/settings-cards.js +62 -0
  9. package/dist/components/sidebar.js +3 -0
  10. package/dist/components/sign-in-page.d.ts +23 -0
  11. package/dist/components/sign-in-page.js +31 -0
  12. package/dist/frame/about-messages.d.ts +5 -0
  13. package/dist/frame/about-messages.js +12 -0
  14. package/dist/frame/crumbs.d.ts +18 -0
  15. package/dist/frame/crumbs.js +33 -0
  16. package/dist/frame/focus.d.ts +7 -0
  17. package/dist/frame/focus.js +22 -0
  18. package/dist/frame/keyboard.d.ts +20 -0
  19. package/dist/frame/keyboard.js +46 -0
  20. package/dist/frame/meldgrove-link.d.ts +6 -0
  21. package/dist/frame/meldgrove-link.js +7 -0
  22. package/dist/frame/messages.d.ts +38 -0
  23. package/dist/frame/messages.js +78 -0
  24. package/dist/frame/nav.d.ts +11 -0
  25. package/dist/frame/nav.js +24 -0
  26. package/dist/frame/part-of-meldgrove.d.ts +5 -0
  27. package/dist/frame/part-of-meldgrove.js +11 -0
  28. package/dist/frame/search.d.ts +47 -0
  29. package/dist/frame/search.js +104 -0
  30. package/dist/frame/shortcuts.d.ts +47 -0
  31. package/dist/frame/shortcuts.js +141 -0
  32. package/dist/lib/theme.d.ts +12 -0
  33. package/dist/lib/theme.js +60 -1
  34. package/dist/standalone/meldgrove-updates.js +3 -3
  35. package/dist/styles.css +1 -0
  36. package/dist/updates/controller.d.ts +41 -2
  37. package/dist/updates/controller.js +72 -7
  38. package/dist/updates/state.d.ts +10 -0
  39. package/dist/updates/state.js +15 -0
  40. package/package.json +4 -2
@@ -0,0 +1,141 @@
1
+ "use client";
2
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
3
+ import { useEffect, useRef, useState, useSyncExternalStore } from "react";
4
+ import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, } from "../components/dialog.js";
5
+ import { Kbd, KbdGroup } from "../components/kbd.js";
6
+ import { useT } from "../lib/i18n.js";
7
+ import { cn } from "../lib/utils.js";
8
+ import { isOverviewKey, layerOpen, letterOf } from "./keyboard.js";
9
+ import { useReturnFocus } from "./focus.js";
10
+ import { frameMessages } from "./messages.js";
11
+ /** How long G waits for its second key. */
12
+ export const GO_WAIT_MS = 1500;
13
+ const SHORTCUTS_KEY = "meldgrove.shortcuts";
14
+ const SHORTCUTS_EVENT = "meldgrove:shortcuts";
15
+ let unsavedShortcuts = true;
16
+ function readShortcuts() {
17
+ try {
18
+ return window.localStorage.getItem(SHORTCUTS_KEY) !== "off";
19
+ }
20
+ catch {
21
+ return unsavedShortcuts;
22
+ }
23
+ }
24
+ function subscribeShortcuts(onChange) {
25
+ window.addEventListener(SHORTCUTS_EVENT, onChange);
26
+ window.addEventListener("storage", onChange);
27
+ return () => {
28
+ window.removeEventListener(SHORTCUTS_EVENT, onChange);
29
+ window.removeEventListener("storage", onChange);
30
+ };
31
+ }
32
+ /**
33
+ * Whether the person uses shortcuts with single letters ("G then a letter",
34
+ * "/"), saved in this browser; on unless turned off. Ctrl and ⌘ shortcuts
35
+ * always work. Turning them off is what WCAG 2.1.4 asks for.
36
+ */
37
+ export function useShortcutsPreference() {
38
+ const enabled = useSyncExternalStore(subscribeShortcuts, readShortcuts, () => true);
39
+ const setEnabled = (next) => {
40
+ try {
41
+ if (next)
42
+ window.localStorage.removeItem(SHORTCUTS_KEY);
43
+ else
44
+ window.localStorage.setItem(SHORTCUTS_KEY, "off");
45
+ }
46
+ catch {
47
+ unsavedShortcuts = next;
48
+ }
49
+ window.dispatchEvent(new Event(SHORTCUTS_EVENT));
50
+ };
51
+ return [enabled, setEnabled];
52
+ }
53
+ const noSubscription = () => () => { };
54
+ /**
55
+ * The key for Search (K) and the sidebar (B): "⌘" on Apple devices, else
56
+ * Ctrl ("Strg" on German keyboards). The overview (Ctrl+/) is Ctrl everywhere.
57
+ */
58
+ export function useModifierLabel() {
59
+ const t = useT(frameMessages);
60
+ const apple = useSyncExternalStore(noSubscription, () => /Mac|iPhone|iPad/.test(navigator.userAgent), () => false);
61
+ return { mod: apple ? "⌘" : t("ctrl"), ctrl: t("ctrl") };
62
+ }
63
+ /**
64
+ * "G then a letter" opens a page, Ctrl+/ the overview. Single letters only
65
+ * when `enabled`; nothing while typing or while a dialog or menu is open.
66
+ * Returns the pages while G waits for its second key.
67
+ */
68
+ export function useGoShortcuts(props) {
69
+ const { targets, enabled, navigate, onOverview } = props;
70
+ const [waiting, setWaiting] = useState(false);
71
+ const waitingRef = useRef(false);
72
+ const timer = useRef(undefined);
73
+ // The latest values, so the listener stays the same while the app re-renders.
74
+ const latest = useRef({ targets, navigate, onOverview });
75
+ useEffect(() => {
76
+ latest.current = { targets, navigate, onOverview };
77
+ });
78
+ useEffect(() => {
79
+ const stop = () => {
80
+ window.clearTimeout(timer.current);
81
+ if (!waitingRef.current)
82
+ return;
83
+ waitingRef.current = false;
84
+ setWaiting(false);
85
+ };
86
+ function onKeyDown(event) {
87
+ if (isOverviewKey(event)) {
88
+ if (layerOpen())
89
+ return;
90
+ event.preventDefault();
91
+ stop();
92
+ latest.current.onOverview();
93
+ return;
94
+ }
95
+ if (!enabled)
96
+ return;
97
+ const letter = letterOf(event);
98
+ if (letter === null || layerOpen()) {
99
+ stop();
100
+ return;
101
+ }
102
+ if (waitingRef.current) {
103
+ stop();
104
+ const target = latest.current.targets.find((candidate) => candidate.key === letter);
105
+ if (target) {
106
+ event.preventDefault();
107
+ latest.current.navigate(target.href);
108
+ }
109
+ return;
110
+ }
111
+ if (letter === "g" && latest.current.targets.length > 0) {
112
+ event.preventDefault();
113
+ waitingRef.current = true;
114
+ setWaiting(true);
115
+ timer.current = window.setTimeout(stop, GO_WAIT_MS);
116
+ }
117
+ }
118
+ document.addEventListener("keydown", onKeyDown);
119
+ return () => {
120
+ document.removeEventListener("keydown", onKeyDown);
121
+ stop();
122
+ };
123
+ }, [enabled]);
124
+ return waiting ? targets : null;
125
+ }
126
+ /** Shown while G waits for its second key: which letter opens which page. */
127
+ export function GoHint({ targets }) {
128
+ const t = useT(frameMessages);
129
+ return (_jsxs("div", { role: "status", "data-slot": "go-hint", className: "bg-popover text-popover-foreground fixed bottom-6 left-1/2 z-50 hidden max-w-[calc(100vw-2rem)] -translate-x-1/2 flex-wrap items-center gap-x-3 gap-y-1 rounded-lg border px-3 py-2 text-sm shadow-lg md:flex", children: [_jsx("span", { className: "text-muted-foreground", children: t("goTo") }), targets.map((target) => (_jsxs("span", { className: "flex items-center gap-1.5", children: [_jsx(Kbd, { children: target.key.toUpperCase() }), target.label] }, target.key)))] }));
130
+ }
131
+ function Row(props) {
132
+ return (_jsxs("li", { className: cn("flex items-center justify-between gap-4 py-1.5 text-sm", props.off && "opacity-50"), children: [_jsx("span", { children: props.label }), _jsx(KbdGroup, { children: props.keys.map((key, index) => (_jsxs("span", { className: "flex items-center gap-1", children: [index > 0 && props.then ? (_jsx("span", { className: "text-muted-foreground text-xs", children: props.then })) : null, _jsx(Kbd, { children: key })] }, `${key}-${index}`))) })] }));
133
+ }
134
+ /** The overview of every shortcut: Ctrl+/, the user menu or the search open it. */
135
+ export function ShortcutsDialog(props) {
136
+ const t = useT(frameMessages);
137
+ const { mod, ctrl } = useModifierLabel();
138
+ const off = !props.enabled;
139
+ const returnFocus = useReturnFocus(props.open);
140
+ return (_jsx(Dialog, { open: props.open, onOpenChange: props.onOpenChange, children: _jsxs(DialogContent, { className: "sm:max-w-md", onCloseAutoFocus: returnFocus, children: [_jsxs(DialogHeader, { children: [_jsx(DialogTitle, { children: t("shortcuts") }), _jsx(DialogDescription, { children: t("shortcutsDescription") })] }), _jsxs("section", { className: "flex flex-col gap-1", children: [_jsx("h3", { className: "text-muted-foreground text-xs font-medium tracking-wide uppercase", children: t("general") }), _jsxs("ul", { className: "divide-y", children: [_jsx(Row, { label: t("search"), keys: [mod, "K"] }), _jsx(Row, { label: t("search"), keys: ["/"], off: off }), _jsx(Row, { label: t("toggleSidebar"), keys: [mod, "B"] }), _jsx(Row, { label: t("showShortcuts"), keys: [ctrl, "/"] })] })] }), props.targets.length > 0 ? (_jsxs("section", { className: "flex flex-col gap-1", children: [_jsx("h3", { className: "text-muted-foreground text-xs font-medium tracking-wide uppercase", children: t("goTo") }), _jsx("ul", { className: "divide-y", children: props.targets.map((target) => (_jsx(Row, { label: target.label, keys: ["G", target.key.toUpperCase()], then: t("then"), off: off }, target.key))) })] })) : null, _jsxs("p", { className: "text-muted-foreground text-xs", children: [t(off ? "singleKeysOff" : "singleKeysOn"), " ", props.settings] })] }) }));
141
+ }
@@ -3,3 +3,15 @@
3
3
  * every Meldgrove app's theme switch sets), else "light". Follows changes.
4
4
  */
5
5
  export declare function useDocumentTheme(): "light" | "dark";
6
+ /** What a person chose under "Appearance": light, dark, or as the device is set. */
7
+ export type ThemePreference = "light" | "dark" | "system";
8
+ export declare const THEME_PREFERENCES: readonly ThemePreference[];
9
+ /** Sets the class `dark` on <html> for a preference; "system" follows the device. */
10
+ export declare function applyThemePreference(preference: ThemePreference): void;
11
+ /**
12
+ * The person's appearance, saved in this browser, with the class `dark` on
13
+ * <html> kept in step (also when the device switches while "system" is
14
+ * chosen). For apps without a theme store of their own; an app that saves
15
+ * the choice with the person passes its own value to the frame instead.
16
+ */
17
+ export declare function useThemePreference(): [ThemePreference, (preference: ThemePreference) => void];
package/dist/lib/theme.js CHANGED
@@ -1,5 +1,5 @@
1
1
  "use client";
2
- import { useSyncExternalStore } from "react";
2
+ import { useCallback, useEffect, useSyncExternalStore } from "react";
3
3
  /**
4
4
  * The theme a page shows: "dark" while <html> has the class `dark` (the class
5
5
  * every Meldgrove app's theme switch sets), else "light". Follows changes.
@@ -15,3 +15,62 @@ function subscribe(onChange) {
15
15
  observer.observe(document.documentElement, { attributes: true, attributeFilter: ["class"] });
16
16
  return () => observer.disconnect();
17
17
  }
18
+ export const THEME_PREFERENCES = ["light", "dark", "system"];
19
+ const THEME_KEY = "meldgrove.theme";
20
+ const THEME_EVENT = "meldgrove:theme";
21
+ const DARK_QUERY = "(prefers-color-scheme: dark)";
22
+ /** The choice when the browser refuses storage (some private windows): it lasts for this page. */
23
+ let unsavedTheme = "system";
24
+ function readThemePreference() {
25
+ try {
26
+ const stored = window.localStorage.getItem(THEME_KEY);
27
+ return stored === "light" || stored === "dark" ? stored : "system";
28
+ }
29
+ catch {
30
+ return unsavedTheme;
31
+ }
32
+ }
33
+ function subscribeThemePreference(onChange) {
34
+ window.addEventListener(THEME_EVENT, onChange);
35
+ window.addEventListener("storage", onChange);
36
+ return () => {
37
+ window.removeEventListener(THEME_EVENT, onChange);
38
+ window.removeEventListener("storage", onChange);
39
+ };
40
+ }
41
+ /** Sets the class `dark` on <html> for a preference; "system" follows the device. */
42
+ export function applyThemePreference(preference) {
43
+ const dark = preference === "dark" || (preference === "system" && window.matchMedia(DARK_QUERY).matches);
44
+ document.documentElement.classList.toggle("dark", dark);
45
+ }
46
+ /**
47
+ * The person's appearance, saved in this browser, with the class `dark` on
48
+ * <html> kept in step (also when the device switches while "system" is
49
+ * chosen). For apps without a theme store of their own; an app that saves
50
+ * the choice with the person passes its own value to the frame instead.
51
+ */
52
+ export function useThemePreference() {
53
+ const preference = useSyncExternalStore(subscribeThemePreference, readThemePreference, () => "system");
54
+ useEffect(() => {
55
+ applyThemePreference(preference);
56
+ if (preference !== "system")
57
+ return;
58
+ const media = window.matchMedia(DARK_QUERY);
59
+ const follow = () => applyThemePreference("system");
60
+ media.addEventListener("change", follow);
61
+ return () => media.removeEventListener("change", follow);
62
+ }, [preference]);
63
+ const setPreference = useCallback((next) => {
64
+ try {
65
+ if (next === "system")
66
+ window.localStorage.removeItem(THEME_KEY);
67
+ else
68
+ window.localStorage.setItem(THEME_KEY, next);
69
+ }
70
+ catch {
71
+ unsavedTheme = next;
72
+ }
73
+ window.dispatchEvent(new Event(THEME_EVENT));
74
+ }, []);
75
+ return [preference, setPreference];
76
+ }