2022-09-29 01:59:28 +00:00
|
|
|
import { useEffect, useState } from 'react'
|
2022-10-01 20:32:01 +00:00
|
|
|
import { SearchIndex } from '../utils/search'
|
2022-10-14 14:55:06 +00:00
|
|
|
import Footer from './Footer'
|
2022-09-29 01:59:28 +00:00
|
|
|
import Header from './Header'
|
|
|
|
|
|
|
|
function darkModeDefault() {
|
|
|
|
if (typeof window === 'undefined') {
|
|
|
|
return false
|
|
|
|
} else {
|
|
|
|
const theme = localStorage.getItem('theme')
|
|
|
|
return (
|
|
|
|
theme === 'dark' ||
|
|
|
|
(!theme && window.matchMedia('(prefers-color-scheme: dark)').matches)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
interface LayoutProps {
|
|
|
|
children: React.ReactNode
|
2022-10-01 20:32:01 +00:00
|
|
|
searchIndex: SearchIndex
|
2022-09-29 01:59:28 +00:00
|
|
|
}
|
|
|
|
|
2022-10-01 20:32:01 +00:00
|
|
|
export default function Layout({ children, searchIndex }: LayoutProps) {
|
2022-09-29 01:59:28 +00:00
|
|
|
const [darkMode, setDarkMode] = useState(darkModeDefault())
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (darkMode) {
|
|
|
|
document.documentElement.classList.add('dark')
|
|
|
|
localStorage.setItem('theme', 'dark')
|
|
|
|
} else {
|
|
|
|
document.documentElement.classList.remove('dark')
|
|
|
|
localStorage.setItem('theme', 'light')
|
|
|
|
}
|
|
|
|
}, [darkMode])
|
|
|
|
|
|
|
|
useEffect(() => {
|
2023-02-26 01:12:11 +00:00
|
|
|
const darkQuery = window.matchMedia('(prefers-color-scheme: dark)')
|
2022-09-29 01:59:28 +00:00
|
|
|
|
|
|
|
darkQuery.onchange = (e) => {
|
|
|
|
if (e.matches) {
|
|
|
|
setDarkMode(true)
|
|
|
|
} else {
|
|
|
|
setDarkMode(false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<div className="sticky top-0 z-10 h-16">
|
2022-10-01 20:32:01 +00:00
|
|
|
<Header
|
|
|
|
searchIndex={searchIndex}
|
|
|
|
name="Conky"
|
|
|
|
darkMode={darkMode}
|
|
|
|
setDarkMode={setDarkMode}
|
|
|
|
/>
|
2022-09-29 01:59:28 +00:00
|
|
|
</div>
|
2022-10-14 14:55:06 +00:00
|
|
|
<div className="relative pb-4">
|
2022-09-30 21:15:06 +00:00
|
|
|
<div className="flex flex-col items-center max-w-3xl w-full mx-auto">
|
2022-09-29 01:59:28 +00:00
|
|
|
{children}
|
|
|
|
</div>
|
|
|
|
</div>
|
2022-10-14 14:55:06 +00:00
|
|
|
<div className="relative">
|
|
|
|
<Footer />
|
|
|
|
</div>
|
2022-09-29 01:59:28 +00:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|