-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Preload theme to prevent screen flicker (#183)
* Preload theme to prevent screen flicker * refactor: extract localStorageCache
- Loading branch information
Showing
9 changed files
with
90 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { ColorScheme, Theme, isColorScheme, isTheme } from './model' | ||
import { Dispatch, useEffect } from 'react' | ||
import { Spec, useChromeStorageWithCache } from '../infrastructure/chromeStorage' | ||
import { getOrInitialValue } from '../infrastructure/localStorageCache' | ||
|
||
const selectedThemeSpec: Spec<Theme> = { | ||
areaName: 'sync', | ||
key: 'v3.selectedTheme', | ||
initialValue: 'standard', | ||
isType: isTheme, | ||
} | ||
|
||
export const useSelectedTheme = (): [Theme, Dispatch<Theme>] => { | ||
const [theme, setTheme] = useChromeStorageWithCache(selectedThemeSpec) | ||
useEffect(() => { | ||
document.documentElement.dataset['theme'] = theme | ||
}, [theme]) | ||
return [theme, setTheme] | ||
} | ||
|
||
const selectedColorSchemeSpec: Spec<ColorScheme> = { | ||
areaName: 'sync', | ||
key: 'v3.selectedColorScheme', | ||
initialValue: 'auto', | ||
isType: isColorScheme, | ||
} | ||
|
||
export const useSelectedColorScheme = (): [ColorScheme, Dispatch<ColorScheme>] => { | ||
const [colorScheme, setColorScheme] = useChromeStorageWithCache(selectedColorSchemeSpec) | ||
useEffect(() => { | ||
document.documentElement.dataset['colorScheme'] = colorScheme | ||
}, [colorScheme]) | ||
return [colorScheme, setColorScheme] | ||
} | ||
|
||
export const preloadFromCache = () => { | ||
document.documentElement.dataset['theme'] = getOrInitialValue(selectedThemeSpec) | ||
document.documentElement.dataset['colorScheme'] = getOrInitialValue(selectedColorSchemeSpec) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { Dispatch, useEffect, useState } from 'react' | ||
|
||
type Spec<T extends string> = { | ||
key: string | ||
initialValue: T | ||
isType: (value: unknown) => value is T | ||
} | ||
|
||
export const useLocalStorageCache = <T extends string>(spec: Spec<T>): [T, Dispatch<T>] => { | ||
const [value, setValue] = useState<T>(getOrInitialValue(spec)) | ||
useEffect(() => { | ||
localStorage.setItem(spec.key, value) | ||
}, [spec.key, value]) | ||
return [value, setValue] | ||
} | ||
|
||
export const getOrInitialValue = <T extends string>(spec: Spec<T>): T => { | ||
const cachedValue = localStorage.getItem(spec.key) | ||
if (spec.isType(cachedValue)) { | ||
return cachedValue | ||
} | ||
return spec.initialValue | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters