{"version":3,"file":"index.mjs","sources":[""],"sourcesContent":["import { getWindow } from \"ssr-window\";\n\nexport type TOnMediaQueryChangeArgs = Parameters<typeof onMediaQueryChange>;\n\nexport type TOnMediaQueryChangeReturn = ReturnType<typeof onMediaQueryChange>;\n\n/**\n * Creates a managed listener for changes to a CSS media query.\n * It is a no-op when `matchMedia` is unavailable, including SSR.\n * @param {string} query Media query\n * @param {(event: MediaQueryListEvent) => void} callback Change callback\n * @param {boolean} [isAutoInit=true] Attach immediately\n * @returns {{ mediaQueryList: MediaQueryList|null; handler: (event: MediaQueryListEvent) => void; addListener: () => void; removeListener: () => void; }} Controls\n * @throws {TypeError} onMediaQueryChange: arguments are invalid\n * @example\n * onMediaQueryChange(\"(prefers-color-scheme: dark)\", ({ matches }) => setDark(matches));\n * @example\n * // React to a desktop breakpoint without leaking a matchMedia listener\n * const desktop = onMediaQueryChange(\"(min-width: 1024px)\", ({ matches }) => {\n *   setSidebarExpanded(matches);\n * });\n * desktop.removeListener();\n */\nexport const onMediaQueryChange = (\n  query: string,\n  callback: (event: MediaQueryListEvent) => void,\n  isAutoInit: boolean = true\n): {\n  addListener: () => void;\n  handler: (event: MediaQueryListEvent) => void;\n  mediaQueryList: MediaQueryList | null;\n  removeListener: () => void;\n} => {\n  if (typeof query !== \"string\" || query.length === 0) {\n    throw new TypeError(\"onMediaQueryChange: query must be a non-empty string\");\n  }\n  if (typeof callback !== \"function\") {\n    throw new TypeError(\"onMediaQueryChange: callback must be a function\");\n  }\n  if (typeof isAutoInit !== \"boolean\") {\n    throw new TypeError(\"onMediaQueryChange: isAutoInit must be a boolean\");\n  }\n  const window = getWindow();\n  const mediaQueryList = typeof window.matchMedia === \"function\" ? window.matchMedia(query) : null;\n  const handler = (event: MediaQueryListEvent): void => callback(event);\n  const addListener = (): void => mediaQueryList?.addEventListener(\"change\", handler);\n  const removeListener = (): void => mediaQueryList?.removeEventListener(\"change\", handler);\n  if (isAutoInit) {\n    addListener();\n  }\n  return {\n    addListener, handler, mediaQueryList, removeListener,\n  };\n};\n"],"names":["onMediaQueryChange","query","callback","isAutoInit","length","TypeError","window","getWindow","mediaQueryList","matchMedia","handler","event","addListener","addEventListener","removeListener","removeEventListener"],"mappings":"kCAuBO,MAAMA,mBAAqB,CAChCC,MACAC,SACAC,WAAsB,QAOtB,UAAWF,QAAU,UAAYA,MAAMG,SAAW,EAChD,MAAM,IAAIC,UAAU,wDAEtB,UAAWH,WAAa,WACtB,MAAM,IAAIG,UAAU,mDAEtB,UAAWF,aAAe,UACxB,MAAM,IAAIE,UAAU,oDAEtB,MAAMC,OAASC,YACf,MAAMC,sBAAwBF,OAAOG,aAAe,WAAaH,OAAOG,WAAWR,OAAS,KAC5F,MAAMS,QAAWC,OAAqCT,SAASS,OAC/D,MAAMC,YAAc,IAAYJ,gBAAgBK,iBAAiB,SAAUH,SAC3E,MAAMI,eAAiB,IAAYN,gBAAgBO,oBAAoB,SAAUL,SACjF,GAAIP,WACFS,cAEF,MAAO,CACLA,wBAAaF,gBAASF,8BAAgBM"}