Scope your component styles and never worry about a collision ever again.
npm install ember-scoped-cssScope your component styles and never worry about a collision ever again.
``gjs
const greeting = "hello world";
{{greeting}}
`
becomes the equivelent of;
`gjs
import './abcd1234.css'; // containing your CSS, but with selectors scoped
const greeting = "hello world";
{{greeting}}
`
This is a build-time-only addon, so there is no need to worry about runtime performance.
You can also write your styles as a co-located .css file, right next to your .gjs/.gts files.
Every selector you write in your styles is automatically scoped to the component.
So you can develop your component with styles isolated from the rest of the application and you don't have to worry about CSS selectors collisions or issues with the CSS cascade.
_See Usage for details_.
If you want to read more specifics on how this addon achieves isolation with CSS you can read more in the detailed CSS isolation documentation
As selectors are scoped/renamed during the build process. So there is no performance hit when running the app.
The philosophy of ember-scoped-css is to stick as close to CSS and HTML as possible and not introduce new syntax or concepts unless it is absolutely necessary.
You may also find the docs on CSS @layer interesting.
This build tool can emit CSS in a @layer.
- Vite
- V2 addons with @embroider/addon-dev @ v8+ (or similar)
- For hbs, broccoli, or webpack-based builds, view this version of the docs
- For bugfixes for the pre-ember-scoped-css-1.0 code, PR here
| You Have | ember-scoped-css | ember-scoped-css-compat | docs |
| -------- | ----------- | ---------------------- | --- |
| vite | >= 1.0.0 | 🚫 | [main][docs-main]
| gjs / gts library (no hbs) | >= 1.0.0 | 🚫 | [main][docs-main]
| webpack | <= 0.24.3 | <= 10.0.0 | [0.24.3][docs-2]
| hbs | <= 0.24.3 | <= 10.0.0 | [0.24.3][docs-2]
| ember-template-imports@v4 or babel-plugin-ember-template-compilation@2.2.5+ | 0.19.0 | 10.0.0 | [0.19][docs-3] - [0.24][docs-2]
| ember-template-imports@v3 or babel-plugin-ember-template-compilation@2.2.1 or rollup-plugin-glimmer-template-tag | <= 0.18.0 | <= 9.0.0 | [0.18][docs-4]
| classic components | <= 0.18.0 | <= 8.0.0 | [0.18][docs-4]
| ember < 4 | <= 0.18.0 | <= 8.0.0 | [0.18][docs-4]
[docs-main]: https://github.com/auditboard/ember-scoped-css/
[docs-2]: https://github.com/auditboard/ember-scoped-css/tree/v0.24.3-ember-scoped-css
[docs-3]: https://github.com/auditboard/ember-scoped-css/tree/v0.19.1-ember-scoped-css
[docs-4]: https://github.com/auditboard/ember-scoped-css/tree/ember-scoped-css%400.18.0
`bash`
npm install --save-dev ember-scoped-css
Configuration happens in multiple locations to take over the need portion of your build steps.
- vite.config.* rollup.config.*
- babel.config.*
-
#### Vite
In your vite.config.*, import and add the scopedCSS plugin:
`js
import { defineConfig } from 'vite';
import { scopedCSS } from 'ember-scoped-css/vite';
export default defineConfig({
// ...
plugins: [
scopedCSS(),
// ...
],
});
`
notes for vite projects
If you're import.meta.globing large chunks of your codebase, you'll want to make sure that your globs exclude CSS files
For example:
❌ Don't do this
`js`
...import.meta.glob('./templates/*/', { eager: true })
✅ Do this
`js`
...import.meta.glob('./templates/*/.{gjs,gts}', { eager: true }),
or better yet, for small projects:
`js`
...import.meta.glob('./templates/{top,level,folders}/{sub,folders}.{gjs,gts}', { eager: true }),
This way you don't import CSS yourself, which would make CSS go through Vite's default CSS processing.
We need the scoped-css plugins to process the CSS instead of Vite's default behaviors.
##### Configuration Options
- layerName: string - Wrap your CSS in a @layer with this given name
#### Rollup
If you have a rollup config:
`js
import { scopedCSS } from 'ember-scoped-css/rollup';
// ...
plugins: [
scopedCSS(),
]
`
##### Configuration Options
- layerName: string - Wrap your CSS in a @layer with this given name
#### Babel
In your babel.config.*, add the plugins:
`js
import { scopedCSS } from 'ember-scoped-css/babel';
export default {
plugins: [
// ...
scopedCSS(),
[
'babel-plugin-ember-template-compilation',
{
// ...
transforms: [scopedCSS.template({})],
},
],
// ...
],
// ...
};
`
There are two plugins, but you made only need one:
- scopedCSS() - handles removing the import for scopedCSS (which you'd use in GJS and GTS for intellisense, and TypeScript)scopedCSS.template()
- - transforms your template
##### Configuration Options (scopedCSS.template())
- layerName: string - Wrap your CSS in a @layer with this given nameadditionalRoots: string[]
- - When you want to procss more folders. For example you want to support pods structure
If you use TypeScript, or rely on information from TypeScript, you'll want to add the special attributes to the
`
Note that
`
separate CSS file
`hbs`Hello, world!
`css
/ src/components/my-component.css /
.hello-class {
color: red;
}
/ the :global() pseudo-class is used to define a global class. It mean that header class wont be scoped to that component /
:global(.header) {
font-size: 20px;
}
b {
color: blue;
}
`
NOTE: that if you're using pods, css co-located with templates/routes/etc will need to be named styles.css
Difference with @scope
The @scope at-rule will scope all CSS defined within a
this is red
not red
In this example, it is effectively the same as:
`html
this is red
not red
`But the nice thing is that you don't need to use classes with
@scope.A _potential_ downside to
@scope is that it operates deeply -- where as
this is red
Where as with
this is red
$3
There is a
scopedClass helper that you can use to pass a class name as an argument to a component. The helper takes a class name and returns a scoped class name. scopedClass helper is replaced at build time so there is no performance hit when running the app.`gjs
import { scopedClass } from 'ember-scoped-css';
@internalClass={{concat (scopedClass 'hello-class') ' other-class'}}
/>
`Testing
As classes are renamed during the build process you can't directly verify if classes are present in your tests. To solve this problem you can use the
scopedClass function from the ember-scoped-css/test-support module. The function takes the class names and path to the CSS file where are the classes defined and returns the scoped class names.The path to the CSS file is always relative to the V2 addon root no matter where the test is located.
`gjs
import { scopedClass } from 'ember-scoped-css/test-support';test('MyComponent has hello-class', async function (assert) {
assert.expect(1);
await render(
);
const rewrittenClass = scopedClass(
'hello-class',
'/components/my-component'
);
assert.dom('[data-test-my-component]').hasClass(rewrittenClass);
});
`Linting
ember-scoped-css exports a ember-template-lint plugin with one rule scoped-class-helper. This lint rule is intended to help you prevent improper use of the scopedClass helper which might not be immediately obvious during regular development. You can read more information in the lint rules documentation$3
1. Add
ember-scoped-css plugin to .template-lintrc.js`diff
'use strict';module.exports = {
plugins: [
+ 'ember-scoped-css/src/template-lint/plugin'
],
`2. Add
scoped-class-helper rule to .template-lintrc.js`diff
'use strict';module.exports = {
plugins: [
'ember-scoped-css/src/template-lint/plugin'
],
rules: {
+ 'scoped-class-helper': 'error',
+ 'no-forbidden-elements': ['meta', 'html', 'script'], // style removed
}
``This project is licensed under the MIT License.