dallas
La classe, dallas.
Install
npm i dallas react
API
The api provide a classe
wrapper function.
Basic usage
import { React, classe } from 'classe'
const Button = classe('my-button', props => <button {...props} />)
<Button className='big' />
<button className='big my-button' />
React
is passed down for convenience
You can pass in an array of classNames
import { React, classe } from 'dallas'
import style from './Wrapper.module.css'
const Wrapper = classe([ 'container', style.Wrapper ])
<Wrapper />
<div class="container Wrapper__Wrapper_aQn7" />
The second argument (the render function) is optionnal
It will default to rendering the className to a div
.
props =>
Merged class names !
)
boolean class flags
all keys that have strings
values genereate boolean flags as short hands for applying css classes.
export const FlagClass = classe({
disabled: 'dis',
selected: 'bp-selected',
})
<FlagClass disabled selected />
<div class="dis bp-selected" />
<FlagClass selected className="pouet"/>
<div class="pouet bp-selected" />
You can not use className
or classNames
as a flag.
class matcher
A class matcher is a switch, it use a string or a function for more complex logic.
export const Matcher = classe({
color: {
info: 'color-blue',
warning: 'color-yellow',
error: 'color-red',
},
status: data => data.endDate > Date.now() ? 'available' : 'unavailable'
})
<Matcher color="error" />
<div class="color-red" />
<Matcher status={{ endDate: Date.now() + 1000 }} />
<div class="available" />
<Matcher status={{ endDate: 0 }} />
<div class="available" />
The render functions are memo
by default,
if you want not to memo
the function, use
either classe.noMemo
or classeNoMemo
.
option consume (Boolean)
Passing the consume option to true
will remove props that are used for flags.
option groups (RegExp)
A RegExp
used to match exclusive props together
export const Matcher = classe({
orange1: 'light-orange',
orange2: 'orange',
orange3: 'dark-orange',
groups: /^([a-z]+)([0-9]+)$/i,
})
<Matcher orange="1" />
<div class="light-orange" />
<Matcher orange3 />
<div class="dark-orange" />
<Matcher orange={2} />
<div class="orange" />
Groups are mutualy exclusives so once flags are matched together, only the last
one will be applied:
<Matcher orange1 orange2 orange3 />
<div class="dark-orange" />
Refs
A special elemRef
props is checked to forward ref to the rendered element.
const ref = useRef(null)
<Matcher elemRef={ref} />