Skip to content

Commit 4003514

Browse files
committed
add react-html-attributes for improved attribute extraction
1 parent aa04ca3 commit 4003514

File tree

5 files changed

+38
-7
lines changed

5 files changed

+38
-7
lines changed

__tests__/__fixtures__/02-jsx/code.js

+2
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ const GDot1 = props => <g.Span {...props}>Hi, I'm a Span!</g.Span>;
44
const GDot2 = props => <g.Div css={{marginTop: 5}}/>;
55
const GDot3 = props => <g.Div marginTop={5}/>;
66
const GDot4 = props => <g.Div marginTop={5} css={{marginTop: 10}} marginBottom="5" onClick={handler}/>;
7+
const GDot5 = props => <g.Img width={100}/>;
8+
const GDot6 = props => <g.Span width={100}/>;

__tests__/__fixtures__/02-jsx/output.js

+6
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,9 @@ const GDot4 = props => <div css={{
1313
marginTop: 5,
1414
marginBottom: "5"
1515
}} onClick={handler} />;
16+
17+
const GDot5 = props => <img width={100} />;
18+
19+
const GDot6 = props => <span css={{
20+
width: 100
21+
}} />;

index.js

+14-7
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
* https://astexplorer.net/#/gist/7bc4771564a12c9f93c4904b3934aa1c/latests
1919
*/
2020

21+
const htmlElementAttributes = require("react-html-attributes");
22+
2123
module.exports = function(babel) {
2224
const {types: t} = babel;
2325

@@ -39,7 +41,7 @@ module.exports = function(babel) {
3941
});
4042
};
4143

42-
const transformJSXAttributes = jsxAttrs => {
44+
const transformJSXAttributes = (tagName, jsxAttrs) => {
4345
if (!jsxAttrs) return [];
4446
const appendToCss = [];
4547
let cssAttr = null;
@@ -49,11 +51,15 @@ module.exports = function(babel) {
4951
if (jsxKey.name === "css") {
5052
cssAttr = attr;
5153
} else {
52-
// TODO: be smarter about finding out which properties
53-
// are CSS properties and which ones are not
54+
// ignore event handlers
55+
if (jsxKey.name.match(/on[A-Z]/)) return true;
56+
57+
// ignore generic attributes like 'id'
58+
if (htmlElementAttributes["*"].includes(jsxKey.name)) return true;
5459

55-
// event handlers are no CSS Props!
56-
if (jsxKey.name.indexOf("on") === 0) return true;
60+
// ignore tag specific attrs like 'disabled'
61+
const tagSpecificAttrs = htmlElementAttributes[tagName];
62+
if (tagSpecificAttrs && tagSpecificAttrs.includes(jsxKey.name)) return true;
5763

5864
appendToCss.push({
5965
name: t.identifier(jsxKey.name),
@@ -119,9 +125,10 @@ module.exports = function(babel) {
119125
// replace <glamorous.Div/> with `<div/>`
120126
case "JSXMemberExpression": {
121127
const grandParent = path.parentPath.parent;
122-
grandParent.name = t.identifier(grandParent.name.property.name.toLowerCase());
128+
const tagName = grandParent.name.property.name.toLowerCase();
129+
grandParent.name = t.identifier(tagName);
123130
if (t.isJSXOpeningElement(grandParent)) {
124-
grandParent.attributes = transformJSXAttributes(grandParent.attributes);
131+
grandParent.attributes = transformJSXAttributes(tagName, grandParent.attributes);
125132
}
126133
break;
127134
}

package-lock.json

+13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+3
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,8 @@
3030
"babel-preset-react": "^7.0.0-beta.3",
3131
"eslint": "4.x",
3232
"jest": "^23.4.2"
33+
},
34+
"dependencies": {
35+
"react-html-attributes": "^1.4.3"
3336
}
3437
}

0 commit comments

Comments
 (0)