@guruhotel/aura-hooks
Version:
🪝 Hooks library designed by the Guruhotel team for Aura UI
1 lines • 1 kB
JavaScript
import e,{useState as t,useEffect as r,createContext as n,useContext as o}from"react";import{jsx as s}from"react/jsx-runtime";const c={},i=n(c);export const BreakpointProvider=({children:e,queries:n})=>{const[o,c]=t({});return r((()=>{var e;const t={},r=Object.keys(n);let o=!1;const s=()=>{const e=r.reduce(((e,r)=>{var n;return e[r]=!(null==(n=t[r])||!n.matches),e}),{});c(e)};if(null!=(e=window)&&e.matchMedia){const e={};r.forEach((r=>{"string"==typeof n[r]?(t[r]=window.matchMedia(n[r]),e[r]=t[r].matches):e[r]=!1})),c(e),o=!0,r.forEach((e=>{"string"==typeof n[e]&&t[e].addListener(s)}))}return()=>{o&&r.forEach((e=>{"string"==typeof n[e]&&t[e].removeListener(s)}))}}),[n]),s(i.Provider,{value:o,children:e})};export const useBreakpoint=()=>{const e=o(i);if(e===c)throw new Error("useBreakpoint must be used within BreakpointProvider");const t=Object.keys(e).findIndex((t=>!1===e[t]));return{currentScreen:-1!==t?Object.keys(e)[t-1]:Object.keys(e)[Object.keys(e).length-1],breakpointContext:e}};