UNPKG

@wordpress/compose

Version:
73 lines (71 loc) 2.26 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // packages/compose/src/hooks/use-media-query/index.js var use_media_query_exports = {}; __export(use_media_query_exports, { default: () => useMediaQuery }); module.exports = __toCommonJS(use_media_query_exports); var import_element = require("@wordpress/element"); var matchMediaCache = /* @__PURE__ */ new Map(); function getMediaQueryList(query) { if (!query) { return null; } let match = matchMediaCache.get(query); if (match) { return match; } if (typeof window !== "undefined" && typeof window.matchMedia === "function") { match = window.matchMedia(query); matchMediaCache.set(query, match); return match; } return null; } function useMediaQuery(query) { const source = (0, import_element.useMemo)(() => { const mediaQueryList = getMediaQueryList(query); return { /** @type {(onStoreChange: () => void) => () => void} */ subscribe(onStoreChange) { if (!mediaQueryList) { return () => { }; } mediaQueryList.addEventListener?.("change", onStoreChange); return () => { mediaQueryList.removeEventListener?.( "change", onStoreChange ); }; }, getValue() { return mediaQueryList?.matches ?? false; } }; }, [query]); return (0, import_element.useSyncExternalStore)( source.subscribe, source.getValue, () => false ); } //# sourceMappingURL=index.js.map