1 line
8.3 KiB
Plaintext
1 line
8.3 KiB
Plaintext
|
|
{"version":3,"file":"index.mjs","sources":["../../src/usestyle/UseStyle.js"],"sourcesContent":["/*\n * Ported from useStyleTag in @vueuse/core\n * https://github.com/vueuse\n */\nimport { isClient, isExist, setAttribute, setAttributes } from '@primeuix/utils/dom';\nimport { getCurrentInstance, nextTick, onMounted, readonly, ref, watch } from 'vue';\n\nfunction tryOnMounted(fn, sync = true) {\n if (getCurrentInstance() && getCurrentInstance().components) onMounted(fn);\n else if (sync) fn();\n else nextTick(fn);\n}\n\nlet _id = 0;\n\nexport function useStyle(css, options = {}) {\n const isLoaded = ref(false);\n const cssRef = ref(css);\n const styleRef = ref(null);\n\n const defaultDocument = isClient() ? window.document : undefined;\n const {\n document = defaultDocument,\n immediate = true,\n manual = false,\n name = `style_${++_id}`,\n id = undefined,\n media = undefined,\n nonce = undefined,\n first = false,\n onMounted: onStyleMounted = undefined,\n onUpdated: onStyleUpdated = undefined,\n onLoad: onStyleLoaded = undefined,\n props = {}\n } = options;\n\n let stop = () => {};\n\n /* @todo: Improve _options params */\n const load = (_css, _props = {}) => {\n if (!document) return;\n\n const _styleProps = { ...props, ..._props };\n const [_name, _id, _nonce] = [_styleProps.name || name, _styleProps.id || id, _styleProps.nonce || nonce];\n\n styleRef.value = document.querySelector(`style[data-primevue-style-id=\"${_name}\"]`) || document.getElementById(_id) || document.createElement('style');\n\n if (!styleRef.value.isConnected) {\n cssRef.value = _css || css;\n\n setAttributes(styleRef.value, {\n type: 'text/css',\n id: _id,\n media,\n nonce: _nonce\n });\n first ? document.head.prepend(styleRef.value) : document.head.appendChild(styleRef.value);\n setAttribute(styleRef.value, 'data-primevue-style-id', _name);\n setAttributes(styleRef.value, _styleProps);\n styleRef.value.onload = (event) => onStyleLoaded?.(event, { name: _name });\n onStyleMounted?.(_name);\n }\n\n if (isLoaded.value) return;\n\n stop = watch(\n cssRef,\n (value) => {\n styleRef.value.textContent = value;\n onStyleUpdated?.(_name);\n },\n { immediate: true }\n );\n\n isLoaded.value = true;\n };\n\n const unload = () => {\n if (!document || !isLoaded.value) return;\n stop();\n isExist(styleRef.value) && document.head.removeChild(styleRef.value);\n isLoaded.value = false;\n styleRef.value = null;\n };\n\n if (immediate && !manual) tryOnMounted(load);\n\n /*if (!manual)\n tryOnScopeDispose(unload)*/\n\n return {\n id,\n name,\n el: styleRef,\n css: cssRef,\n unload,\n load,\n isLoaded: readonly(isLoaded)\n };\n}\n"],"names":["tryOnMounted","fn","sync","arguments","length","undefined","getCurrentInstance","components","onMounted","nextTick","_id","useStyle","css","options","isLoaded","ref","cssRef","styleRef","defaultDocument","isClient","window","document","_options$document","_options$immediate","immediate","_options$manual","manual","_options$name","name","concat","_options$id","id","_options$media","media","_options$nonce","nonce","_options$first","first","_options$onMounted","onStyleMounted","_options$onUpdated","onUpdated","onStyleUpdated","_options$onLoad","onLoad","onStyleLoaded","_options$props","props","stop","load","_css","_props","_styleProps","_objectSpread","_name","_nonce","value","querySelector","getElementById","createElement","isConnected","setAttributes","type","head","prepend","appendChild","setAttribute","onload","event","watch","textContent","unload","isExist","removeChild","el","readonly"],"mappings":";;;;;;;;;AAOA,SAAS
|