Skip to content

Commit d6fa2bd

Browse files
rryterdgp1130
authored andcommitted
feat(@schematics/angular): add opt in option 'displayBlock'
1 parent 1df5a72 commit d6fa2bd

File tree

5 files changed

+55
-2
lines changed

5 files changed

+55
-2
lines changed

packages/angular/cli/lib/config/schema.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@
8686
"default": "Default",
8787
"alias": "c"
8888
},
89+
"displayBlock": {
90+
"description": "Specifies if the style will contain `:host { display: block; }`.",
91+
"type": "boolean",
92+
"default": false,
93+
"alias": "b"
94+
},
8995
"entryComponent": {
9096
"type": "boolean",
9197
"default": false,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<% if(displayBlock){ if(style != 'sass') { %>:host {
2+
display: block;
3+
}
4+
<% } else { %>\:host
5+
display: block;
6+
<% }} %>

packages/schematics/angular/component/files/__name@dasherize@if-flat__/__name@dasherize__.__type@dasherize__.ts.template

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,13 @@ import { Component, OnInit<% if(!!viewEncapsulation) { %>, ViewEncapsulation<% }
88
</p>
99
`,<% } else { %>
1010
templateUrl: './<%= dasherize(name) %>.<%= dasherize(type) %>.html',<% } if(inlineStyle) { %>
11-
styles: []<% } else { %>
11+
styles: [<% if(displayBlock){ %>
12+
`
13+
:host {
14+
display: block;
15+
}
16+
`<% } %>
17+
],<% } else { %>
1218
styleUrls: ['./<%= dasherize(name) %>.<%= dasherize(type) %>.<%= style %>']<% } %><% if(!!viewEncapsulation) { %>,
1319
encapsulation: ViewEncapsulation.<%= viewEncapsulation %><% } if (changeDetection !== 'Default') { %>,
1420
changeDetection: ChangeDetectionStrategy.<%= changeDetection %><% } %>

packages/schematics/angular/component/index_spec.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ describe('Component Schematic', () => {
2222
// path: 'src/app',
2323
inlineStyle: false,
2424
inlineTemplate: false,
25+
displayBlock: false,
2526
changeDetection: ChangeDetection.Default,
2627
style: Style.Css,
2728
type: 'Component',
@@ -248,6 +249,34 @@ describe('Component Schematic', () => {
248249
expect(tree.files).not.toContain('/projects/bar/src/app/foo/foo.component.css');
249250
});
250251

252+
it('should respect the displayBlock option when inlineStyle is `false`', async () => {
253+
const options = { ...defaultOptions, displayBlock: true };
254+
const tree = await schematicRunner.runSchematicAsync('component', options, appTree).toPromise();
255+
const content = tree.readContent('/projects/bar/src/app/foo/foo.component.css');
256+
expect(content).toMatch(/:host {\n display: block;\n}\n/);
257+
});
258+
259+
it('should respect the displayBlock option when inlineStyle is `false` and use correct syntax for `scss`', async () => {
260+
const options = { ...defaultOptions, displayBlock: true, style: 'scss' };
261+
const tree = await schematicRunner.runSchematicAsync('component', options, appTree).toPromise();
262+
const content = tree.readContent('/projects/bar/src/app/foo/foo.component.scss');
263+
expect(content).toMatch(/:host {\n display: block;\n}\n/);
264+
});
265+
266+
it('should respect the displayBlock option when inlineStyle is `false` and use correct syntax for `sass', async () => {
267+
const options = { ...defaultOptions, displayBlock: true, style: 'sass' };
268+
const tree = await schematicRunner.runSchematicAsync('component', options, appTree).toPromise();
269+
const content = tree.readContent('/projects/bar/src/app/foo/foo.component.sass');
270+
expect(content).toMatch(/\\:host\n display: block;\n/);
271+
});
272+
273+
it('should respect the displayBlock option when inlineStyle is `true`', async () => {
274+
const options = { ...defaultOptions, displayBlock: true, inlineStyle: true };
275+
const tree = await schematicRunner.runSchematicAsync('component', options, appTree).toPromise();
276+
const content = tree.readContent('/projects/bar/src/app/foo/foo.component.ts');
277+
expect(content).toMatch(/:host {\n(\s*)display: block;(\s*)}\n/);
278+
});
279+
251280
it('should respect the style option', async () => {
252281
const options = { ...defaultOptions, style: Style.Sass };
253282
const tree = await schematicRunner.runSchematicAsync('component', options, appTree).toPromise();
@@ -263,7 +292,7 @@ describe('Component Schematic', () => {
263292
const content = tree.readContent('/projects/bar/src/app/foo/foo.route.ts');
264293
const testContent = tree.readContent('/projects/bar/src/app/foo/foo.route.spec.ts');
265294
expect(content).toContain('export class FooRoute implements OnInit');
266-
expect(testContent).toContain('describe(\'FooRoute\'');
295+
expect(testContent).toContain("describe('FooRoute'");
267296
expect(tree.files).toContain('/projects/bar/src/app/foo/foo.route.css');
268297
expect(tree.files).toContain('/projects/bar/src/app/foo/foo.route.html');
269298
});

packages/schematics/angular/component/schema.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@
2727
},
2828
"x-prompt": "What name would you like to use for the component?"
2929
},
30+
"displayBlock": {
31+
"description": "Specifies if the style will contain `:host { display: block; }`.",
32+
"type": "boolean",
33+
"default": false,
34+
"alias": "b"
35+
},
3036
"inlineStyle": {
3137
"description": "When true, includes styles inline in the component.ts file. Only CSS styles can be included inline. By default, an external styles file is created and referenced in the component.ts file.",
3238
"type": "boolean",

0 commit comments

Comments
 (0)