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

v0.0.3

Published

Keep CSS with your ReactJS component definitions

Downloads

27

Readme

Keep your CSS with your React component definitions

I've recently been creating a large client side JavaScript application with React and Flux. As of right now the app has 35 basic React components, about 10 of which have their own custom CSS. I was in the bad habit of simply appending CSS for new components to a global stylesheet as I built them, but I knew this approach wouldn't be maintainable in the longer term.

What I really wanted was to keep my CSS in the same file as my component definition so I wouldn't need to go fishing around in my global stylesheet every time I wanted to make a change. This would also mean that CSS would only be included if the component was require'd in the project somewhere. Removing a component from a project would remove its corresponding CSS automatically - no need to remember to remove it from the global stylesheet.

Enough background - here's what it looks like:

var React = require('react');
var Button = React.createClass({
    render: function() {
        return (
          <div class="btn">My Button</div>
        )
    }
});

require('react-styl')(`
  .btn {
    display: inline-block;
    background: blue;
    padding: 5px;
  }
  .btn:hover {
    background: red;
  }
`)

module.exports = Button;

You'll notice we use the convenience of ES6 multiline strings (`) so we can write css directly in our component JS files without any funky quoting. You'll need to run this through an ES6->ES5 converter (such as es6ify) for your resulting code to be cross-browser compatible.

Now, to render the CSS on the client, simply add a line to your bootstrap file. Something like:

React.render(<MyApp/>, document.getElementById('react'));
require('react-styl').addStylesheet();

That's it! But how does it work? Here's the (slimmed down) code for the react-styl module:

var _styles = "";

module.exports = function(styles) {
    _styles += styles;
}

module.exports.addStylesheet = function() {
    var styleTag = document.createElement('style');
    style.type = 'text/css';
    style.appendChild(document.createTextNode(_styles));
    document.head.appendChild(style);
}

Every call to require('react-styl')('css string') appends the given css to the _styles variable. Since each component is cached after being require'd somewhere in your project, component CSS is only appended to the _styles variable once, even though your component may be require'd many times throughout your project. Then it's simply a case of appending the resulting CSS to a style tag and you're done.

If you want to use a CSS preprocessor, like styl, simply edit your addStylesheet function to process the css before it gets added to the style tag. Something like this:

var styl = require('styl');
module.exports.addStylesheet = function() {
    var css = styl(_styles).toString(),
        style = document.createElement('style');

    style.type = 'text/css';
    style.appendChild(document.createTextNode(css));

    document.head.appendChild(style);
}

And that's the crux of it! Now you can keep your component CSS and JS in the same file, easing development and maintainability.

By wrapping your CSS code with annotations, you can strip them out when building for production, instead serving your CSS from a static file.

To try the example, clone this repo, change to the example directory, run npm install then npm build.