|
1 |
| -import React, { useLayoutEffect, useState } from 'react' |
| 1 | +import React, { useEffect, useState } from 'react' |
2 | 2 | import {
|
3 | 3 | ActionButton,
|
4 | 4 | defaultTheme,
|
5 | 5 | Provider as RSProvider
|
6 | 6 | } from '@adobe/react-spectrum'
|
7 | 7 | import Light from '@spectrum-icons/workflow/Light'
|
8 | 8 | import Moon from '@spectrum-icons/workflow/Moon'
|
9 |
| -import useIsMounted from 'react-is-mounted-hook' |
10 | 9 |
|
11 | 10 | function useCurrentColorScheme() {
|
12 | 11 | const [colorScheme, setColorScheme] = useState(undefined)
|
13 |
| - const isMounted = useIsMounted() |
14 |
| - useLayoutEffect(() => { |
15 |
| - setColorScheme(localStorage.theme || 'light') |
16 |
| - }, [isMounted]) |
17 | 12 |
|
18 |
| - useLayoutEffect(() => { |
| 13 | + useEffect(() => { |
| 14 | + const root = window.document.documentElement |
| 15 | + const initialColorValue = root.style.getPropertyValue('--initial-theme') |
| 16 | + setColorScheme(initialColorValue) |
| 17 | + |
19 | 18 | const mq = window.matchMedia('(prefers-color-scheme: dark)')
|
20 | 19 | const onChange = () => {
|
21 |
| - setColorScheme(localStorage.theme || (mq.matches ? 'dark' : 'light')) |
| 20 | + setColorScheme( |
| 21 | + localStorage.getItem('theme') || (mq.matches ? 'dark' : 'light') |
| 22 | + ) |
22 | 23 | }
|
23 | 24 |
|
24 | 25 | mq.addListener(onChange)
|
@@ -59,7 +60,8 @@ export const Provider = ({
|
59 | 60 | export const ThemeSwitcher = () => {
|
60 | 61 | const colorScheme = useCurrentColorScheme()
|
61 | 62 | const onPress = () => {
|
62 |
| - localStorage.theme = colorScheme === 'dark' ? 'light' : 'dark' |
| 63 | + const scheme = colorScheme === 'dark' ? 'light' : 'dark' |
| 64 | + localStorage.setItem('theme', scheme) |
63 | 65 | window.dispatchEvent(new Event('storage'))
|
64 | 66 | }
|
65 | 67 | const label =
|
|
0 commit comments