foxact
Version:
React Hooks/Utils done right. For browser, SSR, and React Server Components.
2 lines • 725 B
JavaScript
'use client';
import{noSSRError as e}from"../no-ssr/index.mjs";import{noop as n}from"../noop/index.mjs";import{useCallback as t,useSyncExternalStore as o}from"react";const r=new Map;function i(){throw e("useMediaQuery cannot be used on the server without a serverValue")}function d(e,d){return"undefined"==typeof window||r.has(e)||r.set(e,window.matchMedia(e).matches),o(t(t=>(function(e,t){if("undefined"==typeof window)return n;const o=window.matchMedia(e),i=()=>{r.set(e,o.matches),t()};return o.addEventListener("change",i),()=>{o.removeEventListener("change",i)}})(e,t),[e]),()=>{var n;return"undefined"!=typeof window&&(null!=(n=r.get(e))?n:window.matchMedia(e).matches)},void 0===d?i:()=>d)}export{d as useMediaQuery};