25576b0be6
Checks (magui2.0) / python-lint (push) Failing after 1s
Checks (magui2.0) / python-test (push) Failing after 0s
Checks (magui2.0) / frontend-lint (push) Failing after 0s
CodeQL Advanced / Analyze (actions) (push) Failing after 1s
Checks (magui2.0) / python-format (push) Failing after 1s
CodeQL Advanced / Analyze (python) (push) Failing after 0s
Checks (magui2.0) / python-pyright (push) Failing after 1s
Checks (magui2.0) / frontend-format (push) Failing after 1s
Checks (magui2.0) / frontend-typecheck (push) Failing after 0s
Checks (magui2.0) / frontend-test (push) Failing after 2s
CodeQL Advanced / Analyze (javascript-typescript) (push) Failing after 1s
28 lines
794 B
TypeScript
28 lines
794 B
TypeScript
import { useCallback, useSyncExternalStore } from 'react'
|
|
|
|
/**
|
|
* Hook to track media query state
|
|
* @param query - CSS media query string (e.g., '(min-width: 768px)')
|
|
* @returns boolean indicating if the query matches
|
|
*/
|
|
export function useMediaQuery(query: string): boolean {
|
|
const subscribe = useCallback(
|
|
(callback: () => void) => {
|
|
const mediaQuery = window.matchMedia(query)
|
|
mediaQuery.addEventListener('change', callback)
|
|
return () => mediaQuery.removeEventListener('change', callback)
|
|
},
|
|
[query]
|
|
)
|
|
|
|
const getSnapshot = useCallback(() => {
|
|
return window.matchMedia(query).matches
|
|
}, [query])
|
|
|
|
const getServerSnapshot = useCallback(() => {
|
|
return false
|
|
}, [])
|
|
|
|
return useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot)
|
|
}
|