Skip to content

Commit 5b4de3a

Browse files
committed
fix: use event syntax for particlesLoaded
1 parent 8d4b6f2 commit 5b4de3a

File tree

3 files changed

+82
-96
lines changed

3 files changed

+82
-96
lines changed

components/vue3-alt/src/components/Particles.vue

Lines changed: 37 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,65 +2,56 @@
22
<div :id="id"></div>
33
</template>
44

5-
<script lang="ts">
6-
import { defineComponent, nextTick } from "vue";
7-
import type { PropType } from "vue";
5+
<script setup lang="ts">
6+
import { nextTick, onMounted, onUnmounted, ref } from "vue";
87
import { tsParticles } from "tsparticles-engine";
98
import type { Container, ISourceOptions, Engine } from "tsparticles-engine";
109
1110
export type IParticlesProps = ISourceOptions;
1211
export type IParticlesParams = IParticlesProps;
1312
14-
let container: Container | undefined;
13+
const container = ref<Container | undefined>();
1514
16-
export default defineComponent({
17-
props: {
18-
id: {
19-
type: String,
20-
required: true,
21-
},
22-
options: {
23-
type: Object as PropType<IParticlesProps>,
24-
},
25-
url: {
26-
type: String,
27-
},
28-
particlesLoaded: {
29-
type: Function as PropType<(container: Container) => void>,
30-
},
31-
particlesInit: {
32-
type: Function as PropType<(engine: Engine) => Promise<void>>,
33-
},
34-
},
35-
mounted(): void {
36-
nextTick(async () => {
37-
if (!this.id) {
38-
throw new Error("Prop 'id' is required!");
39-
}
15+
const props = defineProps<{
16+
id: string;
17+
options?: IParticlesProps;
18+
url?: string;
19+
particlesInit?: (engine: Engine) => Promise<void>;
20+
}>();
4021
41-
tsParticles.init();
22+
const emit = defineEmits<{
23+
(e: "particlesLoaded", container: Container): void;
24+
}>();
4225
43-
if (this.particlesInit) {
44-
await this.particlesInit(tsParticles);
45-
}
26+
onMounted(() => {
27+
nextTick(async () => {
28+
if (!props.id) {
29+
throw new Error("Prop 'id' is required!");
30+
}
31+
32+
tsParticles.init();
4633
47-
container = await tsParticles.load({
48-
id: this.id,
49-
url: this.url,
50-
options: this.options,
51-
});
34+
if (props.particlesInit) {
35+
await props.particlesInit(tsParticles);
36+
}
5237
53-
if (this.particlesLoaded && container) {
54-
this.particlesLoaded(container);
55-
}
38+
container.value = await tsParticles.load({
39+
id: props.id,
40+
url: props.url,
41+
options: props.options,
5642
});
57-
},
58-
unmounted(): void {
59-
if (container) {
60-
container.destroy();
6143
62-
container = undefined;
44+
if (container.value) {
45+
emit("particlesLoaded", container.value);
6346
}
64-
},
47+
});
48+
});
49+
50+
onUnmounted(() => {
51+
if (container.value) {
52+
container.value.destroy();
53+
54+
container.value = undefined;
55+
}
6556
});
6657
</script>

components/vue3/src/components/Particles.vue

Lines changed: 38 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,65 +2,57 @@
22
<div :id="id"></div>
33
</template>
44

5-
<script lang="ts">
6-
import { defineComponent, nextTick } from "vue";
7-
import type { PropType } from "vue";
5+
<script setup lang="ts">
6+
import { nextTick, onMounted, onUnmounted, ref } from "vue";
87
import { tsParticles } from "tsparticles-engine";
98
import type { Container, ISourceOptions, Engine } from "tsparticles-engine";
109
1110
export type IParticlesProps = ISourceOptions;
1211
export type IParticlesParams = IParticlesProps;
1312
14-
let container: Container | undefined;
13+
const container = ref<Container | undefined>();
1514
16-
export default defineComponent({
17-
props: {
18-
id: {
19-
type: String,
20-
required: true,
21-
},
22-
options: {
23-
type: Object as PropType<IParticlesProps>,
24-
},
25-
url: {
26-
type: String,
27-
},
28-
particlesLoaded: {
29-
type: Function as PropType<(container: Container) => void>,
30-
},
31-
particlesInit: {
32-
type: Function as PropType<(engine: Engine) => Promise<void>>,
33-
},
34-
},
35-
mounted(): void {
36-
nextTick(async () => {
37-
if (!this.id) {
38-
throw new Error("Prop 'id' is required!");
39-
}
15+
const props = defineProps<{
16+
id: string;
17+
options?: IParticlesProps;
18+
url?: string;
19+
particlesInit?: (engine: Engine) => Promise<void>;
20+
}>();
4021
41-
tsParticles.init();
22+
const emit = defineEmits<{
23+
(e: "particlesLoaded", container: Container): void;
24+
}>();
4225
43-
if (this.particlesInit) {
44-
await this.particlesInit(tsParticles);
45-
}
26+
onMounted(() => {
27+
nextTick(async () => {
28+
if (!props.id) {
29+
throw new Error("Prop 'id' is required!");
30+
}
31+
32+
tsParticles.init();
4633
47-
container = await tsParticles.load({
48-
id: this.id,
49-
url: this.url,
50-
options: this.options,
51-
});
34+
if (props.particlesInit) {
35+
await props.particlesInit(tsParticles);
36+
}
5237
53-
if (this.particlesLoaded && container) {
54-
this.particlesLoaded(container);
55-
}
38+
container.value = await tsParticles.load({
39+
id: props.id,
40+
url: props.url,
41+
options: props.options,
5642
});
57-
},
58-
unmounted(): void {
59-
if (container) {
60-
container.destroy();
6143
62-
container = undefined;
44+
if (container.value) {
45+
emit("particlesLoaded", container.value);
6346
}
64-
},
47+
});
48+
});
49+
50+
onUnmounted(() => {
51+
if (!container.value) {
52+
return;
53+
}
54+
55+
container.value.destroy();
56+
container.value = undefined;
6557
});
6658
</script>

pnpm-lock.yaml

Lines changed: 7 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)