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-cssobj

v1.2.6

Published

React work with cssobj, make stylesheet localized, runtime updating, and more

Downloads

4

Readme

react-cssobj

React work with cssobj, make stylesheet localized, runtime updating, and more.

npm Build Status

Install

npm install --save react-cssobj

Usage

import React from 'react'
import ReactCSS from 'react-cssobj'

const {css, mapClass} = ReactCSS({
  '.app': {
    background: 'red'
  },
  '.appTitle': {
    '&.large':{
      fontSize:'3rem'
    },
    color: 'blue'
  },
})

export default class App extends React.Component {
  render(){
    return mapClass ( // <<<-- Rocks!
      <header className = 'app'>
        <h2 className = {{'appTitle': true, 'large': this.state}}
            onClick = {() => {
              css.set(['.app'], {background: 'yellow'})
              this.setState({})
            }}>
          Title
        </h2>
      </header>
    )
  }
}

Render result: (stylesheet be rendered in <head>, thank to cssobj)

<header class="app_17fzew31_">
    <h2 class="appTitle_17fzew31_">Title</h2>
</header>

Then, when you clicked on h2, the result:

<header class="app_17fzew31_">
    <h2 class="appTitle_17fzew31_ large_17fzew31_">Title</h2>
</header>

also, the stylesheet for .app rule updated to background: yellow;

Here is Demo

API

MainEntry [function] signature:

function( jsObject, cssobjConfig ) -> Instance of HelperClass
  • jsObject: [object] Javascript object represetation of stylesheet, same as cssobj's 1st argument.

  • cssobjConfig: [optional, object] The config option, same as cssobj's 2nd argument, but with default value: {local: true}.

HelperClass [class] members:

  • css: [object] The cssobj result object.

  • mapClass: [function (jsx) -> jsx] Transform JSX into new JSX, with className props transformed:

    1. First passed into classnames

    2. Then passed into cssobj result.mapClass

    3. The final value is localized className as string

Static Methods

MainEntry has below methods as shortcuts for related modules:

Notes

1. No inject into sub-component

mapClass is only current-level transformer, when met a component like <Foo/>, it will not dig into it, keep the component clean in it's own render().


const {css, mapClass} = ReactCSSObj({
  '.app': {
    'p.foo': {color: 'red'}
  }
})

function Foo() {
  return <p className="foo">Hello</p>
}

function MyComponent() {
  return mapClass(<div className="app"><Foo/></div>)
}

Above example, p will rendered as is, p.foo will not be localized, to work, you should change Foo like below:

function Foo() {
  return mapClass(<p className="foo">Hello</p>)
}

2. Get localized class names

If you want get/use localized className some where (like DOM), you can use cssobj method css.mapClass:

function Foo() {
  return <p className={css.mapClass('foo')} ref={
    el => el.onclick = () => $(el).toggleClass( css.mapClass('bar') )
  }>Hello</p>
}

// any where you want to query a DOM:
document
  .querySelector( css.mapSel('.app p.foo') )
  .removeClass( css.mapClass('bar') )

Live demo here

Other

You may want babel-plugin-transform-cssobj if you don't hope runtime interpolate.