-
Notifications
You must be signed in to change notification settings - Fork 6.7k
/
button-base.ts
171 lines (150 loc) · 5.76 KB
/
button-base.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/**
* @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 {Platform} from '@angular/cdk/platform';
import {Directive, ElementRef, Inject, NgZone, Optional, ViewChild} from '@angular/core';
import {
CanColor,
CanColorCtor,
CanDisable,
CanDisableCtor,
CanDisableRipple,
CanDisableRippleCtor,
MatRipple,
mixinColor,
mixinDisabled,
mixinDisableRipple,
RippleAnimationConfig
} from '@angular/material/core';
import {ANIMATION_MODULE_TYPE} from '@angular/platform-browser/animations';
import {numbers} from '@material/ripple';
/** Inputs common to all buttons. */
export const MAT_BUTTON_INPUTS = ['disabled', 'disableRipple', 'color'];
/** Shared host configuration for all buttons */
export const MAT_BUTTON_HOST = {
'[attr.disabled]': 'disabled || null',
'[class._mat-animation-noopable]': '_animationMode === "NoopAnimations"',
// MDC automatically applies the primary theme color to the button, but we want to support
// an unthemed version. If color is undefined, apply a CSS class that makes it easy to
// select and style this "theme".
'[class.mat-unthemed]': '!color',
};
/** List of classes to add to buttons instances based on host attribute selector. */
const HOST_SELECTOR_MDC_CLASS_PAIR: {selector: string, mdcClasses: string[]}[] = [
{
selector: 'mat-button',
mdcClasses: ['mdc-button', 'mat-mdc-button'],
},
{
selector: 'mat-flat-button',
mdcClasses: ['mdc-button', 'mdc-button--unelevated', 'mat-mdc-unelevated-button'],
},
{
selector: 'mat-raised-button',
mdcClasses: ['mdc-button', 'mdc-button--raised', 'mat-mdc-raised-button'],
},
{
selector: 'mat-stroked-button',
mdcClasses: ['mdc-button', 'mdc-button--outlined', 'mat-mdc-outlined-button'],
},
{
selector: 'mat-fab',
mdcClasses: ['mdc-fab', 'mat-mdc-fab'],
},
{
selector: 'mat-mini-fab',
mdcClasses: ['mdc-fab', 'mdc-fab--mini', 'mat-mdc-mini-fab'],
},
{
selector: 'mat-icon-button',
mdcClasses: ['mdc-icon-button', 'mat-mdc-icon-button'],
}
];
// Boilerplate for applying mixins to MatButton.
/** @docs-private */
export class MatButtonMixinCore {
constructor(public _elementRef: ElementRef) {}
}
export const _MatButtonBaseMixin: CanDisableRippleCtor&CanDisableCtor&CanColorCtor&
typeof MatButtonMixinCore = mixinColor(mixinDisabled(mixinDisableRipple(MatButtonMixinCore)));
/** Base class for all buttons. */
@Directive()
export class MatButtonBase extends _MatButtonBaseMixin implements CanDisable, CanColor,
CanDisableRipple {
/** The ripple animation configuration to use for the buttons. */
_rippleAnimation: RippleAnimationConfig = {
enterDuration: numbers.DEACTIVATION_TIMEOUT_MS,
exitDuration: numbers.FG_DEACTIVATION_MS
};
/** Whether the ripple is centered on the button. */
_isRippleCentered = false;
/** Reference to the MatRipple instance of the button. */
@ViewChild(MatRipple, {static: false}) ripple: MatRipple;
constructor(
elementRef: ElementRef, public _platform: Platform, public _ngZone: NgZone,
// TODO(devversion): Injection can be removed if angular/angular#32981 is fixed.
@Optional() @Inject(ANIMATION_MODULE_TYPE) public _animationMode?: string) {
super(elementRef);
const classList = (elementRef.nativeElement as HTMLElement).classList;
// For each of the variant selectors that is present in the button's host
// attributes, add the correct corresponding MDC classes.
for (const pair of HOST_SELECTOR_MDC_CLASS_PAIR) {
if (this._hasHostAttributes(pair.selector)) {
pair.mdcClasses.forEach(className => {
classList.add(className);
});
}
}
}
/** Focuses the button. */
focus(): void {
this._elementRef.nativeElement.focus();
}
/** Gets whether the button has one of the given attributes. */
private _hasHostAttributes(...attributes: string[]) {
return attributes.some(attribute => this._elementRef.nativeElement.hasAttribute(attribute));
}
_isRippleDisabled() {
return this.disableRipple || this.disabled;
}
}
/** Shared inputs by buttons using the `<a>` tag */
export const MAT_ANCHOR_INPUTS = ['disabled', 'disableRipple', 'color', 'tabIndex'];
/** Shared host configuration for buttons using the `<a>` tag. */
export const MAT_ANCHOR_HOST = {
'[attr.disabled]': 'disabled || null',
'[class._mat-animation-noopable]': '_animationMode === "NoopAnimations"',
// Note that we ignore the user-specified tabindex when it's disabled for
// consistency with the `mat-button` applied on native buttons where even
// though they have an index, they're not tabbable.
'[attr.tabindex]': 'disabled ? -1 : (tabIndex || 0)',
'[attr.aria-disabled]': 'disabled.toString()',
'(click)': '_haltDisabledEvents($event)',
// MDC automatically applies the primary theme color to the button, but we want to support
// an unthemed version. If color is undefined, apply a CSS class that makes it easy to
// select and style this "theme".
'[class.mat-unthemed]': '!color',
};
/**
* Anchor button base.
*/
@Directive()
export class MatAnchorBase extends MatButtonBase {
tabIndex: number;
constructor(elementRef: ElementRef, platform: Platform, ngZone: NgZone,
// TODO(devversion): Injection can be removed if angular/angular#32981 is fixed.
@Optional() @Inject(ANIMATION_MODULE_TYPE) animationMode?: string) {
super(elementRef, platform, ngZone, animationMode);
}
_haltDisabledEvents(event: Event) {
// A disabled button shouldn't apply any actions
if (this.disabled) {
event.preventDefault();
event.stopImmediatePropagation();
}
}
}