-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add sorting rule for search * feedback * fix use colors * pr feedback
- Loading branch information
Showing
13 changed files
with
139 additions
and
91 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
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 |
---|---|---|
@@ -1,49 +1,78 @@ | ||
import { useRecoilState, useRecoilValue, useSetRecoilState } from 'recoil' | ||
import { useRecoilState, useRecoilValue } from 'recoil' | ||
import { AppTheme, themeAtom } from '../themeAtom' | ||
import { darkTheme, lightTheme } from '../theme' | ||
import { useEffect } from 'react' | ||
|
||
export const useTheme = () => { | ||
const theme = useRecoilValue(themeAtom) | ||
const { theme } = useRecoilValue(themeAtom) | ||
return theme === AppTheme.dark ? darkTheme : lightTheme | ||
} | ||
|
||
export const useControlTheme = () => { | ||
const [theme, setTheme] = useRecoilState(themeAtom) | ||
const [{ theme }, setTheme] = useRecoilState(themeAtom) | ||
|
||
return { | ||
theme, | ||
set: setTheme, | ||
set: (value: AppTheme) => { | ||
setTheme({ | ||
theme: value, | ||
touched: true, | ||
}) | ||
}, | ||
setDarkTheme(enabled: boolean) { | ||
setTheme(enabled ? AppTheme.dark : AppTheme.light) | ||
setTheme({ | ||
theme: enabled ? AppTheme.dark : AppTheme.light, | ||
touched: true, | ||
}) | ||
}, | ||
setLightTheme(enabled: boolean) { | ||
setTheme({ | ||
theme: enabled ? AppTheme.light : AppTheme.dark, | ||
touched: true, | ||
}) | ||
}, | ||
toggle() { | ||
setTheme(theme === AppTheme.dark ? AppTheme.light : AppTheme.dark) | ||
setTheme({ | ||
theme: theme === AppTheme.dark ? AppTheme.light : AppTheme.dark, | ||
touched: true, | ||
}) | ||
}, | ||
} | ||
} | ||
|
||
export const useSubscribeDefaultAppTheme = () => { | ||
const setTheme = useSetRecoilState(themeAtom) | ||
const [{ touched }, setTheme] = useRecoilState(themeAtom) | ||
|
||
useEffect(() => { | ||
function handleChangeTheme(event) { | ||
if (event.matches) { | ||
setTheme(AppTheme.dark) | ||
setTheme({ theme: AppTheme.dark, touched: false }) | ||
} else { | ||
setTheme(AppTheme.light) | ||
setTheme({ theme: AppTheme.light, touched: false }) | ||
} | ||
} | ||
|
||
const media = window.matchMedia('(prefers-color-scheme: dark)') | ||
media.addEventListener('change', handleChangeTheme) | ||
if (!touched) { | ||
const media = window.matchMedia('(prefers-color-scheme: dark)') | ||
media.addEventListener('change', handleChangeTheme) | ||
|
||
if (media.matches) { | ||
setTheme(AppTheme.dark) | ||
} | ||
if (media.matches) { | ||
setTheme({ theme: AppTheme.dark, touched: false }) | ||
} | ||
|
||
return () => { | ||
media.removeEventListener('change', handleChangeTheme) | ||
return () => { | ||
media.removeEventListener('change', handleChangeTheme) | ||
} | ||
} | ||
}, [setTheme]) | ||
}, [touched, setTheme]) | ||
} | ||
|
||
export const useThemeClassName = () => { | ||
const { theme } = useRecoilValue(themeAtom) | ||
|
||
if (theme === AppTheme.dark) { | ||
return darkTheme.className | ||
} | ||
|
||
return lightTheme.className | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
export * from './theme' | ||
export { useTheme } from './hooks/useTheme' | ||
export * from './hooks/useTheme' | ||
export { useColors } from './hooks/useColors' | ||
export { useThemeClassName } from './hooks/useThemeClassName' |
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 |
---|---|---|
@@ -1,12 +1,17 @@ | ||
import { atom } from 'recoil' | ||
import { __DARK_MODE_ENABLED_BY_DEFAULT__ } from '../../util/constants' | ||
import { localStorageEffect } from '../../util/localStorageEffect' | ||
|
||
export enum AppTheme { | ||
dark = 'dark', | ||
light = 'light', | ||
} | ||
|
||
export const themeAtom = atom<AppTheme>({ | ||
export const themeAtom = atom<{ theme: AppTheme; touched: boolean }>({ | ||
key: '@theme', | ||
default: __DARK_MODE_ENABLED_BY_DEFAULT__ ? AppTheme.dark : AppTheme.light, | ||
default: { | ||
theme: __DARK_MODE_ENABLED_BY_DEFAULT__ ? AppTheme.dark : AppTheme.light, | ||
touched: false, | ||
}, | ||
effects_UNSTABLE: [localStorageEffect('@theme')], | ||
}) |
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
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,16 @@ | ||
export const localStorageEffect = | ||
(key) => | ||
({ setSelf, onSet }) => { | ||
if (typeof window !== 'undefined') { | ||
const savedValue = localStorage.getItem(key) | ||
if (savedValue != null) { | ||
setSelf(JSON.parse(savedValue)) | ||
} | ||
|
||
onSet((newValue, _, isReset) => { | ||
isReset | ||
? localStorage.removeItem(key) | ||
: localStorage.setItem(key, JSON.stringify(newValue)) | ||
}) | ||
} | ||
} |