-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #53 from vimdotmd/refactor-theme-color
Refactor: Define theme colors in JS instead of CSS
- Loading branch information
Showing
13 changed files
with
157 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import Color from "color"; | ||
import { ThemeColors, ThemeName, THEME_COLORS } from "./theme"; | ||
|
||
interface Props { | ||
theme: ThemeName; | ||
} | ||
|
||
const getCss = (theme: ThemeName): string => { | ||
const variables: string[] = []; | ||
const colors = THEME_COLORS[theme]; | ||
Object.keys(colors).forEach((key) => { | ||
const color = colors[key as keyof ThemeColors] as Color; | ||
variables.push(`--${key}-color: ${color.hex()};`); | ||
const rgb = `${color.red()}, ${color.green()}, ${color.blue()}`; | ||
variables.push(`--${key}-color-rgb: ${rgb};`); | ||
}); | ||
return [":root {", variables.join("\n"), "}"].join("\n"); | ||
}; | ||
|
||
export const ThemeInject = (props: Props): JSX.Element => ( | ||
<style>{getCss(props.theme)}</style> | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,17 @@ | ||
import { useEffect } from "react"; | ||
import { SetState } from "~src/utils/state/type"; | ||
import { useStorageState } from "~src/utils/state/storage"; | ||
import "./styles/bushido.css"; | ||
import "./styles/serika-dark.css"; | ||
|
||
export const THEMES = ["bushido", "serika-dark"] as const; | ||
|
||
export type Theme = typeof THEMES[number]; | ||
import { SetState } from "~src/utils/state/type"; | ||
import { ThemeName } from "./theme"; | ||
|
||
export interface ThemeState { | ||
theme: Theme; | ||
setTheme: SetState<Theme>; | ||
theme: ThemeName; | ||
setTheme: SetState<ThemeName>; | ||
} | ||
|
||
const htmlClass = document.documentElement.classList; | ||
|
||
const useThemeApply = (theme: Theme): void => { | ||
useEffect(() => { | ||
htmlClass.add(`theme-${theme}`); | ||
return () => void htmlClass.remove(`theme-${theme}`); | ||
}, [theme]); | ||
}; | ||
|
||
export const usePrefsTheme = (): ThemeState => { | ||
const [theme, setTheme] = useStorageState<Theme>({ | ||
const [theme, setTheme] = useStorageState<ThemeName>({ | ||
storageKey: "theme", | ||
defaultValue: "bushido", | ||
}); | ||
|
||
useThemeApply(theme); | ||
|
||
return { theme, setTheme }; | ||
}; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.