Skip to content

fix: header theme #51

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
May 17, 2024
2 changes: 1 addition & 1 deletion custom-implementation/src/components/header.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.custom-header {
background-color: white;
z-index: 60;
}

.custom-header nav a {
Expand Down
18 changes: 16 additions & 2 deletions custom-implementation/src/components/header.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use client'

import { useEffect, useState } from 'react'
import {
ComponentConfigContext,
HeaderV30 as SharedHeader,
Expand All @@ -11,10 +12,23 @@ const Header = ({
logo,
links,
actions,
version,
collapseOnScroll = true,
}: // eslint-disable-next-line @typescript-eslint/no-explicit-any
any) => {
const html = document.getElementsByTagName('html')[0]
const [theme, setTheme] = useState(html.getAttribute('class'))
useEffect(() => {
const observer = new MutationObserver(() => {
setTheme(html.getAttribute('class'))
})

const config = { attributes: true, attributeFilter: ['class'] }
observer.observe(html, config)
return () => {
observer.disconnect()
}
}, [html])

return (
<div>
<ComponentConfigContext.Provider
Expand All @@ -25,7 +39,7 @@ any) => {
logo={logo}
items={links}
actions={actions}
version={version}
version={theme === 'dark' ? 'light' : 'dark'}
collapseOnScroll={collapseOnScroll}
wrapperClassName="custom-header"
/>
Expand Down
8 changes: 6 additions & 2 deletions custom-implementation/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,20 @@ const render = async () => {
const sidenav = document.querySelector('button.fern-search-bar')
?.parentElement as HTMLElement

const theme = document.getElementsByTagName('html')[0].getAttribute('class')

if (!document.getElementById('theme-switch')) {
const wrapper = document.createElement('div')
wrapper.setAttribute('id', 'theme-switch')
sidenav.appendChild(wrapper)

ReactDOM.render(React.createElement(ThemeSwitch), wrapper)
}

ReactDOM.render(
React.createElement(Header, { ...data.header }),
React.createElement(Header, {
...data.header,
version: theme == 'dark' ? 'light' : 'dark',
}),
document.getElementById('fern-header'),
() => {
// Once the header component is loaded, make it visible
Expand Down
Loading