1
- import { LitElement , html } from "lit" ;
1
+ import { LitElement , html , PropertyValues } from "lit" ;
2
2
import { property , customElement } from "lit/decorators.js" ;
3
3
import { Container , Engine , tsParticles } from "tsparticles-engine" ;
4
4
@@ -23,37 +23,41 @@ export class LitParticles extends LitElement {
23
23
24
24
container ?: Container ;
25
25
26
+ initialized = false ;
27
+
26
28
@property ( { type : Function } )
27
29
particlesInit ?: ( engine : Engine ) => Promise < void > ;
28
30
29
31
@property ( { type : Function } )
30
32
particlesLoaded ?: ( container ?: Container ) => Promise < void > ;
31
33
32
- constructor ( ) {
33
- super ( ) ;
34
-
35
- this . particlesInit ?.( tsParticles ) ;
36
- }
37
-
38
34
connectedCallback ( ) {
39
35
super . connectedCallback ( ) ;
40
36
41
- tsParticles . load ( this . id , this . options ) . then ( async ( container ) => {
42
- this . container = container ;
43
-
44
- await this . particlesLoaded ?.( container ) ;
37
+ this . particlesInit ?.( tsParticles ) . then ( ( ) => {
38
+ this . initialized = true ;
45
39
} ) ;
46
40
}
47
41
48
- disconnectedCallback ( ) : void {
49
- if ( this . container ) {
50
- this . container . destroy ( ) ;
51
- }
42
+ update ( changedProperties : PropertyValues ) {
43
+ super . update ( changedProperties ) ;
44
+
45
+ if ( this . initialized ) {
46
+ tsParticles . load ( this . id , this . options ) . then ( ( container ) => {
47
+ this . container = container ;
52
48
53
- super . disconnectedCallback ( ) ;
49
+ this . particlesLoaded ?.( container ) ;
50
+ } ) ;
51
+ }
54
52
}
55
53
56
54
render ( ) {
57
- return html `< div id =${ this . id } > </ div > ` ;
55
+ if ( ! this . initialized ) {
56
+ return html `` ;
57
+ }
58
+
59
+ return html `< div id =${ this . id } >
60
+ < canvas > </ canvas >
61
+ </ div > ` ;
58
62
}
59
63
}
0 commit comments