Skip to content

Commit b08aa14

Browse files
committed
refactor(carousel): control flow, use @input() transform, ThemeDirective composition for dark prop
1 parent 15cafa3 commit b08aa14

File tree

4 files changed

+33
-30
lines changed

4 files changed

+33
-30
lines changed

projects/coreui-angular/src/lib/carousel/carousel-indicators/carousel-indicators.component.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
type="button"
66
(click)="onClick(i)"
77
[class]="{ active: active === i }"
8-
[attr.aria-current]="active === i"
9-
></button>
8+
[attr.aria-current]="active === i">
9+
</button>
1010
}
1111
</div>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1+
<!--todo ngIf-->
12
@if (active) {
23
<ng-content />
34
}
4-

projects/coreui-angular/src/lib/carousel/carousel-item/carousel-item.component.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1-
import { AfterViewInit, ChangeDetectorRef, Component, HostBinding, Input, OnDestroy } from '@angular/core';
2-
import { BooleanInput, coerceBooleanProperty } from '@angular/cdk/coercion';
1+
import {
2+
AfterViewInit,
3+
booleanAttribute,
4+
ChangeDetectorRef,
5+
Component,
6+
HostBinding,
7+
Input,
8+
OnDestroy
9+
} from '@angular/core';
310
import { Subscription } from 'rxjs';
411

512
import { CarouselService } from '../carousel.service';
@@ -12,17 +19,15 @@ import { CarouselService } from '../carousel.service';
1219
})
1320
export class CarouselItemComponent implements OnDestroy, AfterViewInit {
1421

15-
static ngAcceptInputType_active: BooleanInput;
16-
1722
index?: number;
1823
private carouselIndexSubscription?: Subscription;
1924

2025
/**
2126
* @ignore
2227
*/
23-
@Input()
28+
@Input({ transform: booleanAttribute })
2429
set active(value) {
25-
this._active = coerceBooleanProperty(value);
30+
this._active = value;
2631
this.changeDetectorRef.markForCheck();
2732
}
2833

projects/coreui-angular/src/lib/carousel/carousel/carousel.component.ts

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,34 @@ import { filter, finalize, withLatestFrom, zipWith } from 'rxjs/operators';
1919
import { IntersectionService } from '../../services';
2020
import { IListenersConfig, ListenersService } from '../../services';
2121

22+
import { Triggers } from '../../coreui.types';
23+
import { ThemeDirective } from '../../shared/theme.directive';
2224
import { CarouselState } from '../carousel-state';
2325
import { CarouselService } from '../carousel.service';
2426
import { CarouselConfig } from '../carousel.config';
25-
import { Triggers } from '../../coreui.types';
2627

2728
@Component({
2829
selector: 'c-carousel',
29-
template: '<ng-content></ng-content>',
30+
template: '<ng-content />',
3031
styleUrls: ['./carousel.component.scss'],
3132
providers: [CarouselService, CarouselState, CarouselConfig, ListenersService],
32-
standalone: true
33+
standalone: true,
34+
hostDirectives: [
35+
{ directive: ThemeDirective, inputs: ['dark'] }
36+
]
3337
})
3438
export class CarouselComponent implements OnInit, OnDestroy, AfterContentInit {
39+
constructor(
40+
@Inject(CarouselConfig) private config: CarouselConfig,
41+
private hostElement: ElementRef,
42+
private carouselService: CarouselService,
43+
private carouselState: CarouselState,
44+
private intersectionService: IntersectionService,
45+
private listenersService: ListenersService
46+
) {
47+
Object.assign(this, config);
48+
}
49+
3550
/**
3651
* Index of the active item.
3752
* @type number
@@ -42,11 +57,6 @@ export class CarouselComponent implements OnInit, OnDestroy, AfterContentInit {
4257
* @type boolean
4358
*/
4459
@Input() animate: boolean = true;
45-
/**
46-
* Add darker controls, indicators, and captions.
47-
* @type boolean
48-
*/
49-
@Input() dark?: boolean;
5060
/**
5161
* Carousel direction. [docs]
5262
* @type {'next' | 'prev'}
@@ -92,7 +102,6 @@ export class CarouselComponent implements OnInit, OnDestroy, AfterContentInit {
92102
return {
93103
carousel: true,
94104
slide: true,
95-
'carousel-dark': !!this.dark,
96105
'carousel-fade': this.transition === 'crossfade'
97106
};
98107
}
@@ -102,17 +111,6 @@ export class CarouselComponent implements OnInit, OnDestroy, AfterContentInit {
102111
private swipeSubscription?: Subscription;
103112
readonly #destroyRef = inject(DestroyRef);
104113

105-
constructor(
106-
@Inject(CarouselConfig) private config: CarouselConfig,
107-
private hostElement: ElementRef,
108-
private carouselService: CarouselService,
109-
private carouselState: CarouselState,
110-
private intersectionService: IntersectionService,
111-
private listenersService: ListenersService
112-
) {
113-
Object.assign(this, config);
114-
}
115-
116114
ngOnInit(): void {
117115
this.carouselStateSubscribe();
118116
}
@@ -190,6 +188,7 @@ export class CarouselComponent implements OnInit, OnDestroy, AfterContentInit {
190188
}
191189

192190
private intersectionServiceSubscribe(): void {
191+
this.intersectionService.createIntersectionObserver(this.hostElement);
193192
this.intersectionService.intersecting$
194193
.pipe(
195194
filter(next => next.hostElement === this.hostElement),
@@ -202,7 +201,6 @@ export class CarouselComponent implements OnInit, OnDestroy, AfterContentInit {
202201
this.visible = next.isIntersecting;
203202
next.isIntersecting ? this.setTimer() : this.resetTimer();
204203
});
205-
this.intersectionService.createIntersectionObserver(this.hostElement);
206204
}
207205

208206
private swipeSubscribe(subscribe: boolean = true): void {

0 commit comments

Comments
 (0)