Skip to content

Commit

Permalink
still shit
Browse files Browse the repository at this point in the history
  • Loading branch information
adhamu committed Sep 26, 2023
1 parent 00ea3f5 commit a169688
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
13 changes: 12 additions & 1 deletion src/hooks/useTheme.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { useContext, useMemo, useState } from 'react'
import { useContext, useEffect, useMemo, useState } from 'react'

import { SettingsContext } from '../context/SettingsProvider'
import { getTheme } from '../theme'

export const useTheme = () => {
const {
setSetting,
settings: {
themeLight,
themeDark,
Expand All @@ -14,6 +15,10 @@ export const useTheme = () => {

const [theme, setTheme] = useState(getTheme(prefersDarkMode))

useEffect(() => {
setSetting('prefersDarkMode', prefersDarkMode)
}, [])

useMemo(() => {
let themeToSet

Expand All @@ -25,6 +30,9 @@ export const useTheme = () => {
.map(([key, value]) => `--startpage-${key}: ${value}`)
.join(';')}}`
)
document.styleSheets[0].insertRule(
localStorage.getItem('darkCssProperties') || ''
)
} else {
themeToSet = themeLight ?? getTheme(false)
window.localStorage.setItem(
Expand All @@ -33,6 +41,9 @@ export const useTheme = () => {
.map(([key, value]) => `--startpage-${key}: ${value}`)
.join(';')}}`
)
document.styleSheets[0].insertRule(
localStorage.getItem('lightCssProperties') || ''
)
}

if (JSON.stringify(themeToSet) !== JSON.stringify(theme)) {
Expand Down
3 changes: 2 additions & 1 deletion src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
</head>

<body>
<main id="app"></main>
<script>
function appendCssVariables() {
if (typeof window !== 'undefined') {
Expand Down Expand Up @@ -40,9 +39,11 @@
; (() => {
appendCssVariables()
document.body.classList.toggle('dark-mode', prefersDarkMode())
localStorage.setItem('prefersDarkMode', prefersDarkMode())
})()
</script>
<script type="module" src="./index.tsx"></script>
<main id="app"></main>
</body>

</html>

0 comments on commit a169688

Please sign in to comment.