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-css-components

v1.0.1

Published

Define styled React components using CSS based module format

Downloads

45

Readme

React CSS components

Join the chat at https://gitter.im/andreypopp/react-css-components Travis build status npm

Table of Contents

Motivation

Define React presentational components with CSS (or even SASS or Less if you like).

The implementation is based on CSS modules. In fact React CSS Components is just a thin API on top of CSS modules.

NOTE: The current implementation is based on Webpack but everything is ready to be ported onto other build systems (generic API is here just not yet documented). Raise an issue or better submit a PR if you have some ideas.

Installation & Usage

Install from npm:

% npm install react-css-components style-loader css-loader

Configure in webpack.config.js:

module.exports = {
  ...
  module: {
    loaders: [
      {
        test: /\.react.css$/,
        loader: 'react-css-components',
      }
    ]
  }
  ...
}

Now you can author React components in Styles.react.css:

Label {
  color: red;
}

Label:hover {
  color: white;
}

And consume them like regular React components:

import {Label} from './styles.react.css'

<Label /> // => <div class="<autogenerated classname>">...</div>

Base components

DOM components

By default React CSS Components produces styled <div /> components. You can change that by defining base: property:

FancyButton {
  base: button;
  color: red;
}

Now <FancyButton /> renders into <button />:

import {FancyButton} from './styles.react.css'

<FancyButton /> // => <button class="<autogenerated classname>">...</button>

Composite components

In fact any React components which accepts className props can be used as a base. That's means that React CSS Components can be used as theming tool for any UI library.

Example:

DangerButton {
  base: react-ui-library/components/Button;
  color: red;
}

Variants

Variants is a mechanism which allows to define styling variants for a component.

Named variants

You can define additional styling variants for your components:

Label {
  color: red;
}

Label:emphasis {
  font-weight: bold;
}

They are compiled as CSS classes which then can be controlled from JS via variant prop:

<Label variant={{emphasis: true}} /> // sets both classes with `color` and `font-weight`

JavaScript expressions

You can define variants which are conditionally applied if JS expression against props evaluates to a truthy value.

Example:

Label {
  color: red;
}

Label:prop(mode == "emphasis") {
  font-weight: bold;
}

Note that any free variable reference a member of props, thus in JS mode becomes props.mode in the example above.

They are compiled as CSS classes as well. JS expressions within prop(..) are used to determine if corresponding CSS classes should be applied to DOM:

<Label mode="emphasis" /> // sets both classes with `color` and `font-weight`

Customizing CSS loading

By default React CSS components loads CSS using style-loader!css-loader loader chain. That could be configured differently using loadCSS loader parameter.

This could be used to enable features such as CSS extraction, processing stylesheets with PostCSS/Autoprefixer or even authoring stylesheets with SASS or LESS.

CSS extraction

See the complete example which configures extract-text-webpack-plugin to extract stylesheets to a separate chunk.

Using with SASS/SCSS/LESS/Stylus/...

See the complete example which uses SASS/SCSS to create React components.

Using with PostCSS (including autoprefixer)

See the complete example which configures PostCSS with Autoprefixer to automatically add vendor prefixes to stylesheets.