-
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.
Merge pull request #4 from deriv-com/meenu-implement-theme
- Loading branch information
Showing
5 changed files
with
73 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { useContext } from "react"; | ||
import { ThemeContext } from "../providers/theme/themeContext"; | ||
|
||
const UseTheme = () => { | ||
const { theme, toggleTheme } = useContext(ThemeContext); | ||
return { theme, toggleTheme }; | ||
}; | ||
|
||
export default UseTheme; |
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,11 @@ | ||
import React, { type Dispatch, type SetStateAction } from "react"; | ||
|
||
export type Theme = "dark" | "light"; | ||
|
||
export const initialThemeState = { | ||
theme: "light" as Theme, | ||
setTheme: (() => null) as Dispatch<SetStateAction<Theme>>, | ||
toggleTheme: (() => null) as () => void, | ||
}; | ||
|
||
export const ThemeContext = React.createContext(initialThemeState); |
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,36 @@ | ||
import { useEffect, useState, PropsWithChildren } from "react"; | ||
import { ThemeContext, initialThemeState } from "./themeContext"; | ||
import { Theme } from "./themeContext"; | ||
|
||
export const ThemeProvider = ({ children }: PropsWithChildren) => { | ||
const savedThemeLocal = window?.localStorage.getItem( | ||
"globalTheme", | ||
) as Theme; | ||
const [theme, setTheme] = useState<Theme>( | ||
savedThemeLocal ?? initialThemeState.theme, | ||
); | ||
|
||
const applyThemeClass = (newTheme: Theme) => { | ||
document.body.classList.remove(`theme--${theme}`); | ||
document.body.classList.add(`theme--${newTheme}`); | ||
}; | ||
|
||
const toggleTheme = () => { | ||
const newTheme = theme === "dark" ? "light" : "dark"; | ||
setTheme(newTheme); | ||
applyThemeClass(newTheme); | ||
}; | ||
|
||
useEffect(() => { | ||
if (!document.body.classList.contains(`theme--${theme}`)) { | ||
applyThemeClass(theme); | ||
} | ||
window?.localStorage.setItem("globalTheme", theme); | ||
}, [theme]); | ||
|
||
return ( | ||
<ThemeContext.Provider value={{ theme, setTheme, toggleTheme }}> | ||
{children} | ||
</ThemeContext.Provider> | ||
); | ||
}; |
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,13 +1,16 @@ | ||
import React from "react"; | ||
import ReactDOM from "react-dom/client"; | ||
import { Typography, H1, Hero, BodyText } from "../dist/components/Typography"; | ||
import { Typography, H1, Hero } from "../dist/components/Typography"; | ||
import { ThemeProvider } from "../lib/components/providers/theme/themeProvider"; | ||
|
||
ReactDOM.createRoot(document.getElementById("root")!).render( | ||
<React.StrictMode> | ||
<Typography as="span">Quill UI</Typography> | ||
<Hero>Quill UI</Hero> | ||
<H1>Quill UI</H1> | ||
<H1 as="div">Quill UI - h1 as div</H1> | ||
<H1 as="span">Quill UI - h1 as span</H1> | ||
<ThemeProvider> | ||
<Typography as="span">Quill UI</Typography> | ||
<Hero>Quill UI</Hero> | ||
<H1>Quill UI</H1> | ||
<H1 as="div">Quill UI - h1 as div</H1> | ||
<H1 as="span">Quill UI - h1 as span</H1> | ||
</ThemeProvider> | ||
</React.StrictMode>, | ||
); |