108 lines
5.6 KiB
JavaScript
108 lines
5.6 KiB
JavaScript
|
|
import { isClient, setAttributes, setAttribute, isExist } from '@primeuix/utils/dom';
|
||
|
|
import { ref, readonly, watch, getCurrentInstance, onMounted, nextTick } from 'vue';
|
||
|
|
|
||
|
|
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
||
|
|
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
||
|
|
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), true).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
||
|
|
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: true, configurable: true, writable: true }) : e[r] = t, e; }
|
||
|
|
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
|
||
|
|
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
||
|
|
function tryOnMounted(fn) {
|
||
|
|
var sync = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
|
||
|
|
if (getCurrentInstance() && getCurrentInstance().components) onMounted(fn);else if (sync) fn();else nextTick(fn);
|
||
|
|
}
|
||
|
|
var _id = 0;
|
||
|
|
function useStyle(css) {
|
||
|
|
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
||
|
|
var isLoaded = ref(false);
|
||
|
|
var cssRef = ref(css);
|
||
|
|
var styleRef = ref(null);
|
||
|
|
var defaultDocument = isClient() ? window.document : undefined;
|
||
|
|
var _options$document = options.document,
|
||
|
|
document = _options$document === void 0 ? defaultDocument : _options$document,
|
||
|
|
_options$immediate = options.immediate,
|
||
|
|
immediate = _options$immediate === void 0 ? true : _options$immediate,
|
||
|
|
_options$manual = options.manual,
|
||
|
|
manual = _options$manual === void 0 ? false : _options$manual,
|
||
|
|
_options$name = options.name,
|
||
|
|
name = _options$name === void 0 ? "style_".concat(++_id) : _options$name,
|
||
|
|
_options$id = options.id,
|
||
|
|
id = _options$id === void 0 ? undefined : _options$id,
|
||
|
|
_options$media = options.media,
|
||
|
|
media = _options$media === void 0 ? undefined : _options$media,
|
||
|
|
_options$nonce = options.nonce,
|
||
|
|
nonce = _options$nonce === void 0 ? undefined : _options$nonce,
|
||
|
|
_options$first = options.first,
|
||
|
|
first = _options$first === void 0 ? false : _options$first,
|
||
|
|
_options$onMounted = options.onMounted,
|
||
|
|
onStyleMounted = _options$onMounted === void 0 ? undefined : _options$onMounted,
|
||
|
|
_options$onUpdated = options.onUpdated,
|
||
|
|
onStyleUpdated = _options$onUpdated === void 0 ? undefined : _options$onUpdated,
|
||
|
|
_options$onLoad = options.onLoad,
|
||
|
|
onStyleLoaded = _options$onLoad === void 0 ? undefined : _options$onLoad,
|
||
|
|
_options$props = options.props,
|
||
|
|
props = _options$props === void 0 ? {} : _options$props;
|
||
|
|
var stop = function stop() {};
|
||
|
|
|
||
|
|
/* @todo: Improve _options params */
|
||
|
|
var load = function load(_css) {
|
||
|
|
var _props = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
||
|
|
if (!document) return;
|
||
|
|
var _styleProps = _objectSpread(_objectSpread({}, props), _props);
|
||
|
|
var _name = _styleProps.name || name,
|
||
|
|
_id = _styleProps.id || id,
|
||
|
|
_nonce = _styleProps.nonce || nonce;
|
||
|
|
styleRef.value = document.querySelector("style[data-primevue-style-id=\"".concat(_name, "\"]")) || document.getElementById(_id) || document.createElement('style');
|
||
|
|
if (!styleRef.value.isConnected) {
|
||
|
|
cssRef.value = _css || css;
|
||
|
|
setAttributes(styleRef.value, {
|
||
|
|
type: 'text/css',
|
||
|
|
id: _id,
|
||
|
|
media: media,
|
||
|
|
nonce: _nonce
|
||
|
|
});
|
||
|
|
first ? document.head.prepend(styleRef.value) : document.head.appendChild(styleRef.value);
|
||
|
|
setAttribute(styleRef.value, 'data-primevue-style-id', _name);
|
||
|
|
setAttributes(styleRef.value, _styleProps);
|
||
|
|
styleRef.value.onload = function (event) {
|
||
|
|
return onStyleLoaded === null || onStyleLoaded === void 0 ? void 0 : onStyleLoaded(event, {
|
||
|
|
name: _name
|
||
|
|
});
|
||
|
|
};
|
||
|
|
onStyleMounted === null || onStyleMounted === void 0 || onStyleMounted(_name);
|
||
|
|
}
|
||
|
|
if (isLoaded.value) return;
|
||
|
|
stop = watch(cssRef, function (value) {
|
||
|
|
styleRef.value.textContent = value;
|
||
|
|
onStyleUpdated === null || onStyleUpdated === void 0 || onStyleUpdated(_name);
|
||
|
|
}, {
|
||
|
|
immediate: true
|
||
|
|
});
|
||
|
|
isLoaded.value = true;
|
||
|
|
};
|
||
|
|
var unload = function unload() {
|
||
|
|
if (!document || !isLoaded.value) return;
|
||
|
|
stop();
|
||
|
|
isExist(styleRef.value) && document.head.removeChild(styleRef.value);
|
||
|
|
isLoaded.value = false;
|
||
|
|
styleRef.value = null;
|
||
|
|
};
|
||
|
|
if (immediate && !manual) tryOnMounted(load);
|
||
|
|
|
||
|
|
/*if (!manual)
|
||
|
|
tryOnScopeDispose(unload)*/
|
||
|
|
|
||
|
|
return {
|
||
|
|
id: id,
|
||
|
|
name: name,
|
||
|
|
el: styleRef,
|
||
|
|
css: cssRef,
|
||
|
|
unload: unload,
|
||
|
|
load: load,
|
||
|
|
isLoaded: readonly(isLoaded)
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
export { useStyle };
|
||
|
|
//# sourceMappingURL=index.mjs.map
|