UNPKG

@navikt/ds-react

Version:

React components from the Norwegian Labour and Welfare Administration.

31 lines 1.06 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useMedia = exports.noMatchMedia = void 0; const react_1 = require("react"); exports.noMatchMedia = typeof window !== "undefined" && window.matchMedia === undefined; /** * @example useMedia("screen and (min-width: 1024px)") * @param media string * @param fallback boolean * @returns boolean | undefined */ const useMedia = (media, fallback) => { const [matches, setMatches] = (0, react_1.useState)(fallback); (0, react_1.useEffect)(() => { if (exports.noMatchMedia) { return; } const mediaQueryList = window.matchMedia(media); setMatches(mediaQueryList.matches); const listener = (evt) => { setMatches(evt.matches); }; mediaQueryList.addEventListener("change", listener); return () => { mediaQueryList.removeEventListener("change", listener); }; }, [media]); return matches; }; exports.useMedia = useMedia; //# sourceMappingURL=useMedia.js.map