Skip to content

Commit 1fe7873

Browse files
committed
fix(collapse): collapse not expanded when initial visible=true
1 parent c7e4181 commit 1fe7873

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

projects/coreui-angular/src/lib/collapse/collapse.directive.ts

+20-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
import { AnimationBuilder, AnimationPlayer, useAnimation } from '@angular/animations';
2+
13
import {
4+
afterNextRender,
25
booleanAttribute,
36
computed,
47
Directive,
@@ -11,7 +14,6 @@ import {
1114
Renderer2,
1215
signal
1316
} from '@angular/core';
14-
import { AnimationBuilder, AnimationPlayer, useAnimation } from '@angular/animations';
1517

1618
import {
1719
collapseAnimation,
@@ -23,22 +25,30 @@ import {
2325
@Directive({
2426
selector: '[cCollapse]',
2527
exportAs: 'cCollapse',
26-
host: { '[class]': 'hostClasses()', '[style]': '{display: "none"}' }
28+
host: { '[class]': 'hostClasses()', '[style]': '{ display: "none" }' }
2729
})
2830
export class CollapseDirective implements OnDestroy {
2931
readonly #hostElement = inject(ElementRef);
3032
readonly #renderer = inject(Renderer2);
3133
readonly #animationBuilder = inject(AnimationBuilder);
3234
#player: AnimationPlayer | undefined = undefined;
3335

36+
constructor() {
37+
afterNextRender({
38+
read: () => {
39+
this.#initialized.set(true);
40+
}
41+
});
42+
}
43+
3444
/**
3545
* @ignore
3646
*/
3747
readonly animateInput = input(true, { transform: booleanAttribute, alias: 'animate' });
3848

3949
readonly animate = signal(true);
4050

41-
readonly animateInputEffect = effect(() => {
51+
readonly #animateInputEffect = effect(() => {
4252
this.animate.set(this.animateInput());
4353
});
4454

@@ -58,19 +68,18 @@ export class CollapseDirective implements OnDestroy {
5868

5969
readonly visibleChange = output<boolean>();
6070

61-
readonly visibleInputEffect = effect(() => {
71+
readonly #visibleInputEffect = effect(() => {
6272
this.visible.set(this.visibleInput());
6373
});
6474

65-
readonly visible = signal<boolean>(false);
75+
readonly visible = signal(false);
6676

67-
#init = false;
77+
readonly #initialized = signal(false);
6878

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+
}
7483
});
7584

7685
/**

0 commit comments

Comments
 (0)