An open API service indexing awesome lists of open source software.

https://github.com/sms-system/postcss-scopes

PostCSS plugin that automagic adds scopes to CSS
https://github.com/sms-system/postcss-scopes

css css-modules html postcss postcss-scopes

Last synced: 4 months ago
JSON representation

PostCSS plugin that automagic adds scopes to CSS

Awesome Lists containing this project

README

          

# postcss-scopes

[PostCSS](https://github.com/postcss/postcss) plugin that automagic adds scopes to CSS

## Features

- Nested scopes
- Support for pseudo-elements on scoped elements
- Global classes inside the scopes, using `global` attribute, or automatically by matching pattern in `globalPatterns` option
- Automatic html recomposition, which doesn't break initial code
- Easy integration into an existing project

## What is it ?

Imagine that you have HTML like this:

```html

Main title


Block Title


```

and CSS:

```css
.title {
background: #da9a9a;
}
.block .title {
color: #da9a9a;
}
```

If we try to display this markup, we get a problem:

![Without scopes](https://raw.githubusercontent.com/sms-system/postcss-scopes/master/img/without_scopes.png)

`.block .title` inherited styles from root `.title`, but we don't want this.

**And what to do ?**

Just add attribute `scoped` to element with class `.block`,

```html

Main title


Block Title


```

and all classes inside it automagically become isolated.

![Without scopes](https://raw.githubusercontent.com/sms-system/postcss-scopes/master/img/with_scopes.png)

After the transformation HTML and CSS become like this:

```html

Main title


Block Title


```

```css
.title {
background: #da9a9a;
}
.block .title_scope1 {
color: #da9a9a;
}
```

## Options

| Option | Description |
| :-------------------------------------------------------------- | :----------------------------------------------------------------------- |
| **optimiseCss**
Type: `Boolean`, default: `true` |
Remove unused classes after processing |
| **globalPatterns**
Type: `Array` |
Array of RegExp patterns matching global classes |
| **html**
Type: `String` |
Initial html |
| **getHTML**
Type: `Function`, arguments `html` |
The function takes an argument compiled html for further processing |

## Example

```js
postcss([
scopes({
globalPatterns: ['^js-'],
html: `

Main title


Block Title

`,
getHTML: function (html) {
console.log(html)
}
})
]).process(`
.title {
background: #da9a9a;
}
.js-title-class {
text-decoration: underline;
}
.block .title {
color: #da9a9a;
}`
)
```

## Support

Please [open an issue](https://github.com/sms-system/postcss-scopes/issues/new) for support.