shared-lib-web 0.0.2 → 0.0.3

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 (186) hide show
  1. package/README.md +381 -127
  2. package/dist/components/app-brand.d.ts.map +1 -1
  3. package/dist/components/app-brand.js +9 -5
  4. package/dist/components/app-brand.js.map +1 -1
  5. package/dist/components/coming-soon.d.ts.map +1 -1
  6. package/dist/components/coming-soon.js +3 -2
  7. package/dist/components/coming-soon.js.map +1 -1
  8. package/dist/components/language-switcher.d.ts +5 -1
  9. package/dist/components/language-switcher.d.ts.map +1 -1
  10. package/dist/components/language-switcher.js +20 -7
  11. package/dist/components/language-switcher.js.map +1 -1
  12. package/dist/components/settings-menu.d.ts +4 -0
  13. package/dist/components/settings-menu.d.ts.map +1 -1
  14. package/dist/components/settings-menu.js +15 -2
  15. package/dist/components/settings-menu.js.map +1 -1
  16. package/dist/context/use-ui.d.ts.map +1 -1
  17. package/dist/context/use-ui.js +4 -3
  18. package/dist/context/use-ui.js.map +1 -1
  19. package/dist/i18n/browser-language.d.ts +9 -0
  20. package/dist/i18n/browser-language.d.ts.map +1 -0
  21. package/dist/i18n/browser-language.js +89 -0
  22. package/dist/i18n/browser-language.js.map +1 -0
  23. package/dist/i18n/default-messages.d.ts.map +1 -1
  24. package/dist/i18n/default-messages.js +3 -0
  25. package/dist/i18n/default-messages.js.map +1 -1
  26. package/dist/i18n/locale-groups.d.ts +18 -0
  27. package/dist/i18n/locale-groups.d.ts.map +1 -0
  28. package/dist/i18n/locale-groups.js +74 -0
  29. package/dist/i18n/locale-groups.js.map +1 -0
  30. package/dist/i18n/locale-service.d.ts +22 -4
  31. package/dist/i18n/locale-service.d.ts.map +1 -1
  32. package/dist/i18n/locale-service.js +95 -27
  33. package/dist/i18n/locale-service.js.map +1 -1
  34. package/dist/i18n/locale-signal.d.ts +18 -0
  35. package/dist/i18n/locale-signal.d.ts.map +1 -0
  36. package/dist/i18n/locale-signal.js +142 -0
  37. package/dist/i18n/locale-signal.js.map +1 -0
  38. package/dist/i18n/translator.d.ts +8 -1
  39. package/dist/i18n/translator.d.ts.map +1 -1
  40. package/dist/i18n/translator.js +36 -12
  41. package/dist/i18n/translator.js.map +1 -1
  42. package/dist/index.d.ts +21 -10
  43. package/dist/index.d.ts.map +1 -1
  44. package/dist/index.js +19 -8
  45. package/dist/index.js.map +1 -1
  46. package/dist/interfaces/app-types.d.ts +6 -0
  47. package/dist/interfaces/app-types.d.ts.map +1 -1
  48. package/dist/interfaces/ui-config.d.ts +162 -15
  49. package/dist/interfaces/ui-config.d.ts.map +1 -1
  50. package/dist/interfaces/ui-context.d.ts +1 -1
  51. package/dist/interfaces/ui-context.d.ts.map +1 -1
  52. package/dist/layouts/auth-layout.js +1 -1
  53. package/dist/layouts/auth-layout.js.map +1 -1
  54. package/dist/providers/provider-effects.d.ts +2 -2
  55. package/dist/providers/provider-effects.d.ts.map +1 -1
  56. package/dist/providers/provider-effects.js +4 -1
  57. package/dist/providers/provider-effects.js.map +1 -1
  58. package/dist/providers/register-config.d.ts.map +1 -1
  59. package/dist/providers/register-config.js +2 -0
  60. package/dist/providers/register-config.js.map +1 -1
  61. package/dist/providers/shared-web-ui-provider.d.ts +7 -19
  62. package/dist/providers/shared-web-ui-provider.d.ts.map +1 -1
  63. package/dist/providers/shared-web-ui-provider.js +21 -22
  64. package/dist/providers/shared-web-ui-provider.js.map +1 -1
  65. package/dist/query/query-param.d.ts +4 -1
  66. package/dist/query/query-param.d.ts.map +1 -1
  67. package/dist/query/query-param.js +6 -3
  68. package/dist/query/query-param.js.map +1 -1
  69. package/dist/state/api-loader.d.ts +19 -0
  70. package/dist/state/api-loader.d.ts.map +1 -0
  71. package/dist/state/api-loader.js +187 -0
  72. package/dist/state/api-loader.js.map +1 -0
  73. package/dist/state/auth-service.d.ts +4 -0
  74. package/dist/state/auth-service.d.ts.map +1 -1
  75. package/dist/state/auth-service.js +47 -3
  76. package/dist/state/auth-service.js.map +1 -1
  77. package/dist/state/branch-service.d.ts +1 -1
  78. package/dist/state/branch-service.js +3 -3
  79. package/dist/state/branch-service.js.map +1 -1
  80. package/dist/state/company-service.d.ts +16 -0
  81. package/dist/state/company-service.d.ts.map +1 -0
  82. package/dist/state/company-service.js +97 -0
  83. package/dist/state/company-service.js.map +1 -0
  84. package/dist/state/language-service.d.ts +5 -2
  85. package/dist/state/language-service.d.ts.map +1 -1
  86. package/dist/state/language-service.js +12 -7
  87. package/dist/state/language-service.js.map +1 -1
  88. package/dist/state/layout-choice-service.d.ts +10 -0
  89. package/dist/state/layout-choice-service.d.ts.map +1 -0
  90. package/dist/state/layout-choice-service.js +34 -0
  91. package/dist/state/layout-choice-service.js.map +1 -0
  92. package/dist/state/menu-service.d.ts +1 -1
  93. package/dist/state/menu-service.js +2 -2
  94. package/dist/state/menu-service.js.map +1 -1
  95. package/dist/state/preference-sync.d.ts +1 -1
  96. package/dist/state/preference-sync.d.ts.map +1 -1
  97. package/dist/state/preference-sync.js +11 -3
  98. package/dist/state/preference-sync.js.map +1 -1
  99. package/dist/state/signals.d.ts +9 -1
  100. package/dist/state/signals.d.ts.map +1 -1
  101. package/dist/state/signals.js +28 -4
  102. package/dist/state/signals.js.map +1 -1
  103. package/dist/theme/custom-themes.d.ts +28 -0
  104. package/dist/theme/custom-themes.d.ts.map +1 -0
  105. package/dist/theme/custom-themes.js +218 -0
  106. package/dist/theme/custom-themes.js.map +1 -0
  107. package/dist/theme/dark-themes.d.ts +9 -1
  108. package/dist/theme/dark-themes.d.ts.map +1 -1
  109. package/dist/theme/dark-themes.js +74 -2
  110. package/dist/theme/dark-themes.js.map +1 -1
  111. package/dist/theme/logo.d.ts +6 -3
  112. package/dist/theme/logo.d.ts.map +1 -1
  113. package/dist/theme/logo.js +17 -3
  114. package/dist/theme/logo.js.map +1 -1
  115. package/dist/theme/theme-service.d.ts +14 -3
  116. package/dist/theme/theme-service.d.ts.map +1 -1
  117. package/dist/theme/theme-service.js +64 -10
  118. package/dist/theme/theme-service.js.map +1 -1
  119. package/dist/utils/app-http.d.ts +29 -1
  120. package/dist/utils/app-http.d.ts.map +1 -1
  121. package/dist/utils/app-http.js +113 -11
  122. package/dist/utils/app-http.js.map +1 -1
  123. package/dist/utils/content-util.d.ts +6 -0
  124. package/dist/utils/content-util.d.ts.map +1 -0
  125. package/dist/utils/content-util.js +45 -0
  126. package/dist/utils/content-util.js.map +1 -0
  127. package/dist/utils/error-util.d.ts +3 -0
  128. package/dist/utils/error-util.d.ts.map +1 -0
  129. package/dist/utils/error-util.js +58 -0
  130. package/dist/utils/error-util.js.map +1 -0
  131. package/dist/utils/language-util.d.ts +3 -3
  132. package/dist/utils/language-util.d.ts.map +1 -1
  133. package/dist/utils/language-util.js +7 -7
  134. package/dist/utils/language-util.js.map +1 -1
  135. package/dist/utils/menu-util.js +1 -1
  136. package/dist/utils/menu-util.js.map +1 -1
  137. package/dist/utils/signal-source.d.ts +4 -0
  138. package/dist/utils/signal-source.d.ts.map +1 -0
  139. package/dist/utils/signal-source.js +13 -0
  140. package/dist/utils/signal-source.js.map +1 -0
  141. package/dist/utils/storage-keys.d.ts +5 -0
  142. package/dist/utils/storage-keys.d.ts.map +1 -1
  143. package/dist/utils/storage-keys.js +5 -0
  144. package/dist/utils/storage-keys.js.map +1 -1
  145. package/package.json +3 -3
  146. package/src/components/app-brand.tsx +9 -5
  147. package/src/components/coming-soon.tsx +3 -2
  148. package/src/components/language-switcher.tsx +26 -7
  149. package/src/components/settings-menu.tsx +28 -2
  150. package/src/context/use-ui.ts +4 -3
  151. package/src/i18n/browser-language.ts +98 -0
  152. package/src/i18n/default-messages.ts +3 -0
  153. package/src/i18n/locale-groups.ts +87 -0
  154. package/src/i18n/locale-service.ts +99 -27
  155. package/src/i18n/locale-signal.ts +173 -0
  156. package/src/i18n/translator.ts +38 -12
  157. package/src/index.ts +71 -3
  158. package/src/interfaces/app-types.ts +6 -0
  159. package/src/interfaces/ui-config.ts +168 -15
  160. package/src/interfaces/ui-context.ts +1 -1
  161. package/src/layouts/auth-layout.tsx +1 -1
  162. package/src/providers/provider-effects.ts +5 -2
  163. package/src/providers/register-config.ts +2 -0
  164. package/src/providers/shared-web-ui-provider.tsx +26 -43
  165. package/src/query/query-param.ts +6 -2
  166. package/src/state/api-loader.ts +205 -0
  167. package/src/state/auth-service.ts +58 -3
  168. package/src/state/branch-service.ts +3 -3
  169. package/src/state/company-service.ts +107 -0
  170. package/src/state/language-service.ts +12 -7
  171. package/src/state/layout-choice-service.ts +40 -0
  172. package/src/state/menu-service.ts +2 -2
  173. package/src/state/preference-sync.ts +11 -3
  174. package/src/state/signals.ts +36 -4
  175. package/src/theme/custom-themes.ts +236 -0
  176. package/src/theme/dark-themes.ts +80 -3
  177. package/src/theme/logo.ts +19 -4
  178. package/src/theme/org-themes.json +70 -0
  179. package/src/theme/theme-service.ts +70 -10
  180. package/src/utils/app-http.ts +125 -11
  181. package/src/utils/content-util.ts +49 -0
  182. package/src/utils/error-util.ts +64 -0
  183. package/src/utils/language-util.ts +7 -7
  184. package/src/utils/menu-util.ts +1 -1
  185. package/src/utils/signal-source.ts +15 -0
  186. package/src/utils/storage-keys.ts +6 -0
@@ -1,15 +1,16 @@
1
1
  // language-service.ts
2
2
  // Languages that come from the backend at runtime, separate from config.ts (like the menu: nav).
3
3
  //
4
- // <SharedWebUiProvider config={config} lang={langResponse}> // null first, the reply later
4
+ // config.loadSupportLanguages: "/app-api/languages" // the provider calls it on start
5
5
  // setLanguages(langResponse); // or call it yourself
6
6
  //
7
7
  // langResponse is the backend reply { data: [...], meta } or only the list.
8
8
  // Texts load from config.loadLocale(code): locales/en-US.json, locales/ar-SA.json ...
9
9
  // When the reply arrives: the current language stays if it is in the list (and the user picked it),
10
- // otherwise the language marked isDefault (else config.defaultLanguage, else the first) is loaded.
10
+ // otherwise: the language marked isDefault, else config.defaultLanguage, else the browser / system language,
11
+ // else "en-US" (when listed), else the first one.
11
12
 
12
- import { applyLanguage, readDefaultLanguage, readIsLanguageChosen } from "../i18n/locale-service.ts";
13
+ import { applyLanguage, readBrowserLanguage, readFallbackLanguage, readIsLanguageChosen } from "../i18n/locale-service.ts";
13
14
  import type { LangSource } from "../interfaces/app-types.ts";
14
15
  import { findLanguageOption } from "../i18n/direction.ts";
15
16
  import { readDefaultLanguageCode, toLanguageOptions } from "../utils/language-util.ts";
@@ -28,7 +29,10 @@ export function isLanguageListed(languageCode: string | undefined): boolean {
28
29
  return Boolean(language);
29
30
  }
30
31
 
31
- /** The language to switch to after a new list: backend default, config default, or the first one. */
32
+ /**
33
+ * The language to switch to after a new list (when the user did not pick one): the backend isDefault,
34
+ * config.defaultLanguage, the browser / system language, "en-US", or the first one.
35
+ */
32
36
  export function readLanguageAfterUpdate(source: LangSource): string | undefined {
33
37
  const languages = languagesSignal.peek();
34
38
  const currentCode = localeSignal.peek();
@@ -39,9 +43,10 @@ export function readLanguageAfterUpdate(source: LangSource): string | undefined
39
43
  }
40
44
  const backendDefault = readDefaultLanguageCode(source);
41
45
  const config = uiConfigSignal.peek();
42
- const configDefault = readDefaultLanguage(config);
46
+ const browserLanguage = readBrowserLanguage();
47
+ const fallbackLanguage = readFallbackLanguage(config);
43
48
  const firstLanguage = languages[0];
44
- const candidates = [backendDefault, configDefault, firstLanguage?.code];
49
+ const candidates = [backendDefault, config?.defaultLanguage, browserLanguage, fallbackLanguage, firstLanguage?.code];
45
50
  for (const candidate of candidates) {
46
51
  const isListed = isLanguageListed(candidate);
47
52
  if (isListed) {
@@ -63,7 +68,7 @@ export function setLanguages(source: LangSource): void {
63
68
  }
64
69
 
65
70
  /**
66
- * Used by <SharedWebUiProvider lang={...}>. Applies the value only when it changed.
71
+ * Used by the config loaders (and tests). Applies the value only when it changed.
67
72
  * undefined / null -> keep config.languages (or no list yet); texts of config.defaultLanguage still load
68
73
  * reply / list -> show it (a new reply replaces the list)
69
74
  */
@@ -0,0 +1,40 @@
1
+ // layout-choice-service.ts
2
+ // Which app layout the user picked — for a shell with microfrontends, where the settings MFE offers the choice and
3
+ // the shell renders the layout:
4
+ // "app" AppLayout: header + menu (sidebar or mega menu)
5
+ // "aside" AsideLayout: menu only, no header, no mega menu
6
+ //
7
+ // changeLayoutChoice("aside"); // saves ORG-LAYOUT_CHOICE (localStorage) and fires PREFERENCE_EVENT
8
+ // layoutChoiceSignal.value // "app" | "aside" — every copy of the library on the page follows at once
9
+ //
10
+ // The provider reads the saved choice on mount; preference-sync.ts follows later changes (same tab and other tabs).
11
+ // An MFE has no layout of its own: it reads layoutChoiceSignal to know whether the shell shows a header.
12
+
13
+ import { signal } from "@preact/signals-react";
14
+ import { AppStorage } from "../utils/app-storage.ts";
15
+ import { LAYOUT_CHOICE } from "../utils/storage-keys.ts";
16
+
17
+ export type LayoutChoice = "app" | "aside";
18
+
19
+ /** The current layout choice ("app" until a saved one is read). */
20
+ export const layoutChoiceSignal = signal<LayoutChoice>("app");
21
+
22
+ /** "aside" stays "aside"; anything else (nothing saved, an old value) is "app". */
23
+ export function toLayoutChoice(value: string | null | undefined): LayoutChoice {
24
+ return value === "aside" ? "aside" : "app";
25
+ }
26
+
27
+ /** Reads the saved choice into layoutChoiceSignal (not saved again). */
28
+ export function applySavedLayoutChoice(): void {
29
+ const savedValue = AppStorage.getText(LAYOUT_CHOICE);
30
+ const choice = toLayoutChoice(savedValue);
31
+ if (choice !== layoutChoiceSignal.peek()) {
32
+ layoutChoiceSignal.value = choice;
33
+ }
34
+ }
35
+
36
+ /** Picks a layout: saved as LAYOUT_CHOICE; the shell and every MFE on the page follow at once. */
37
+ export function changeLayoutChoice(choice: LayoutChoice): void {
38
+ layoutChoiceSignal.value = choice;
39
+ AppStorage.setData(LAYOUT_CHOICE, choice);
40
+ }
@@ -1,7 +1,7 @@
1
1
  // menu-service.ts
2
2
  // Menus that come from the backend at runtime, separate from config.ts.
3
3
  //
4
- // <SharedWebUiProvider config={config} nav={menuResponse}> // reactive: a new reply replaces the menu
4
+ // config.loadMenuNavigation: "/app-api/menu-access" // the provider calls it after sign in
5
5
  // setMenu(menuResponse); // or call it yourself, for example after login
6
6
  // setMenuLoading(); // sidebar shows a skeleton until setMenu()
7
7
  //
@@ -44,7 +44,7 @@ export function clearMenu(): void {
44
44
  }
45
45
 
46
46
  /**
47
- * Used by <SharedWebUiProvider nav={...}>. Applies the value only when it changed.
47
+ * Used by the config loaders (and tests). Applies the value only when it changed.
48
48
  * undefined -> use config.navigation
49
49
  * null -> still loading: menu skeleton (typical first value before the API answers)
50
50
  * reply / list -> show it (a new reply object later replaces the menu)
@@ -1,5 +1,5 @@
1
1
  // preference-sync.ts
2
- // Keeps the theme, language and menu mode the same in every copy of the library on the page — for microfrontends,
2
+ // Keeps the theme, language, menu mode and layout choice the same in every copy of the library on the page — for microfrontends,
3
3
  // where each MFE bundles its own shared-lib-web — and in other tabs.
4
4
  //
5
5
  // AppStorage.setData(THEME | LANG, ...) -> fires window event PREFERENCE_EVENT (same tab, storage-keys.ts)
@@ -9,7 +9,8 @@
9
9
  import { applyLanguage } from "../i18n/locale-service.ts";
10
10
  import { isKnownTheme } from "../theme/theme-service.ts";
11
11
  import { AppStorage, readPrefixedKey } from "../utils/app-storage.ts";
12
- import { LANG, MENU_MODE, PREFERENCE_EVENT, THEME } from "../utils/storage-keys.ts";
12
+ import { LANG, LAYOUT_CHOICE, MENU_MODE, PREFERENCE_EVENT, THEME } from "../utils/storage-keys.ts";
13
+ import { applySavedLayoutChoice } from "./layout-choice-service.ts";
13
14
  import { localeSignal, navigationModeSignal, themeSignal, uiConfigSignal } from "./signals.ts";
14
15
 
15
16
  let isPreferenceSyncStarted = false;
@@ -45,7 +46,7 @@ export function applySavedMenuMode(): void {
45
46
  }
46
47
  }
47
48
 
48
- /** Applies the preference named by the key ("THEME", "LANG" or "MENU_MODE"). */
49
+ /** Applies the preference named by the key ("THEME", "LANG", "MENU_MODE" or "LAYOUT_CHOICE"). */
49
50
  export function applySavedPreference(key: string): void {
50
51
  if (key === THEME) {
51
52
  applySavedTheme();
@@ -56,6 +57,9 @@ export function applySavedPreference(key: string): void {
56
57
  if (key === MENU_MODE) {
57
58
  applySavedMenuMode();
58
59
  }
60
+ if (key === LAYOUT_CHOICE) {
61
+ applySavedLayoutChoice();
62
+ }
59
63
  }
60
64
 
61
65
  /** Same tab: another library copy (or this one) saved a preference. */
@@ -70,6 +74,7 @@ export function handleStorageEvent(event: StorageEvent): void {
70
74
  const themeKey = readPrefixedKey(THEME);
71
75
  const languageKey = readPrefixedKey(LANG);
72
76
  const menuModeKey = readPrefixedKey(MENU_MODE);
77
+ const layoutChoiceKey = readPrefixedKey(LAYOUT_CHOICE);
73
78
  if (event.key === themeKey) {
74
79
  applySavedTheme();
75
80
  }
@@ -79,6 +84,9 @@ export function handleStorageEvent(event: StorageEvent): void {
79
84
  if (event.key === menuModeKey) {
80
85
  applySavedMenuMode();
81
86
  }
87
+ if (event.key === layoutChoiceKey) {
88
+ applySavedLayoutChoice();
89
+ }
82
90
  }
83
91
 
84
92
  /** Starts listening once (called by SharedWebUiProvider). */
@@ -15,6 +15,9 @@ import { ConstValue } from "dff-util";
15
15
  import { toPagePath } from "../query/page-path.ts";
16
16
  import { resolveDirection } from "../i18n/direction.ts";
17
17
  import type {
18
+ AppType,
19
+ CompanyDetails,
20
+ CustomTheme,
18
21
  Direction,
19
22
  LanguageOption,
20
23
  LocaleMessages,
@@ -24,17 +27,26 @@ import type {
24
27
  UiConfig,
25
28
  } from "../interfaces/ui-config.ts";
26
29
  import type { ToastItem } from "../interfaces/toast.ts";
27
- import { DEFAULT_DARK_THEMES } from "../theme/dark-themes.ts";
30
+ import { buildDarkThemeNames } from "../theme/dark-themes.ts";
28
31
 
29
32
  /** The config given to <SharedWebUiProvider>. null until the provider renders. */
30
33
  export const uiConfigSignal = signal<UiConfig | null>(null);
31
34
 
35
+ /** What the app is: <SharedWebUiProvider type="spa" | "shell" | "mfe"> (default "spa"). */
36
+ export const appTypeSignal = signal<AppType>("spa");
37
+
32
38
  /** Current language code. */
33
39
  export const localeSignal = signal<string>(ConstValue.EMPTY);
34
40
 
35
41
  /** Current DaisyUI theme. */
36
42
  export const themeSignal = signal<Theme>(ConstValue.LIGHT);
37
43
 
44
+ /** The custom themes (config.customThemes or setCustomThemes), listed first in the theme switcher. */
45
+ export const customThemesSignal = signal<CustomTheme[]>([]);
46
+
47
+ /** The company details (config.loadCompanyDetails or setCompanyDetails); null = not here yet (config values are used). */
48
+ export const companyDetailsSignal = signal<CompanyDetails | null>(null);
49
+
38
50
  /** Messages of the current language. */
39
51
  export const translationSignal = signal<LocaleMessages>({});
40
52
 
@@ -83,10 +95,29 @@ export function toPageFromPath(pathname: string): string {
83
95
  return pathname.slice(0, end);
84
96
  }
85
97
 
98
+ /** true when pages live in the path: layout.pageMode "path", or no pageMode and the app type "shell". */
99
+ export function readIsPathPageMode(config: UiConfig | null): boolean {
100
+ const pageMode = config?.layout?.pageMode;
101
+ if (pageMode) {
102
+ return pageMode === "path";
103
+ }
104
+ return appTypeSignal.value === "shell";
105
+ }
106
+
107
+ /** config.homePath (not used by an "mfe": the shell owns the home page), else "/". */
108
+ function readHomePage(config: UiConfig | null): string {
109
+ const isMfe = appTypeSignal.value === "mfe";
110
+ if (isMfe) {
111
+ return DEFAULT_HOME_PAGE;
112
+ }
113
+ return config?.homePath ?? DEFAULT_HOME_PAGE;
114
+ }
115
+
86
116
  function readCurrentPage(): string {
87
117
  const config = uiConfigSignal.value;
88
- const homePage = config?.homePath ?? DEFAULT_HOME_PAGE;
89
- if (config?.layout?.pageMode === "path") {
118
+ const homePage = readHomePage(config);
119
+ const isPathMode = readIsPathPageMode(config);
120
+ if (isPathMode) {
90
121
  const pathname = pathnameSignal.value;
91
122
  const pathPage = toPageFromPath(pathname);
92
123
  return pathPage || homePage;
@@ -174,7 +205,8 @@ export const directionSignal = computed(readDirection);
174
205
  function readIsDarkTheme(): boolean {
175
206
  const config = uiConfigSignal.value;
176
207
  const theme = themeSignal.value;
177
- const darkThemes = config?.darkThemes ?? DEFAULT_DARK_THEMES;
208
+ const customThemes = customThemesSignal.value;
209
+ const darkThemes = buildDarkThemeNames(config?.darkThemes, customThemes);
178
210
  const isDark = darkThemes.includes(theme);
179
211
  return isDark;
180
212
  }
@@ -0,0 +1,236 @@
1
+ // custom-themes.ts
2
+ // Custom DaisyUI themes from plain data (a JSON file or an API reply) — no CSS file, no rebuild.
3
+ // The themes are an object keyed by theme name ("name" inside is optional, the key is the name):
4
+ //
5
+ // { "org-light": { "isDark": false, "name": "org-light", "variables": { "color-primary": "oklch(55.8% 0.154 249.1)", ... } },
6
+ // "org-dark": { "isDark": true, "name": "org-dark", "variables": { ... } } }
7
+ //
8
+ // A plain list [{ "name": "org-light", ... }] is accepted too.
9
+ //
10
+ // setCustomThemes(list) writes ONE <style> element:
11
+ // [data-theme="org-light"] { color-scheme: light; --color-primary: oklch(55.8% 0.154 249.1); ... }
12
+ // DaisyUI components read these CSS variables, so a custom theme works like a built-in one, and the theme switcher
13
+ // lists it first. Pass them in config.customThemes, load them lazily with config.loadThemes (like loadLocale),
14
+ // or set them any time later with setCustomThemes(json).
15
+ // The built-in DaisyUI themes still come from your CSS: @plugin "daisyui" { themes: all; }.
16
+
17
+ import type { CustomTheme, CustomThemeMap, CustomThemes, CustomThemesModule, UiConfig } from "../interfaces/ui-config.ts";
18
+ import { customThemesSignal, uiConfigSignal } from "../state/signals.ts";
19
+ import { isSignalSource } from "../utils/signal-source.ts";
20
+ import { applySavedThemeWhenKnown, leaveUnknownTheme } from "./theme-service.ts";
21
+
22
+ const STYLE_ELEMENT_ID = "shared-lib-web-custom-themes";
23
+ /** Theme names: lowercase letters, digits and "-" (they go into a CSS selector). */
24
+ const SAFE_NAME = /^[a-z0-9][a-z0-9-]*$/;
25
+ /** Variable names, with or without the leading "--". */
26
+ const SAFE_VARIABLE = /^(--)?[a-z0-9][a-z0-9-]*$/i;
27
+ /** Characters that could end the CSS rule (values come from JSON, maybe from a server). */
28
+ const UNSAFE_VALUE = /[;{}<>"\\]/;
29
+ const VARIABLE_PREFIX = "--";
30
+
31
+ /** "color-primary" -> "--color-primary" ("--color-primary" stays). */
32
+ function toVariableName(key: string): string {
33
+ const hasPrefix = key.startsWith(VARIABLE_PREFIX);
34
+ return hasPrefix ? key : `${VARIABLE_PREFIX}${key}`;
35
+ }
36
+
37
+ /** The CSS lines of the variables; unsafe names or values are skipped. */
38
+ function buildVariableLines(variables: Record<string, string>): string[] {
39
+ const lines: string[] = [];
40
+ const entries = Object.entries(variables);
41
+ for (const [key, value] of entries) {
42
+ const valueText = String(value);
43
+ const isSafeKey = SAFE_VARIABLE.test(key);
44
+ const isUnsafeValue = UNSAFE_VALUE.test(valueText);
45
+ if (!isSafeKey || isUnsafeValue) {
46
+ continue;
47
+ }
48
+ const variableName = toVariableName(key);
49
+ lines.push(` ${variableName}: ${valueText};`);
50
+ }
51
+ return lines;
52
+ }
53
+
54
+ /** true when the theme has a usable name. */
55
+ export function isValidCustomTheme(theme: CustomTheme): boolean {
56
+ const hasName = typeof theme?.name === "string";
57
+ return hasName && SAFE_NAME.test(theme.name);
58
+ }
59
+
60
+ /** The CSS rule of one custom theme: [data-theme="name"] { color-scheme: ...; --color-primary: ...; } */
61
+ export function buildCustomThemeCss(theme: CustomTheme): string {
62
+ const isValid = isValidCustomTheme(theme);
63
+ if (!isValid) {
64
+ return "";
65
+ }
66
+ const colorScheme = theme.isDark ? "dark" : "light";
67
+ const variableLines = buildVariableLines(theme.variables ?? {});
68
+ const bodyLines = [` color-scheme: ${colorScheme};`, ...variableLines];
69
+ const bodyText = bodyLines.join("\n");
70
+ return `[data-theme="${theme.name}"] {\n${bodyText}\n}`;
71
+ }
72
+
73
+ /** Writes the CSS into the one <style> element of the custom themes (created on first use). */
74
+ function writeThemeStyle(cssText: string): void {
75
+ if (typeof document === "undefined") {
76
+ return;
77
+ }
78
+ let styleElement = document.getElementById(STYLE_ELEMENT_ID);
79
+ if (!styleElement) {
80
+ styleElement = document.createElement("style");
81
+ styleElement.id = STYLE_ELEMENT_ID;
82
+ document.head.append(styleElement);
83
+ }
84
+ styleElement.textContent = cssText;
85
+ }
86
+
87
+ /** true for a plain object (not null, not a list). */
88
+ function isPlainObject(value: unknown): value is Record<string, unknown> {
89
+ const isObject = typeof value === "object" && value !== null;
90
+ return isObject && !Array.isArray(value);
91
+ }
92
+
93
+ /** The themes of an object keyed by theme name; an entry without "name" gets its key as name. */
94
+ function readThemeMapEntries(themeMap: CustomThemeMap): CustomTheme[] {
95
+ const themeList: CustomTheme[] = [];
96
+ const entries = Object.entries(themeMap);
97
+ for (const [key, entry] of entries) {
98
+ if (!isPlainObject(entry)) {
99
+ continue;
100
+ }
101
+ const hasName = typeof entry.name === "string";
102
+ const name = hasName ? entry.name : key;
103
+ themeList.push({ ...entry, name } as CustomTheme);
104
+ }
105
+ return themeList;
106
+ }
107
+
108
+ /**
109
+ * The custom themes as a list: an object keyed by theme name ({ "org-light": {...}, "org-dark": {...} }) is turned
110
+ * into a list (in key order); a list stays as it is; anything else is an empty list.
111
+ */
112
+ export function toCustomThemeList(themes?: CustomThemes | null): CustomTheme[] {
113
+ if (Array.isArray(themes)) {
114
+ return themes;
115
+ }
116
+ if (isPlainObject(themes)) {
117
+ return readThemeMapEntries(themes as CustomThemeMap);
118
+ }
119
+ return [];
120
+ }
121
+
122
+ /**
123
+ * Sets the custom themes (replaces the earlier ones): writes their CSS, adds them to the theme switcher and,
124
+ * when the saved theme is one of them, switches to it. Accepts the parsed JSON as it is — the object keyed by theme
125
+ * name, or a list; invalid entries are skipped.
126
+ */
127
+ export function setCustomThemes(themes?: CustomThemes | null): void {
128
+ const validThemes: CustomTheme[] = [];
129
+ const givenThemes = toCustomThemeList(themes);
130
+ for (const theme of givenThemes) {
131
+ const isValid = isValidCustomTheme(theme);
132
+ if (isValid) {
133
+ validThemes.push(theme);
134
+ }
135
+ }
136
+ const cssParts = validThemes.map(buildCustomThemeCss);
137
+ const cssText = cssParts.join("\n\n");
138
+ writeThemeStyle(cssText);
139
+ customThemesSignal.value = validThemes;
140
+ applySavedThemeWhenKnown();
141
+ }
142
+
143
+ /** true when the value is a JSON module ({ default: themes }), not a theme map with a theme called "default". */
144
+ function isThemesJsonModule(themesModule: CustomThemesModule): themesModule is { default: CustomThemes } {
145
+ if (!isPlainObject(themesModule)) {
146
+ return false;
147
+ }
148
+ const defaultValue = themesModule.default;
149
+ if (Array.isArray(defaultValue)) {
150
+ return true;
151
+ }
152
+ return isPlainObject(defaultValue) && !("variables" in defaultValue);
153
+ }
154
+
155
+ /** The themes inside a themes module as a list: the object keyed by name or the list, or the default export of a JSON module. */
156
+ export function unwrapThemesModule(themesModule: CustomThemesModule): CustomTheme[] {
157
+ const isJsonModule = isThemesJsonModule(themesModule);
158
+ if (isJsonModule) {
159
+ return toCustomThemeList(themesModule.default);
160
+ }
161
+ return toCustomThemeList(themesModule as CustomThemes);
162
+ }
163
+
164
+ /** baseThemes + extraThemes; a theme with the same name in extraThemes replaces the one in baseThemes. */
165
+ export function mergeCustomThemes(baseThemes: CustomTheme[], extraThemes: CustomTheme[]): CustomTheme[] {
166
+ const mergedThemes: CustomTheme[] = [];
167
+ const extraNames: string[] = [];
168
+ for (const extraTheme of extraThemes) {
169
+ extraNames.push(extraTheme?.name);
170
+ }
171
+ for (const baseTheme of baseThemes) {
172
+ const isReplaced = extraNames.includes(baseTheme.name);
173
+ if (!isReplaced) {
174
+ mergedThemes.push(baseTheme);
175
+ }
176
+ }
177
+ mergedThemes.push(...extraThemes);
178
+ return mergedThemes;
179
+ }
180
+
181
+ /** Configs whose loadThemes already ran (StrictMode mounts twice). */
182
+ const loadedConfigs = new WeakSet<UiConfig>();
183
+
184
+ /**
185
+ * Runs config.loadThemes once per config and adds the result to config.customThemes.
186
+ * When it fails, or the saved theme is not in the result, the default theme is used.
187
+ */
188
+ export async function loadConfigThemes(config: UiConfig): Promise<void> {
189
+ const loadThemes = config.loadThemes;
190
+ const isLoaded = loadedConfigs.has(config);
191
+ if (typeof loadThemes !== "function" || isLoaded) {
192
+ return;
193
+ }
194
+ loadedConfigs.add(config);
195
+ try {
196
+ const themesModule = await loadThemes();
197
+ const loadedThemes = unwrapThemesModule(themesModule);
198
+ const isConfigStillActive = uiConfigSignal.peek() === config;
199
+ if (isConfigStillActive) {
200
+ const baseThemes = toCustomThemeList(config.customThemes);
201
+ const mergedThemes = mergeCustomThemes(baseThemes, loadedThemes);
202
+ setCustomThemes(mergedThemes);
203
+ }
204
+ } catch (error) {
205
+ console.error("shared-lib-web: could not load the custom themes (config.loadThemes).", error);
206
+ }
207
+ leaveUnknownTheme();
208
+ }
209
+
210
+ /** One value of a loadThemes signal: null = not here yet; a list (or JSON module) replaces the loaded themes. */
211
+ function applyThemesFromSignal(config: UiConfig, themesModule: CustomThemesModule | null): void {
212
+ const isConfigActive = uiConfigSignal.peek() === config;
213
+ if (!themesModule || !isConfigActive) {
214
+ return;
215
+ }
216
+ const loadedThemes = unwrapThemesModule(themesModule);
217
+ const baseThemes = toCustomThemeList(config.customThemes);
218
+ const mergedThemes = mergeCustomThemes(baseThemes, loadedThemes);
219
+ setCustomThemes(mergedThemes);
220
+ leaveUnknownTheme();
221
+ }
222
+
223
+ /** Follows config.loadThemes when it is a signal. Returns the stop function (nothing to stop otherwise). */
224
+ export function followConfigThemes(config: UiConfig): () => void {
225
+ const themesSource = config.loadThemes;
226
+ if (!isSignalSource(themesSource)) {
227
+ return stopNothing;
228
+ }
229
+ const applyThemes = applyThemesFromSignal.bind(null, config);
230
+ return themesSource.subscribe(applyThemes as (value: unknown) => void);
231
+ }
232
+
233
+ /** Nothing to clean up. */
234
+ function stopNothing(): void {
235
+ return;
236
+ }
@@ -1,8 +1,51 @@
1
1
  // dark-themes.ts
2
- // DaisyUI 5 themes with "color-scheme: dark". Used for dark logos and the markdown editor colours.
3
- // An app can replace this list with config.darkThemes.
2
+ // The DaisyUI 5 theme names, and which of them are dark ("color-scheme: dark": dark logos, dark markdown editor).
3
+ // config.themes defaults to DAISYUI_THEMES; config.darkThemes replaces DEFAULT_DARK_THEMES.
4
+ // Custom themes (config.customThemes / setCustomThemes) are added to both lists by their own isDark flag.
4
5
 
5
- export const DEFAULT_DARK_THEMES = [
6
+ import type { CustomTheme, Theme } from "../interfaces/ui-config.ts";
7
+
8
+ /** All 35 DaisyUI 5 themes (enable them in CSS with @plugin "daisyui" { themes: all; }). */
9
+ export const DAISYUI_THEMES: Theme[] = [
10
+ "light",
11
+ "dark",
12
+ "cupcake",
13
+ "bumblebee",
14
+ "emerald",
15
+ "corporate",
16
+ "synthwave",
17
+ "retro",
18
+ "cyberpunk",
19
+ "valentine",
20
+ "halloween",
21
+ "garden",
22
+ "forest",
23
+ "aqua",
24
+ "lofi",
25
+ "pastel",
26
+ "fantasy",
27
+ "wireframe",
28
+ "black",
29
+ "luxury",
30
+ "dracula",
31
+ "cmyk",
32
+ "autumn",
33
+ "business",
34
+ "acid",
35
+ "lemonade",
36
+ "night",
37
+ "coffee",
38
+ "winter",
39
+ "dim",
40
+ "nord",
41
+ "sunset",
42
+ "caramellatte",
43
+ "abyss",
44
+ "silk",
45
+ ];
46
+
47
+ /** The dark DaisyUI 5 themes. */
48
+ export const DEFAULT_DARK_THEMES: Theme[] = [
6
49
  "dark",
7
50
  "aqua",
8
51
  "synthwave",
@@ -18,3 +61,37 @@ export const DEFAULT_DARK_THEMES = [
18
61
  "sunset",
19
62
  "abyss",
20
63
  ];
64
+
65
+ /** The dark theme names: the dark custom themes + (darkThemes, else the DaisyUI dark themes). */
66
+ export function buildDarkThemeNames(darkThemes: Theme[] | undefined, customThemes: CustomTheme[]): Theme[] {
67
+ const names: Theme[] = [];
68
+ for (const customTheme of customThemes) {
69
+ if (customTheme.isDark) {
70
+ names.push(customTheme.name);
71
+ }
72
+ }
73
+ const baseNames = darkThemes ?? DEFAULT_DARK_THEMES;
74
+ for (const name of baseNames) {
75
+ const isAdded = names.includes(name);
76
+ if (!isAdded) {
77
+ names.push(name);
78
+ }
79
+ }
80
+ return names;
81
+ }
82
+
83
+ /** The theme names of the switcher: the custom themes first, then (themes, else all DaisyUI themes). */
84
+ export function buildThemeNames(themes: Theme[] | undefined, customThemes: CustomTheme[]): Theme[] {
85
+ const names: Theme[] = [];
86
+ for (const customTheme of customThemes) {
87
+ names.push(customTheme.name);
88
+ }
89
+ const baseNames = themes ?? DAISYUI_THEMES;
90
+ for (const name of baseNames) {
91
+ const isAdded = names.includes(name);
92
+ if (!isAdded) {
93
+ names.push(name);
94
+ }
95
+ }
96
+ return names;
97
+ }
package/src/theme/logo.ts CHANGED
@@ -1,10 +1,25 @@
1
1
  // logo.ts
2
- // Picks the logo for the current theme.
2
+ // Picks the logo for the current theme. Company details (loadCompanyDetails / setCompanyDetails) win over the config.
3
3
 
4
- import type { UiConfig } from "../interfaces/ui-config.ts";
4
+ import type { CompanyDetails, UiConfig } from "../interfaces/ui-config.ts";
5
5
 
6
- /** Returns logoDarkUrl on dark themes (when set), else logoUrl. */
7
- export function readLogoUrl(config: UiConfig, isDarkTheme: boolean): string | undefined {
6
+ /** The company logo for the theme: logoDarkUrl on dark themes (when set), else logoUrl. */
7
+ function readCompanyLogoUrl(details: CompanyDetails | null | undefined, isDarkTheme: boolean): string | undefined {
8
+ if (isDarkTheme && details?.logoDarkUrl) {
9
+ return details.logoDarkUrl;
10
+ }
11
+ return details?.logoUrl;
12
+ }
13
+
14
+ /**
15
+ * The logo URL. Dark themes: company logoDarkUrl, company logoUrl, config.logoDarkUrl, config.logoUrl.
16
+ * Light themes: company logoUrl, config.logoUrl.
17
+ */
18
+ export function readLogoUrl(config: UiConfig, isDarkTheme: boolean, details?: CompanyDetails | null): string | undefined {
19
+ const companyLogoUrl = readCompanyLogoUrl(details, isDarkTheme);
20
+ if (companyLogoUrl) {
21
+ return companyLogoUrl;
22
+ }
8
23
  if (isDarkTheme && config.logoDarkUrl) {
9
24
  return config.logoDarkUrl;
10
25
  }
@@ -0,0 +1,70 @@
1
+ {
2
+ "org-dark": {
3
+ "isDark": true,
4
+ "name": "org-dark",
5
+ "variables": {
6
+ "border": "1px",
7
+ "color-accent": "oklch(70.4% 0.04 256.8)",
8
+ "color-accent-content": "oklch(12.9% 0.042 264.7)",
9
+ "color-base-100": "oklch(25.5% 0.04 264.5)",
10
+ "color-base-200": "oklch(20.8% 0.042 265.8)",
11
+ "color-base-300": "oklch(32% 0.04 262)",
12
+ "color-base-content": "oklch(92.9% 0.013 255.5)",
13
+ "color-error": "oklch(70.4% 0.191 22.2)",
14
+ "color-error-content": "oklch(25.8% 0.092 26)",
15
+ "color-info": "oklch(78.9% 0.154 211.5)",
16
+ "color-info-content": "oklch(30.2% 0.056 229.7)",
17
+ "color-neutral": "oklch(37.2% 0.044 257.3)",
18
+ "color-neutral-content": "oklch(96.8% 0.007 247.9)",
19
+ "color-primary": "oklch(74.6% 0.16 232.7)",
20
+ "color-primary-content": "oklch(29.3% 0.066 243.2)",
21
+ "color-secondary": "oklch(82.8% 0.189 84.4)",
22
+ "color-secondary-content": "oklch(27.9% 0.077 45.6)",
23
+ "color-success": "oklch(79.2% 0.209 151.7)",
24
+ "color-success-content": "oklch(26.6% 0.065 152.9)",
25
+ "color-warning": "oklch(75% 0.183 55.9)",
26
+ "color-warning-content": "oklch(26.6% 0.079 36.3)",
27
+ "depth": "1",
28
+ "noise": "0",
29
+ "radius-box": "0.75rem",
30
+ "radius-field": "0.5rem",
31
+ "radius-selector": "1rem",
32
+ "size-field": "0.25rem",
33
+ "size-selector": "0.25rem"
34
+ }
35
+ },
36
+ "org-light": {
37
+ "isDark": false,
38
+ "name": "org-light",
39
+ "variables": {
40
+ "border": "1px",
41
+ "color-accent": "oklch(55.4% 0.046 257.4)",
42
+ "color-accent-content": "oklch(98.4% 0.003 247.9)",
43
+ "color-base-100": "oklch(100% 0 0)",
44
+ "color-base-200": "oklch(96.8% 0.007 247.9)",
45
+ "color-base-300": "oklch(92.9% 0.013 255.5)",
46
+ "color-base-content": "oklch(20.8% 0.042 265.8)",
47
+ "color-error": "oklch(55% 0.23 27.3)",
48
+ "color-error-content": "oklch(97.1% 0.013 17.4)",
49
+ "color-info": "oklch(71.5% 0.143 215.2)",
50
+ "color-info-content": "oklch(30.2% 0.056 229.7)",
51
+ "color-neutral": "oklch(27.9% 0.041 260)",
52
+ "color-neutral-content": "oklch(98.4% 0.003 247.9)",
53
+ "color-primary": "oklch(55.8% 0.154 249.1)",
54
+ "color-primary-content": "oklch(98.5% 0.01 240)",
55
+ "color-secondary": "oklch(76.9% 0.188 70.1)",
56
+ "color-secondary-content": "oklch(27.9% 0.077 45.6)",
57
+ "color-success": "oklch(52.7% 0.154 150.1)",
58
+ "color-success-content": "oklch(98.2% 0.018 155.8)",
59
+ "color-warning": "oklch(75% 0.183 55.9)",
60
+ "color-warning-content": "oklch(26.6% 0.079 36.3)",
61
+ "depth": "1",
62
+ "noise": "0",
63
+ "radius-box": "0.75rem",
64
+ "radius-field": "0.5rem",
65
+ "radius-selector": "1rem",
66
+ "size-field": "0.25rem",
67
+ "size-selector": "0.25rem"
68
+ }
69
+ }
70
+ }