Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/babel-plugin-jsx-dom-expressions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,13 @@ Example usage:
const template = <div>Hello</div>;
```

### inlineStyles

- Type: `boolean`
- Default: `true`

Style attributes in templates are inlined when possible: value is a string, `Record<string, string>`, etc. Inlining styles may not be desired when using strong CSP configurations. Disabling `inlineStyles` will cause all of the style definitions to be set at runtime.

## Special Binding

### ref
Expand Down
3 changes: 2 additions & 1 deletion packages/babel-plugin-jsx-dom-expressions/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ export default {
staticMarker: "@once",
effectWrapper: "effect",
memoWrapper: "memo",
validate: true
validate: true,
inlineStyles: true
};
21 changes: 21 additions & 0 deletions packages/babel-plugin-jsx-dom-expressions/src/dom/element.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,27 @@ export function transformElement(path, info) {
skipTemplate: false
};

if (!config.inlineStyles) {
path
.get("openingElement")
.get("attributes")
.forEach(a => {
if (a.node.name?.name === "style") {
let value = a.node.value.expression ? a.node.value.expression : a.node.value;
if (t.isStringLiteral(value)) {
// jsx attribute value is a sting that may takes more than one line
value = t.templateLiteral(
[t.templateElement({ raw: value.value, cooked: value.value })],
[]
);
}
a.get("value").replaceWith(
t.jSXExpressionContainer(t.callExpression(t.arrowFunctionExpression([], value), []))
);
}
});
}

path
.get("openingElement")
.get("attributes")
Expand Down
Loading