2023-05-16 10:30:26 +00:00
|
|
|
/*
|
|
|
|
* Ported from useStyleTag in @vueuse/core
|
|
|
|
* https://github.com/vueuse
|
|
|
|
*/
|
|
|
|
import { DomHandler } from 'primevue/utils';
|
|
|
|
import { getCurrentInstance, nextTick, onMounted, readonly, ref, watch } from 'vue';
|
|
|
|
|
|
|
|
function tryOnMounted(fn, sync = true) {
|
|
|
|
if (getCurrentInstance()) onMounted(fn);
|
|
|
|
else if (sync) fn();
|
|
|
|
else nextTick(fn);
|
|
|
|
}
|
|
|
|
|
|
|
|
let _id = 0;
|
|
|
|
|
|
|
|
export function useStyle(css, options = {}) {
|
|
|
|
const isLoaded = ref(false);
|
2023-07-12 09:19:35 +00:00
|
|
|
const cssRef = ref(css);
|
|
|
|
const styleRef = ref(null);
|
2023-05-16 10:30:26 +00:00
|
|
|
|
|
|
|
const defaultDocument = DomHandler.isClient() ? window.document : undefined;
|
2023-07-03 22:20:35 +00:00
|
|
|
const { document = defaultDocument, immediate = true, manual = false, name = `style_${++_id}`, id = undefined, media = undefined } = options;
|
2023-05-16 10:30:26 +00:00
|
|
|
|
|
|
|
let stop = () => {};
|
|
|
|
|
2023-08-02 10:39:12 +00:00
|
|
|
/* @todo: Improve _options params */
|
|
|
|
const load = (_css, _options = {}) => {
|
2023-05-16 10:30:26 +00:00
|
|
|
if (!document) return;
|
|
|
|
|
2023-08-02 10:39:12 +00:00
|
|
|
const [_name, _id] = [_options.name || name, _options.id || id];
|
|
|
|
|
|
|
|
styleRef.value = document.querySelector(`style[data-primevue-style-id="${_name}"]`) || document.getElementById(_id) || document.createElement('style');
|
2023-05-16 10:30:26 +00:00
|
|
|
|
2023-07-12 09:19:35 +00:00
|
|
|
if (!styleRef.value.isConnected) {
|
2023-08-02 10:39:12 +00:00
|
|
|
cssRef.value = _css || css;
|
|
|
|
|
2023-07-12 09:19:35 +00:00
|
|
|
styleRef.value.type = 'text/css';
|
2023-08-02 10:39:12 +00:00
|
|
|
_id && (styleRef.value.id = _id);
|
2023-07-12 09:19:35 +00:00
|
|
|
media && (styleRef.value.media = media);
|
|
|
|
document.head.appendChild(styleRef.value);
|
|
|
|
name && styleRef.value.setAttribute('data-primevue-style-id', name);
|
2023-08-02 10:39:12 +00:00
|
|
|
DomHandler.setAttributes(styleRef.value, _options);
|
2023-05-16 10:30:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isLoaded.value) return;
|
|
|
|
|
|
|
|
stop = watch(
|
|
|
|
cssRef,
|
|
|
|
(value) => {
|
2023-07-12 09:19:35 +00:00
|
|
|
styleRef.value.textContent = value;
|
2023-05-16 10:30:26 +00:00
|
|
|
},
|
|
|
|
{ immediate: true }
|
|
|
|
);
|
|
|
|
|
|
|
|
isLoaded.value = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
const unload = () => {
|
|
|
|
if (!document || !isLoaded.value) return;
|
|
|
|
stop();
|
2023-07-12 09:19:35 +00:00
|
|
|
DomHandler.isExist(styleRef.value) && document.head.removeChild(styleRef.value);
|
2023-05-16 10:30:26 +00:00
|
|
|
isLoaded.value = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
if (immediate && !manual) tryOnMounted(load);
|
|
|
|
|
|
|
|
/*if (!manual)
|
|
|
|
tryOnScopeDispose(unload)*/
|
|
|
|
|
|
|
|
return {
|
|
|
|
id,
|
2023-07-12 09:19:35 +00:00
|
|
|
name,
|
2023-05-16 10:30:26 +00:00
|
|
|
css: cssRef,
|
|
|
|
unload,
|
|
|
|
load,
|
|
|
|
isLoaded: readonly(isLoaded)
|
|
|
|
};
|
|
|
|
}
|