Skip to content

Commit

Permalink
Leave it at that
Browse files Browse the repository at this point in the history
  • Loading branch information
adhamu committed Sep 26, 2023
1 parent 0921e5f commit 81d0cf7
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 45 deletions.
9 changes: 5 additions & 4 deletions src/Wrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,18 @@ import { globalStyles } from './theme'

import type { ReactNode } from 'react'

const matchMediaFallback = (): boolean =>
window.matchMedia?.('(prefers-color-scheme: dark)')?.matches

const Wrapper = ({ children }: { children: ReactNode }) => {
const {
settings: { prefersDarkMode = matchMediaFallback() },
settings: { prefersDarkMode },
setSetting,
} = useContext(SettingsContext)

const theme = useTheme()

if (!theme) {
return null
}

return (
<ThemeProvider theme={theme}>
<Global styles={globalStyles(theme)} />
Expand Down
2 changes: 1 addition & 1 deletion src/components/DarkModeToggle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const ToggleArea = styled(Switch)`
`

type Props = {
isDarkMode: boolean
isDarkMode?: boolean
toggleDarkMode: () => void
}

Expand Down
3 changes: 1 addition & 2 deletions src/components/Preferences/Theme.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import styled from '@emotion/styled'
import Button from './Button'
import { SettingsContext } from '../../context/SettingsProvider'
import { useTheme } from '../../hooks/useTheme'
import { matchMediaFallback } from '../../utils'

import type { Theme } from '@emotion/react'

Expand Down Expand Up @@ -48,7 +47,7 @@ type AvailableOption =

const Themer = () => {
const {
settings: { prefersDarkMode = matchMediaFallback() },
settings: { prefersDarkMode },
setSetting,
deleteSetting,
} = useContext(SettingsContext)
Expand Down
4 changes: 2 additions & 2 deletions src/context/SettingsProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ const SettingsProvider = ({ children }: SettingsProviderProps) => {
})()
}, [])

const setSetting = <
const setSetting = async <
T extends string | boolean | BookmarkLink[] | undefined | Theme,
>(
setting: string,
value: T
) => {
set(setting, value, store).then(() => {
await set(setting, value, store).then(() => {
setSettings(prevState => ({
...prevState,
[setting]: value,
Expand Down
43 changes: 25 additions & 18 deletions src/hooks/useTheme.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,37 @@
import { useContext, useEffect, useState } from 'react'
import { useContext, useMemo, useState } from 'react'

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

import type { Theme } from '@emotion/react'

export const useTheme = (): Theme => {
export const useTheme = () => {
const {
settings: { themeLight, themeDark, prefersDarkMode = matchMediaFallback() },
settings: { themeLight, themeDark, prefersDarkMode },
} = useContext(SettingsContext)
const [theme, setTheme] = useState(getTheme(prefersDarkMode))

useEffect(() => {
const refreshTheme = () => {
if (prefersDarkMode) {
return themeDark !== undefined
? setTheme(themeDark)
: setTheme(getTheme(true))
}

return themeLight !== undefined
? setTheme(themeLight)
: setTheme(getTheme(false))

const [theme, setTheme] = useState<Theme>()

useMemo(() => {
let themeToSet

if (
prefersDarkMode ||
window.matchMedia?.('(prefers-color-scheme: dark)')?.matches
) {
themeToSet = themeDark ?? getTheme(true)
}

if (
!prefersDarkMode ||
!window.matchMedia?.('(prefers-color-scheme: dark)')?.matches
) {
themeToSet = themeLight ?? getTheme(false)
}

if (JSON.stringify(themeToSet) !== JSON.stringify(theme)) {
setTheme(themeToSet)
}
refreshTheme()
}, [prefersDarkMode, themeLight, themeDark])

return theme
Expand Down
34 changes: 18 additions & 16 deletions src/index.html
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
<!doctype html>
<html lang="en">
<head>
<title>Start 🚀</title>
<meta charset="utf-8" />
<meta name="description" content="Start Page" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="preconnect" href="https://fonts.gstatic.com" />
<link
href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:wght@400;600&display=swap"
rel="stylesheet"
/>
</head>

<body>
<main id="app"></main>
<script type="module" src="./index.tsx"></script>
</body>
</html>
<head>
<title>Start 🚀</title>
<meta charset="utf-8" />
<meta name="description" content="Start Page" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="preconnect" href="https://fonts.gstatic.com" />
<link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:wght@400;600&display=swap" rel="stylesheet" />
<style>

</style>
</head>

<body>
<main id="app"></main>
<script type="module" src="./index.tsx"></script>
</body>

</html>
2 changes: 0 additions & 2 deletions src/utils.ts

This file was deleted.

0 comments on commit 81d0cf7

Please sign in to comment.