diff options
| author | Armand Philippot <git@armandphilippot.com> | 2023-10-27 18:07:45 +0200 |
|---|---|---|
| committer | Armand Philippot <git@armandphilippot.com> | 2023-11-11 18:15:27 +0100 |
| commit | 05f1dfc6896d3affa7c494a1b955f230d836a4b7 (patch) | |
| tree | 3089d5c3145f241293b88b9a1bfe4bb85e8ca9e0 /src/utils/hooks/use-match-media | |
| parent | 757201fdc5c04a3f15504f74bf8ab85bb6018c2b (diff) | |
feat: replace next-themes with a custom ThemeProvider
To be honest, next-themes was working fine. However since I use a theme
provider for Prism code blocks, some code is duplicated between this
app and the library. So I prefer to use a custom Provider without the
options I don't need.
Diffstat (limited to 'src/utils/hooks/use-match-media')
| -rw-r--r-- | src/utils/hooks/use-match-media/index.ts | 1 | ||||
| -rw-r--r-- | src/utils/hooks/use-match-media/use-match-media.ts | 17 |
2 files changed, 18 insertions, 0 deletions
diff --git a/src/utils/hooks/use-match-media/index.ts b/src/utils/hooks/use-match-media/index.ts new file mode 100644 index 0000000..7d79e64 --- /dev/null +++ b/src/utils/hooks/use-match-media/index.ts @@ -0,0 +1 @@ +export * from './use-match-media'; diff --git a/src/utils/hooks/use-match-media/use-match-media.ts b/src/utils/hooks/use-match-media/use-match-media.ts new file mode 100644 index 0000000..50ac038 --- /dev/null +++ b/src/utils/hooks/use-match-media/use-match-media.ts @@ -0,0 +1,17 @@ +import { useEffect } from 'react'; + +export type UseMatchMediaCallback = (ev: MediaQueryListEvent) => void; + +/** + * React hook to watch for media changes based on the given query. + * + * @param {string} query - A media query. + * @param {UseMatchMediaCallback} cb - A callback function to execute on change. + */ +export const useMatchMedia = (query: string, cb: UseMatchMediaCallback) => { + useEffect(() => { + window.matchMedia(query).addEventListener('change', cb); + + return () => window.matchMedia(query).removeEventListener('change', cb); + }, [cb, query]); +}; |
