-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: respond to the change of system theme appearance
- Loading branch information
Showing
8 changed files
with
165 additions
and
73 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
'@rspress/theme-default': minor | ||
'@rspress/core': minor | ||
--- | ||
|
||
feat: respond to the change of system theme appearance |
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
27 changes: 1 addition & 26 deletions
27
packages/theme-default/src/components/SwitchAppearance/index.tsx
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,10 @@ | ||
import { useRef } from 'react'; | ||
|
||
/** | ||
* Create a memoized handler function with stable reference | ||
*/ | ||
export const useHandler = <T extends (...args: any[]) => any>(handler: T) => { | ||
const handlerRef = useRef<T>(handler); | ||
handlerRef.current = handler; | ||
return useRef(((...args: any[]) => handlerRef.current(...args)) as T).current; | ||
}; |
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,21 @@ | ||
import { useEffect, useState } from 'react'; | ||
|
||
/** | ||
* Wrapper of CSS Media Query API | ||
*/ | ||
export const useMediaQuery = (query: string) => { | ||
const [matches, setMatches] = useState(() => { | ||
return typeof window !== 'undefined' | ||
? window.matchMedia(query).matches | ||
: false; | ||
}); | ||
|
||
useEffect(() => { | ||
const mediaQueryList = window.matchMedia(query); | ||
const listener = (e: MediaQueryListEvent) => setMatches(e.matches); | ||
mediaQueryList.addEventListener('change', listener); | ||
return () => mediaQueryList.removeEventListener('change', listener); | ||
}, [query]); | ||
|
||
return matches; | ||
}; |
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,42 @@ | ||
import { useCallback, useEffect, useState } from 'react'; | ||
|
||
/** | ||
* Read/update the value in localStorage, and keeping it in sync with other tabs. | ||
*/ | ||
export const useStorageValue = (key: string, defaultValue = null) => { | ||
const [value, setValueInternal] = useState(() => { | ||
if (typeof window === 'undefined') { | ||
return defaultValue; | ||
} | ||
return localStorage.getItem(key) ?? defaultValue; | ||
}); | ||
|
||
const setValue = useCallback( | ||
value => { | ||
setValueInternal(prev => { | ||
const next = typeof value === 'function' ? value(prev) : value; | ||
if (next == null) { | ||
localStorage.removeItem(key); | ||
} else { | ||
localStorage.setItem(key, next); | ||
} | ||
return next; | ||
}); | ||
}, | ||
[key], | ||
); | ||
|
||
useEffect(() => { | ||
const listener = (e: StorageEvent) => { | ||
if (e.key === key) { | ||
setValueInternal(localStorage.getItem(key) ?? defaultValue); | ||
} | ||
}; | ||
window.addEventListener('storage', listener); | ||
return () => { | ||
window.removeEventListener('storage', listener); | ||
}; | ||
}, [key, defaultValue]); | ||
|
||
return [value, setValue] as const; | ||
}; |