{"version":3,"file":"vue-screen-utils-BZVCQ3MN.js","sources":["../../node_modules/vue-screen-utils/dist/index.mjs"],"sourcesContent":["import { ref, onMounted, onUnmounted, watch, computed, reactive, provide } from 'vue';\n\nfunction windowExists() {\n return typeof window !== 'undefined';\n}\nfunction windowHasFeature(feature) {\n return windowExists() && feature in window;\n}\n\nfunction useMediaQuery(query, callback) {\n let mediaQuery;\n const matches = ref(false);\n function listener(ev) {\n if (callback)\n callback(ev);\n matches.value = ev.matches;\n }\n function cleanup() {\n if (mediaQuery) {\n mediaQuery.removeEventListener('change', listener);\n mediaQuery = undefined;\n }\n }\n function setup(newQuery = query) {\n cleanup();\n if (windowHasFeature('matchMedia') && newQuery) {\n mediaQuery = window.matchMedia(newQuery);\n mediaQuery.addEventListener('change', listener);\n matches.value = mediaQuery.matches;\n }\n }\n onMounted(() => setup());\n onUnmounted(() => cleanup());\n return { matches, setup, cleanup };\n}\n\nfunction useResizeObserver(target, callback, options = {}) {\n let observer;\n const rect = ref();\n const listener = (...args) => {\n if (callback)\n callback(...args);\n const entry = args[0][0];\n rect.value = entry.contentRect;\n };\n const stopObserver = () => {\n if (observer) {\n observer.disconnect();\n observer = undefined;\n }\n };\n const stopWatch = watch(() => target.value, (elOrComp) => {\n stopObserver();\n if (windowHasFeature('ResizeObserver') && elOrComp) {\n observer = new ResizeObserver(listener);\n observer.observe(elOrComp.$el ?? elOrComp, options);\n }\n }, { immediate: true, flush: 'post' });\n const cleanup = () => {\n stopObserver();\n stopWatch();\n };\n onUnmounted(() => cleanup());\n return { rect, cleanup };\n}\n\nfunction useDarkMode(config) {\n const isDark = ref(false);\n const displayMode = computed(() => (isDark.value ? 'dark' : 'light'));\n let mediaQuery;\n let mutationObserver;\n function mqListener(ev) {\n isDark.value = ev.matches;\n }\n function setupSystem() {\n if (windowHasFeature('matchMedia')) {\n mediaQuery = window.matchMedia('(prefers-color-scheme: dark)');\n mediaQuery.addEventListener('change', mqListener);\n isDark.value = mediaQuery.matches;\n }\n }\n function moListener() {\n const { selector = ':root', darkClass = 'dark' } = config.value;\n const el = document.querySelector(selector);\n isDark.value = el.classList.contains(darkClass);\n }\n function setupClass(config) {\n const { selector = ':root', darkClass = 'dark' } = config;\n if (windowExists() && selector && darkClass) {\n const el = document.querySelector(selector);\n if (el) {\n mutationObserver = new MutationObserver(moListener);\n mutationObserver.observe(el, {\n attributes: true,\n attributeFilter: ['class'],\n });\n isDark.value = el.classList.contains(darkClass);\n }\n }\n }\n function setup() {\n stopObservers();\n const type = typeof config.value;\n if (type === 'string' && config.value.toLowerCase() === 'system') {\n setupSystem();\n }\n else if (type === 'object') {\n setupClass(config.value);\n }\n else {\n isDark.value = !!config.value;\n }\n }\n const stopWatch = watch(() => config.value, () => setup(), {\n immediate: true,\n });\n function stopObservers() {\n if (mediaQuery) {\n mediaQuery.removeEventListener('change', mqListener);\n mediaQuery = undefined;\n }\n if (mutationObserver) {\n mutationObserver.disconnect();\n mutationObserver = undefined;\n }\n }\n function cleanup() {\n stopObservers();\n stopWatch();\n }\n onUnmounted(() => cleanup());\n return {\n isDark,\n displayMode,\n cleanup,\n };\n}\n\nfunction resolveValue({ 'min-width': _minWidth, min = _minWidth, max, raw } = {}) {\n return { min, max, raw };\n}\n/**\n * A function that normalizes the various forms that the screens object can be\n * provided in.\n *\n * Input(s):\n * - ['100px', '200px'] // Raw strings\n * - { sm: '100px', md: '200px' } // Object with string values\n * - { sm: { min: '100px' }, md: { max: '100px' } } // Object with object values\n * - { sm: [{ min: '100px' }, { max: '200px' }] } // Object with object array (multiple values)\n *\n * Output(s):\n * - [{ name: 'sm', values: [{ min: '100px', max: '200px' }] }] // List of objects, that contains multiple values\n */\nfunction normalizeScreens(screens, root = true) {\n if (Array.isArray(screens)) {\n return screens.map((screen) => {\n if (root && Array.isArray(screen)) {\n throw new Error('The tuple syntax is not supported for `screens`.');\n }\n if (typeof screen === 'string') {\n return { name: screen.toString(), values: [{ min: screen, max: undefined }] };\n }\n let [name, options] = screen;\n name = name.toString();\n if (typeof options === 'string') {\n return { name, values: [{ min: options, max: undefined }] };\n }\n if (Array.isArray(options)) {\n return { name, values: options.map((option) => resolveValue(option)) };\n }\n return { name, values: [resolveValue(options)] };\n });\n }\n return normalizeScreens(Object.entries(screens ?? {}), false);\n}\n\n// This function gratuitously borrowed from TailwindCSS\n// https://github.com/tailwindcss/tailwindcss/blob/master/src/util/buildMediaQuery.js\nfunction buildMediaQuery(screenValues) {\n return screenValues\n .map((sv) => {\n if (sv.raw !== undefined)\n return sv.raw;\n return [sv.min && `(min-width: ${sv.min})`, sv.max && `(max-width: ${sv.max})`].filter(Boolean).join(' and ');\n })\n .join(', ');\n}\n\nvar defaultScreens = {\n xs: '0px',\n sm: '640px',\n md: '768px',\n lg: '1024px',\n xl: '1280px',\n};\n\nconst defaultInjectKey = '$screens';\nfunction initScreens(screens) {\n const state = reactive({\n screens: normalizeScreens(screens || defaultScreens),\n queries: {},\n matches: {},\n hasSetup: false,\n });\n function refreshMatches() {\n Object.entries(state.queries).forEach(([key, query]) => {\n state.matches[key] = query.matches;\n });\n }\n function mapList(config) {\n return computed(() => Object.keys(state.matches)\n .filter((key) => state.matches[key] === true && config.hasOwnProperty(key))\n .map((key) => config[key]));\n }\n const list = computed(() => Object.keys(state.matches).filter((k) => state.matches[k]));\n function mapCurrent(config, def) {\n return computed(() => {\n const curr = current.value;\n if (curr && config.hasOwnProperty(curr))\n return config[curr];\n return def;\n });\n }\n const current = computed(() => {\n const arr = list.value;\n if (arr.length)\n return arr[arr.length - 1];\n return '';\n });\n function cleanup() {\n Object.values(state.queries).forEach((query) => query.removeEventListener('change', refreshMatches));\n state.queries = {};\n state.matches = {};\n }\n if (!state.hasSetup && windowHasFeature('matchMedia')) {\n cleanup();\n state.queries = state.screens.reduce((result, { name, values }) => {\n const mediaQuery = window.matchMedia(buildMediaQuery(values));\n mediaQuery.addEventListener('change', refreshMatches);\n result[name] = mediaQuery;\n return result;\n }, {});\n refreshMatches();\n state.hasSetup = true;\n }\n return { matches: state.matches, list, mapList, current, mapCurrent, cleanup };\n}\n\nfunction useScreens(screens, opts) {\n const s = initScreens(screens);\n provide(opts?.injectKey || defaultInjectKey, s);\n onUnmounted(() => s.cleanup());\n return s;\n}\n\nconst plugin = {\n install: (app, screens, opts) => {\n const s = initScreens(screens);\n const key = opts?.injectKey || defaultInjectKey;\n // Inject a globally available screens object method\n app.config.globalProperties[key] = s;\n // Provide screens object\n app.provide(key, s);\n },\n};\n\nexport { buildMediaQuery, normalizeScreens, plugin as screens, useDarkMode, useMediaQuery, useResizeObserver, useScreens };\n//# sourceMappingURL=index.mjs.map\n"],"names":["windowExists","windowHasFeature","feature","resolveValue","_minWidth","min","max","raw","normalizeScreens","screens","root","screen","name","options","option","buildMediaQuery","screenValues","sv","defaultScreens","defaultInjectKey","initScreens","state","reactive","refreshMatches","key","query","mapList","config","computed","list","k","mapCurrent","def","curr","current","arr","cleanup","result","values","mediaQuery","useScreens","opts","s","provide","onUnmounted"],"mappings":"qYAEA,SAASA,GAAe,CACpB,OAAO,OAAO,OAAW,GAC7B,CACA,SAASC,EAAiBC,EAAS,CAC/B,OAAOF,EAAY,GAAME,KAAW,MACxC,CAmIA,SAASC,EAAa,CAAE,YAAaC,EAAW,IAAAC,EAAMD,EAAW,IAAAE,EAAK,IAAAC,CAAK,EAAG,GAAI,CAC9E,MAAO,CAAE,IAAAF,EAAK,IAAAC,EAAK,IAAAC,CAAK,CAC5B,CAcA,SAASC,EAAiBC,EAASC,EAAO,GAAM,CAC5C,OAAI,MAAM,QAAQD,CAAO,EACdA,EAAQ,IAAKE,GAAW,CAC3B,GAAID,GAAQ,MAAM,QAAQC,CAAM,EAC5B,MAAM,IAAI,MAAM,kDAAkD,EAEtE,GAAI,OAAOA,GAAW,SAClB,MAAO,CAAE,KAAMA,EAAO,SAAU,EAAE,OAAQ,CAAC,CAAE,IAAKA,EAAQ,IAAK,MAAW,CAAA,CAAG,EAEjF,GAAI,CAACC,EAAMC,CAAO,EAAIF,EAEtB,OADA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