-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add theming using base16 themes
fix issue where page refreshes on query plan update minor ui tweaks
- Loading branch information
Showing
11 changed files
with
359 additions
and
71 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import { | ||
Dispatch, | ||
SetStateAction, | ||
createContext, | ||
useEffect, | ||
useState, | ||
} from "react"; | ||
|
||
import * as base16Themes from "base16"; | ||
|
||
type IThemes = Exclude<keyof typeof base16Themes, "default">; | ||
|
||
const THEMES_MAP = Object.fromEntries( | ||
Object.entries(base16Themes).filter(([k]) => k !== "default"), | ||
) as Pick<typeof base16Themes, IThemes>; | ||
const THEMES = Object.keys(THEMES_MAP) as IThemes[]; | ||
|
||
const THEME_PROPERTIES = [ | ||
"base00", | ||
"base01", | ||
"base02", | ||
"base03", | ||
"base04", | ||
"base05", | ||
"base06", | ||
"base07", | ||
"base08", | ||
"base09", | ||
"base0A", | ||
"base0B", | ||
"base0C", | ||
"base0D", | ||
"base0E", | ||
"base0F", | ||
] as const; | ||
|
||
type IMode = "light" | "dark"; | ||
const applyTheme = (name: keyof typeof THEMES_MAP, mode: IMode = "light") => { | ||
const theme = THEMES_MAP[name]; | ||
const light = mode === "light"; | ||
for (let i = 0; i < 16; i++) { | ||
document.documentElement.style.setProperty( | ||
`--${THEME_PROPERTIES[i]}`, | ||
theme[THEME_PROPERTIES[i < 8 && !light ? 7 - i : i]], | ||
); | ||
} | ||
document.documentElement.style.setProperty("--scheme", mode); | ||
document.documentElement.style.setProperty("--theme", theme["scheme"]); | ||
localStorage.setItem("theme", JSON.stringify([name, mode])); | ||
}; | ||
|
||
interface IThemeState { | ||
name: IThemes; | ||
mode: IMode; | ||
} | ||
interface IThemeContextValue { | ||
theme: IThemeState; | ||
THEMES: typeof THEMES; | ||
setTheme: Dispatch<SetStateAction<IThemeState>>; | ||
} | ||
const defaultTheme: IThemeState = { | ||
name: "solarized", | ||
mode: "dark", | ||
}; | ||
function loadThemeFromLocalStorage(): IThemeState | null { | ||
const themeStr = localStorage.getItem("theme"); | ||
if (!themeStr) return null; | ||
try { | ||
const _theme = JSON.parse(themeStr); | ||
if (_theme[0] in THEMES_MAP && ["light", "dark"].includes(_theme[1])) { | ||
return { | ||
name: _theme[0], | ||
mode: _theme[1], | ||
}; | ||
} | ||
} catch (err) { | ||
if (err instanceof SyntaxError) { | ||
console.error("Invalid Theme stored: ", themeStr); | ||
} | ||
} | ||
return null; | ||
} | ||
const initialTheme: IThemeState = loadThemeFromLocalStorage() || defaultTheme; | ||
export const ThemeContext = createContext<IThemeContextValue>({ | ||
theme: initialTheme, | ||
THEMES, | ||
setTheme: () => {}, | ||
}); | ||
|
||
function ThemeProvider(props: React.PropsWithChildren<{}>) { | ||
const { children } = props; | ||
const [theme, setTheme] = useState<IThemeState>(initialTheme); | ||
useEffect(() => { | ||
applyTheme(theme.name, theme.mode); | ||
}, [theme]); | ||
return ( | ||
<ThemeContext.Provider value={{ theme, setTheme: setTheme, THEMES }}> | ||
{children} | ||
</ThemeContext.Provider> | ||
); | ||
} | ||
|
||
export default ThemeProvider; |
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,43 @@ | ||
import { useCallback, useContext } from "react"; | ||
import { ThemeContext } from "./ThemeProvider"; | ||
|
||
const ThemeSelect = () => { | ||
const themeContext = useContext(ThemeContext); | ||
const handleThemeChange = useCallback((e) => { | ||
const name = e.target.form["theme-select"].selectedOptions[0].value; | ||
const mode = e.target.form["dark-mode-input"].checked ? "dark" : "light"; | ||
themeContext.setTheme({ name, mode }); | ||
console.log(e); | ||
}, []); | ||
return ( | ||
<> | ||
<form> | ||
<label> | ||
Theme:{" "} | ||
<select name="theme-select" onChange={handleThemeChange}> | ||
{themeContext.THEMES.map((t) => ( | ||
<option | ||
key={t} | ||
value={t} | ||
selected={t === themeContext.theme.name} | ||
> | ||
{t} | ||
</option> | ||
))} | ||
</select> | ||
</label> | ||
<label> | ||
Dark | ||
<input | ||
name="dark-mode-input" | ||
onChange={handleThemeChange} | ||
type="checkbox" | ||
checked={themeContext.theme.mode === "dark"} | ||
/> | ||
</label> | ||
</form> | ||
</> | ||
); | ||
}; | ||
|
||
export default ThemeSelect; |
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,19 @@ | ||
svg.progress { | ||
width: 100%; | ||
height: 1em; | ||
} | ||
svg.progress text { | ||
color: var(--base07); | ||
font-size: 1em; | ||
} | ||
div.node-card { | ||
background-color: var(--base07); | ||
border: 1px solid var(--base04); | ||
padding: 4px; | ||
} | ||
/* @media (prefers-color-scheme: light) { */ | ||
/* div.node-card { */ | ||
/* background-color: #efefef; */ | ||
/* border-color: #101010; */ | ||
/* } */ | ||
/* } */ |
Oops, something went wrong.