@plexusone/shell 0.2.3 → 0.2.4
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/dist/index.d.mts +32 -7
- package/dist/index.d.ts +32 -7
- package/dist/index.js +139 -87
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +134 -85
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -4
package/dist/index.mjs
CHANGED
|
@@ -1,36 +1,151 @@
|
|
|
1
|
+
// src/theme.ts
|
|
2
|
+
import { useCallback, useEffect, useState } from "react";
|
|
3
|
+
var THEME_STORAGE_KEY = "systemforge:theme";
|
|
4
|
+
var THEME_CHANGE_EVENT = "systemforge:theme-change";
|
|
5
|
+
var darkQuery = "(prefers-color-scheme: dark)";
|
|
6
|
+
function isTheme(v) {
|
|
7
|
+
return v === "light" || v === "dark" || v === "system";
|
|
8
|
+
}
|
|
9
|
+
function readStoredTheme() {
|
|
10
|
+
try {
|
|
11
|
+
const v = window.localStorage.getItem(THEME_STORAGE_KEY);
|
|
12
|
+
return isTheme(v) ? v : "system";
|
|
13
|
+
} catch {
|
|
14
|
+
return "system";
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
function storeTheme(theme) {
|
|
18
|
+
try {
|
|
19
|
+
window.localStorage.setItem(THEME_STORAGE_KEY, theme);
|
|
20
|
+
} catch {
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
function systemPrefersDark() {
|
|
24
|
+
return typeof window.matchMedia === "function" && window.matchMedia(darkQuery).matches;
|
|
25
|
+
}
|
|
26
|
+
function resolveTheme(theme, prefersDark = systemPrefersDark()) {
|
|
27
|
+
if (theme === "system") return prefersDark ? "dark" : "light";
|
|
28
|
+
return theme;
|
|
29
|
+
}
|
|
30
|
+
function applyTheme(theme, root = document.documentElement) {
|
|
31
|
+
const resolved = resolveTheme(theme);
|
|
32
|
+
root.classList.toggle("dark", resolved === "dark");
|
|
33
|
+
root.style.colorScheme = resolved;
|
|
34
|
+
}
|
|
35
|
+
function applyStoredTheme() {
|
|
36
|
+
adoptCarriedPreferences();
|
|
37
|
+
applyTheme(readStoredTheme());
|
|
38
|
+
}
|
|
39
|
+
function nextTheme(theme) {
|
|
40
|
+
return theme === "light" ? "dark" : theme === "dark" ? "system" : "light";
|
|
41
|
+
}
|
|
42
|
+
function useTheme() {
|
|
43
|
+
const [theme, setThemeState] = useState(
|
|
44
|
+
() => typeof window === "undefined" ? "system" : readStoredTheme()
|
|
45
|
+
);
|
|
46
|
+
useEffect(() => {
|
|
47
|
+
applyTheme(theme);
|
|
48
|
+
if (theme !== "system" || typeof window.matchMedia !== "function") return void 0;
|
|
49
|
+
const mq = window.matchMedia(darkQuery);
|
|
50
|
+
const onChange = () => applyTheme("system");
|
|
51
|
+
mq.addEventListener("change", onChange);
|
|
52
|
+
return () => mq.removeEventListener("change", onChange);
|
|
53
|
+
}, [theme]);
|
|
54
|
+
useEffect(() => {
|
|
55
|
+
const onThemeChange = (e) => {
|
|
56
|
+
const next = e.detail;
|
|
57
|
+
if (isTheme(next)) setThemeState(next);
|
|
58
|
+
};
|
|
59
|
+
window.addEventListener(THEME_CHANGE_EVENT, onThemeChange);
|
|
60
|
+
return () => window.removeEventListener(THEME_CHANGE_EVENT, onThemeChange);
|
|
61
|
+
}, []);
|
|
62
|
+
const setTheme = useCallback((next) => {
|
|
63
|
+
storeTheme(next);
|
|
64
|
+
setThemeState(next);
|
|
65
|
+
window.dispatchEvent(new CustomEvent(THEME_CHANGE_EVENT, { detail: next }));
|
|
66
|
+
}, []);
|
|
67
|
+
return [theme, setTheme];
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// src/preferences.ts
|
|
71
|
+
var COLLAPSED_KEY = "systemforge:sidebar-collapsed";
|
|
72
|
+
var SIDEBAR_PARAM = "sf-sidebar";
|
|
73
|
+
var THEME_PARAM = "sf-theme";
|
|
74
|
+
function read(key) {
|
|
75
|
+
try {
|
|
76
|
+
return window.localStorage.getItem(key);
|
|
77
|
+
} catch {
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
function write(key, value) {
|
|
82
|
+
try {
|
|
83
|
+
window.localStorage.setItem(key, value);
|
|
84
|
+
} catch {
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
function carryPreferences(url, prefs = {}) {
|
|
88
|
+
if (typeof window === "undefined" || url.includes("#")) return url;
|
|
89
|
+
const p = new URLSearchParams();
|
|
90
|
+
const collapsed = prefs.sidebarCollapsed ?? read(COLLAPSED_KEY) === "true";
|
|
91
|
+
p.set(SIDEBAR_PARAM, collapsed ? "collapsed" : "expanded");
|
|
92
|
+
const theme = prefs.theme ?? read(THEME_STORAGE_KEY);
|
|
93
|
+
if (theme === "light" || theme === "dark" || theme === "system") p.set(THEME_PARAM, theme);
|
|
94
|
+
return `${url}#${p.toString()}`;
|
|
95
|
+
}
|
|
96
|
+
function adoptCarriedPreferences() {
|
|
97
|
+
if (typeof window === "undefined" || !window.location.hash) return;
|
|
98
|
+
const p = new URLSearchParams(window.location.hash.slice(1));
|
|
99
|
+
const sidebar = p.get(SIDEBAR_PARAM);
|
|
100
|
+
const theme = p.get(THEME_PARAM);
|
|
101
|
+
if (sidebar === null && theme === null) return;
|
|
102
|
+
if (sidebar === "collapsed" || sidebar === "expanded")
|
|
103
|
+
write(COLLAPSED_KEY, String(sidebar === "collapsed"));
|
|
104
|
+
if (theme === "light" || theme === "dark" || theme === "system") write(THEME_STORAGE_KEY, theme);
|
|
105
|
+
p.delete(SIDEBAR_PARAM);
|
|
106
|
+
p.delete(THEME_PARAM);
|
|
107
|
+
const rest = p.toString();
|
|
108
|
+
const { pathname, search } = window.location;
|
|
109
|
+
window.history.replaceState(
|
|
110
|
+
window.history.state,
|
|
111
|
+
"",
|
|
112
|
+
`${pathname}${search}${rest ? `#${rest}` : ""}`
|
|
113
|
+
);
|
|
114
|
+
}
|
|
115
|
+
|
|
1
116
|
// src/ShellContext.tsx
|
|
2
117
|
import {
|
|
3
118
|
createContext,
|
|
4
119
|
useContext,
|
|
5
|
-
useState,
|
|
6
|
-
useCallback,
|
|
120
|
+
useState as useState2,
|
|
121
|
+
useCallback as useCallback2,
|
|
7
122
|
useMemo,
|
|
8
|
-
useEffect
|
|
123
|
+
useEffect as useEffect2
|
|
9
124
|
} from "react";
|
|
10
125
|
import { jsx } from "react/jsx-runtime";
|
|
11
126
|
var ShellContext = createContext(null);
|
|
12
|
-
var COLLAPSED_KEY = "systemforge:sidebar-collapsed";
|
|
13
127
|
function ShellProvider({
|
|
14
128
|
children,
|
|
15
129
|
defaultCollapsed = false,
|
|
16
130
|
onNavigate,
|
|
17
131
|
onCollapseChange
|
|
18
132
|
}) {
|
|
19
|
-
const [sidebarCollapsed, setSidebarCollapsedState] =
|
|
133
|
+
const [sidebarCollapsed, setSidebarCollapsedState] = useState2(() => {
|
|
20
134
|
if (typeof window !== "undefined") {
|
|
135
|
+
adoptCarriedPreferences();
|
|
21
136
|
const stored = localStorage.getItem(COLLAPSED_KEY);
|
|
22
137
|
return stored ? stored === "true" : defaultCollapsed;
|
|
23
138
|
}
|
|
24
139
|
return defaultCollapsed;
|
|
25
140
|
});
|
|
26
|
-
const [sidebarVisible, setSidebarVisible] =
|
|
27
|
-
const [currentPath, setCurrentPath] =
|
|
141
|
+
const [sidebarVisible, setSidebarVisible] = useState2(false);
|
|
142
|
+
const [currentPath, setCurrentPath] = useState2(() => {
|
|
28
143
|
if (typeof window !== "undefined") {
|
|
29
144
|
return window.location.pathname;
|
|
30
145
|
}
|
|
31
146
|
return "/";
|
|
32
147
|
});
|
|
33
|
-
const toggleSidebar =
|
|
148
|
+
const toggleSidebar = useCallback2(() => {
|
|
34
149
|
setSidebarCollapsedState((prev) => {
|
|
35
150
|
const next = !prev;
|
|
36
151
|
if (typeof window !== "undefined") {
|
|
@@ -40,7 +155,7 @@ function ShellProvider({
|
|
|
40
155
|
return next;
|
|
41
156
|
});
|
|
42
157
|
}, [onCollapseChange]);
|
|
43
|
-
const setSidebarCollapsed =
|
|
158
|
+
const setSidebarCollapsed = useCallback2(
|
|
44
159
|
(collapsed) => {
|
|
45
160
|
setSidebarCollapsedState(collapsed);
|
|
46
161
|
if (typeof window !== "undefined") {
|
|
@@ -50,10 +165,10 @@ function ShellProvider({
|
|
|
50
165
|
},
|
|
51
166
|
[onCollapseChange]
|
|
52
167
|
);
|
|
53
|
-
const toggleSidebarVisible =
|
|
168
|
+
const toggleSidebarVisible = useCallback2(() => {
|
|
54
169
|
setSidebarVisible((prev) => !prev);
|
|
55
170
|
}, []);
|
|
56
|
-
const navigate =
|
|
171
|
+
const navigate = useCallback2(
|
|
57
172
|
(href) => {
|
|
58
173
|
if (onNavigate) {
|
|
59
174
|
onNavigate(href);
|
|
@@ -65,7 +180,7 @@ function ShellProvider({
|
|
|
65
180
|
},
|
|
66
181
|
[onNavigate]
|
|
67
182
|
);
|
|
68
|
-
|
|
183
|
+
useEffect2(() => {
|
|
69
184
|
const handlePopState = () => {
|
|
70
185
|
setCurrentPath(window.location.pathname);
|
|
71
186
|
};
|
|
@@ -388,9 +503,8 @@ import { cn as cn4 } from "@plexusone/ui";
|
|
|
388
503
|
|
|
389
504
|
// src/switcher.ts
|
|
390
505
|
function switcherApps(apps, self, membership) {
|
|
391
|
-
const
|
|
392
|
-
|
|
393
|
-
return [fromCatalog ? { ...fromCatalog, ...self } : self, ...others];
|
|
506
|
+
const listed = apps.filter((a) => a.id === self.id || Boolean(membership?.applications[a.id])).map((a) => a.id === self.id ? { ...a, ...self } : a);
|
|
507
|
+
return listed.some((a) => a.id === self.id) ? listed : [self, ...listed];
|
|
394
508
|
}
|
|
395
509
|
|
|
396
510
|
// src/ProductSwitcher.tsx
|
|
@@ -408,6 +522,7 @@ function ProductSwitcher({
|
|
|
408
522
|
const membership = session ? membershipFor(session, organizationId) : void 0;
|
|
409
523
|
const entries = switcherApps(catalog, self, membership);
|
|
410
524
|
const follow = (url) => (e) => {
|
|
525
|
+
if (!onNavigate) e.currentTarget.href = carryPreferences(url);
|
|
411
526
|
if (!onNavigate || e.defaultPrevented || e.button !== 0 || e.metaKey || e.ctrlKey || e.shiftKey) {
|
|
412
527
|
return;
|
|
413
528
|
}
|
|
@@ -428,6 +543,7 @@ function ProductSwitcher({
|
|
|
428
543
|
{
|
|
429
544
|
href: app.url,
|
|
430
545
|
onClick: follow(app.url),
|
|
546
|
+
onAuxClick: follow(app.url),
|
|
431
547
|
className: "px-3 py-1.5 rounded-md text-sm text-muted-foreground hover:bg-accent hover:text-foreground",
|
|
432
548
|
children: app.name
|
|
433
549
|
},
|
|
@@ -550,76 +666,6 @@ function ProfileMenu({
|
|
|
550
666
|
|
|
551
667
|
// src/ThemeToggle.tsx
|
|
552
668
|
import { Button as Button2, MonitorIcon, MoonIcon, SunIcon } from "@plexusone/ui";
|
|
553
|
-
|
|
554
|
-
// src/theme.ts
|
|
555
|
-
import { useCallback as useCallback2, useEffect as useEffect2, useState as useState2 } from "react";
|
|
556
|
-
var THEME_STORAGE_KEY = "systemforge:theme";
|
|
557
|
-
var THEME_CHANGE_EVENT = "systemforge:theme-change";
|
|
558
|
-
var darkQuery = "(prefers-color-scheme: dark)";
|
|
559
|
-
function isTheme(v) {
|
|
560
|
-
return v === "light" || v === "dark" || v === "system";
|
|
561
|
-
}
|
|
562
|
-
function readStoredTheme() {
|
|
563
|
-
try {
|
|
564
|
-
const v = window.localStorage.getItem(THEME_STORAGE_KEY);
|
|
565
|
-
return isTheme(v) ? v : "system";
|
|
566
|
-
} catch {
|
|
567
|
-
return "system";
|
|
568
|
-
}
|
|
569
|
-
}
|
|
570
|
-
function storeTheme(theme) {
|
|
571
|
-
try {
|
|
572
|
-
window.localStorage.setItem(THEME_STORAGE_KEY, theme);
|
|
573
|
-
} catch {
|
|
574
|
-
}
|
|
575
|
-
}
|
|
576
|
-
function systemPrefersDark() {
|
|
577
|
-
return typeof window.matchMedia === "function" && window.matchMedia(darkQuery).matches;
|
|
578
|
-
}
|
|
579
|
-
function resolveTheme(theme, prefersDark = systemPrefersDark()) {
|
|
580
|
-
if (theme === "system") return prefersDark ? "dark" : "light";
|
|
581
|
-
return theme;
|
|
582
|
-
}
|
|
583
|
-
function applyTheme(theme, root = document.documentElement) {
|
|
584
|
-
const resolved = resolveTheme(theme);
|
|
585
|
-
root.classList.toggle("dark", resolved === "dark");
|
|
586
|
-
root.style.colorScheme = resolved;
|
|
587
|
-
}
|
|
588
|
-
function applyStoredTheme() {
|
|
589
|
-
applyTheme(readStoredTheme());
|
|
590
|
-
}
|
|
591
|
-
function nextTheme(theme) {
|
|
592
|
-
return theme === "light" ? "dark" : theme === "dark" ? "system" : "light";
|
|
593
|
-
}
|
|
594
|
-
function useTheme() {
|
|
595
|
-
const [theme, setThemeState] = useState2(
|
|
596
|
-
() => typeof window === "undefined" ? "system" : readStoredTheme()
|
|
597
|
-
);
|
|
598
|
-
useEffect2(() => {
|
|
599
|
-
applyTheme(theme);
|
|
600
|
-
if (theme !== "system" || typeof window.matchMedia !== "function") return void 0;
|
|
601
|
-
const mq = window.matchMedia(darkQuery);
|
|
602
|
-
const onChange = () => applyTheme("system");
|
|
603
|
-
mq.addEventListener("change", onChange);
|
|
604
|
-
return () => mq.removeEventListener("change", onChange);
|
|
605
|
-
}, [theme]);
|
|
606
|
-
useEffect2(() => {
|
|
607
|
-
const onThemeChange = (e) => {
|
|
608
|
-
const next = e.detail;
|
|
609
|
-
if (isTheme(next)) setThemeState(next);
|
|
610
|
-
};
|
|
611
|
-
window.addEventListener(THEME_CHANGE_EVENT, onThemeChange);
|
|
612
|
-
return () => window.removeEventListener(THEME_CHANGE_EVENT, onThemeChange);
|
|
613
|
-
}, []);
|
|
614
|
-
const setTheme = useCallback2((next) => {
|
|
615
|
-
storeTheme(next);
|
|
616
|
-
setThemeState(next);
|
|
617
|
-
window.dispatchEvent(new CustomEvent(THEME_CHANGE_EVENT, { detail: next }));
|
|
618
|
-
}, []);
|
|
619
|
-
return [theme, setTheme];
|
|
620
|
-
}
|
|
621
|
-
|
|
622
|
-
// src/ThemeToggle.tsx
|
|
623
669
|
import { jsx as jsx7 } from "react/jsx-runtime";
|
|
624
670
|
var labels = {
|
|
625
671
|
light: "Light theme",
|
|
@@ -942,6 +988,7 @@ function AppShell(props) {
|
|
|
942
988
|
export {
|
|
943
989
|
AppShell,
|
|
944
990
|
Breadcrumbs,
|
|
991
|
+
COLLAPSED_KEY,
|
|
945
992
|
Navbar,
|
|
946
993
|
OrgSwitcher,
|
|
947
994
|
OrganizationSwitcher,
|
|
@@ -953,8 +1000,10 @@ export {
|
|
|
953
1000
|
THEME_STORAGE_KEY,
|
|
954
1001
|
ThemeToggle,
|
|
955
1002
|
UserMenu,
|
|
1003
|
+
adoptCarriedPreferences,
|
|
956
1004
|
applyStoredTheme,
|
|
957
1005
|
applyTheme,
|
|
1006
|
+
carryPreferences,
|
|
958
1007
|
initials,
|
|
959
1008
|
nextTheme,
|
|
960
1009
|
readStoredTheme,
|