Use this if you want if you make it into a npm package please credit me :D
Last active
September 25, 2017 21:48
-
-
Save ccutch/9451064040ce63d58e493a7c764a69b1 to your computer and use it in GitHub Desktop.
Classable react classes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react' | |
const newGetter = getFunc => new Proxy({}, { get: getFunc }) | |
const classable = Tag => { | |
return newGetter((_, className) => { | |
let taggedFn = props => <Tag className={className} {...props} /> | |
taggedFn.displayName = `Classable(${Tag.displayName || Tag})` | |
return taggedFn | |
}) | |
} | |
module.exports = newGetter((_, Tag) => { | |
if (Tag === 'default' || Tag === 'classable') return classable | |
else return classable(Tag) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react' | |
import classable, { div, h2 } from './classable' | |
import { Link as _Link } from 'react-router-dom' | |
const Link = classable(_Link) | |
const Link2 = Link['link-two'] | |
export default props => ( | |
<div.container> | |
<h2.title>{props.title}</h2.title> | |
<Link.linkOne>page one</Link.linkOne> | |
<Link2>page two</Link2> | |
</div.container> | |
) | |
// rendered html => | |
// <div class="container"> | |
// <h2 class="title">Title</h2> | |
// <a class="linkOne"> | |
// page one | |
// </a> | |
// <a class="link-two"> | |
// page one | |
// </a> | |
// </div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment