Skip to content

Commit 4d678d4

Browse files
jelbourntinayuangao
authored andcommitted
chore: add style compatibility modes and mat- prefix (#1932)
* wip no-conflict * add to modules * re-adding export
1 parent dfc580d commit 4d678d4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+435
-236
lines changed

src/lib/button-toggle/button-toggle.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,12 @@ import {
1919
} from '@angular/core';
2020
import {NG_VALUE_ACCESSOR, ControlValueAccessor, FormsModule} from '@angular/forms';
2121
import {Observable} from 'rxjs/Observable';
22-
import {MdUniqueSelectionDispatcher, coerceBooleanProperty} from '../core';
22+
import {
23+
MdUniqueSelectionDispatcher,
24+
coerceBooleanProperty,
25+
DefaultStyleCompatibilityModeModule,
26+
} from '../core';
27+
2328

2429
export type ToggleType = 'checkbox' | 'radio';
2530

@@ -436,8 +441,13 @@ export class MdButtonToggle implements OnInit {
436441

437442

438443
@NgModule({
439-
imports: [FormsModule],
440-
exports: [MdButtonToggleGroup, MdButtonToggleGroupMultiple, MdButtonToggle],
444+
imports: [FormsModule, DefaultStyleCompatibilityModeModule],
445+
exports: [
446+
MdButtonToggleGroup,
447+
MdButtonToggleGroupMultiple,
448+
MdButtonToggle,
449+
DefaultStyleCompatibilityModeModule,
450+
],
441451
declarations: [MdButtonToggleGroup, MdButtonToggleGroupMultiple, MdButtonToggle],
442452
})
443453
export class MdButtonToggleModule {

src/lib/button/button.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import {
1010
ModuleWithProviders,
1111
} from '@angular/core';
1212
import {CommonModule} from '@angular/common';
13-
import {MdRippleModule, coerceBooleanProperty} from '../core';
13+
import {MdRippleModule, coerceBooleanProperty, DefaultStyleCompatibilityModeModule} from '../core';
14+
1415

1516
// TODO(jelbourn): Make the `isMouseDown` stuff done with one global listener.
1617
// TODO(kara): Convert attribute selectors to classes when attr maps become available
@@ -157,8 +158,8 @@ export class MdAnchor extends MdButton {
157158

158159

159160
@NgModule({
160-
imports: [CommonModule, MdRippleModule],
161-
exports: [MdButton, MdAnchor],
161+
imports: [CommonModule, MdRippleModule, DefaultStyleCompatibilityModeModule],
162+
exports: [MdButton, MdAnchor, DefaultStyleCompatibilityModeModule],
162163
declarations: [MdButton, MdAnchor],
163164
})
164165
export class MdButtonModule {

src/lib/card/card-header.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
<ng-content select="[md-card-avatar]"></ng-content>
1+
<ng-content select="[md-card-avatar], [mat-card-avatar]"></ng-content>
22
<div class="md-card-header-text">
3-
<ng-content select="md-card-title, md-card-subtitle"></ng-content>
3+
<ng-content
4+
select="md-card-title, mat-card-title, md-card-subtitle, mat-card-subtitle"></ng-content>
45
</div>
56
<ng-content></ng-content>

src/lib/card/card-title-group.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<div>
2-
<ng-content select="md-card-title, md-card-subtitle"></ng-content>
2+
<ng-content
3+
select="md-card-title, mat-card-title, md-card-subtitle, mat-card-subtitle"></ng-content>
34
</div>
45
<ng-content select="img"></ng-content>
56
<ng-content></ng-content>

src/lib/card/card.ts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,45 +6,46 @@ import {
66
ChangeDetectionStrategy,
77
Directive
88
} from '@angular/core';
9+
import {DefaultStyleCompatibilityModeModule} from '../core';
910

1011

1112
/**
1213
* Content of a card, needed as it's used as a selector in the API.
1314
*/
1415
@Directive({
15-
selector: 'md-card-content'
16+
selector: 'md-card-content, mat-card-content'
1617
})
1718
export class MdCardContent {}
1819

1920
/**
2021
* Title of a card, needed as it's used as a selector in the API.
2122
*/
2223
@Directive({
23-
selector: 'md-card-title'
24+
selector: 'md-card-title, mat-card-title'
2425
})
2526
export class MdCardTitle {}
2627

2728
/**
2829
* Sub-title of a card, needed as it's used as a selector in the API.
2930
*/
3031
@Directive({
31-
selector: 'md-card-subtitle'
32+
selector: 'md-card-subtitle, mat-card-subtitle'
3233
})
3334
export class MdCardSubtitle {}
3435

3536
/**
3637
* Action section of a card, needed as it's used as a selector in the API.
3738
*/
3839
@Directive({
39-
selector: 'md-card-actions'
40+
selector: 'md-card-actions, mat-card-actions'
4041
})
4142
export class MdCardActions {}
4243

4344
/**
4445
* Footer of a card, needed as it's used as a selector in the API.
4546
*/
4647
@Directive({
47-
selector: 'md-card-footer'
48+
selector: 'md-card-footer, mat-card-footer'
4849
})
4950
export class MdCardFooter {}
5051

@@ -70,7 +71,7 @@ it also provides a number of preset styles for common card sections, including:
7071

7172
@Component({
7273
moduleId: module.id,
73-
selector: 'md-card',
74+
selector: 'md-card, mat-card',
7475
templateUrl: 'card.html',
7576
styleUrls: ['card.css'],
7677
encapsulation: ViewEncapsulation.None,
@@ -95,7 +96,7 @@ TODO(kara): update link to demo site when it exists
9596

9697
@Component({
9798
moduleId: module.id,
98-
selector: 'md-card-header',
99+
selector: 'md-card-header, mat-card-header',
99100
templateUrl: 'card-header.html',
100101
encapsulation: ViewEncapsulation.None,
101102
changeDetection: ChangeDetectionStrategy.OnPush,
@@ -115,7 +116,7 @@ TODO(kara): update link to demo site when it exists
115116

116117
@Component({
117118
moduleId: module.id,
118-
selector: 'md-card-title-group',
119+
selector: 'md-card-title-group, mat-card-title-group',
119120
templateUrl: 'card-title-group.html',
120121
encapsulation: ViewEncapsulation.None,
121122
changeDetection: ChangeDetectionStrategy.OnPush,
@@ -124,9 +125,17 @@ export class MdCardTitleGroup {}
124125

125126

126127
@NgModule({
128+
imports: [DefaultStyleCompatibilityModeModule],
127129
exports: [
128-
MdCard, MdCardHeader, MdCardTitleGroup, MdCardContent, MdCardTitle, MdCardSubtitle,
129-
MdCardActions, MdCardFooter
130+
MdCard,
131+
MdCardHeader,
132+
MdCardTitleGroup,
133+
MdCardContent,
134+
MdCardTitle,
135+
MdCardSubtitle,
136+
MdCardActions,
137+
MdCardFooter,
138+
DefaultStyleCompatibilityModeModule,
130139
],
131140
declarations: [
132141
MdCard, MdCardHeader, MdCardTitleGroup, MdCardContent, MdCardTitle, MdCardSubtitle,

src/lib/checkbox/checkbox.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ import {
1515
import {CommonModule} from '@angular/common';
1616
import {NG_VALUE_ACCESSOR, ControlValueAccessor} from '@angular/forms';
1717
import {coerceBooleanProperty} from '../core/coersion/boolean-property';
18-
import {MdRippleModule} from '../core';
18+
import {MdRippleModule, DefaultStyleCompatibilityModeModule} from '../core';
19+
1920

2021
/**
2122
* Monotonically increasing integer used to auto-generate unique ids for checkbox components.
@@ -62,7 +63,7 @@ export class MdCheckboxChange {
6263
*/
6364
@Component({
6465
moduleId: module.id,
65-
selector: 'md-checkbox',
66+
selector: 'md-checkbox, mat-checkbox',
6667
templateUrl: 'checkbox.html',
6768
styleUrls: ['checkbox.css'],
6869
host: {
@@ -378,8 +379,8 @@ export class MdCheckbox implements ControlValueAccessor {
378379

379380

380381
@NgModule({
381-
imports: [CommonModule, MdRippleModule],
382-
exports: [MdCheckbox],
382+
imports: [CommonModule, MdRippleModule, DefaultStyleCompatibilityModeModule],
383+
exports: [MdCheckbox, DefaultStyleCompatibilityModeModule],
383384
declarations: [MdCheckbox],
384385
})
385386
export class MdCheckboxModule {
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import {Component} from '@angular/core';
2+
import {async, TestBed} from '@angular/core/testing';
3+
import {MdCheckboxModule} from '../../checkbox/checkbox';
4+
import {NoConflictStyleCompatibilityMode} from './no-conflict-mode';
5+
6+
7+
describe('Style compatibility', () => {
8+
9+
describe('in default mode', () => {
10+
beforeEach(async(() => {
11+
TestBed.configureTestingModule({
12+
// Specifically do *not* directly import the DefaultStyleCompatibilityModeModule
13+
// to ensure that it is the default behavior.
14+
imports: [MdCheckboxModule.forRoot()],
15+
declarations: [ComponentWithMdCheckbox, ComponentWithMatCheckbox],
16+
});
17+
18+
TestBed.compileComponents();
19+
}));
20+
21+
it('should throw an error when trying to use the "mat-" prefix', () => {
22+
expect(() => {
23+
TestBed.createComponent(ComponentWithMatCheckbox);
24+
}).toThrowError(/The "mat-" prefix cannot be used out of ng-material v1 compatibility mode/);
25+
});
26+
});
27+
28+
describe('in no-conflict mode', () => {
29+
beforeEach(async(() => {
30+
TestBed.configureTestingModule({
31+
imports: [MdCheckboxModule.forRoot(), NoConflictStyleCompatibilityMode],
32+
declarations: [ComponentWithMdCheckbox, ComponentWithMatCheckbox],
33+
});
34+
35+
TestBed.compileComponents();
36+
}));
37+
38+
it('should not throw an error when trying to use the "mat-" prefix', () => {
39+
TestBed.createComponent(ComponentWithMatCheckbox);
40+
});
41+
42+
it('should throw an error when trying to use the "md-" prefix', () => {
43+
expect(() => {
44+
TestBed.createComponent(ComponentWithMdCheckbox);
45+
}).toThrowError(/The "md-" prefix cannot be used in ng-material v1 compatibility mode/);
46+
});
47+
});
48+
});
49+
50+
51+
@Component({ template: `<md-checkbox>Hungry</md-checkbox>` })
52+
class ComponentWithMdCheckbox { }
53+
54+
@Component({ template: `<mat-checkbox>Hungry</mat-checkbox>` })
55+
class ComponentWithMatCheckbox { }
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import {NgModule, ModuleWithProviders, Directive, OpaqueToken, Inject} from '@angular/core';
2+
3+
4+
export const MATERIAL_COMPATIBILITY_MODE = new OpaqueToken('md-compatibiility-mode');
5+
6+
/** Selector that matches all elements that may have style collisions with material1. */
7+
export const MAT_ELEMENTS_SELECTOR = `
8+
mat-card,
9+
mat-card-actions,
10+
mat-card-content,
11+
mat-card-footer,
12+
mat-card-header,
13+
mat-card-subtitle,
14+
mat-card-title,
15+
mat-card-title-group,
16+
mat-checkbox,
17+
mat-chip,
18+
mat-dialog-container,
19+
mat-divider,
20+
mat-grid-list,
21+
mat-grid-tile,
22+
mat-grid-tile-footer,
23+
mat-grid-tile-header,
24+
mat-hint,
25+
mat-icon,
26+
mat-ink-bar,
27+
mat-input,
28+
mat-list,
29+
mat-list-item,
30+
mat-menu,
31+
mat-nav-list,
32+
mat-option,
33+
mat-placeholder,
34+
mat-progress-bar,
35+
mat-progress-circle,
36+
mat-radio-button,
37+
mat-radio-group,
38+
mat-select,
39+
mat-sidenav,
40+
mat-slider,
41+
mat-spinner,
42+
mat-tab,
43+
mat-toolbar
44+
`;
45+
46+
/** Directive that enforces that the `mat-` prefix cannot be used. */
47+
@Directive({selector: MAT_ELEMENTS_SELECTOR})
48+
export class MatPrefixEnforcer {
49+
constructor(@Inject(MATERIAL_COMPATIBILITY_MODE) isCompatibilityMode: boolean) {
50+
if (!isCompatibilityMode) {
51+
throw Error('The "mat-" prefix cannot be used out of ng-material v1 compatibility mode.');
52+
}
53+
}
54+
}
55+
56+
57+
/**
58+
* Module that enforces the default "compatibility mode" settings. When this module is loaded
59+
* without NoConflictStyleCompatibilityMode also being imported, it will throw an error if
60+
* there are any uses of the `mat-` prefix.
61+
*
62+
* Because the point of this directive is to *not* be used, it will be tree-shaken away by
63+
* optimizers when not in compatibility mode.
64+
*/
65+
@NgModule({
66+
declarations: [MatPrefixEnforcer],
67+
exports: [MatPrefixEnforcer],
68+
providers: [{
69+
provide: MATERIAL_COMPATIBILITY_MODE, useValue: false,
70+
}]
71+
})
72+
export class DefaultStyleCompatibilityModeModule {
73+
static forRoot(): ModuleWithProviders {
74+
return {
75+
ngModule: DefaultStyleCompatibilityModeModule,
76+
providers: [],
77+
};
78+
}
79+
}

0 commit comments

Comments
 (0)