-
Notifications
You must be signed in to change notification settings - Fork 371
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'pr/hyoban/890' into dev
- Loading branch information
Showing
13 changed files
with
191 additions
and
81 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { useDark } from "~/lib/hooks/use-dark" | ||
|
||
export function AppearanceSwitch({ className = "" }: { className?: string }) { | ||
const { toggleDark } = useDark() | ||
|
||
return ( | ||
<button type="button" onClick={toggleDark} className={"flex " + className}> | ||
<div className="i-mingcute-sun-2-line scale-100 dark:scale-0 transition-transform duration-500 rotate-0 dark:-rotate-90" /> | ||
<div className="i-mingcute-moon-line absolute scale-0 dark:scale-100 transition-transform duration-500 rotate-90 dark:rotate-0" /> | ||
<span className="sr-only">Toggle theme</span> | ||
</button> | ||
) | ||
} |
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,67 @@ | ||
import { useLocalStorage } from "foxact/use-local-storage" | ||
import { useEffect, useMemo, useSyncExternalStore } from "react" | ||
|
||
const query = "(prefers-color-scheme: dark)" | ||
|
||
function getSnapshot() { | ||
return window.matchMedia(query).matches | ||
} | ||
|
||
function getServerSnapshot(): undefined { | ||
return undefined | ||
} | ||
|
||
function subscribe(callback: () => void) { | ||
const matcher = window.matchMedia(query) | ||
matcher.addEventListener("change", callback) | ||
return () => { | ||
matcher.removeEventListener("change", callback) | ||
} | ||
} | ||
|
||
function useSystemDark() { | ||
return useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot) | ||
} | ||
|
||
const themeOptions = ["system", "light", "dark"] as const | ||
export type Theme = (typeof themeOptions)[number] | ||
|
||
function isDarkMode(setting?: Theme | null, isSystemDark?: boolean) { | ||
return setting === "dark" || (isSystemDark && setting !== "light") | ||
} | ||
|
||
export function useDark(themeKey = "use-dark") { | ||
const [theme, setTheme] = useLocalStorage<Theme>(themeKey, "system") | ||
const isSystemDark = useSystemDark() | ||
|
||
const isDark = useMemo( | ||
() => isDarkMode(theme, isSystemDark), | ||
[isSystemDark, theme], | ||
) | ||
|
||
const toggleDark = () => { | ||
if (theme === "system") { | ||
setTheme(isSystemDark ? "light" : "dark") | ||
} else { | ||
setTheme("system") | ||
} | ||
} | ||
|
||
useEffect(() => { | ||
const isDark = isDarkMode(theme, isSystemDark) | ||
if (isDark) { | ||
document.documentElement.classList.toggle("dark", true) | ||
} else { | ||
document.documentElement.classList.toggle("dark", false) | ||
} | ||
|
||
if ( | ||
(theme === "dark" && isSystemDark) || | ||
(theme === "light" && !isSystemDark) | ||
) { | ||
setTheme("system") | ||
} | ||
}, [theme, isSystemDark, setTheme]) | ||
|
||
return { isDark, toggleDark } | ||
} |
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
Oops, something went wrong.