@vuesax-alpha/nightly
Version:
A Component Library for Vue 3
75 lines (72 loc) • 2.3 kB
JavaScript
import { unref, ref, createVNode, reactive, render } from 'vue';
import { isClient, unrefElement } from '@vueuse/core';
import '../../../utils/index.mjs';
import '../../../constants/index.mjs';
import LoadingConstructor from './loading2.mjs';
import { isElement } from '../../../utils/types.mjs';
import { isString } from '@vue/shared';
import { debugWarn } from '../../../utils/error.mjs';
import { LOADING_RECT, SCALE_PARENT } from '../../../constants/loading.mjs';
const loading = (options = {}) => {
if (!options || !isClient)
return {
setPercent: () => void 0,
setProgress: () => void 0,
close: () => void 0,
setText: () => void 0
};
const getOption = (key) => {
const data = (options == null ? void 0 : options[key]) || key;
if (unref(data))
return ref(data);
return data;
};
const optionsRef = {};
Object.keys(options).forEach((e) => {
optionsRef[e] = getOption(e);
});
let appendTo = document.body;
if (isElement(unrefElement(optionsRef.target))) {
appendTo = unrefElement(optionsRef.target);
} else if (isString(unref(optionsRef.target))) {
appendTo = document.querySelector(unref(optionsRef.target));
}
if (!isElement(appendTo)) {
debugWarn(
"VsLoading",
"the appendTo option is not an HTMLElement. Falling back to document.body."
);
appendTo = document.body;
optionsRef.target.value = void 0;
}
if (appendTo.clientHeight < LOADING_RECT.height) {
const eightyPercentParentHeight = appendTo.clientHeight * SCALE_PARENT;
const loadingScale = eightyPercentParentHeight / LOADING_RECT.height;
optionsRef.scale = ref(loadingScale);
}
const vm = createVNode(LoadingConstructor, reactive(optionsRef));
const container = document.createElement("div");
render(vm, container);
appendTo.appendChild(container.firstChild);
const close = () => {
;
vm.component.exposed.close();
};
const setText = (val) => {
optionsRef.text.value = val;
};
const setProgress = (val) => {
optionsRef.progress.value = val;
};
const setPercent = (val) => {
optionsRef.percent.value = val;
};
return {
close,
setText,
setProgress,
setPercent
};
};
export { loading as default, loading };
//# sourceMappingURL=method.mjs.map