-
-
Notifications
You must be signed in to change notification settings - Fork 238
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
1 parent
cabaee5
commit ef21bac
Showing
4 changed files
with
140 additions
and
28 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 |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { | ||
Box, | ||
Center, | ||
ColorScheme, | ||
SegmentedControl, | ||
Stack, | ||
useMantineColorScheme, | ||
} from "@mantine/core"; | ||
import { useColorScheme } from "@mantine/hooks"; | ||
import { useState } from "react"; | ||
import { TbDeviceLaptop, TbMoon, TbSun } from "react-icons/tb"; | ||
import usePreferences from "../../hooks/usePreferences"; | ||
|
||
const ThemeSwitcher = () => { | ||
const preferences = usePreferences(); | ||
const [colorScheme, setColorScheme] = useState( | ||
preferences.get("colorScheme") | ||
); | ||
const { toggleColorScheme } = useMantineColorScheme(); | ||
const systemColorScheme = useColorScheme(); | ||
|
||
return ( | ||
<Stack> | ||
<SegmentedControl | ||
value={colorScheme} | ||
onChange={(value) => { | ||
preferences.set("colorScheme", value); | ||
setColorScheme(value); | ||
toggleColorScheme( | ||
value == "system" ? systemColorScheme : (value as ColorScheme) | ||
); | ||
}} | ||
data={[ | ||
{ | ||
label: ( | ||
<Center> | ||
<TbMoon size={16} /> | ||
<Box ml={10}>Dark</Box> | ||
</Center> | ||
), | ||
value: "dark", | ||
}, | ||
{ | ||
label: ( | ||
<Center> | ||
<TbSun size={16} /> | ||
<Box ml={10}>Light</Box> | ||
</Center> | ||
), | ||
value: "light", | ||
}, | ||
{ | ||
label: ( | ||
<Center> | ||
<TbDeviceLaptop size={16} /> | ||
<Box ml={10}>System</Box> | ||
</Center> | ||
), | ||
value: "system", | ||
}, | ||
]} | ||
/> | ||
</Stack> | ||
); | ||
}; | ||
|
||
export default ThemeSwitcher; |
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,30 @@ | ||
const defaultPreferences = [ | ||
{ | ||
key: "colorScheme", | ||
value: "system", | ||
}, | ||
]; | ||
|
||
const get = (key: string) => { | ||
if (typeof window !== "undefined") { | ||
const preferences = JSON.parse(localStorage.getItem("preferences") ?? "{}"); | ||
return ( | ||
preferences[key] ?? | ||
defaultPreferences.find((p) => p.key == key)?.value ?? | ||
null | ||
); | ||
} | ||
}; | ||
|
||
const set = (key: string, value: string) => { | ||
if (typeof window !== "undefined") { | ||
const preferences = JSON.parse(localStorage.getItem("preferences") ?? "{}"); | ||
preferences[key] = value; | ||
localStorage.setItem("preferences", JSON.stringify(preferences)); | ||
} | ||
}; | ||
const usePreferences = () => { | ||
return { get, set }; | ||
}; | ||
|
||
export default usePreferences; |
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