Skip to content

Instantly share code, notes, and snippets.

@lnked
Created August 17, 2018 10:19
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 lnked/9135cb6a0a3549b18a4ae4ef568936e4 to your computer and use it in GitHub Desktop.
Save lnked/9135cb6a0a3549b18a4ae4ef568936e4 to your computer and use it in GitHub Desktop.
export interface S {
bind: (css: any, args: Array<any>) => string;
styler: (css: any) => void;
}
export const classes: S = {
styler: (css, ...args: Array<any>): string => {
const r: Array<string> = []
if (args.length) {
Object.keys(args).map(id => {
const item = args[id] || ''
const type = typeof item
if (['string', 'number'].indexOf(type) >= 0) {
r.push(item)
} else {
Object.keys(item).map(name => {
if (typeof item[name] !== 'undefined' && item[name]) {
r.push(css[name])
}
})
}
})
}
return r.filter(i => i !== '').join(' ')
},
bind: (css) => {
return classes.styler.bind(classes, css)
},
}
import * as React from 'react'
import * as css from './styles.scss'
import { classes } from 'helpers'
export interface P {
className?: string;
}
const cx = classes.bind(css)
export const Example = ({ className = '' }: P) => {
const cond1 = true
const cond2 = false
return (
<div className={cx({ content: cond1 })}>
<div cx({ class: cond2 }, className) />
</div>
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment