UNPKG

@guruhotel/aura-hooks

Version:

🪝 Hooks library designed by the Guruhotel team for Aura UI

1 lines 594 B
import{useState as e,useEffect as t,useRef as n}from"react";export function useMediaQuery(r,i,{getInitialValueInEffect:a}={getInitialValueInEffect:!0}){const[c,o]=e(!a&&function(e,t){return"boolean"==typeof t?t:"undefined"!=typeof window&&"matchMedia"in window&&window.matchMedia(e).matches}(r,i)),d=n();return t((()=>{if("matchMedia"in window)return d.current=window.matchMedia(r),o(d.current.matches),function(e,t){try{return e.addEventListener("change",t),()=>e.removeEventListener("change",t)}catch(n){return e.addListener(t),()=>e.removeListener(t)}}(d.current,(e=>o(e.matches)))}),[r]),c}