()
| 13 | FeatureThemeVariant {} |
| 14 | |
| 15 | export const useFeatureTheme = (): UseFeatureThemeResult | undefined => { |
| 16 | const { themeMode } = useSettingsContext(); |
| 17 | const featureTheme = useFeature(feature.featureTheme) as |
| 18 | | FeatureTheme |
| 19 | | undefined; |
| 20 | const router = useRouter(); |
| 21 | const isOnboarding = router?.pathname?.startsWith('/onboarding') ?? false; |
| 22 | const useTheme = !isOnboarding && !!featureTheme?.version; |
| 23 | |
| 24 | const isLight = useMemo(() => { |
| 25 | if (themeMode === ThemeMode.Auto) { |
| 26 | return globalThis?.window?.matchMedia?.('(prefers-color-scheme:light)') |
| 27 | .matches; |
| 28 | } |
| 29 | |
| 30 | return themeMode === ThemeMode.Light; |
| 31 | }, [themeMode]); |
| 32 | const theme = isLight ? ThemeMode.Light : ThemeMode.Dark; |
| 33 | |
| 34 | useEffect(() => { |
| 35 | const id = 'feature-theme-styles'; |
| 36 | const styleElement = document.createElement('style'); |
| 37 | styleElement.setAttribute('id', id); |
| 38 | styleElement.setAttribute('type', 'text/css'); |
| 39 | |
| 40 | const bodyStyles = []; |
| 41 | if (featureTheme?.[theme]?.body) { |
| 42 | Object.entries(featureTheme[theme]?.body).forEach(([key, value]) => |
| 43 | bodyStyles.push(`${key}: ${value}`), |
| 44 | ); |
| 45 | } |
| 46 | |
| 47 | if (featureTheme?.cursor) { |
| 48 | bodyStyles.push(`cursor: url('${featureTheme.cursor}'), auto`); |
| 49 | } |
| 50 | let css = `body { ${bodyStyles.join('; ')} }`; |
| 51 | |
| 52 | if (featureTheme?.[theme]?.body?.background) { |
| 53 | css = `${css} .modal { background: ${featureTheme[theme].body.background}; }`; |
| 54 | } |
| 55 | |
| 56 | const oldStyleElem = document.getElementById(id); |
| 57 | if (oldStyleElem?.innerText === css) { |
| 58 | // nothing to do, styles were alreay applied |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | styleElement.textContent = css; |
| 63 | |
| 64 | if (oldStyleElem) { |
| 65 | document.head.replaceChild(styleElement, oldStyleElem); |
| 66 | } else { |
| 67 | document.head.appendChild(styleElement); |
| 68 | } |
| 69 | }, [featureTheme, theme, useTheme]); |
| 70 | |
| 71 | if (useTheme) { |
| 72 | const { light, dark, ...rest } = featureTheme; |
no test coverage detected