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