Apply CSS to React components conditionally.
className={decide(styles, {
header: true,
mobile: props.isMobile,
narrow: parseInt(props.width) < 400,
hidden: (props.hidden !== "false" && Boolean(props.hidden)),
fixed: parseInt(props.width) >= 400 || !props.isMobile
})}Sometimes you need to switch class names conditionally. Here are some examples:
- You have a shopping cart which can be either
cartorcart emptyaccording to its props; - You have a Header component. Its class name is always
header, but on a mobile device, it should be alsomobile. Oh, and if thewidthprop is less than '400px', it should also benarrow. Oh, and it should behiddenwhen eitherhiddenprop is truthy but not equals to"false"string, and it should also befixedwhen it is neithernarrownormobile.
npm i --save textpain/decider
import decide from 'decider'
decide(styles, {
className: condition
})The first argument is your styles imported as a CSS module. The second is decision matrix, where keys are class names, and values are conditions that will be interpreted as boolean values. If it's true, the corresponding class name will be applied.
import decide from 'decider'
import styles from './header.module.css'
export default props => (
<header className={ decide(styles, {
// 'header' class name is always applied
header: true,
// 'mobile' class name will apply if 'mobile' prop is truthy, so the result would be 'header mobile'
mobile: props.mobile,
// 'fixed' would be applied either if there's an explicitly set 'fixed' prop, or if the 'mobile' prop is falsy
fixed: props.fixed || !props.mobile,
// 'narrow' class name would be applied if 'width' prop is less than 400
narrow: props.width < 400
})} >
...
</header>
)Differences from classnames
decideis a pure function. It works with CSS modules without bind magic, so you can use it virtually anywhere.- Smaller bundle size: Decider is around 176 bytes gzipped!
- Syntax was much easier for me to adopt.