Skip to content

Commit 114fe3b

Browse files
kleiramwebark
authored andcommitted
feat(route-namesapce): Enable name-spacing of route styles
Added in a `styleNamespace` on a controller and a `routeStyleNamespaceClassSet` on the application controller to enable styling for routes.
1 parent 3b9f292 commit 114fe3b

20 files changed

Lines changed: 183 additions & 10 deletions

File tree

README.md

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,25 @@ Note: If you are using more than one type of component style files (ie a .less f
8484

8585
To use this with pods, you just need to include a style file in your component pods directory alongside your `template.hbs` or `component.js` files.
8686

87+
### Usage with routes
88+
89+
To use this with routes you need to use pods for the routes and modify the `application.hbs` template a little bit.
90+
Let's assume your `application.hbs` template looks like this:
91+
92+
```hbs
93+
{{outlet}}
94+
```
95+
96+
To be able to use this for routes, you need to add a wrapping `div` around the outlet:
97+
98+
```hbs
99+
<div class={{routeStyleNamespaceClassSet}}>
100+
{{outlet}}
101+
</div>
102+
```
103+
104+
After that it's quite easy: add a style file in your route directory alongside your `route.js` or `template.hbs` files.
105+
87106
### Usage with classic (non pod) structure
88107

89108
You can use classic Ember app structure by placing component styles in
@@ -96,13 +115,13 @@ the same component both are included but the pod style will take precedence.
96115
### Use in addons
97116
In order to use this inside of an addon, you need to add your style files inside of the components in the
98117
addon directory. You will then be able to import the 'pod-styles' file inside of your addon style file which
99-
is in the `/addon/styles` directory. These styles will then be added to the `vendor.css` file like normal.
118+
is in the `/addon/styles` directory. These styles will then be added to the `vendor.css` file like normal.
100119

101120
If you are using classic (non pod) structure, your addon directory structure might look like:
102121
```
103122
yourAddonDirectory
104123
│ index.js
105-
│ ... etc
124+
│ ... etc
106125
└───addon
107126
│ └───components
108127
│ │ yourAddonComponent.js

addon/initializers/route-styles.js

Whitespace-only changes.

app/initializers/route-styles.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import Router from '@ember/routing/router';
2+
import { getOwner } from '@ember/application';
3+
import podNames from 'ember-component-css/pod-names';
4+
5+
Router.reopen({
6+
didTransition(routes) {
7+
this._super(...arguments);
8+
9+
const classes = [];
10+
for (let route of routes) {
11+
let currentPath = route.name.replace(/\./g, '/');
12+
13+
if (podNames[currentPath]) {
14+
getOwner(this).lookup(`controller:${route.name}`).set('routeStyleNamespaceClass', podNames[currentPath]);
15+
classes.push(podNames[currentPath]);
16+
}
17+
}
18+
19+
getOwner(this).lookup('controller:application').set('routeStyleNamespaceClassSet', classes.join(' '));
20+
}
21+
});
22+
23+
export function initialize() {}
24+
25+
export default {
26+
name: 'route-styles',
27+
initialize
28+
};

tests/acceptance/css-test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,19 @@ test('BEM variant rule followed', function(assert) {
4444
assert.equal(find('[class$=__element--variant]').css('color'), 'rgb(0, 0, 5)');
4545
});
4646
});
47+
48+
test('route style followed', function(assert) {
49+
visit(`/${TYPE}`);
50+
51+
andThen(function() {
52+
assert.equal(find(`div[class^="__${TYPE}"]`).css('color'), 'rgb(0, 1, 0)');
53+
});
54+
});
55+
56+
test('nested route style followed', function(assert) {
57+
visit(`/${TYPE}/nested`);
58+
59+
andThen(function() {
60+
assert.equal(find(`div[class*="__${TYPE}__nested"]`).css('color'), 'rgb(0, 2, 0)');
61+
});
62+
});

tests/acceptance/less-test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,23 @@ test('BEM variant rule followed', function(assert) {
4444
assert.equal(find('[class$=__element--variant]').css('color'), 'rgb(0, 0, 5)');
4545
});
4646
});
47+
48+
test('route style followed', function(assert) {
49+
visit(`/${TYPE}`);
50+
51+
andThen(function() {
52+
assert.equal(find(`div[class^="__${TYPE}"]`).css('color'), 'rgb(0, 1, 0)');
53+
});
54+
55+
andThen(function() {
56+
visit(`/${TYPE}/nested`);
57+
});
58+
});
59+
60+
test('nested route style followed', function(assert) {
61+
visit(`/${TYPE}/nested`);
62+
63+
andThen(function() {
64+
assert.equal(find(`div[class*="__${TYPE}__nested"]`).css('color'), 'rgb(0, 2, 0)');
65+
});
66+
});

tests/acceptance/sass-test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,19 @@ test('mixin psudo elements do not get scoped', function(assert) {
5454
assert.equal(item.css('color'), 'rgb(0, 0, 6)');
5555
});
5656
});
57+
58+
test('route style followed', function(assert) {
59+
visit(`/${TYPE}`);
60+
61+
andThen(function() {
62+
assert.equal(find(`div[class^="__${TYPE}"]`).css('color'), 'rgb(0, 1, 0)');
63+
});
64+
});
65+
66+
test('nested route style followed', function(assert) {
67+
visit(`/${TYPE}/nested`);
68+
69+
andThen(function() {
70+
assert.equal(find(`div[class*="__${TYPE}__nested"]`).css('color'), 'rgb(0, 2, 0)');
71+
});
72+
});

tests/acceptance/scss-test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,19 @@ test('children of root @for rules are namspaced', function(assert) {
6565
}
6666
});
6767
});
68+
69+
test('route style followed', function(assert) {
70+
visit(`/${TYPE}`);
71+
72+
andThen(function() {
73+
assert.equal(find(`div[class^="__${TYPE}"]`).css('color'), 'rgb(0, 1, 0)');
74+
});
75+
});
76+
77+
test('nested route style followed', function(assert) {
78+
visit(`/${TYPE}/nested`);
79+
80+
andThen(function() {
81+
assert.equal(find(`div[class*="__${TYPE}__nested"]`).css('color'), 'rgb(0, 2, 0)');
82+
});
83+
});

tests/acceptance/styl-test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,19 @@ test('BEM variant rule followed', function(assert) {
4444
assert.equal(find('[class$=__element--variant]').css('color'), 'rgb(0, 0, 5)');
4545
});
4646
});
47+
48+
test('route style followed', function(assert) {
49+
visit(`/${TYPE}`);
50+
51+
andThen(function() {
52+
assert.equal(find(`div[class^="__${TYPE}"]`).css('color'), 'rgb(0, 1, 0)');
53+
});
54+
});
55+
56+
test('nested route style followed', function(assert) {
57+
visit(`/${TYPE}/nested`);
58+
59+
andThen(function() {
60+
assert.equal(find(`div[class*="__${TYPE}__nested"]`).css('color'), 'rgb(0, 2, 0)');
61+
});
62+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
& {
2+
color: rgb(0, 2, 0);
3+
}

tests/dummy/app/css/styles.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
& {
2+
color: rgb(0, 1, 0);
3+
}

0 commit comments

Comments
 (0)