Skip to content

Commit

Permalink
refactor: improve DarkModeSwitch theme handling
Browse files Browse the repository at this point in the history
  • Loading branch information
princeeze committed Dec 11, 2024
1 parent 0c4f784 commit 71f1756
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/theme/BackToTopButton/DarkModeSwitch.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React , {useEffect} from 'react'
import React, { useEffect } from 'react'
import clsx from 'clsx'
import {
IconShapeSunLong,
Expand All @@ -7,12 +7,16 @@ import {
import { useColorMode } from '@docusaurus/theme-common'

export default function DarkModeSwitch() {

const { colorMode, setColorMode } = useColorMode(null)
useEffect(() => {
setColorMode(localStorage.getItem('theme'))
}, [])
useEffect(() => {
const savedTheme = localStorage.getItem('theme')
if (savedTheme !== colorMode) {
setColorMode(savedTheme)
}
}, [colorMode, setColorMode])

const isDark = colorMode === 'dark'

return (
<button
className={clsx(
Expand Down

0 comments on commit 71f1756

Please sign in to comment.