Files
supabase/apps/www/lib/theme.utils.ts
T
919427231b no longer forcing theme in next/theme (#22817)
* init

* Update index.tsx

* Update apps/www/pages/ga.tsx

Co-authored-by: Jonathan Summers-Muir <MildTomato@users.noreply.github.com>

* Update apps/www/pages/ga.tsx

Co-authored-by: Jonathan Summers-Muir <MildTomato@users.noreply.github.com>

* Update apps/www/pages/ga-week/index.tsx

Co-authored-by: Jonathan Summers-Muir <MildTomato@users.noreply.github.com>

* Update apps/www/pages/ga-week/index.tsx

Co-authored-by: Jonathan Summers-Muir <MildTomato@users.noreply.github.com>

* Update index.tsx

* Update ga.tsx

* hack

* Update index.tsx

* use hook and check router

* update import

---------

Co-authored-by: Francesco Sansalvadore <f.sansalvadore@gmail.com>
Co-authored-by: Francesco Sansalvadore <francescosansalvadore@MacBook-Pro-2.local>
2024-04-17 12:39:15 +02:00

44 lines
1.4 KiB
TypeScript

import { useEffect } from 'react'
import { useTheme } from 'next-themes'
import { useRouter } from 'next/router'
export function useForceDeepDark() {
const router = useRouter()
const { resolvedTheme, theme } = useTheme()
const isDarkTheme = resolvedTheme?.includes('dark')
const isGaSection = router.pathname.includes('/ga-week') || router.pathname === '/ga'
useEffect(() => {
const handleDocumentLoad = () => {
// Update the HTML element attributes
const theme = isDarkTheme ? (isGaSection ? 'deep-dark' : 'dark') : 'light'
document.documentElement.setAttribute('data-theme', theme)
document.documentElement.style.colorScheme = theme
// wait before setting the theme
setTimeout(() => {
document.documentElement.setAttribute('data-theme', theme)
document.documentElement.style.colorScheme = theme
}, 200)
// Clean up the event listener
window.removeEventListener('load', handleDocumentLoad)
}
// Check if document is already loaded
if (document.readyState === 'complete') {
handleDocumentLoad()
} else {
// Add a global load event listener
window.addEventListener('load', handleDocumentLoad)
}
// Clean up the event listener on component unmount
return () => {
window.removeEventListener('load', handleDocumentLoad)
}
}, [resolvedTheme, theme, isDarkTheme, router])
}