1
+ import { AnimationBuilder , AnimationPlayer , useAnimation } from '@angular/animations' ;
2
+
1
3
import {
4
+ afterNextRender ,
2
5
booleanAttribute ,
3
6
computed ,
4
7
Directive ,
@@ -11,7 +14,6 @@ import {
11
14
Renderer2 ,
12
15
signal
13
16
} from '@angular/core' ;
14
- import { AnimationBuilder , AnimationPlayer , useAnimation } from '@angular/animations' ;
15
17
16
18
import {
17
19
collapseAnimation ,
@@ -23,22 +25,30 @@ import {
23
25
@Directive ( {
24
26
selector : '[cCollapse]' ,
25
27
exportAs : 'cCollapse' ,
26
- host : { '[class]' : 'hostClasses()' , '[style]' : '{display: "none"}' }
28
+ host : { '[class]' : 'hostClasses()' , '[style]' : '{ display: "none" }' }
27
29
} )
28
30
export class CollapseDirective implements OnDestroy {
29
31
readonly #hostElement = inject ( ElementRef ) ;
30
32
readonly #renderer = inject ( Renderer2 ) ;
31
33
readonly #animationBuilder = inject ( AnimationBuilder ) ;
32
34
#player: AnimationPlayer | undefined = undefined ;
33
35
36
+ constructor ( ) {
37
+ afterNextRender ( {
38
+ read : ( ) => {
39
+ this . #initialized. set ( true ) ;
40
+ }
41
+ } ) ;
42
+ }
43
+
34
44
/**
35
45
* @ignore
36
46
*/
37
47
readonly animateInput = input ( true , { transform : booleanAttribute , alias : 'animate' } ) ;
38
48
39
49
readonly animate = signal ( true ) ;
40
50
41
- readonly animateInputEffect = effect ( ( ) => {
51
+ readonly # animateInputEffect = effect ( ( ) => {
42
52
this . animate . set ( this . animateInput ( ) ) ;
43
53
} ) ;
44
54
@@ -58,19 +68,18 @@ export class CollapseDirective implements OnDestroy {
58
68
59
69
readonly visibleChange = output < boolean > ( ) ;
60
70
61
- readonly visibleInputEffect = effect ( ( ) => {
71
+ readonly # visibleInputEffect = effect ( ( ) => {
62
72
this . visible . set ( this . visibleInput ( ) ) ;
63
73
} ) ;
64
74
65
- readonly visible = signal < boolean > ( false ) ;
75
+ readonly visible = signal ( false ) ;
66
76
67
- #init = false ;
77
+ readonly #initialized = signal ( false ) ;
68
78
69
- readonly visibleEffect = effect ( ( ) => {
70
- const visible = this . visible ( ) ;
71
-
72
- ( this . #init || visible ) && this . createPlayer ( visible ) ;
73
- this . #init = true ;
79
+ readonly #visibleEffect = effect ( ( ) => {
80
+ if ( this . #initialized( ) ) {
81
+ this . createPlayer ( this . visible ( ) ) ;
82
+ }
74
83
} ) ;
75
84
76
85
/**
0 commit comments