From 71f175679f56ed8ac640f4afa534a31b14c9ad57 Mon Sep 17 00:00:00 2001 From: Prince Eze Date: Wed, 11 Dec 2024 09:27:51 +0100 Subject: [PATCH] refactor: improve DarkModeSwitch theme handling --- src/theme/BackToTopButton/DarkModeSwitch.js | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/theme/BackToTopButton/DarkModeSwitch.js b/src/theme/BackToTopButton/DarkModeSwitch.js index 3f4c20297f..3087817499 100644 --- a/src/theme/BackToTopButton/DarkModeSwitch.js +++ b/src/theme/BackToTopButton/DarkModeSwitch.js @@ -1,4 +1,4 @@ -import React , {useEffect} from 'react' +import React, { useEffect } from 'react' import clsx from 'clsx' import { IconShapeSunLong, @@ -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 (