18
18
* https://astexplorer.net/#/gist/7bc4771564a12c9f93c4904b3934aa1c/latests
19
19
*/
20
20
21
+ const htmlElementAttributes = require ( "react-html-attributes" ) ;
22
+
21
23
module . exports = function ( babel ) {
22
24
const { types : t } = babel ;
23
25
@@ -39,7 +41,7 @@ module.exports = function(babel) {
39
41
} ) ;
40
42
} ;
41
43
42
- const transformJSXAttributes = jsxAttrs => {
44
+ const transformJSXAttributes = ( tagName , jsxAttrs ) => {
43
45
if ( ! jsxAttrs ) return [ ] ;
44
46
const appendToCss = [ ] ;
45
47
let cssAttr = null ;
@@ -49,11 +51,15 @@ module.exports = function(babel) {
49
51
if ( jsxKey . name === "css" ) {
50
52
cssAttr = attr ;
51
53
} 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 ( / o n [ A - Z ] / ) ) return true ;
56
+
57
+ // ignore generic attributes like 'id'
58
+ if ( htmlElementAttributes [ "*" ] . includes ( jsxKey . name ) ) return true ;
54
59
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 ;
57
63
58
64
appendToCss . push ( {
59
65
name : t . identifier ( jsxKey . name ) ,
@@ -111,9 +117,10 @@ module.exports = function(babel) {
111
117
}
112
118
case "JSXMemberExpression" : {
113
119
const grandParent = path . parentPath . parent ;
114
- grandParent . name = t . identifier ( grandParent . name . property . name . toLowerCase ( ) ) ;
120
+ const tagName = grandParent . name . property . name . toLowerCase ( ) ;
121
+ grandParent . name = t . identifier ( tagName ) ;
115
122
if ( t . isJSXOpeningElement ( grandParent ) ) {
116
- grandParent . attributes = transformJSXAttributes ( grandParent . attributes ) ;
123
+ grandParent . attributes = transformJSXAttributes ( tagName , grandParent . attributes ) ;
117
124
}
118
125
break ;
119
126
}
0 commit comments