Skip to content

Instantly share code, notes, and snippets.

@ccutch
Last active September 25, 2017 21:48
Show Gist options
  • Save ccutch/9451064040ce63d58e493a7c764a69b1 to your computer and use it in GitHub Desktop.
Save ccutch/9451064040ce63d58e493a7c764a69b1 to your computer and use it in GitHub Desktop.
Classable react classes

Was kinda bored and was wondering what HAML link react components would look like

Use this if you want if you make it into a npm package please credit me :D

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)
})
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