@exmg/exm-theme 1.1.35 → 1.1.37-alpha.31

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 (25) hide show
  1. package/.rollup.cache/root/repo/packages/exm-theme/dist/config.d.ts +2 -0
  2. package/.rollup.cache/root/repo/packages/exm-theme/dist/config.js +3 -0
  3. package/.rollup.cache/root/repo/packages/exm-theme/dist/index.d.ts +28 -0
  4. package/.rollup.cache/root/repo/packages/exm-theme/dist/index.js +70 -0
  5. package/.rollup.cache/root/repo/packages/exm-theme/dist/material-color-helpers.d.ts +48 -0
  6. package/.rollup.cache/root/repo/packages/exm-theme/dist/material-color-helpers.js +134 -0
  7. package/.rollup.cache/root/repo/packages/exm-theme/dist/storage.d.ts +3 -0
  8. package/.rollup.cache/root/repo/packages/exm-theme/dist/storage.js +12 -0
  9. package/.rollup.cache/root/repo/packages/exm-theme/dist/theme-mixin.d.ts +8 -0
  10. package/.rollup.cache/root/repo/packages/exm-theme/dist/theme-mixin.js +21 -0
  11. package/.rollup.cache/root/repo/packages/exm-theme/dist/theme-signals.d.ts +2 -0
  12. package/.rollup.cache/root/repo/packages/exm-theme/dist/theme-signals.js +3 -0
  13. package/.rollup.cache/root/repo/packages/exm-theme/dist/theme.d.ts +75 -0
  14. package/.rollup.cache/root/repo/packages/exm-theme/dist/theme.js +136 -0
  15. package/.rollup.cache/root/repo/packages/exm-theme/dist/types.d.ts +32 -0
  16. package/.rollup.cache/root/repo/packages/exm-theme/dist/types.js +26 -0
  17. package/dist/config.js +5 -3
  18. package/dist/index.js +11 -9
  19. package/dist/material-color-helpers.js +12 -9
  20. package/dist/storage.js +3 -1
  21. package/dist/theme-mixin.js +6 -5
  22. package/dist/theme-signals.js +5 -2
  23. package/dist/theme.js +16 -13
  24. package/dist/types.js +5 -3
  25. package/package.json +2 -2
@@ -0,0 +1,2 @@
1
+ export declare const STORAGE_KEY_THEME = "exm-theme";
2
+ export declare const DEFAULT_THEME_COLOR = "#3284FF";
@@ -0,0 +1,3 @@
1
+ export const STORAGE_KEY_THEME = 'exm-theme';
2
+ export const DEFAULT_THEME_COLOR = '#3284FF';
3
+ //# sourceMappingURL=config.js.map
@@ -0,0 +1,28 @@
1
+ import { changeColor, changeColorAndMode, changeColorMode, getCurrentMode, getCurrentSeedColor, getCurrentThemeString, getLastSavedAutoColorMode, isModeDark } from './theme.js';
2
+ import { ThemeChangeColorEvent, ThemeChangeDarkModeEvent, ColorMode } from './types.js';
3
+ import { hctFromHex, hexFromHct } from './material-color-helpers.js';
4
+ export declare const initTheme: (config?: {
5
+ themeColor?: string;
6
+ colorMode?: ColorMode;
7
+ }) => void;
8
+ declare global {
9
+ interface HTMLElementEventMap {
10
+ 'theme-change-color': ThemeChangeColorEvent;
11
+ 'theme-change-mode': ThemeChangeDarkModeEvent;
12
+ }
13
+ }
14
+ export { ThemeChangeColorEvent, ThemeChangeDarkModeEvent, ColorMode } from './types.js';
15
+ export declare const ThemeUtils: {
16
+ changeColor: typeof changeColor;
17
+ changeColorAndMode: typeof changeColorAndMode;
18
+ changeColorMode: typeof changeColorMode;
19
+ getCurrentMode: typeof getCurrentMode;
20
+ getCurrentSeedColor: typeof getCurrentSeedColor;
21
+ getCurrentThemeString: typeof getCurrentThemeString;
22
+ getLastSavedAutoColorMode: typeof getLastSavedAutoColorMode;
23
+ isModeDark: typeof isModeDark;
24
+ hctFromHex: typeof hctFromHex;
25
+ hexFromHct: typeof hexFromHct;
26
+ };
27
+ export { ThemeMixin } from './theme-mixin.js';
28
+ export { themeCurrentMode } from './theme-signals.js';
@@ -0,0 +1,70 @@
1
+ import { DEFAULT_THEME_COLOR } from './config.js';
2
+ import { applyTheme } from './material-color-helpers.js';
3
+ import { loadfromStorage } from './storage.js';
4
+ import { changeColor, changeColorAndMode, changeColorMode, getCurrentMode, getCurrentSeedColor, getCurrentThemeString, getLastSavedAutoColorMode, isModeDark, } from './theme.js';
5
+ import { hctFromHex, hexFromHct } from './material-color-helpers.js';
6
+ /**
7
+ * Determines whether to update the theme on page navigation if the mode is
8
+ * 'auto'.
9
+ *
10
+ * This is necessary in the edge case where the user has set color mode to
11
+ * 'auto', and the system mode is A. They navigate away from the catalog, and
12
+ * over time the system mode changes to B. When they navigate back to the
13
+ * catalog, the mode may be 'auto', but color theme with mode A is saved instead
14
+ * of B.
15
+ */
16
+ function determinePageNavigationAutoMode() {
17
+ if (getCurrentMode() !== 'auto') {
18
+ return;
19
+ }
20
+ const actualColorMode = isModeDark('auto', false) ? 'dark' : 'light';
21
+ const lastSavedAutoColorMode = getLastSavedAutoColorMode();
22
+ if (actualColorMode !== lastSavedAutoColorMode) {
23
+ // Recalculate auto mode with the same theme color.
24
+ changeColorMode('auto');
25
+ }
26
+ }
27
+ function setupListeners() {
28
+ document.body.addEventListener('theme-change-color', (event) => {
29
+ changeColor(event.color);
30
+ });
31
+ document.body.addEventListener('theme-change-mode', (event) => {
32
+ changeColorMode(event.mode);
33
+ });
34
+ window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', () => {
35
+ if (getCurrentMode() !== 'auto') {
36
+ return;
37
+ }
38
+ changeColor(getCurrentSeedColor());
39
+ });
40
+ }
41
+ // Initializes the theme by applying the theme string from localStorage.
42
+ export const initTheme = (config) => {
43
+ const { themeColor, colorMode } = config || {};
44
+ const theme = loadfromStorage();
45
+ // Applies the theme string to the document if available.
46
+ if (theme) {
47
+ applyTheme(document, theme);
48
+ }
49
+ setupListeners();
50
+ if (!getCurrentThemeString()) {
51
+ changeColorAndMode(themeColor || DEFAULT_THEME_COLOR, colorMode || 'auto');
52
+ }
53
+ determinePageNavigationAutoMode();
54
+ };
55
+ export { ThemeChangeColorEvent, ThemeChangeDarkModeEvent } from './types.js';
56
+ export const ThemeUtils = {
57
+ changeColor,
58
+ changeColorAndMode,
59
+ changeColorMode,
60
+ getCurrentMode,
61
+ getCurrentSeedColor,
62
+ getCurrentThemeString,
63
+ getLastSavedAutoColorMode,
64
+ isModeDark,
65
+ hctFromHex,
66
+ hexFromHct,
67
+ };
68
+ export { ThemeMixin } from './theme-mixin.js';
69
+ export { themeCurrentMode } from './theme-signals.js';
70
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,48 @@
1
+ /**
2
+ * @license
3
+ * Copyright 2023 Google LLC
4
+ * SPDX-License-Identifier: Apache-2.0
5
+ */
6
+ import { Hct } from '@material/material-color-utilities';
7
+ import type { Theme } from './types.js';
8
+ export declare const applyTheme: (doc: DocumentOrShadowRoot, theme: string) => void;
9
+ /**
10
+ * Converts a hex value to a HCT tuple.
11
+ *
12
+ * @param value A stringified hex color e.g. '#C01075'
13
+ * @return Material Color Utilities HCT color tuple.
14
+ */
15
+ export declare function hctFromHex(value: string): Hct;
16
+ /**
17
+ * Converts a hue chroma and tone to a hex color value clamped in the hex
18
+ * colorspace.
19
+ *
20
+ * @param hue The hue of the color of value [0,360]
21
+ * @param chroma The chroma of the color of value [0,150]
22
+ * @param tone The tone of the color of value [0,100]
23
+ * @return A clamped, stringified hex color value representing the HCT values.
24
+ */
25
+ export declare function hexFromHct(hue: number, chroma: number, tone: number): string;
26
+ /**
27
+ * Generates a theme object mapping of kebab-system-color-token to stringified
28
+ * sRGB hex value in the Material SchemeContent color scheme given a single
29
+ * color.
30
+ *
31
+ * @param color A stringified hex color e.g. '#C01075'
32
+ * @param isDark Whether or not to generate a dark mode theme.
33
+ * @return A theme object that maps the sys color token to its value (not a
34
+ * custom property).
35
+ */
36
+ export declare function themeFromSourceColor(color: string, isDark: boolean): Theme;
37
+ /**
38
+ * Generates a stylesheet string of custom properties from the given theme, and
39
+ * applies the styles to the given document or shadow root, and caches the value
40
+ * in memory and localstorage given an optional ssName.
41
+ *
42
+ * @param doc Document or ShadowRoot to apply theme.
43
+ * @param theme A theme object that maps the sys color token to its value
44
+ * (output of themeFromSourceColor).
45
+ * @param ssName Optional global identifier of the constructable stylesheet and
46
+ * used to generate the localstorage name.
47
+ */
48
+ export declare function applyMaterialTheme(doc: DocumentOrShadowRoot, theme: Theme): void;
@@ -0,0 +1,134 @@
1
+ /**
2
+ * @license
3
+ * Copyright 2023 Google LLC
4
+ * SPDX-License-Identifier: Apache-2.0
5
+ */
6
+ import { argbFromHex, Hct, hexFromArgb, MaterialDynamicColors, SchemeContent, } from '@material/material-color-utilities';
7
+ import { STORAGE_KEY_THEME } from './config.js';
8
+ import { saveToStorage } from './storage.js';
9
+ // If supported by the browser, sets the theme color in the URL bar.
10
+ const setUrlBarColor = (themeString) => {
11
+ var _a, _b;
12
+ const surfaceContainer = (_a = themeString.match(/--md-sys-color-surface-container:(.+?);/)) === null || _a === void 0 ? void 0 : _a[1];
13
+ if (surfaceContainer) {
14
+ (_b = document.querySelector('meta[name="theme-color"]')) === null || _b === void 0 ? void 0 : _b.setAttribute('content', surfaceContainer);
15
+ }
16
+ };
17
+ // eslint-disable-next-line
18
+ export const applyTheme = (doc, theme) => {
19
+ // eslint-disable-next-line
20
+ let sheet = globalThis[STORAGE_KEY_THEME];
21
+ if (!sheet) {
22
+ sheet = new CSSStyleSheet();
23
+ // eslint-disable-next-line
24
+ globalThis[STORAGE_KEY_THEME] = sheet;
25
+ doc.adoptedStyleSheets.push(sheet);
26
+ }
27
+ setUrlBarColor(theme);
28
+ sheet.replaceSync(theme);
29
+ saveToStorage(theme);
30
+ };
31
+ /**
32
+ * A Mapping of color token name to MCU HCT color function generator.
33
+ */
34
+ const materialColors = {
35
+ background: MaterialDynamicColors.background,
36
+ 'on-background': MaterialDynamicColors.onBackground,
37
+ surface: MaterialDynamicColors.surface,
38
+ 'surface-dim': MaterialDynamicColors.surfaceDim,
39
+ 'surface-bright': MaterialDynamicColors.surfaceBright,
40
+ 'surface-container-lowest': MaterialDynamicColors.surfaceContainerLowest,
41
+ 'surface-container-low': MaterialDynamicColors.surfaceContainerLow,
42
+ 'surface-container': MaterialDynamicColors.surfaceContainer,
43
+ 'surface-container-high': MaterialDynamicColors.surfaceContainerHigh,
44
+ 'surface-container-highest': MaterialDynamicColors.surfaceContainerHighest,
45
+ 'on-surface': MaterialDynamicColors.onSurface,
46
+ 'surface-variant': MaterialDynamicColors.surfaceVariant,
47
+ 'on-surface-variant': MaterialDynamicColors.onSurfaceVariant,
48
+ 'inverse-surface': MaterialDynamicColors.inverseSurface,
49
+ 'inverse-on-surface': MaterialDynamicColors.inverseOnSurface,
50
+ outline: MaterialDynamicColors.outline,
51
+ 'outline-variant': MaterialDynamicColors.outlineVariant,
52
+ shadow: MaterialDynamicColors.shadow,
53
+ scrim: MaterialDynamicColors.scrim,
54
+ 'surface-tint': MaterialDynamicColors.surfaceTint,
55
+ primary: MaterialDynamicColors.primary,
56
+ 'on-primary': MaterialDynamicColors.onPrimary,
57
+ 'primary-container': MaterialDynamicColors.primaryContainer,
58
+ 'on-primary-container': MaterialDynamicColors.onPrimaryContainer,
59
+ 'inverse-primary': MaterialDynamicColors.inversePrimary,
60
+ secondary: MaterialDynamicColors.secondary,
61
+ 'on-secondary': MaterialDynamicColors.onSecondary,
62
+ 'secondary-container': MaterialDynamicColors.secondaryContainer,
63
+ 'on-secondary-container': MaterialDynamicColors.onSecondaryContainer,
64
+ tertiary: MaterialDynamicColors.tertiary,
65
+ 'on-tertiary': MaterialDynamicColors.onTertiary,
66
+ 'tertiary-container': MaterialDynamicColors.tertiaryContainer,
67
+ 'on-tertiary-container': MaterialDynamicColors.onTertiaryContainer,
68
+ error: MaterialDynamicColors.error,
69
+ 'on-error': MaterialDynamicColors.onError,
70
+ 'error-container': MaterialDynamicColors.errorContainer,
71
+ 'on-error-container': MaterialDynamicColors.onErrorContainer,
72
+ };
73
+ /**
74
+ * Converts a hex value to a HCT tuple.
75
+ *
76
+ * @param value A stringified hex color e.g. '#C01075'
77
+ * @return Material Color Utilities HCT color tuple.
78
+ */
79
+ export function hctFromHex(value) {
80
+ return Hct.fromInt(argbFromHex(value));
81
+ }
82
+ /**
83
+ * Converts a hue chroma and tone to a hex color value clamped in the hex
84
+ * colorspace.
85
+ *
86
+ * @param hue The hue of the color of value [0,360]
87
+ * @param chroma The chroma of the color of value [0,150]
88
+ * @param tone The tone of the color of value [0,100]
89
+ * @return A clamped, stringified hex color value representing the HCT values.
90
+ */
91
+ export function hexFromHct(hue, chroma, tone) {
92
+ const hct = Hct.from(hue, chroma, tone);
93
+ const value = hct.toInt();
94
+ return hexFromArgb(value);
95
+ }
96
+ /**
97
+ * Generates a theme object mapping of kebab-system-color-token to stringified
98
+ * sRGB hex value in the Material SchemeContent color scheme given a single
99
+ * color.
100
+ *
101
+ * @param color A stringified hex color e.g. '#C01075'
102
+ * @param isDark Whether or not to generate a dark mode theme.
103
+ * @return A theme object that maps the sys color token to its value (not a
104
+ * custom property).
105
+ */
106
+ export function themeFromSourceColor(color, isDark) {
107
+ const scheme = new SchemeContent(Hct.fromInt(argbFromHex(color)), isDark, 0);
108
+ const theme = {};
109
+ for (const [key, value] of Object.entries(materialColors)) {
110
+ theme[key] = hexFromArgb(value.getArgb(scheme));
111
+ }
112
+ return theme;
113
+ }
114
+ /**
115
+ * Generates a stylesheet string of custom properties from the given theme, and
116
+ * applies the styles to the given document or shadow root, and caches the value
117
+ * in memory and localstorage given an optional ssName.
118
+ *
119
+ * @param doc Document or ShadowRoot to apply theme.
120
+ * @param theme A theme object that maps the sys color token to its value
121
+ * (output of themeFromSourceColor).
122
+ * @param ssName Optional global identifier of the constructable stylesheet and
123
+ * used to generate the localstorage name.
124
+ */
125
+ // eslint-disable-next-line
126
+ export function applyMaterialTheme(doc, theme) {
127
+ let styleString = ':root,:host{';
128
+ for (const [key, value] of Object.entries(theme)) {
129
+ styleString += `--md-sys-color-${key}:${value};`;
130
+ }
131
+ styleString += '}';
132
+ applyTheme(doc, styleString);
133
+ }
134
+ //# sourceMappingURL=material-color-helpers.js.map
@@ -0,0 +1,3 @@
1
+ declare const loadfromStorage: () => string | null;
2
+ declare const saveToStorage: (theme: string) => void;
3
+ export { loadfromStorage, saveToStorage };
@@ -0,0 +1,12 @@
1
+ import { STORAGE_KEY_THEME } from './config.js';
2
+ // Load the theme string from localStorage.
3
+ const loadfromStorage = () => {
4
+ const theme = localStorage.getItem(STORAGE_KEY_THEME);
5
+ return theme;
6
+ };
7
+ // Saves the theme string to localStorage.
8
+ const saveToStorage = (theme) => {
9
+ localStorage.setItem(STORAGE_KEY_THEME, theme);
10
+ };
11
+ export { loadfromStorage, saveToStorage };
12
+ //# sourceMappingURL=storage.js.map
@@ -0,0 +1,8 @@
1
+ import { LitElement } from 'lit';
2
+ export type Constructor<T> = new (...args: any[]) => T;
3
+ export declare abstract class ThemeClass extends LitElement {
4
+ }
5
+ export declare class ThemeInterface {
6
+ isDarkMode(): boolean;
7
+ }
8
+ export declare const ThemeMixin: <T extends Constructor<LitElement & ThemeClass>>(base: T) => Constructor<ThemeInterface> & T;
@@ -0,0 +1,21 @@
1
+ import { LitElement } from 'lit';
2
+ import { SignalWatcher } from '@lit-labs/preact-signals';
3
+ import { themeCurrentMode } from './theme-signals.js';
4
+ import { getCurrentMode, isModeDark } from './theme.js';
5
+ export class ThemeClass extends LitElement {
6
+ }
7
+ export const ThemeMixin = (base) => {
8
+ // eslint-disable-next-line
9
+ class ExmTheme extends SignalWatcher(base) {
10
+ connectedCallback() {
11
+ super.connectedCallback();
12
+ // Read current mode from local storage
13
+ themeCurrentMode.value = getCurrentMode() || 'auto';
14
+ }
15
+ isDarkMode() {
16
+ return isModeDark(themeCurrentMode.value);
17
+ }
18
+ }
19
+ return ExmTheme;
20
+ };
21
+ //# sourceMappingURL=theme-mixin.js.map
@@ -0,0 +1,2 @@
1
+ import { ColorMode } from './types.js';
2
+ export declare const themeCurrentMode: import("@preact/signals-core").Signal<ColorMode>;
@@ -0,0 +1,3 @@
1
+ import { signal } from '@lit-labs/preact-signals';
2
+ export const themeCurrentMode = signal('light');
3
+ //# sourceMappingURL=theme-signals.js.map
@@ -0,0 +1,75 @@
1
+ import type { ColorMode } from './types.js';
2
+ /**
3
+ * Determines whether or not the mode should be Dark. This also means
4
+ * calculating whether it should be dark if the current mode is 'auto'.
5
+ *
6
+ * @param mode The current color mode 'light', 'dark', or 'auto'.
7
+ * @param saveAutoMode (Optional) Whether or not to save last auto mode to
8
+ * localstorage. Set to false if you simply want to query whether auto mode
9
+ * is dark or not. Defaults to true.
10
+ * @return Whether or not the dark mode color tokens should apply.
11
+ */
12
+ export declare function isModeDark(mode: ColorMode, saveAutoMode?: boolean): boolean;
13
+ /**
14
+ * Gets the current stringified material theme css string from localstorage.
15
+ *
16
+ * @return The current stringified material theme css string.
17
+ */
18
+ export declare function getCurrentThemeString(): string | null;
19
+ /**
20
+ * Gets the current color mode from localstorage.
21
+ *
22
+ * @return The current color mode.
23
+ */
24
+ export declare function getCurrentMode(): ColorMode | null;
25
+ /**
26
+ * Saves the given color mode to localstorage.
27
+ *
28
+ * @param mode The color mode to save to localstorage.
29
+ */
30
+ export declare function saveColorMode(mode: ColorMode): void;
31
+ /**
32
+ * Gets the current seed color from localstorage.
33
+ *
34
+ * @return The current seed color.
35
+ */
36
+ export declare function getCurrentSeedColor(): string | null;
37
+ /**
38
+ * Saves the given seed color to localstorage.
39
+ *
40
+ * @param color The seed color to save to local storage.
41
+ */
42
+ export declare function saveSeedColor(color: string): void;
43
+ /**
44
+ * Gets last applied color mode while in "auto" from localstorage.
45
+ *
46
+ * @return The last applied color mode while in "auto".
47
+ */
48
+ export declare function getLastSavedAutoColorMode(): "light" | "dark" | null;
49
+ /**
50
+ * Saves last applied color mode while in "auto" from localstorage.
51
+ *
52
+ * @param mode The last applied color mode while in "auto" to be saved to local
53
+ * storage.
54
+ */
55
+ export declare function saveLastSavedAutoColorMode(mode: 'light' | 'dark'): void;
56
+ /**
57
+ * Generates and applies a new theme due to a change in source color.
58
+ *
59
+ * @param color The new source color from which to generate the new theme.
60
+ */
61
+ export declare function changeColor(color: string): void;
62
+ /**
63
+ * Generates and applies a new theme due to a change in color mode.
64
+ *
65
+ * @param mode The new color mode from which to generate the new theme.
66
+ */
67
+ export declare function changeColorMode(mode: ColorMode): void;
68
+ /**
69
+ * Generates and applies a new theme due to a change in both source color and
70
+ * color mode.
71
+ *
72
+ * @param color The new source color from which to generate the new theme.
73
+ * @param mode The new color mode from which to generate the new theme.
74
+ */
75
+ export declare function changeColorAndMode(color: string, mode: ColorMode): void;
@@ -0,0 +1,136 @@
1
+ import { STORAGE_KEY_THEME } from './config.js';
2
+ import { themeCurrentMode } from './theme-signals.js';
3
+ import { applyMaterialTheme, themeFromSourceColor } from './material-color-helpers.js';
4
+ /**
5
+ * Generates a Material Theme from a given color and dark mode boolean, and
6
+ * applies the theme to the document and lets the app know that the theme has
7
+ * changed.
8
+ *
9
+ * @param color The source color to generate the theme.
10
+ * @param isDark Whether or not the theme should be in dark mode.
11
+ */
12
+ function applyThemeFromColor(color, isDark) {
13
+ const theme = themeFromSourceColor(color, isDark);
14
+ applyMaterialTheme(document, theme);
15
+ // Dispatches event to communicate with components pages' JS to update the
16
+ // theme in the playground preview.
17
+ window.dispatchEvent(new Event('theme-changed'));
18
+ }
19
+ /**
20
+ * Determines whether or not the mode should be Dark. This also means
21
+ * calculating whether it should be dark if the current mode is 'auto'.
22
+ *
23
+ * @param mode The current color mode 'light', 'dark', or 'auto'.
24
+ * @param saveAutoMode (Optional) Whether or not to save last auto mode to
25
+ * localstorage. Set to false if you simply want to query whether auto mode
26
+ * is dark or not. Defaults to true.
27
+ * @return Whether or not the dark mode color tokens should apply.
28
+ */
29
+ export function isModeDark(mode, saveAutoMode = true) {
30
+ let isDark = mode === 'dark';
31
+ // Determines whether the auto mode should display light or dark.
32
+ if (mode === 'auto') {
33
+ isDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
34
+ if (saveAutoMode) {
35
+ // We have to save this because if the user closes the tab when it's light
36
+ // and reopens it when it's dark, we need to know whether the last applied
37
+ // 'auto' mode was correct.
38
+ saveLastSavedAutoColorMode(isDark ? 'dark' : 'light');
39
+ }
40
+ }
41
+ return isDark;
42
+ }
43
+ /**
44
+ * Gets the current stringified material theme css string from localstorage.
45
+ *
46
+ * @return The current stringified material theme css string.
47
+ */
48
+ export function getCurrentThemeString() {
49
+ return localStorage.getItem(STORAGE_KEY_THEME);
50
+ }
51
+ /**
52
+ * Gets the current color mode from localstorage.
53
+ *
54
+ * @return The current color mode.
55
+ */
56
+ export function getCurrentMode() {
57
+ return localStorage.getItem('color-mode');
58
+ }
59
+ /**
60
+ * Saves the given color mode to localstorage.
61
+ *
62
+ * @param mode The color mode to save to localstorage.
63
+ */
64
+ export function saveColorMode(mode) {
65
+ themeCurrentMode.value = mode;
66
+ localStorage.setItem('color-mode', mode);
67
+ }
68
+ /**
69
+ * Gets the current seed color from localstorage.
70
+ *
71
+ * @return The current seed color.
72
+ */
73
+ export function getCurrentSeedColor() {
74
+ return localStorage.getItem('seed-color');
75
+ }
76
+ /**
77
+ * Saves the given seed color to localstorage.
78
+ *
79
+ * @param color The seed color to save to local storage.
80
+ */
81
+ export function saveSeedColor(color) {
82
+ localStorage.setItem('seed-color', color);
83
+ }
84
+ /**
85
+ * Gets last applied color mode while in "auto" from localstorage.
86
+ *
87
+ * @return The last applied color mode while in "auto".
88
+ */
89
+ export function getLastSavedAutoColorMode() {
90
+ return localStorage.getItem('last-auto-color-mode');
91
+ }
92
+ /**
93
+ * Saves last applied color mode while in "auto" from localstorage.
94
+ *
95
+ * @param mode The last applied color mode while in "auto" to be saved to local
96
+ * storage.
97
+ */
98
+ export function saveLastSavedAutoColorMode(mode) {
99
+ localStorage.setItem('last-auto-color-mode', mode);
100
+ }
101
+ /**
102
+ * Generates and applies a new theme due to a change in source color.
103
+ *
104
+ * @param color The new source color from which to generate the new theme.
105
+ */
106
+ export function changeColor(color) {
107
+ const lastColorMode = getCurrentMode();
108
+ const isDark = isModeDark(lastColorMode);
109
+ applyThemeFromColor(color, isDark);
110
+ saveSeedColor(color);
111
+ }
112
+ /**
113
+ * Generates and applies a new theme due to a change in color mode.
114
+ *
115
+ * @param mode The new color mode from which to generate the new theme.
116
+ */
117
+ export function changeColorMode(mode) {
118
+ const color = getCurrentSeedColor();
119
+ const isDark = isModeDark(mode);
120
+ applyThemeFromColor(color, isDark);
121
+ saveColorMode(mode);
122
+ }
123
+ /**
124
+ * Generates and applies a new theme due to a change in both source color and
125
+ * color mode.
126
+ *
127
+ * @param color The new source color from which to generate the new theme.
128
+ * @param mode The new color mode from which to generate the new theme.
129
+ */
130
+ export function changeColorAndMode(color, mode) {
131
+ const isDark = isModeDark(mode);
132
+ applyThemeFromColor(color, isDark);
133
+ saveSeedColor(color);
134
+ saveColorMode(mode);
135
+ }
136
+ //# sourceMappingURL=theme.js.map
@@ -0,0 +1,32 @@
1
+ export type WithStylesheet = typeof globalThis & {
2
+ [stylesheetName: string]: CSSStyleSheet | undefined;
3
+ };
4
+ export type ColorMode = 'light' | 'dark' | 'auto';
5
+ /**
6
+ * A theme mapping of token name (not custom property name) to stringified CSS
7
+ * value.
8
+ */
9
+ export interface Theme {
10
+ [tokenName: string]: string;
11
+ }
12
+ /**
13
+ * Requests the global theme listener change the theme due to a color change.
14
+ */
15
+ export declare class ThemeChangeColorEvent extends Event {
16
+ color: string;
17
+ /**
18
+ * @param color The new source color to apply.
19
+ */
20
+ constructor(color: string);
21
+ }
22
+ /**
23
+ * Requests the global theme listener change the theme due to a dark mode
24
+ * change.
25
+ */
26
+ export declare class ThemeChangeDarkModeEvent extends Event {
27
+ mode: 'light' | 'dark' | 'auto';
28
+ /**
29
+ * @param mode The new color mode to apply.
30
+ */
31
+ constructor(mode: 'light' | 'dark' | 'auto');
32
+ }
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Requests the global theme listener change the theme due to a color change.
3
+ */
4
+ export class ThemeChangeColorEvent extends Event {
5
+ /**
6
+ * @param color The new source color to apply.
7
+ */
8
+ constructor(color) {
9
+ super('theme-change-color', { bubbles: true, composed: true });
10
+ this.color = color;
11
+ }
12
+ }
13
+ /**
14
+ * Requests the global theme listener change the theme due to a dark mode
15
+ * change.
16
+ */
17
+ export class ThemeChangeDarkModeEvent extends Event {
18
+ /**
19
+ * @param mode The new color mode to apply.
20
+ */
21
+ constructor(mode) {
22
+ super('theme-change-mode', { bubbles: true, composed: true });
23
+ this.mode = mode;
24
+ }
25
+ }
26
+ //# sourceMappingURL=types.js.map
package/dist/config.js CHANGED
@@ -1,3 +1,5 @@
1
- export const STORAGE_KEY_THEME = 'exm-theme';
2
- export const DEFAULT_THEME_COLOR = '#3284FF';
3
- //# sourceMappingURL=config.js.map
1
+ const STORAGE_KEY_THEME = 'exm-theme';
2
+ const DEFAULT_THEME_COLOR = '#3284FF';
3
+
4
+ export { DEFAULT_THEME_COLOR, STORAGE_KEY_THEME };
5
+ //# sourceMappingURL=config.js.map
package/dist/index.js CHANGED
@@ -1,8 +1,11 @@
1
1
  import { DEFAULT_THEME_COLOR } from './config.js';
2
- import { applyTheme } from './material-color-helpers.js';
2
+ import { hexFromHct, hctFromHex, applyTheme } from './material-color-helpers.js';
3
3
  import { loadfromStorage } from './storage.js';
4
- import { changeColor, changeColorAndMode, changeColorMode, getCurrentMode, getCurrentSeedColor, getCurrentThemeString, getLastSavedAutoColorMode, isModeDark, } from './theme.js';
5
- import { hctFromHex, hexFromHct } from './material-color-helpers.js';
4
+ import { isModeDark, getLastSavedAutoColorMode, getCurrentThemeString, getCurrentSeedColor, getCurrentMode, changeColorMode, changeColorAndMode, changeColor } from './theme.js';
5
+ export { ThemeChangeColorEvent, ThemeChangeDarkModeEvent } from './types.js';
6
+ export { ThemeMixin } from './theme-mixin.js';
7
+ export { themeCurrentMode } from './theme-signals.js';
8
+
6
9
  /**
7
10
  * Determines whether to update the theme on page navigation if the mode is
8
11
  * 'auto'.
@@ -39,7 +42,7 @@ function setupListeners() {
39
42
  });
40
43
  }
41
44
  // Initializes the theme by applying the theme string from localStorage.
42
- export const initTheme = (config) => {
45
+ const initTheme = (config) => {
43
46
  const { themeColor, colorMode } = config || {};
44
47
  const theme = loadfromStorage();
45
48
  // Applies the theme string to the document if available.
@@ -52,8 +55,7 @@ export const initTheme = (config) => {
52
55
  }
53
56
  determinePageNavigationAutoMode();
54
57
  };
55
- export { ThemeChangeColorEvent, ThemeChangeDarkModeEvent } from './types.js';
56
- export const ThemeUtils = {
58
+ const ThemeUtils = {
57
59
  changeColor,
58
60
  changeColorAndMode,
59
61
  changeColorMode,
@@ -65,6 +67,6 @@ export const ThemeUtils = {
65
67
  hctFromHex,
66
68
  hexFromHct,
67
69
  };
68
- export { ThemeMixin } from './theme-mixin.js';
69
- export { themeCurrentMode } from './theme-signals.js';
70
- //# sourceMappingURL=index.js.map
70
+
71
+ export { ThemeUtils, initTheme };
72
+ //# sourceMappingURL=index.js.map
@@ -1,11 +1,12 @@
1
+ import { MaterialDynamicColors, Hct, hexFromArgb, argbFromHex, SchemeContent } from '@material/material-color-utilities';
2
+ import { STORAGE_KEY_THEME } from './config.js';
3
+ import { saveToStorage } from './storage.js';
4
+
1
5
  /**
2
6
  * @license
3
7
  * Copyright 2023 Google LLC
4
8
  * SPDX-License-Identifier: Apache-2.0
5
9
  */
6
- import { argbFromHex, Hct, hexFromArgb, MaterialDynamicColors, SchemeContent, } from '@material/material-color-utilities';
7
- import { STORAGE_KEY_THEME } from './config.js';
8
- import { saveToStorage } from './storage.js';
9
10
  // If supported by the browser, sets the theme color in the URL bar.
10
11
  const setUrlBarColor = (themeString) => {
11
12
  var _a, _b;
@@ -15,7 +16,7 @@ const setUrlBarColor = (themeString) => {
15
16
  }
16
17
  };
17
18
  // eslint-disable-next-line
18
- export const applyTheme = (doc, theme) => {
19
+ const applyTheme = (doc, theme) => {
19
20
  // eslint-disable-next-line
20
21
  let sheet = globalThis[STORAGE_KEY_THEME];
21
22
  if (!sheet) {
@@ -76,7 +77,7 @@ const materialColors = {
76
77
  * @param value A stringified hex color e.g. '#C01075'
77
78
  * @return Material Color Utilities HCT color tuple.
78
79
  */
79
- export function hctFromHex(value) {
80
+ function hctFromHex(value) {
80
81
  return Hct.fromInt(argbFromHex(value));
81
82
  }
82
83
  /**
@@ -88,7 +89,7 @@ export function hctFromHex(value) {
88
89
  * @param tone The tone of the color of value [0,100]
89
90
  * @return A clamped, stringified hex color value representing the HCT values.
90
91
  */
91
- export function hexFromHct(hue, chroma, tone) {
92
+ function hexFromHct(hue, chroma, tone) {
92
93
  const hct = Hct.from(hue, chroma, tone);
93
94
  const value = hct.toInt();
94
95
  return hexFromArgb(value);
@@ -103,7 +104,7 @@ export function hexFromHct(hue, chroma, tone) {
103
104
  * @return A theme object that maps the sys color token to its value (not a
104
105
  * custom property).
105
106
  */
106
- export function themeFromSourceColor(color, isDark) {
107
+ function themeFromSourceColor(color, isDark) {
107
108
  const scheme = new SchemeContent(Hct.fromInt(argbFromHex(color)), isDark, 0);
108
109
  const theme = {};
109
110
  for (const [key, value] of Object.entries(materialColors)) {
@@ -123,7 +124,7 @@ export function themeFromSourceColor(color, isDark) {
123
124
  * used to generate the localstorage name.
124
125
  */
125
126
  // eslint-disable-next-line
126
- export function applyMaterialTheme(doc, theme) {
127
+ function applyMaterialTheme(doc, theme) {
127
128
  let styleString = ':root,:host{';
128
129
  for (const [key, value] of Object.entries(theme)) {
129
130
  styleString += `--md-sys-color-${key}:${value};`;
@@ -131,4 +132,6 @@ export function applyMaterialTheme(doc, theme) {
131
132
  styleString += '}';
132
133
  applyTheme(doc, styleString);
133
134
  }
134
- //# sourceMappingURL=material-color-helpers.js.map
135
+
136
+ export { applyMaterialTheme, applyTheme, hctFromHex, hexFromHct, themeFromSourceColor };
137
+ //# sourceMappingURL=material-color-helpers.js.map
package/dist/storage.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { STORAGE_KEY_THEME } from './config.js';
2
+
2
3
  // Load the theme string from localStorage.
3
4
  const loadfromStorage = () => {
4
5
  const theme = localStorage.getItem(STORAGE_KEY_THEME);
@@ -8,5 +9,6 @@ const loadfromStorage = () => {
8
9
  const saveToStorage = (theme) => {
9
10
  localStorage.setItem(STORAGE_KEY_THEME, theme);
10
11
  };
12
+
11
13
  export { loadfromStorage, saveToStorage };
12
- //# sourceMappingURL=storage.js.map
14
+ //# sourceMappingURL=storage.js.map
@@ -1,10 +1,9 @@
1
- import { LitElement } from 'lit';
1
+ import 'lit';
2
2
  import { SignalWatcher } from '@lit-labs/preact-signals';
3
3
  import { themeCurrentMode } from './theme-signals.js';
4
4
  import { getCurrentMode, isModeDark } from './theme.js';
5
- export class ThemeClass extends LitElement {
6
- }
7
- export const ThemeMixin = (base) => {
5
+
6
+ const ThemeMixin = (base) => {
8
7
  // eslint-disable-next-line
9
8
  class ExmTheme extends SignalWatcher(base) {
10
9
  connectedCallback() {
@@ -18,4 +17,6 @@ export const ThemeMixin = (base) => {
18
17
  }
19
18
  return ExmTheme;
20
19
  };
21
- //# sourceMappingURL=theme-mixin.js.map
20
+
21
+ export { ThemeMixin };
22
+ //# sourceMappingURL=theme-mixin.js.map
@@ -1,3 +1,6 @@
1
1
  import { signal } from '@lit-labs/preact-signals';
2
- export const themeCurrentMode = signal('light');
3
- //# sourceMappingURL=theme-signals.js.map
2
+
3
+ const themeCurrentMode = signal('light');
4
+
5
+ export { themeCurrentMode };
6
+ //# sourceMappingURL=theme-signals.js.map
package/dist/theme.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import { STORAGE_KEY_THEME } from './config.js';
2
2
  import { themeCurrentMode } from './theme-signals.js';
3
- import { applyMaterialTheme, themeFromSourceColor } from './material-color-helpers.js';
3
+ import { themeFromSourceColor, applyMaterialTheme } from './material-color-helpers.js';
4
+
4
5
  /**
5
6
  * Generates a Material Theme from a given color and dark mode boolean, and
6
7
  * applies the theme to the document and lets the app know that the theme has
@@ -26,7 +27,7 @@ function applyThemeFromColor(color, isDark) {
26
27
  * is dark or not. Defaults to true.
27
28
  * @return Whether or not the dark mode color tokens should apply.
28
29
  */
29
- export function isModeDark(mode, saveAutoMode = true) {
30
+ function isModeDark(mode, saveAutoMode = true) {
30
31
  let isDark = mode === 'dark';
31
32
  // Determines whether the auto mode should display light or dark.
32
33
  if (mode === 'auto') {
@@ -45,7 +46,7 @@ export function isModeDark(mode, saveAutoMode = true) {
45
46
  *
46
47
  * @return The current stringified material theme css string.
47
48
  */
48
- export function getCurrentThemeString() {
49
+ function getCurrentThemeString() {
49
50
  return localStorage.getItem(STORAGE_KEY_THEME);
50
51
  }
51
52
  /**
@@ -53,7 +54,7 @@ export function getCurrentThemeString() {
53
54
  *
54
55
  * @return The current color mode.
55
56
  */
56
- export function getCurrentMode() {
57
+ function getCurrentMode() {
57
58
  return localStorage.getItem('color-mode');
58
59
  }
59
60
  /**
@@ -61,7 +62,7 @@ export function getCurrentMode() {
61
62
  *
62
63
  * @param mode The color mode to save to localstorage.
63
64
  */
64
- export function saveColorMode(mode) {
65
+ function saveColorMode(mode) {
65
66
  themeCurrentMode.value = mode;
66
67
  localStorage.setItem('color-mode', mode);
67
68
  }
@@ -70,7 +71,7 @@ export function saveColorMode(mode) {
70
71
  *
71
72
  * @return The current seed color.
72
73
  */
73
- export function getCurrentSeedColor() {
74
+ function getCurrentSeedColor() {
74
75
  return localStorage.getItem('seed-color');
75
76
  }
76
77
  /**
@@ -78,7 +79,7 @@ export function getCurrentSeedColor() {
78
79
  *
79
80
  * @param color The seed color to save to local storage.
80
81
  */
81
- export function saveSeedColor(color) {
82
+ function saveSeedColor(color) {
82
83
  localStorage.setItem('seed-color', color);
83
84
  }
84
85
  /**
@@ -86,7 +87,7 @@ export function saveSeedColor(color) {
86
87
  *
87
88
  * @return The last applied color mode while in "auto".
88
89
  */
89
- export function getLastSavedAutoColorMode() {
90
+ function getLastSavedAutoColorMode() {
90
91
  return localStorage.getItem('last-auto-color-mode');
91
92
  }
92
93
  /**
@@ -95,7 +96,7 @@ export function getLastSavedAutoColorMode() {
95
96
  * @param mode The last applied color mode while in "auto" to be saved to local
96
97
  * storage.
97
98
  */
98
- export function saveLastSavedAutoColorMode(mode) {
99
+ function saveLastSavedAutoColorMode(mode) {
99
100
  localStorage.setItem('last-auto-color-mode', mode);
100
101
  }
101
102
  /**
@@ -103,7 +104,7 @@ export function saveLastSavedAutoColorMode(mode) {
103
104
  *
104
105
  * @param color The new source color from which to generate the new theme.
105
106
  */
106
- export function changeColor(color) {
107
+ function changeColor(color) {
107
108
  const lastColorMode = getCurrentMode();
108
109
  const isDark = isModeDark(lastColorMode);
109
110
  applyThemeFromColor(color, isDark);
@@ -114,7 +115,7 @@ export function changeColor(color) {
114
115
  *
115
116
  * @param mode The new color mode from which to generate the new theme.
116
117
  */
117
- export function changeColorMode(mode) {
118
+ function changeColorMode(mode) {
118
119
  const color = getCurrentSeedColor();
119
120
  const isDark = isModeDark(mode);
120
121
  applyThemeFromColor(color, isDark);
@@ -127,10 +128,12 @@ export function changeColorMode(mode) {
127
128
  * @param color The new source color from which to generate the new theme.
128
129
  * @param mode The new color mode from which to generate the new theme.
129
130
  */
130
- export function changeColorAndMode(color, mode) {
131
+ function changeColorAndMode(color, mode) {
131
132
  const isDark = isModeDark(mode);
132
133
  applyThemeFromColor(color, isDark);
133
134
  saveSeedColor(color);
134
135
  saveColorMode(mode);
135
136
  }
136
- //# sourceMappingURL=theme.js.map
137
+
138
+ export { changeColor, changeColorAndMode, changeColorMode, getCurrentMode, getCurrentSeedColor, getCurrentThemeString, getLastSavedAutoColorMode, isModeDark, saveColorMode, saveLastSavedAutoColorMode, saveSeedColor };
139
+ //# sourceMappingURL=theme.js.map
package/dist/types.js CHANGED
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * Requests the global theme listener change the theme due to a color change.
3
3
  */
4
- export class ThemeChangeColorEvent extends Event {
4
+ class ThemeChangeColorEvent extends Event {
5
5
  /**
6
6
  * @param color The new source color to apply.
7
7
  */
@@ -14,7 +14,7 @@ export class ThemeChangeColorEvent extends Event {
14
14
  * Requests the global theme listener change the theme due to a dark mode
15
15
  * change.
16
16
  */
17
- export class ThemeChangeDarkModeEvent extends Event {
17
+ class ThemeChangeDarkModeEvent extends Event {
18
18
  /**
19
19
  * @param mode The new color mode to apply.
20
20
  */
@@ -23,4 +23,6 @@ export class ThemeChangeDarkModeEvent extends Event {
23
23
  this.mode = mode;
24
24
  }
25
25
  }
26
- //# sourceMappingURL=types.js.map
26
+
27
+ export { ThemeChangeColorEvent, ThemeChangeDarkModeEvent };
28
+ //# sourceMappingURL=types.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@exmg/exm-theme",
3
- "version": "1.1.35",
3
+ "version": "1.1.37-alpha.31+513140a",
4
4
  "type": "module",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -38,5 +38,5 @@
38
38
  "publishConfig": {
39
39
  "access": "public"
40
40
  },
41
- "gitHead": "1e671cd5dd060b682c8d9bb8c8a519a690c87782"
41
+ "gitHead": "513140a59e3a5a9a0fa572147ba6c0cf9801816e"
42
42
  }