Skip to content

Commit

Permalink
Merge pull request #2005 from quadratichq/jim-fix-theme
Browse files Browse the repository at this point in the history
fix: support theming in app bar
  • Loading branch information
davidkircos authored Oct 31, 2024
2 parents 1ac02f1 + 33cd271 commit ccca1fa
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
15 changes: 15 additions & 0 deletions quadratic-client/src/shared/hooks/useThemeAppearanceMode.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useFeatureFlag } from '@/shared/hooks/useFeatureFlag';
import useLocalStorage from '@/shared/hooks/useLocalStorage';
import { getCSSVariableAsHexColor } from '@/shared/utils/colors';
import { useEffect } from 'react';

const DEFAULT_APPEARANCE_MODE = 'light';
Expand Down Expand Up @@ -53,6 +54,20 @@ export const ThemeAppearanceModeEffects = () => {
};
}, [appearanceMode, userPrefesDarkMode]);

useEffect(() => {
const metaTag = document.querySelector('meta[name="theme-color"]');
const hexColor = getCSSVariableAsHexColor('background');

if (metaTag) {
metaTag.setAttribute('content', hexColor);
} else {
const meta = document.createElement('meta');
meta.name = 'theme-color';
meta.content = hexColor;
document.head.appendChild(meta);
}
}, [appearanceMode, userPrefesDarkMode]);

return null;
};

Expand Down
15 changes: 15 additions & 0 deletions quadratic-client/src/shared/utils/colors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import Color from 'color';

export function getCSSVariableAsHexColor(cssVariableName: string) {
if (cssVariableName.startsWith('--')) {
console.warn(
'`getCSSVariableTint` expects a CSS variable name without the `--` prefix. Are you sure you meant: `%s`',
cssVariableName
);
}

const hslColorString = getComputedStyle(document.documentElement).getPropertyValue(`--${cssVariableName}`).trim();
const parsed = Color.hsl(hslColorString.split(' ').map(parseFloat));
const out = parsed.hex();
return out;
}

0 comments on commit ccca1fa

Please sign in to comment.