MCPcopy Create free account
hub / github.com/dailydotdev/apps / useFeatureTheme

Function useFeatureTheme

packages/shared/src/hooks/utils/useFeatureTheme.ts:15–79  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

13 FeatureThemeVariant {}
14
15export 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;

Callers 10

LogoWithPlusFunction · 0.90
MainLayoutHeaderFunction · 0.90
HeaderLogoFunction · 0.90
RecruiterLayoutHeaderFunction · 0.90
SidebarFunction · 0.90
FeedNavFunction · 0.90
GoBackButtonFunction · 0.90
GoBackHeaderMobileFunction · 0.90
PostPageFunction · 0.90

Calls 4

useSettingsContextFunction · 0.90
useFeatureFunction · 0.90
pushMethod · 0.80
useRouterFunction · 0.50

Tested by

no test coverage detected