Spaces:
Paused
Paused
Download web/src/hooks/useMediaQuery.ts from bolikoto/daili-usage: direct link, hf CLI and curl.
- Browser
- Download file 681 Bytes
-
https://huggingface.co/spaces/bolikoto/daili-usage/resolve/main/web/src/hooks/useMediaQuery.ts
- Command line
-
hf download hf://spaces/bolikoto/daili-usage/web/src/hooks/useMediaQuery.ts
-
curl -L -o useMediaQuery.ts https://huggingface.co/spaces/bolikoto/daili-usage/resolve/main/web/src/hooks/useMediaQuery.ts
681 Bytes
| /** | |
| * 媒体查询 Hook | |
| */ | |
| import { useState, useEffect } from 'react'; | |
| export function useMediaQuery(query: string): boolean { | |
| const [matches, setMatches] = useState(() => { | |
| return window.matchMedia(query).matches; | |
| }); | |
| useEffect(() => { | |
| const media = window.matchMedia(query); | |
| const listener = (event: MediaQueryListEvent) => { | |
| setMatches(event.matches); | |
| }; | |
| // Set initial value via listener to avoid direct setState in effect | |
| listener({ matches: media.matches } as MediaQueryListEvent); | |
| media.addEventListener('change', listener); | |
| return () => media.removeEventListener('change', listener); | |
| }, [query]); | |
| return matches; | |
| } | |