-
Notifications
You must be signed in to change notification settings - Fork 6.7k
/
color.ts
77 lines (65 loc) · 2.01 KB
/
color.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {AbstractConstructor, Constructor} from './constructor';
import {ElementRef} from '@angular/core';
/**
* @docs-private
* @deprecated Will be removed together with `mixinColor`.
* @breaking-change 19.0.0
*/
export interface CanColor {
/** Theme color palette for the component. */
color: ThemePalette;
/** Default color to fall back to if no value is set. */
defaultColor: ThemePalette | undefined;
}
type CanColorCtor = Constructor<CanColor> & AbstractConstructor<CanColor>;
/** @docs-private */
interface HasElementRef {
_elementRef: ElementRef;
}
/** Possible color palette values. */
export type ThemePalette = 'primary' | 'accent' | 'warn' | undefined;
/**
* Mixin to augment a directive with a `color` property.
* @deprecated Use a plain input and host bindings instead.
* @breaking-change 19.0.0
*/
export function mixinColor<T extends AbstractConstructor<HasElementRef>>(
base: T,
defaultColor?: ThemePalette,
): CanColorCtor & T;
export function mixinColor<T extends Constructor<HasElementRef>>(
base: T,
defaultColor?: ThemePalette,
): CanColorCtor & T {
return class extends base {
private _color: ThemePalette;
defaultColor = defaultColor;
get color(): ThemePalette {
return this._color;
}
set color(value: ThemePalette) {
const colorPalette = value || this.defaultColor;
if (colorPalette !== this._color) {
if (this._color) {
this._elementRef.nativeElement.classList.remove(`mat-${this._color}`);
}
if (colorPalette) {
this._elementRef.nativeElement.classList.add(`mat-${colorPalette}`);
}
this._color = colorPalette;
}
}
constructor(...args: any[]) {
super(...args);
// Set the default color that can be specified from the mixin.
this.color = defaultColor;
}
};
}