@tsparticles/vue3
Version:
Official tsParticles Vue.js 3.x Component - Easily create highly customizable particle, confetti and fireworks animations and use them as animated backgrounds for your website. Ready to use components available also for Web Components, React, Vue.js 2.x,
66 lines (65 loc) • 2.12 kB
JavaScript
import { createElementBlock as e, defineComponent as t, inject as n, nextTick as r, onMounted as i, onUnmounted as a, openBlock as o, reactive as s, ref as c, watch as l } from "vue";
import { tsParticles as u } from "@tsparticles/engine";
//#region src/components/particles-provider.ts
var d = Symbol("particles-provider"), f = "__tsparticles_vue3_init_state__";
function p() {
let e = globalThis;
return e[f] ??= { loaded: !1 }, e[f];
}
function m() {
return s({ loaded: !1 });
}
function h(e, t, n) {
let r = p();
if (r.promise && r.callback !== n && !r.loaded) throw Error("@tsparticles/vue3 init callback must be stable across the app lifecycle.");
return r.loaded ? (t.loaded = !0, Promise.resolve()) : r.promise ? r.promise.then(() => {
t.loaded = !0;
}) : (r.callback = n, r.promise = (async () => {
n && await n(u), await u.init(), r.loaded = !0, t.loaded = !0;
})().catch((e) => {
throw r.promise = void 0, r.callback = void 0, r.loaded = !1, e;
}), r.promise);
}
function g() {
let e = n(d, void 0);
if (!e) throw Error("@tsparticles/vue3 plugin is required. Register it with app.use(Particles, { init }).");
return e;
}
//#endregion
//#region src/components/vue-particles.vue?vue&type=script&setup=true&lang.ts
var _ = ["id"], v = /* @__PURE__ */ t({
__name: "vue-particles",
props: {
id: {},
options: {},
url: {},
theme: {}
},
emits: ["particlesLoaded"],
setup(t, { emit: n }) {
let s, d = t, f = n, p = g(), m = c(!1), h = async () => {
s?.destroy(), s = await u.load({
id: d.id,
url: d.url,
options: d.options
}), f("particlesLoaded", s);
};
return i(() => {
r(() => {
if (!d.id) throw Error("Prop 'id' is required!");
m.value = !0;
});
}), a(() => {
s?.destroy(), s = void 0;
}), l(() => p.loaded, (e) => {
e && m.value && h();
}, { immediate: !0 }), l(() => m.value, (e) => {
e && p.loaded && h();
}, { immediate: !0 }), (n, r) => (o(), e("div", { id: t.id }, null, 8, _));
}
}), y = (e, t) => {
let n = m();
e.provide(d, n), e.component("VueParticles", v), h(e, n, t?.init);
};
//#endregion
export { y as default };