Skip to content

Commit d58fde4

Browse files
committed
refactor(PlaceholderDirective): extract cPlaceholderAnimation, remove cPlaceholderColor prop
1 parent c5ea18d commit d58fde4

File tree

5 files changed

+68
-20
lines changed

5 files changed

+68
-20
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { PlaceholderAnimationDirective } from './placeholder-animation.directive';
2+
3+
describe('PlaceholderAnimationDirective', () => {
4+
it('should create an instance', () => {
5+
const directive = new PlaceholderAnimationDirective();
6+
expect(directive).toBeTruthy();
7+
});
8+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { AfterContentInit, ContentChild, Directive, HostBinding, Input } from '@angular/core';
2+
import { PlaceholderDirective } from './placeholder.directive';
3+
4+
@Directive({
5+
selector: '[cPlaceholderAnimation]'
6+
})
7+
export class PlaceholderAnimationDirective implements AfterContentInit {
8+
9+
constructor() { }
10+
11+
/**
12+
* Animation type for placeholder
13+
* @type 'glow' | 'wave'
14+
* @default undefined
15+
*/
16+
@Input('cPlaceholderAnimation') animation?: 'glow' | 'wave';
17+
18+
@HostBinding('class')
19+
get hostClasses(): any {
20+
return {
21+
[`placeholder-${this.animation}`]: this.animate && !!this.animation
22+
};
23+
}
24+
25+
@ContentChild(PlaceholderDirective) placeholder!: PlaceholderDirective;
26+
private animate: boolean = false;
27+
28+
ngAfterContentInit() {
29+
this.animate = this.placeholder?.visible;
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,45 @@
11
import { Directive, HostBinding, Input } from '@angular/core';
2-
import { Colors } from '../coreui.types';
2+
import { BooleanInput, coerceBooleanProperty } from '@angular/cdk/coercion';
33

44
@Directive({
5-
selector: '[cPlaceholder]'
5+
selector: '[cPlaceholder]',
6+
exportAs: 'cPlaceholder'
67
})
78
export class PlaceholderDirective {
89

9-
constructor() { }
10+
static ngAcceptInputType_visible: BooleanInput;
1011

11-
/**
12-
* Animation type for placeholder
13-
* @type 'glow' | 'wave'
14-
* @default undefined
15-
*/
16-
@Input() animation?: 'glow' | 'wave';
12+
constructor() { }
1713

1814
/**
19-
* Custom color for placeholder
20-
* @type Colors
21-
* @default undefined
15+
* placeholder toggler
16+
* @type boolean
17+
* @default true
2218
*/
23-
@Input('cPlaceholderColor') color?: Colors;
19+
@Input('cPlaceholder')
20+
set visible(value: boolean) {
21+
this._visible = coerceBooleanProperty(value);
22+
}
23+
get visible() {
24+
return this._visible;
25+
}
26+
private _visible: boolean = false;
2427

2528
/**
2629
* Size the placeholder extra small, small, large.
2730
*/
2831
@Input('cPlaceholderSize') size?: 'xs' | 'sm' | 'lg';
2932

33+
@HostBinding('attr.aria-hidden')
34+
get ariaHidden(): boolean | null {
35+
return this.visible ? null : true;
36+
};
37+
3038
@HostBinding('class')
3139
get hostClasses(): any {
3240
return {
33-
'placeholder': true,
34-
[`placeholder-${this.animation}`]: !!this.animation,
35-
[`placeholder-${this.size}`]: !!this.size,
36-
[`bg-${this.color}`]: !!this.color
41+
'placeholder': this.visible,
42+
[`placeholder-${this.size}`]: !!this.size
3743
};
3844
}
39-
4045
}

projects/coreui-angular/src/lib/placeholder/placeholder.module.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
import { NgModule } from '@angular/core';
22
import { CommonModule } from '@angular/common';
33
import { PlaceholderDirective } from './placeholder.directive';
4+
import { PlaceholderAnimationDirective } from './placeholder-animation.directive';
45

56
@NgModule({
67
declarations: [
7-
PlaceholderDirective
8+
PlaceholderDirective,
9+
PlaceholderAnimationDirective
810
],
911
imports: [
1012
CommonModule
1113
],
1214
exports: [
13-
PlaceholderDirective
15+
PlaceholderDirective,
16+
PlaceholderAnimationDirective
1417
]
1518
})
1619
export class PlaceholderModule {
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
export { PlaceholderAnimationDirective } from './placeholder-animation.directive';
12
export { PlaceholderDirective } from './placeholder.directive';
23
export { PlaceholderModule } from './placeholder.module';

0 commit comments

Comments
 (0)