Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions src/context/themeContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@ const ThemeContext = createContext(null)
// const { theme, setTheme, toggleTheme } = useTheme()
export const useTheme = () => useContext(ThemeContext)


const ThemeProvider = ({ children }) => {
// default theme: light
const DEFAULT_THEME = "light"
const localTheme = localStorage.getItem("theme")
const DEFAULT_THEME = 'light'
const localTheme = typeof window !== 'undefined' ? localStorage.getItem('theme') : undefined
const deviseTheme = getDeviseTheme()
const [theme, setTheme] = useState(localTheme || deviseTheme || DEFAULT_THEME)

Expand All @@ -48,11 +49,11 @@ const ThemeProvider = ({ children }) => {
// }, [])

useEffect(() => {
localStorage.setItem("theme", theme)
localStorage.setItem('theme', theme)
}, [theme])

const toggleTheme = () => {
setTheme(prevTheme => (prevTheme === "light" ? "dark" : "light"))
setTheme((prevTheme) => (prevTheme === 'light' ? 'dark' : 'light'))
}

return (
Expand All @@ -63,18 +64,21 @@ const ThemeProvider = ({ children }) => {
}

const getDeviseTheme = () => {
if (window.matchMedia) {
if (window.matchMedia('(prefers-color-scheme: dark)').matches) {
return 'dark'
if (typeof window !== 'undefined') {
if (window.matchMedia) {
if (window.matchMedia('(prefers-color-scheme: dark)').matches) {
return 'dark'
} else {
return 'light'
}
} else {
// cambiar si cambia el tema por defecto
return 'light'
}
} else {
// cambiar si cambia el tema por defecto
return 'light'
return undefined
}
}

ThemeProvider.propTypes = {
children: PropTypes.object
}
Expand Down