Skip to content

Commit f7f9c96

Browse files
committed
refactor: Cleaned up theming utilities
1 parent 48df82f commit f7f9c96

File tree

2 files changed

+44
-60
lines changed

2 files changed

+44
-60
lines changed

src/theming/config.ts

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { isDefined } from '../components/common/util.js';
1+
import { isServer } from 'lit';
22
import {
33
CHANGE_THEME_EVENT,
44
type ChangeThemeEventDetail,
@@ -12,8 +12,8 @@ let themeVariant: ThemeVariant;
1212
/**
1313
* Dispatch an "igc-change-theme" event to `window` with the given detail.
1414
*/
15-
function dispatchThemingEvent(detail: ChangeThemeEventDetail) {
16-
if (isDefined(globalThis.dispatchEvent)) {
15+
function dispatchThemingEvent(detail: ChangeThemeEventDetail): void {
16+
if (!isServer) {
1717
globalThis.dispatchEvent(new CustomEvent(CHANGE_THEME_EVENT, { detail }));
1818
}
1919
}
@@ -26,34 +26,23 @@ function isOfTypeThemeVariant(variant: string): variant is ThemeVariant {
2626
return ['light', 'dark'].includes(variant);
2727
}
2828

29-
export const getTheme: () => {
30-
theme: Theme;
31-
themeVariant: ThemeVariant;
32-
} = () => {
29+
export function getTheme() {
3330
if (!(theme && themeVariant)) {
34-
const [_theme, _variant] =
35-
Object.entries(getAllCssVariables()).filter(
36-
([v]) => v === 'igTheme' || v === 'igThemeVariant'
37-
) || [];
31+
const cssVars = getAllCssVariables();
32+
const foundTheme = cssVars.igTheme;
33+
const foundVariant = cssVars.igThemeVariant;
3834

39-
theme =
40-
_theme?.[1] && isOfTypeTheme(_theme[1])
41-
? (_theme[1] as Theme)
42-
: 'bootstrap';
43-
44-
themeVariant =
45-
_variant?.[1] && isOfTypeThemeVariant(_variant[1])
46-
? (_variant[1] as ThemeVariant)
47-
: 'light';
35+
theme = isOfTypeTheme(foundTheme) ? foundTheme : 'bootstrap';
36+
themeVariant = isOfTypeThemeVariant(foundVariant) ? foundVariant : 'light';
4837
}
4938

5039
return { theme, themeVariant };
51-
};
40+
}
5241

53-
export const setTheme = (value: Theme, variant: ThemeVariant) => {
42+
export function setTheme(value: Theme, variant: ThemeVariant): void {
5443
theme = value;
5544
themeVariant = variant;
56-
};
45+
}
5746

5847
/**
5948
* Allows the global configuration of the active theme.
@@ -65,10 +54,9 @@ export const setTheme = (value: Theme, variant: ThemeVariant) => {
6554
* configureTheme('material', 'light');
6655
* ```
6756
*/
68-
export const configureTheme = (t: Theme, v: ThemeVariant = 'light') => {
57+
export function configureTheme(t: Theme, v: ThemeVariant = 'light'): void {
6958
if (isOfTypeTheme(t) && isOfTypeThemeVariant(v)) {
7059
setTheme(t, v);
60+
dispatchThemingEvent({ theme, themeVariant });
7161
}
72-
73-
dispatchThemingEvent({ theme, themeVariant });
74-
};
62+
}

src/theming/utils.ts

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,45 @@
11
import { isServer } from 'lit';
22

3-
import { isDefined } from '../components/common/util.js';
4-
53
function isStyleRule(rule: CSSRule): rule is CSSStyleRule {
6-
return !!rule && 'style' in rule;
4+
return rule != null && 'style' in rule;
75
}
86

97
function cssKeyToJsKey(key: string): string {
10-
return key.replace('--', '').replace(/-./g, (x) => x.toUpperCase()[1]);
8+
return key.replace(/^--|-./g, (match) => {
9+
return match.startsWith('--') ? '' : match.charAt(1).toUpperCase();
10+
});
1111
}
1212

1313
function getAllCssVariableNames(): Set<string> {
1414
const cssVars = new Set<string>();
15+
const styleSheets = Array.from(document.styleSheets);
1516

16-
/* c8 ignore next 3 */
17-
if (isServer || !isDefined(globalThis.document)) {
18-
return cssVars;
19-
}
17+
for (const sheet of styleSheets) {
18+
let rules: CSSRuleList | undefined;
2019

21-
// Filter out any external stylesheets which throw CORS errors
22-
const styleSheets = Array.from(globalThis.document.styleSheets).filter(
23-
(sheet) => {
24-
try {
25-
return sheet.cssRules;
26-
} catch {
27-
return false;
28-
}
20+
// Potential CORS or access errors
21+
try {
22+
rules = sheet.cssRules;
23+
} catch {
24+
continue;
2925
}
30-
);
3126

32-
for (const sheet of styleSheets) {
33-
const rules = Array.from(sheet.cssRules).filter(isStyleRule);
27+
if (!rules) {
28+
continue;
29+
}
3430

35-
for (const rule of rules) {
36-
Array.from(rule.style).forEach((style) => {
37-
if (style.startsWith('--')) {
38-
cssVars.add(style);
31+
for (const rule of Array.from(rules)) {
32+
if (isStyleRule(rule)) {
33+
const length = rule.style.length;
34+
35+
for (let i = 0; i < length; i++) {
36+
const style = rule.style[i];
37+
38+
if (style.startsWith('--')) {
39+
cssVars.add(style);
40+
}
3941
}
40-
});
42+
}
4143
}
4244
}
4345

@@ -50,13 +52,7 @@ function getElementCssVariables(
5052
pseudo?: string
5153
): Record<string, string> {
5254
const cssVars: Record<string, string> = {};
53-
54-
/* c8 ignore next 3 */
55-
if (!isDefined(globalThis.getComputedStyle)) {
56-
return cssVars;
57-
}
58-
59-
const styles = globalThis.getComputedStyle(element, pseudo);
55+
const styles = getComputedStyle(element, pseudo);
6056

6157
for (const key of allCssVars) {
6258
const value = styles.getPropertyValue(key);
@@ -71,10 +67,10 @@ function getElementCssVariables(
7167

7268
export function getAllCssVariables(): Record<string, string> {
7369
/* c8 ignore next 2 */
74-
return isServer || !isDefined(globalThis.document)
70+
return isServer
7571
? {}
7672
: getElementCssVariables(
7773
getAllCssVariableNames(),
78-
globalThis.document.documentElement
74+
document.documentElement
7975
);
8076
}

0 commit comments

Comments
 (0)