mirror of
https://github.com/linsa-io/linsa.git
synced 2026-03-17 23:03:55 +01:00
24 lines
556 B
TypeScript
24 lines
556 B
TypeScript
import * as React from "react"
|
|
|
|
export function useMedia(query: string): boolean {
|
|
const [matches, setMatches] = React.useState<boolean>(false)
|
|
|
|
React.useEffect(() => {
|
|
if (window.matchMedia) {
|
|
const media = window.matchMedia(query)
|
|
if (media.matches !== matches) {
|
|
setMatches(media.matches)
|
|
}
|
|
const listener = () => {
|
|
setMatches(media.matches)
|
|
}
|
|
media.addListener(listener)
|
|
return () => media.removeListener(listener)
|
|
}
|
|
|
|
return undefined
|
|
}, [matches, query])
|
|
|
|
return matches
|
|
}
|