Skip to content

Commit f9bd6b5

Browse files
authored
feat(design): change daffColorMixin to a directive (#2942)
BREAKING CHANGE: daffColorMixin has been removed in favor of DaffColorableDirective. Update usage by using the hostDirective feature.
1 parent 688b92d commit f9bd6b5

20 files changed

+338
-423
lines changed

libs/design/button/src/button/button.component.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,13 @@ import {
1111
} from '@angular/core';
1212

1313
import {
14-
daffColorMixin,
15-
DaffColorable,
1614
DaffPrefixable,
1715
DaffSuffixable,
1816
daffPrefixableMixin,
1917
daffSuffixableMixin,
2018
DaffArticleEncapsulatedDirective,
2119
DaffStatusableDirective,
20+
DaffColorableDirective,
2221
} from '@daffodil/design';
2322

2423
import { DaffButtonSizableDirective } from './button-sizable.directive';
@@ -42,7 +41,7 @@ class DaffButtonBase{
4241
constructor(public _elementRef: ElementRef, public _renderer: Renderer2) {}
4342
}
4443

45-
const _daffButtonBase = daffPrefixableMixin(daffSuffixableMixin(daffColorMixin((DaffButtonBase))));
44+
const _daffButtonBase = daffPrefixableMixin(daffSuffixableMixin((DaffButtonBase)));
4645

4746
export type DaffButtonType = 'daff-button' | 'daff-stroked-button' | 'daff-raised-button' | 'daff-flat-button' | 'daff-icon-button' | 'daff-underline-button' | undefined;
4847

@@ -77,7 +76,6 @@ enum DaffButtonTypeEnum {
7776
styleUrls: ['./button.component.scss'],
7877
//todo(damienwebdev): remove once decorators hit stage 3 - https://github.com/microsoft/TypeScript/issues/7342
7978
// eslint-disable-next-line @angular-eslint/no-inputs-metadata-property
80-
inputs: ['color'],
8179
hostDirectives: [
8280
{ directive: DaffArticleEncapsulatedDirective },
8381
{
@@ -88,13 +86,17 @@ enum DaffButtonTypeEnum {
8886
directive: DaffStatusableDirective,
8987
inputs: ['status'],
9088
},
89+
{
90+
directive: DaffColorableDirective,
91+
inputs: ['color'],
92+
},
9193
],
9294
encapsulation: ViewEncapsulation.None,
9395
changeDetection: ChangeDetectionStrategy.OnPush,
9496
})
9597
export class DaffButtonComponent
9698
extends _daffButtonBase
97-
implements OnInit, DaffPrefixable, DaffSuffixable, DaffColorable {
99+
implements OnInit, DaffPrefixable, DaffSuffixable {
98100

99101
private buttonType: DaffButtonType;
100102

libs/design/callout/src/callout/callout.component.spec.ts

+4-10
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,11 @@ describe('@daffodil/design/callout | DaffCalloutComponent', () => {
6262
});
6363
});
6464

65-
describe('using a colored variant of a callout',() => {
66-
it('should not set a default color', () => {
67-
expect(component.color).toBeFalsy();
68-
});
69-
70-
it('should set a color class on the callout', () => {
71-
wrapper.color = 'primary';
72-
fixture.detectChanges();
65+
it('should take color as an input', () => {
66+
wrapper.color = 'primary';
67+
fixture.detectChanges();
7368

74-
expect(de.nativeElement.classList.contains('daff-primary')).toEqual(true);
75-
});
69+
expect(de.nativeElement.classList.contains('daff-primary')).toEqual(true);
7670
});
7771

7872
it('should take textAlignment as an input', () => {

libs/design/callout/src/callout/callout.component.ts

+10-25
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,20 @@
11
import {
22
Component,
33
ViewEncapsulation,
4-
ElementRef,
54
ChangeDetectionStrategy,
65
HostBinding,
7-
Renderer2,
86
} from '@angular/core';
97

10-
import { DaffCompactableDirective } from '@daffodil/design';
8+
import {
9+
DaffColorableDirective,
10+
DaffCompactableDirective,
11+
} from '@daffodil/design';
1112
import {
1213
DaffArticleEncapsulatedDirective,
13-
DaffColorable,
14-
daffColorMixin,
1514
DaffManageContainerLayoutDirective,
1615
DaffTextAlignableDirective,
1716
} from '@daffodil/design';
1817

19-
/**
20-
* An _elementRef and an instance of renderer2 are needed for the Colorable mixin
21-
*/
22-
class DaffCalloutBase {
23-
constructor(public _elementRef: ElementRef, public _renderer: Renderer2) { }
24-
}
25-
26-
const _daffCalloutBase = daffColorMixin(DaffCalloutBase);
27-
2818
/**
2919
* @inheritdoc
3020
*/
@@ -33,9 +23,6 @@ const _daffCalloutBase = daffColorMixin(DaffCalloutBase);
3323
template: '<ng-content></ng-content>',
3424
styleUrls: ['./callout.component.scss'],
3525
encapsulation: ViewEncapsulation.None,
36-
//todo(damienwebdev): remove once decorators hit stage 3 - https://github.com/microsoft/TypeScript/issues/7342
37-
// eslint-disable-next-line @angular-eslint/no-inputs-metadata-property
38-
inputs: ['color'],
3926
hostDirectives: [
4027
{ directive: DaffArticleEncapsulatedDirective },
4128
{ directive: DaffManageContainerLayoutDirective },
@@ -47,17 +34,15 @@ const _daffCalloutBase = daffColorMixin(DaffCalloutBase);
4734
directive: DaffCompactableDirective,
4835
inputs: ['compact'],
4936
},
37+
{
38+
directive: DaffColorableDirective,
39+
inputs: ['color'],
40+
},
5041
],
5142
changeDetection: ChangeDetectionStrategy.OnPush,
5243
})
53-
export class DaffCalloutComponent extends _daffCalloutBase implements DaffColorable {
54-
constructor(
55-
private elementRef: ElementRef,
56-
private renderer: Renderer2,
57-
private textAlignable: DaffTextAlignableDirective,
58-
) {
59-
super(elementRef, renderer);
60-
44+
export class DaffCalloutComponent {
45+
constructor(private textAlignable: DaffTextAlignableDirective) {
6146
this.textAlignable.textAlignment = 'left';
6247
}
6348

libs/design/card/src/card/card.component.spec.ts

+4-10
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,11 @@ describe('@daffodil/design/card | DaffCardComponent', () => {
6161
});
6262
});
6363

64-
describe('using the color property of a card', () => {
65-
it('should not set a default color', () => {
66-
expect(component.color).toBeFalsy();
67-
});
68-
69-
it('should add the class of the defined color to the host element', () => {
70-
wrapper.color = 'primary';
71-
fixture.detectChanges();
64+
it('should take color as an input', () => {
65+
wrapper.color = 'primary';
66+
fixture.detectChanges();
7267

73-
expect(de.nativeElement.classList.contains('daff-primary')).toEqual(true);
74-
});
68+
expect(de.nativeElement.classList.contains('daff-primary')).toEqual(true);
7569
});
7670

7771
it('should take orientation as an input', () => {

libs/design/card/src/card/card.component.ts

+9-20
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ import {
1111

1212
import {
1313
DaffArticleEncapsulatedDirective,
14-
DaffColorable,
15-
daffColorMixin,
14+
DaffColorableDirective,
1615
} from '@daffodil/design';
1716

1817
export type DaffCardType = null | 'daff-raised-card' | 'daff-stroked-card';
@@ -31,15 +30,6 @@ export enum DaffCardOrientationEnum {
3130
Horizontal = 'horizontal',
3231
}
3332

34-
/**
35-
* An _elementRef and an instance of renderer2 are needed for the Colorable mixin
36-
*/
37-
class DaffCardBase {
38-
constructor(public _elementRef: ElementRef, public _renderer: Renderer2) {}
39-
}
40-
41-
const _daffCardBase = daffColorMixin(DaffCardBase);
42-
4333
/**
4434
* @inheritdoc
4535
*/
@@ -54,16 +44,17 @@ const _daffCardBase = daffColorMixin(DaffCardBase);
5444
templateUrl: './card.component.html',
5545
styleUrls: ['./card.component.scss'],
5646
encapsulation: ViewEncapsulation.None,
57-
//todo(damienwebdev): remove once decorators hit stage 3 - https://github.com/microsoft/TypeScript/issues/7342
58-
// eslint-disable-next-line @angular-eslint/no-inputs-metadata-property
59-
inputs: ['color'],
60-
hostDirectives: [{
61-
directive: DaffArticleEncapsulatedDirective,
62-
}],
47+
hostDirectives: [
48+
{ directive: DaffArticleEncapsulatedDirective },
49+
{
50+
directive: DaffColorableDirective,
51+
inputs: ['color'],
52+
},
53+
],
6354
changeDetection: ChangeDetectionStrategy.OnPush,
6455
})
6556

66-
export class DaffCardComponent extends _daffCardBase implements OnInit, DaffColorable {
57+
export class DaffCardComponent implements OnInit {
6758
private _orientation: DaffCardOrientation = DaffCardOrientationEnum.Vertical;
6859

6960
@Input()
@@ -101,8 +92,6 @@ export class DaffCardComponent extends _daffCardBase implements OnInit, DaffColo
10192
}
10293

10394
constructor(private elementRef: ElementRef, private renderer: Renderer2) {
104-
super(elementRef, renderer);
105-
10695
this.cardType = this.initCardType();
10796
}
10897

libs/design/hero/src/hero/hero.component.spec.ts

+4-10
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,11 @@ describe('@daffodil/design/hero | DaffHeroComponent', () => {
5959
});
6060
});
6161

62-
describe('using a color variant of a hero', () => {
63-
it('should set a color class on the hero', () => {
64-
wrapper.color = 'primary';
65-
fixture.detectChanges();
66-
67-
expect(de.nativeElement.classList.contains('daff-primary')).toEqual(true);
68-
});
62+
it('should take color as an input', () => {
63+
wrapper.color = 'primary';
64+
fixture.detectChanges();
6965

70-
it('should not set a default color', () => {
71-
expect(component.color).toBeFalsy();
72-
});
66+
expect(de.nativeElement.classList.contains('daff-primary')).toEqual(true);
7367
});
7468

7569
it('should take textAlignment as an input', () => {

libs/design/hero/src/hero/hero.component.ts

+10-25
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,20 @@
11
import {
22
Component,
33
ViewEncapsulation,
4-
ElementRef,
54
ChangeDetectionStrategy,
65
HostBinding,
7-
Renderer2,
86
} from '@angular/core';
97

10-
import { DaffCompactableDirective } from '@daffodil/design';
8+
import {
9+
DaffColorableDirective,
10+
DaffCompactableDirective,
11+
} from '@daffodil/design';
1112
import {
1213
DaffArticleEncapsulatedDirective,
13-
DaffColorable,
14-
daffColorMixin,
1514
DaffManageContainerLayoutDirective,
1615
DaffTextAlignableDirective,
1716
} from '@daffodil/design';
1817

19-
/**
20-
* An _elementRef and an instance of renderer2 are needed for the hero mixins
21-
*/
22-
class DaffHeroBase {
23-
constructor(public _elementRef: ElementRef, public _renderer: Renderer2) {}
24-
}
25-
26-
const _daffHeroBase = daffColorMixin(DaffHeroBase);
27-
2818
/**
2919
* @inheritdoc
3020
*/
@@ -33,9 +23,6 @@ const _daffHeroBase = daffColorMixin(DaffHeroBase);
3323
template: '<ng-content></ng-content>',
3424
styleUrls: ['./hero.component.scss'],
3525
encapsulation: ViewEncapsulation.None,
36-
//todo(damienwebdev): remove once decorators hit stage 3 - https://github.com/microsoft/TypeScript/issues/7342
37-
// eslint-disable-next-line @angular-eslint/no-inputs-metadata-property
38-
inputs: ['color'],
3926
hostDirectives: [
4027
{ directive: DaffArticleEncapsulatedDirective },
4128
{ directive: DaffManageContainerLayoutDirective },
@@ -47,17 +34,15 @@ const _daffHeroBase = daffColorMixin(DaffHeroBase);
4734
directive: DaffCompactableDirective,
4835
inputs: ['compact'],
4936
},
37+
{
38+
directive: DaffColorableDirective,
39+
inputs: ['color'],
40+
},
5041
],
5142
changeDetection: ChangeDetectionStrategy.OnPush,
5243
})
53-
export class DaffHeroComponent extends _daffHeroBase implements DaffColorable {
54-
constructor(
55-
private elementRef: ElementRef,
56-
private renderer: Renderer2,
57-
private textAlignable: DaffTextAlignableDirective,
58-
){
59-
super(elementRef, renderer);
60-
44+
export class DaffHeroComponent {
45+
constructor(private textAlignable: DaffTextAlignableDirective) {
6146
this.textAlignable.defaultAlignment = 'left';
6247
}
6348

libs/design/loading-icon/src/loading-icon/loading-icon.component.spec.ts

+4-13
Original file line numberDiff line numberDiff line change
@@ -65,19 +65,10 @@ describe('@daffodil/design/loading-icon | DaffLoadingIconComponent', () => {
6565
expect(de.nativeElement.style.maxWidth).toEqual('50px');
6666
});
6767

68-
describe('using a colored variant of a loading icon',() => {
69-
let loadingIconDe;
70-
71-
it('should not set a default color', () => {
72-
expect(component.color).toBeFalsy();
73-
});
74-
75-
it('should set a color class on the loading icon', () => {
76-
wrapper.color = 'secondary';
77-
fixture.detectChanges();
68+
it('should take color as an input', () => {
69+
wrapper.color = 'primary';
70+
fixture.detectChanges();
7871

79-
loadingIconDe = fixture.debugElement.query(By.css('daff-loading-icon'));
80-
expect(loadingIconDe.nativeElement.classList.contains('daff-secondary')).toEqual(true);
81-
});
72+
expect(de.nativeElement.classList.contains('daff-primary')).toEqual(true);
8273
});
8374
});

libs/design/loading-icon/src/loading-icon/loading-icon.component.ts

+8-23
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,10 @@ import {
22
Component,
33
ChangeDetectionStrategy,
44
Input,
5-
ElementRef,
6-
Renderer2,
75
HostBinding,
86
} from '@angular/core';
97

10-
import {
11-
daffColorMixin,
12-
DaffColorable,
13-
} from '@daffodil/design';
14-
15-
/**
16-
* An _elementRef and an instance of renderer2 are needed for the Colorable mixin
17-
*/
18-
class DaffLoadingIconBase{
19-
constructor(public _elementRef: ElementRef, public _renderer: Renderer2) {}
20-
}
21-
22-
const _daffLoadingIconBase = daffColorMixin(DaffLoadingIconBase);
8+
import { DaffColorableDirective } from '@daffodil/design';
239

2410
/**
2511
* @inheritdoc
@@ -28,12 +14,15 @@ const _daffLoadingIconBase = daffColorMixin(DaffLoadingIconBase);
2814
selector: 'daff-loading-icon',
2915
templateUrl: './loading-icon.component.html',
3016
styleUrls: ['./loading-icon.component.scss'],
31-
//todo(damienwebdev): remove once decorators hit stage 3 - https://github.com/microsoft/TypeScript/issues/7342
32-
// eslint-disable-next-line @angular-eslint/no-inputs-metadata-property
33-
inputs: ['color'],
17+
hostDirectives: [
18+
{
19+
directive: DaffColorableDirective,
20+
inputs: ['color'],
21+
},
22+
],
3423
changeDetection: ChangeDetectionStrategy.OnPush,
3524
})
36-
export class DaffLoadingIconComponent extends _daffLoadingIconBase implements DaffColorable {
25+
export class DaffLoadingIconComponent {
3726

3827
/**
3928
* The (pixel) diameter of the animation
@@ -50,8 +39,4 @@ export class DaffLoadingIconComponent extends _daffLoadingIconBase implements Da
5039
@HostBinding('style.max-width') get maxWidth() {
5140
return this.diameter + 'px';
5241
}
53-
54-
constructor(private elementRef: ElementRef, private renderer: Renderer2) {
55-
super(elementRef, renderer);
56-
}
5742
}

0 commit comments

Comments
 (0)