Skip to content

Commit 37df5d3

Browse files
devversionandrewseguin
authored andcommitted
refactor(button): use color binding for fab button color (#4694)
* refactor(button): use color binding for fab button color * No longer creates extra CSS selectors to set the default color for round buttons to `accent`. * Server-side rendering improvements * Use forwardRef for constructor DI
1 parent 3bc82f6 commit 37df5d3

File tree

4 files changed

+56
-22
lines changed

4 files changed

+56
-22
lines changed

src/lib/button/_button-theme.scss

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -104,18 +104,6 @@
104104
.mat-icon-button {
105105
@include _mat-button-ripple-color($theme, default);
106106
}
107-
108-
// TODO(devversion): The color class accent should be just set from TS code. No need for this.
109-
.mat-fab, .mat-mini-fab {
110-
background-color: mat-color($accent);
111-
color: mat-color($accent, default-contrast);
112-
113-
// Button fab elements are using the accent palette by default. The color classes won't
114-
// be set on the element. To have a proper ripple color for those, we set the ripple color.
115-
.mat-ripple-element {
116-
background-color: mat-color($accent, default-contrast, 0.2);
117-
}
118-
}
119107
}
120108

121109
@mixin mat-button-typography($config) {

src/lib/button/button.spec.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,30 @@ describe('MdButton', () => {
6767
expect(buttonDebugElement.nativeElement.classList.contains('custom-class')).toBe(true);
6868
});
6969

70+
describe('button[md-fab]', () => {
71+
it('should have accent palette by default', () => {
72+
const fixture = TestBed.createComponent(TestApp);
73+
const fabButtonDebugEl = fixture.debugElement.query(By.css('button[md-fab]'));
74+
75+
fixture.detectChanges();
76+
77+
expect(fabButtonDebugEl.nativeElement.classList)
78+
.toContain('mat-accent', 'Expected fab buttons to use accent palette by default');
79+
});
80+
});
81+
82+
describe('button[md-mini-fab]', () => {
83+
it('should have accent palette by default', () => {
84+
const fixture = TestBed.createComponent(TestApp);
85+
const miniFabButtonDebugEl = fixture.debugElement.query(By.css('button[md-mini-fab]'));
86+
87+
fixture.detectChanges();
88+
89+
expect(miniFabButtonDebugEl.nativeElement.classList)
90+
.toContain('mat-accent', 'Expected mini-fab buttons to use accent palette by default');
91+
});
92+
});
93+
7094
// Regular button tests
7195
describe('button[md-button]', () => {
7296
it('should handle a click on the button', () => {
@@ -223,6 +247,8 @@ describe('MdButton', () => {
223247
Go
224248
</button>
225249
<a href="http://www.google.com" md-button [disabled]="isDisabled" [color]="buttonColor">Link</a>
250+
<button md-fab>Fab Button</button>
251+
<button md-mini-fab>Mini Fab Button</button>
226252
`
227253
})
228254
class TestApp {

src/lib/button/button.ts

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@ import {
33
Component,
44
Directive,
55
ElementRef,
6+
forwardRef,
67
HostBinding,
78
Input,
89
OnDestroy,
10+
Optional,
911
Renderer2,
10-
ViewEncapsulation
12+
Self,
13+
ViewEncapsulation,
14+
Inject
1115
} from '@angular/core';
1216
import {coerceBooleanProperty, FocusOriginMonitor, Platform} from '../core';
1317
import {mixinDisabled, CanDisable} from '../core/common-behaviors/disabled';
@@ -16,6 +20,9 @@ import {CanColor, mixinColor} from '../core/common-behaviors/color';
1620

1721
// TODO(kara): Convert attribute selectors to classes when attr maps become available
1822

23+
/** Default color palette for round buttons (md-fab and md-mini-fab) */
24+
const DEFAULT_ROUND_BUTTON_COLOR = 'accent';
25+
1926

2027
/**
2128
* Directive whose purpose is to add the mat- CSS styling to this selector.
@@ -58,17 +65,30 @@ export class MdIconButtonCssMatStyler {}
5865
selector: 'button[md-fab], button[mat-fab], a[md-fab], a[mat-fab]',
5966
host: {'class': 'mat-fab'}
6067
})
61-
export class MdFabCssMatStyler {}
68+
export class MdFab {
69+
constructor(@Self() @Optional() @Inject(forwardRef(() => MdButton)) button: MdButton,
70+
@Self() @Optional() @Inject(forwardRef(() => MdAnchor)) anchor: MdAnchor) {
71+
// Set the default color palette for the md-fab components.
72+
(button || anchor).color = DEFAULT_ROUND_BUTTON_COLOR;
73+
}
74+
}
6275

6376
/**
64-
* Directive whose purpose is to add the mat- CSS styling to this selector.
77+
* Directive that targets mini-fab buttons and anchors. It's used to apply the `mat-` class
78+
* to all mini-fab buttons and also is responsible for setting the default color palette.
6579
* @docs-private
6680
*/
6781
@Directive({
6882
selector: 'button[md-mini-fab], button[mat-mini-fab], a[md-mini-fab], a[mat-mini-fab]',
6983
host: {'class': 'mat-mini-fab'}
7084
})
71-
export class MdMiniFabCssMatStyler {}
85+
export class MdMiniFab {
86+
constructor(@Self() @Optional() @Inject(forwardRef(() => MdButton)) button: MdButton,
87+
@Self() @Optional() @Inject(forwardRef(() => MdAnchor)) anchor: MdAnchor) {
88+
// Set the default color palette for the md-mini-fab components.
89+
(button || anchor).color = DEFAULT_ROUND_BUTTON_COLOR;
90+
}
91+
}
7292

7393

7494
// Boilerplate for applying mixins to MdButton.

src/lib/button/index.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import {MdCommonModule, MdRippleModule, StyleModule} from '../core';
44
import {
55
MdAnchor,
66
MdButton,
7+
MdMiniFab,
78
MdButtonCssMatStyler,
8-
MdFabCssMatStyler,
9+
MdFab,
910
MdIconButtonCssMatStyler,
10-
MdMiniFabCssMatStyler,
1111
MdRaisedButtonCssMatStyler
1212
} from './button';
1313

@@ -25,21 +25,21 @@ export * from './button';
2525
exports: [
2626
MdButton,
2727
MdAnchor,
28+
MdMiniFab,
29+
MdFab,
2830
MdCommonModule,
2931
MdButtonCssMatStyler,
3032
MdRaisedButtonCssMatStyler,
3133
MdIconButtonCssMatStyler,
32-
MdFabCssMatStyler,
33-
MdMiniFabCssMatStyler,
3434
],
3535
declarations: [
3636
MdButton,
3737
MdAnchor,
38+
MdMiniFab,
39+
MdFab,
3840
MdButtonCssMatStyler,
3941
MdRaisedButtonCssMatStyler,
4042
MdIconButtonCssMatStyler,
41-
MdFabCssMatStyler,
42-
MdMiniFabCssMatStyler,
4343
],
4444
})
4545
export class MdButtonModule {}

0 commit comments

Comments
 (0)