npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-cx

v0.1.12

Published

Style React components with style

Downloads

259

Readme

react-cx

Version

Add styles from CSS Modules with a cx prop:

<div
  cx={['Arrow', { active }, color, 'length-'+length]}
  className={className}
 />

Inspired by jareware/css-ns. Uses jedwatson/classnames under the hood.

Install with npm:

# Adds react-cx to node_modules and package.json
npm install react-cx --save

Why?

When styling components with CSS modules, you'll often need join multiple class names together before passing them to the React className prop.

// CSS Modules provide an object with your stylesheet's class names
import styles from './Theme.less'

export function({ active, className, color, length=1 }) {
  const className = `
    ${styles.Arrow}
    ${active ? styles.active : ''}
    ${styles[color] || ''}
    ${styles['length-+'length] || ''}
    ${className}
  `
  <div className={className} />
}

The classnames package can help, but the resulting code still feels a little verbose after you've typed it for the 50th time.

import styles from './Theme.less'

// classnames provides a helper to build `className` strings
import bindClassNames from 'classnames/bind'
const cx = bindClassNames(styles)

export function Arrow({ active, className='', color, length=1 }) {
  return <div className={cx('Arrow', { active }, color, 'length-'+length)+' '+className} />
}

With react-cx, you can add styles from your CSS modules directly to your elements by using the cx prop. It uses the same syntax as the classnames package, and still lets you append a raw className prop.

import styles from './Theme.less'

// react-cx adds a `cx` prop to React by wrapping `React.createElement`
import getReactWithCX from './react-cx'
const React = getReactWithCX(styles)

export function Arrow({ active, className, color, length=1 }) {
  return (
    <div
      cx={['Arrow', { active }, color, 'length-'+length]}
      className={className}
     />
  )
}

Of course, the cx prop can also accept strings or plain objects:

export function Switch({ active, className, color='lifecycle', direction='down' }) {
  return (
    <div cx={['Switch', { active }, color, direction]} className={className}>
      <div cx="pivot" />
      <Arrow active={active} color={color} />
    </div>
  )
}

These components are taken from my React lifecycle simulators.

How does it work?

In React, JSX calls are translated to calls to React.createElement(). Thus, by wrapping React.createElement() with our own function, we can add support for new props without ever touching React's internals.

If you understand how React.createElement() works, the easiest way to grok this is to just view the source below. If you're not yet familiar with React.createElement(), head over to React Armory for a simple explanation with live examples and exercises.

import UnprefixedReact from 'react'
import classnames from 'classnames/bind'

export default function cx(styles, prop='cx') {
  const bound = classnames.bind(styles)

  function getProps(props) {
    if (!props) return props

    if (props.cx) {
      const result = Object.assign({}, props)
      delete result.cx
      const args = Array.isArray(props.cx) ? props.cx : [props.cx]
      result.className = bound(...args)
      if (props.className) result.className += ' '+props.className
      return result
    }
    return props
  }
  function createElement(type, props, ...children) {
    return UnprefixedReact.createElement(type, getProps(props), ...children)
  }
  function cloneElement(element, props, ...children) {
    return UnprefixedReact.cloneElement(element, getProps(props), ...children)
  }
  return Object.assign({}, UnprefixedReact, {
    createElement,
    cloneElement
  })
}

More examples

These components are all taken from my React lifecycle simulators.

export function Indicator({ active, className, color, icon, label, noMobile, scheduled, style }) {
  return (
    <div cx={['Indicator', (scheduled || active) && color, { active, 'no-mobile': noMobile }]} className={className} style={style}>
      { icon &&
        <div cx="icon">{icon}</div>
      }
      <div cx="label">{label}</div>
    </div>
  )
}

export function Switch({ active, className, color='lifecycle', direction='down', style }) {
  return (
    <div cx={['Switch', { active }, color, direction]} className={className} style={style}>
      <div cx="pivot" />
      <Arrow active={active} color={color} headless={direction === 'down'} />
    </div>
  )
}

function SwitchedSetter({ className, color, icon, label, noMobile, setActive, style, switchActive, willSet }) {
  return (
    <div cx={['SwitchedSetter', { 'no-mobile': noMobile }]} className={className} style={style}>
      <Switch active={switchActive} direction={willSet ? 'right' : 'down' } />
      <Indicator active={setActive} color={color} icon={icon} label={label} scheduled={willSet} />
      <Arrow cx='horizontal-wire' active={setActive} color={color} headless length={4} />
      <Arrow cx='vertical-wire' active={setActive} color={color} headless />
      <Arrow cx='output' active={setActive || (switchActive && !willSet)} color={switchActive ? 'lifecycle' : color} />
    </div>
  )
}

License

MIT Copyright © 2017 James K Nelson