@meldgrove/ui 0.1.1 → 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 +138 -2
- 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/styles.css +1 -0
- package/package.json +4 -2
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
|
+
}
|
package/dist/styles.css
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@meldgrove/ui",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"description": "The shared interface of the Meldgrove apps: theme, components and the update button.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"author": "Simon Schubert",
|
|
@@ -84,7 +84,9 @@
|
|
|
84
84
|
"test": "vitest run",
|
|
85
85
|
"test:visual": "playwright test",
|
|
86
86
|
"test:visual:update": "node scripts/visual-update.mjs",
|
|
87
|
-
"pack:check": "node scripts/pack-check.mjs"
|
|
87
|
+
"pack:check": "node scripts/pack-check.mjs",
|
|
88
|
+
"dev:templates": "vite --config demo/templates/vite.config.ts",
|
|
89
|
+
"build:templates": "node scripts/build-templates.mjs"
|
|
88
90
|
},
|
|
89
91
|
"peerDependencies": {
|
|
90
92
|
"cmdk": "^1.1.0",
|