Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow to pass options to insert function through style.use() #535

Merged
merged 3 commits into from Sep 14, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 40 additions & 0 deletions README.md
Expand Up @@ -540,6 +540,46 @@ module.exports = {

Insert styles at top of `head` tag.

You can pass any parameters to `style.use(anythingHere)` and this value will be passed to `insert` function. These options will be passed to `styleTagTransform` function too.

**webpack.config.js**

```js
module.exports = {
module: {
rules: [
{
test: /\.css$/i,
use: [
{
loader: "style-loader",
options: {
insert: function insertIntoTarget(element, options) {
var parent = options.target || document.querySelector("head");
parent.appendChild(element);
},
},
},
"css-loader",
],
},
],
},
};
```

Insert styles to the provided element or to the `head` tag if target isn't provided. Now you can inject styles into Shadow DOM (or any other element).

**component.js**

```js
import style from "./file.css";

style.use({
target: document.querySelector('#myShadowDom').shadowRoot,
})
```

### `styleTagTransform`

Type: `String | Function`
Expand Down
3 changes: 2 additions & 1 deletion src/index.js
Expand Up @@ -125,7 +125,8 @@ ${getInsertOptionCode(insertType, options)}
options.domAPI = ${getdomAPI(isAuto)};
options.insertStyleElement = insertStyleElement;

exported.use = function() {
exported.use = function(insertOptions) {
options.insertOptions = insertOptions;
if (!(refs++)) {
update = API(content, options);
}
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/insertStyleElement.js
Expand Up @@ -4,7 +4,7 @@ function insertStyleElement(options) {

options.setAttributes(style, options.attributes);

options.insert(style);
options.insert(style, options.insertOptions);

return style;
}
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/styleDomAPI.js
Expand Up @@ -18,7 +18,7 @@ function apply(style, options, obj) {

// For old IE
/* istanbul ignore if */
options.styleTagTransform(css, style);
options.styleTagTransform(css, style, options.insertOptions);
}

function removeStyleElement(style) {
Expand Down