-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(app): add theme switcher (#383)
* feat(app): add theme switcher * fix(app): fix ThemeProvider build error * feat(app): add dark theme colors * refactor(app): remove violet constant * refactor(app): refactor ThemeProvider Co-authored-by: Michał Miszczyszyn <michal@mmiszy.pl>
- Loading branch information
Showing
11 changed files
with
150 additions
and
23 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
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,23 @@ | ||
"use client"; | ||
|
||
import { twMerge } from "tailwind-merge"; | ||
import { useThemeContext } from "../../providers/ThemeProvider"; | ||
|
||
export const DarkModeSwitch = () => { | ||
const { theme, changeTheme } = useThemeContext(); | ||
|
||
return ( | ||
<input | ||
type="checkbox" | ||
role="switch" | ||
aria-label="Switch between Dark and Light mode" | ||
className={twMerge( | ||
"relative h-6 w-10 cursor-pointer appearance-none rounded-full bg-violet-800 dark:bg-violet-700", | ||
"before:absolute before:top-1 before:left-1 before:h-[16px] before:w-[16px] before:rounded-full before:bg-white before:transition-all before:duration-200", | ||
"[&:checked]:before:left-[calc(100%-0.25rem)] [&:checked]:before:-translate-x-full", | ||
)} | ||
checked={theme === "dark"} | ||
onChange={() => changeTheme(theme === "dark" ? "light" : "dark")} | ||
/> | ||
); | ||
}; |
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,17 @@ | ||
import { createContext, useContext } from "react"; | ||
|
||
export const createSafeContext = <T>() => { | ||
const context = createContext<T | undefined>(undefined); | ||
|
||
const useSafeContext = () => { | ||
const ctx = useContext(context); | ||
|
||
if (ctx === undefined) { | ||
throw new Error("useContext must be use inside Provider!"); | ||
} | ||
|
||
return ctx; | ||
}; | ||
|
||
return [useSafeContext, context.Provider] as const; | ||
}; |
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,10 @@ | ||
import { ReactNode } from "react"; | ||
import { ThemeProvider } from "./ThemeProvider"; | ||
|
||
type AppProvidersProps = Readonly<{ | ||
children: ReactNode; | ||
}>; | ||
|
||
export const AppProviders = ({ children }: AppProvidersProps) => ( | ||
<ThemeProvider>{children}</ThemeProvider> | ||
); |
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,60 @@ | ||
"use client"; | ||
|
||
import { ReactNode, useEffect, useState } from "react"; | ||
import { createSafeContext } from "../lib/createSafeContext"; | ||
|
||
type Theme = "light" | "dark"; | ||
|
||
interface ThemeContextValue { | ||
theme: Theme; | ||
changeTheme: (theme: Theme) => void; | ||
} | ||
|
||
const [useThemeContext, ThemeContextProvider] = createSafeContext<ThemeContextValue>(); | ||
|
||
const getCurrentTheme = (): Theme => { | ||
const localStorageTheme = window.localStorage.getItem("theme"); | ||
|
||
if (localStorageTheme === "light" || localStorageTheme === "dark") { | ||
return localStorageTheme; | ||
} | ||
|
||
return window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light"; | ||
}; | ||
|
||
const ThemeProvider = ({ children }: { readonly children: ReactNode }) => { | ||
const [theme, setTheme] = useState<Theme>("light"); | ||
|
||
const changeTheme = (theme: Theme) => { | ||
window.localStorage.setItem("theme", theme); | ||
|
||
setTheme(theme); | ||
}; | ||
|
||
useEffect(() => { | ||
setTheme(getCurrentTheme()); | ||
}, []); | ||
|
||
useEffect(() => { | ||
const target = document.querySelector("html"); | ||
|
||
if (theme === "dark") { | ||
target?.classList.add("dark"); | ||
} else { | ||
target?.classList.remove("dark"); | ||
} | ||
}, [theme]); | ||
|
||
return ( | ||
<ThemeContextProvider | ||
value={{ | ||
theme, | ||
changeTheme, | ||
}} | ||
> | ||
{children} | ||
</ThemeContextProvider> | ||
); | ||
}; | ||
|
||
export { useThemeContext, ThemeProvider }; |
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 +1,6 @@ | ||
@import url("./tailwind.css"); | ||
@import url("./variables.css"); | ||
|
||
body { | ||
@apply bg-gray-50 dark:bg-neutral-800; | ||
} |
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,7 @@ | ||
:root { | ||
--primary: theme("colors.violet.700"); | ||
} | ||
|
||
:root.dark { | ||
--primary: theme("colors.violet.800"); | ||
} |
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