forked from angular/components
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbutton.ts
123 lines (106 loc) · 4.04 KB
/
button.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/**
* @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.dev/license
*/
import {ChangeDetectionStrategy, Component, Input, ViewEncapsulation} from '@angular/core';
import {MatButtonAppearance, MatButtonBase} from './button-base';
/**
* Classes that need to be set for each appearance of the button.
* Note that we use a `Map` here to avoid issues with property renaming.
*/
const APPEARANCE_CLASSES: Map<MatButtonAppearance, readonly string[]> = new Map([
['text', ['mat-mdc-button']],
['filled', ['mdc-button--unelevated', 'mat-mdc-unelevated-button']],
['elevated', ['mdc-button--raised', 'mat-mdc-raised-button']],
['outlined', ['mdc-button--outlined', 'mat-mdc-outlined-button']],
['tonal', ['mat-tonal-button']],
]);
/**
* Material Design button component. Users interact with a button to perform an action.
* See https://m3.material.io/components/buttons/overview
*/
@Component({
selector: `
button[matButton], a[matButton], button[mat-button], button[mat-raised-button],
button[mat-flat-button], button[mat-stroked-button], a[mat-button], a[mat-raised-button],
a[mat-flat-button], a[mat-stroked-button]
`,
templateUrl: 'button.html',
styleUrls: ['button.css', 'button-high-contrast.css'],
host: {
'class': 'mdc-button',
},
exportAs: 'matButton, matAnchor',
encapsulation: ViewEncapsulation.None,
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class MatButton extends MatButtonBase {
/** Appearance of the button. */
@Input('matButton')
get appearance(): MatButtonAppearance | null {
return this._appearance;
}
set appearance(value: MatButtonAppearance | '') {
// Allow empty string so users can do `<button matButton></button>`
// without having to write out `="text"` every time.
this.setAppearance(value || this._config?.defaultAppearance || 'text');
}
private _appearance: MatButtonAppearance | null = null;
constructor(...args: unknown[]);
constructor() {
super();
const inferredAppearance = _inferAppearance(this._elementRef.nativeElement);
// Only set the appearance if we managed to infer it from the static attributes, rather than
// doing something like `setAppearance(inferredAppearance || 'text')`, because doing so can
// cause the fallback appearance's classes to be set and then immediately replaced when
// the input value is assigned.
if (inferredAppearance) {
this.setAppearance(inferredAppearance);
}
}
/** Programmatically sets the appearance of the button. */
setAppearance(appearance: MatButtonAppearance): void {
if (appearance === this._appearance) {
return;
}
const classList = this._elementRef.nativeElement.classList;
const previousClasses = this._appearance ? APPEARANCE_CLASSES.get(this._appearance) : null;
const newClasses = APPEARANCE_CLASSES.get(appearance)!;
if ((typeof ngDevMode === 'undefined' || ngDevMode) && !newClasses) {
throw new Error(`Unsupported MatButton appearance "${appearance}"`);
}
if (previousClasses) {
classList.remove(...previousClasses);
}
classList.add(...newClasses);
this._appearance = appearance;
}
}
/** Infers the button's appearance from its static attributes. */
function _inferAppearance(button: HTMLElement): MatButtonAppearance | null {
if (button.hasAttribute('mat-raised-button')) {
return 'elevated';
}
if (button.hasAttribute('mat-stroked-button')) {
return 'outlined';
}
if (button.hasAttribute('mat-flat-button')) {
return 'filled';
}
if (button.hasAttribute('mat-button')) {
return 'text';
}
return null;
}
// tslint:disable:variable-name
/**
* Material Design button component for anchor elements. Anchor elements are used to provide
* links for the user to navigate across different routes or pages.
* See https://m3.material.io/components/buttons/overview
*/
export const MatAnchor = MatButton;
export type MatAnchor = MatButton;
// tslint:enable:variable-name