move theme code to own file
This commit is contained in:
parent
44b66c37fc
commit
52e09ae5ef
1 changed files with 50 additions and 21 deletions
44
tank-frontend/src/theme.ts
Normal file
44
tank-frontend/src/theme.ts
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
function getRandomColor() {
|
||||
const letters = '0123456789ABCDEF';
|
||||
let color = '#';
|
||||
for (let i = 0; i < 3; i++) {
|
||||
color += letters[Math.floor(Math.random() * letters.length)];
|
||||
}
|
||||
return color;
|
||||
}
|
||||
|
||||
|
||||
export type Theme = {
|
||||
primary?: string;
|
||||
secondary?: string;
|
||||
background?: string;
|
||||
}
|
||||
|
||||
// @ts-ignore
|
||||
const rootStyle = document.querySelector(':root')?.style;
|
||||
|
||||
export function setTheme(theme: Theme) {
|
||||
if (!rootStyle)
|
||||
return;
|
||||
rootStyle.setProperty('--color-primary', theme.primary);
|
||||
rootStyle.setProperty('--color-secondary', theme.secondary);
|
||||
rootStyle.setProperty('--color-background', theme.background);
|
||||
}
|
||||
|
||||
export function getTheme(): Theme {
|
||||
if (!rootStyle)
|
||||
return {};
|
||||
return {
|
||||
primary: rootStyle.getPropertyValue('--color-primary'),
|
||||
secondary: rootStyle.getPropertyValue('--color-secondary'),
|
||||
background: rootStyle.getPropertyValue('--color-background')
|
||||
};
|
||||
}
|
||||
|
||||
export function setRandomTheme() {
|
||||
setTheme({
|
||||
primary: getRandomColor(),
|
||||
secondary: getRandomColor(),
|
||||
background: getRandomColor()
|
||||
})
|
||||
}
|
||||
Reference in a new issue