-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
96 additions
and
34 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,22 @@ | ||
import {FC, useEffect, useState} from "react"; | ||
import {Helmet} from "react-helmet"; | ||
import {useEffect} from "react"; | ||
import {Helmet, HelmetProps} from "react-helmet"; | ||
import {useAppSelector} from "store"; | ||
import {useAutoTheme} from "utils/hooks/useAutoTheme"; | ||
|
||
type HelmetProps = React.ComponentProps<typeof Helmet>; | ||
const HelmetWorkaround: FC<HelmetProps> = ({...rest}) => <Helmet {...rest} />; | ||
const HelmetWorkaround = ({...rest}: HelmetProps) => <Helmet {...rest} />; | ||
|
||
export const Html: FC = () => { | ||
export const Html = () => { | ||
const lang = useAppSelector((state) => state.view.language); | ||
let title = useAppSelector((state) => state.board.data?.name); | ||
const [theme, setTheme] = useState(localStorage.getItem("theme") ?? (!window.matchMedia || window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light")); | ||
|
||
if (title) title = `scrumlr.io - ${title}`; | ||
|
||
useEffect(() => { | ||
if (theme === "auto") { | ||
const autoTheme = window.matchMedia("(prefers-color-scheme: dark)")?.matches ? "dark" : "light"; | ||
setTheme(autoTheme); | ||
} | ||
}, [theme]); | ||
|
||
window.matchMedia("(prefers-color-scheme: dark)").addEventListener("change", (e) => { | ||
const colorScheme = e.matches ? "dark" : "light"; | ||
const theme = useAppSelector((state) => state.view.theme); | ||
const autoTheme = useAutoTheme(theme); | ||
|
||
if (!localStorage.getItem("theme") || localStorage.getItem("theme") === "auto") { | ||
setTheme(colorScheme); | ||
document.documentElement.setAttribute("theme", colorScheme); | ||
} | ||
}); | ||
// set the theme as an attribute, which can then be used inside stylesheets, e.g. [theme="dark"] {...} | ||
useEffect(() => { | ||
document.documentElement.setAttribute("theme", autoTheme.toString()); | ||
}, [autoTheme]); | ||
|
||
return <HelmetWorkaround title={title} htmlAttributes={{lang, theme}} />; | ||
return <HelmetWorkaround title={title} htmlAttributes={{lang, theme: autoTheme.toString()}} />; | ||
}; |
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
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,38 @@ | ||
import {useState, useEffect, useCallback} from "react"; | ||
import {Theme, AutoTheme} from "types/view"; | ||
|
||
/** this hook return the theme that should be used, regarding the current system preferences. */ | ||
export const useAutoTheme = (theme: Theme): AutoTheme => { | ||
const getInitialAutoTheme = useCallback((): AutoTheme => { | ||
if (theme === "auto") { | ||
return window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light"; | ||
} | ||
return theme; | ||
}, [theme]); | ||
|
||
const [autoTheme, setAutoTheme] = useState<AutoTheme>(getInitialAutoTheme()); | ||
|
||
// update the theme. if it's set to auto, use the system preference, otherwise use the value. | ||
useEffect(() => { | ||
const handleColorSchemeChange = (e: MediaQueryListEvent) => { | ||
const colorSchemePreference: AutoTheme = e.matches ? "dark" : "light"; | ||
|
||
if (theme === "auto") { | ||
setAutoTheme(colorSchemePreference); | ||
} | ||
}; | ||
|
||
const mediaQuery = window.matchMedia("(prefers-color-scheme: dark)"); | ||
mediaQuery.addEventListener("change", handleColorSchemeChange); | ||
|
||
// update the theme based on the initial value and theme changes | ||
setAutoTheme(getInitialAutoTheme()); | ||
|
||
return () => { | ||
// cleanup | ||
mediaQuery.removeEventListener("change", handleColorSchemeChange); | ||
}; | ||
}, [getInitialAutoTheme, theme]); | ||
|
||
return autoTheme; | ||
}; |
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