Skip to content

Instantly share code, notes, and snippets.

@jsonnull
Forked from jollytoad/jsx-h.js
Created December 24, 2015 04:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsonnull/a4dde3ee6b66f840fdce to your computer and use it in GitHub Desktop.
Save jsonnull/a4dde3ee6b66f840fdce to your computer and use it in GitHub Desktop.
Wrapper for virtual-dom/h to support compiled JSX (ES6)
import h from 'virtual-dom/h'
export default function(tag, props, ...children) {
return h(tag, transformProps(props), children)
}
// Add any attribute -> property mapping in here that you may want
const attrMap = {
"class": "className"
}
function transformProps(props) {
if (props) {
Object.getOwnPropertyNames(props).forEach(key => {
const sub = attrMap[key]
if (sub) {
// Apply any attribute to property mappings
props[sub] = props[key]
delete props[key]
} else if (key.startsWith("data-")) {
// Move any data- attributes into the dataset property
props.dataset = props.dataset || {}
props.dataset[key.substr(5)] = props[key]
delete props[key]
} else if (key.indexOf("-") !== -1) {
// Move any other attrs contains a '-' into attributes property
props.attributes = props.attributes || {}
props.attributes[key] = props[key]
delete props[key]
}
})
}
return props
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment