Skip to content

Commit

Permalink
Merge pull request #1041 from LoneCoder21/forcetheme
Browse files Browse the repository at this point in the history
Override color theme preferences. #323
  • Loading branch information
molly authored Nov 25, 2023
2 parents dee3abe + 8f23ab0 commit 68c4920
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 32 deletions.
8 changes: 6 additions & 2 deletions src/components/Layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ import clsx from "clsx";
import { useAppState } from "../context/AppContext";

export default function Layout({ children }) {
const { useDarkMode, useSansSerif } = useAppState();
const { useSansSerif, theme } = useAppState();

return (
<main
className={clsx([
{ "use-sans-serif": useSansSerif, "use-dark": useDarkMode },
{
"use-sans-serif": useSansSerif,
"use-dark": theme === "dark",
"use-system": theme === "system",
},
])}
>
{children}
Expand Down
53 changes: 44 additions & 9 deletions src/components/timeline/SettingsPanel.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ export default function SettingsPanel({
isGriftCounterCountingUp,
toggleIsGriftCounterCountingUp,
}) {
const { useDarkMode, useSansSerif, toggleUseSansSerif, toggleDarkMode } =
useAppState();
const { theme, setTheme, useSansSerif, toggleUseSansSerif } = useAppState();

return (
<div className="settings-panel">
Expand All @@ -32,13 +31,49 @@ export default function SettingsPanel({
>
Use sans-serif font
</SettingsCheckbox>
<SettingsCheckbox
id="use-dark-mode"
checked={!!useDarkMode}
toggleCheckbox={toggleDarkMode}
>
Force dark mode
</SettingsCheckbox>

<div className="radio-group">
<h4>Theme</h4>
<div className="input-group">
<input
type="radio"
id="use-system-theme"
name="system-theme"
value="use-system-theme"
checked={theme === "system"}
onChange={() => {
setTheme("system");
}}
/>
<label htmlFor="use-system-theme">Use system theme</label>
</div>
<div className="input-group">
<input
type="radio"
id="use-dark-mode"
name="dark-mode"
value="use-dark-mode"
checked={theme === "dark"}
onChange={() => {
setTheme("dark");
}}
/>
<label htmlFor="use-dark-mode">Force dark mode</label>
</div>
<div className="input-group">
<input
type="radio"
id="use-light-mode"
name="light-mode"
value="use-light-mode"
checked={theme === "light"}
onChange={() => {
setTheme("light");
}}
/>
<label htmlFor="use-light-mode">Force light mode</label>
</div>
</div>
</div>
<h3>Grift counter</h3>
<div className="settings-section">
Expand Down
24 changes: 12 additions & 12 deletions src/context/AppContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,30 @@ import {
export const AppContext = createContext();

export function AppProvider({ children }) {
const [useDarkMode, setUseDarkMode] = useState(null);
const [useSansSerif, setUseSansSerif] = useState(null);
const [theme, _setTheme] = useState("system");
const [useSansSerif, _setUseSansSerif] = useState(null);

const toggleUseSansSerif = useCallback(() => {
setLocalStorage(LOCALSTORAGE_KEYS.useSansSerif, !useSansSerif);
setUseSansSerif(!useSansSerif);
_setUseSansSerif(!useSansSerif);
}, [useSansSerif]);

const toggleDarkMode = useCallback(() => {
setLocalStorage(LOCALSTORAGE_KEYS.useDarkMode, !useDarkMode);
setUseDarkMode(!useDarkMode);
}, [useDarkMode]);
const setTheme = useCallback((theme) => {
setLocalStorage(LOCALSTORAGE_KEYS.theme, theme);
_setTheme(theme);
}, []);

useEffect(() => {
// Localstorage can't be accessed during SSR
setUseSansSerif(getLocalStorage(LOCALSTORAGE_KEYS.useSansSerif));
setUseDarkMode(getLocalStorage(LOCALSTORAGE_KEYS.useDarkMode));
}, [setUseSansSerif, setUseDarkMode]);
_setUseSansSerif(getLocalStorage(LOCALSTORAGE_KEYS.useSansSerif));
_setTheme(getLocalStorage(LOCALSTORAGE_KEYS.theme, "system"));
}, [_setUseSansSerif, _setTheme]);

const state = {
useDarkMode,
theme,
setTheme,
useSansSerif,
toggleUseSansSerif,
toggleDarkMode,
};

return <AppContext.Provider value={state}>{children}</AppContext.Provider>;
Expand Down
2 changes: 1 addition & 1 deletion src/js/localStorage.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export const LOCALSTORAGE_KEYS = {
// General appearance
useDarkMode: "use-dark-mode",
theme: "theme",
useSansSerif: "use-sans-serif",

// Grift counter
Expand Down
18 changes: 10 additions & 8 deletions src/styles/main.sass
Original file line number Diff line number Diff line change
Expand Up @@ -1076,11 +1076,13 @@ table
@include chartsDarkMode()
@include reactDateRangeDarkMode()

@media (prefers-color-scheme: dark)
@include mainDarkMode()
@include navigationDarkMode()
@include filtersDarkMode()
@include fixedDarkMode()
@include popoversDarkMode()
@include chartsDarkMode()
@include reactDateRangeDarkMode()
.use-system
@media (prefers-color-scheme: dark)
background-color: $dark-main-background
@include mainDarkMode()
@include navigationDarkMode()
@include filtersDarkMode()
@include fixedDarkMode()
@include popoversDarkMode()
@include chartsDarkMode()
@include reactDateRangeDarkMode()

0 comments on commit 68c4920

Please sign in to comment.