forked from pancakeswap/pancake-swap-interface-v1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThemeContext.tsx
34 lines (27 loc) · 1 KB
/
ThemeContext.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import React, { useState } from 'react'
import { ThemeProvider as SCThemeProvider } from 'styled-components'
import { light, dark } from '@pancakeswap-libs/uikit'
const CACHE_KEY = 'IS_DARK'
export interface ThemeContextType {
isDark: boolean;
toggleTheme: () => void;
}
const ThemeContext = React.createContext<ThemeContextType>({ isDark: false, toggleTheme: () => null })
const ThemeContextProvider: React.FC = ({ children }) => {
const [isDark, setIsDark] = useState(() => {
const isDarkUserSetting = localStorage.getItem(CACHE_KEY)
return isDarkUserSetting ? JSON.parse(isDarkUserSetting) : false
})
const toggleTheme = () => {
setIsDark((prevState: any) => {
localStorage.setItem(CACHE_KEY, JSON.stringify(!prevState))
return !prevState
})
}
return (
<ThemeContext.Provider value={{ isDark, toggleTheme }}>
<SCThemeProvider theme={isDark ? dark : light}>{children}</SCThemeProvider>
</ThemeContext.Provider>
)
}
export { ThemeContext, ThemeContextProvider }