Skip to content

Commit cd95859

Browse files
theme
1 parent c7dee6a commit cd95859

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

custom-implementation/src/components/header.tsx

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use client'
22

3-
import { useEffect, useState } from 'react'
3+
import { useEffect, useState, useRef } from 'react'
44
import {
55
ComponentConfigContext,
66
HeaderV30 as SharedHeader,
@@ -16,10 +16,17 @@ const Header = ({
1616
}: // eslint-disable-next-line @typescript-eslint/no-explicit-any
1717
any) => {
1818
const html = document.getElementsByTagName('html')[0]
19-
const [theme, setTheme] = useState(html.getAttribute('class'))
19+
const [theme, setTheme] = useState(html.getAttribute('class') || 'light') // Default to 'light' if no class is present
20+
21+
const themeRef = useRef(theme)
22+
themeRef.current = theme
23+
2024
useEffect(() => {
2125
const observer = new MutationObserver(() => {
22-
setTheme(html.getAttribute('class'))
26+
const newTheme = html.getAttribute('class') || 'light'
27+
if (themeRef.current !== newTheme) {
28+
setTheme(newTheme)
29+
}
2330
})
2431

2532
const config = { attributes: true, attributeFilter: ['class'] }
@@ -39,7 +46,7 @@ any) => {
3946
logo={logo}
4047
items={links}
4148
actions={actions}
42-
version={theme === 'dark' ? 'light' : 'dark'}
49+
version={theme === 'dark' ? 'dark' : 'light'} // Ensure the theme is correctly reflected
4350
collapseOnScroll={collapseOnScroll}
4451
wrapperClassName="custom-header"
4552
/>

0 commit comments

Comments
 (0)