-
Notifications
You must be signed in to change notification settings - Fork 13
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 #135 from alley-rs/dev
feat: 支持手动控制明暗色
- Loading branch information
Showing
8 changed files
with
146 additions
and
43 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 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,35 @@ | ||
import { children } from "solid-js"; | ||
import { LazyLabel, LazySpace, LazyTag } from "~/lazy"; | ||
|
||
interface DarkModeProps { | ||
mode: Config["dark_mode"]; | ||
onChoice: (mode: Config["dark_mode"]) => void; | ||
} | ||
|
||
const modes: Array<{ value: Config["dark_mode"]; label: string }> = [ | ||
{ value: "light", label: "亮色" }, | ||
{ value: "dark", label: "暗色" }, | ||
{ value: "system", label: "跟随系统" }, | ||
]; | ||
|
||
const DarkMode = (props: DarkModeProps) => { | ||
const items = children(() => | ||
modes.map((mode) => ( | ||
<LazyTag | ||
onClick={() => props.onChoice(mode.value)} | ||
color={props.mode === mode.value ? "success" : "default"} | ||
> | ||
{mode.label} | ||
</LazyTag> | ||
)), | ||
); | ||
return ( | ||
<LazySpace gap={16}> | ||
<LazyLabel>暗色模式</LazyLabel> | ||
|
||
<LazySpace gap={8}>{items()}</LazySpace> | ||
</LazySpace> | ||
); | ||
}; | ||
|
||
export default DarkMode; |
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,37 @@ | ||
import { createEffect, type Resource } from "solid-js"; | ||
|
||
export function useDarkMode(config: Resource<Config>) { | ||
const switchDark = (isDark: boolean) => { | ||
window.document.documentElement.classList.toggle("dark", isDark); | ||
}; | ||
|
||
const mediaQuery = window.matchMedia("(prefers-color-scheme: dark)"); | ||
|
||
createEffect(() => { | ||
const darkMode = config()?.dark_mode; | ||
if (!darkMode) return; | ||
|
||
const handleSystemTheme = (event?: MediaQueryListEvent) => { | ||
const isDark = event ? event.matches : mediaQuery.matches; | ||
switchDark(darkMode === "system" ? isDark : darkMode === "dark"); | ||
}; | ||
|
||
if (darkMode === "system") { | ||
mediaQuery.addEventListener("change", handleSystemTheme); | ||
handleSystemTheme(); | ||
|
||
return () => { | ||
// cleanup 移除监听器 | ||
mediaQuery.removeEventListener("change", handleSystemTheme); | ||
}; | ||
} | ||
switchDark(darkMode === "dark"); | ||
}); | ||
|
||
return { | ||
isDarkMode: () => | ||
config()?.dark_mode === "dark" || | ||
(config()?.dark_mode === "system" && | ||
matchMedia("(prefers-color-scheme: dark)").matches), | ||
}; | ||
} |
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