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-style-sheets

v0.1.0

Published

Create cascading style sheets from within your React components

Downloads

20

Readme

React Style Sheets CircleCI

Create cascading style sheets from within your React components

About

React Style Sheets allows defining styles in your javascript in a similar way to React Native, but with a huge host of additional benefits

// Define your styles

var classNames = ReactStyleSheets.createUniqueClassStyles({
  myClass: {
    color: 'red'
  }
});

// You can then use this in your React components

<div className={classNames.myClass}>
  Hello, World!
</div>

Benefits

Unlike React Native (or libraries like Radium / React Style), it has the following benefits:

  • Styles are only generated once, when the script is first loaded
  • The styles are not inlined, instead they are added in a style tag and either available globally, or accessed by a unique class name (see examples)
  • No need to manually implement hover, active, etc states, or media queries in javascript
  • You can still use inline styles without the need for complex extending of styles
  • Easily extend / override reusable components styles by concatenating class names
  • You can add global styles for all HTML elements (by tag name)
  • You can create and utilize CSS keyframe animations
  • You can use media queries to adjust styles on different devices
  • You can use advanced selectors like hover, active, disabled, firstChild, etc
  • You can use selectors for pseudo elements like before, after, and selection
  • You can nest selectors
  • You can easily define styles with our intuitive API (automatically adds units, joins arrays, and allows breaking up styles with nested objects)

Benefits of React Style Sheets over using CSS, and CSS pre/post-processors include

  • Keep your styles, component logic, and markup in the same file
  • Easily bundle styles with your component libraries
  • Automatically add vendor prefixes
  • Use variables and mixins (functions)
  • No additional dependencies - parse and bundle your styles with your chosen javascript build tools
  • Class names are unique (and obfuscated by default) preventing accidental style inheritance
  • No need to invalidate CSS cache

Examples

Obfuscated class names

// Define your styles
var classNames = ReactStyleSheets.createUniqueClassStyles({
  myClass: {
    color: 'red'
  }
});

// And you end up with some classNames like the following
{
  myClass: 'myClass_obfus'
}

// You can then use this in your React components
<div className={classNames.myClass}>
  Hello, World!
</div>

This also generates the following style tag and appends it to the head tag

<style type="text/css">
  .myClass_obfus {
    color: red;
  }
</style>

Global tag styles

ReactStyleSheets.createGlobalTagStyles({
  p: {
    margin: '10px auto'
  }
});

Generates the following styles (available globally)

<style type="text/css">
  p {
    margin: 10px auto;
  }
</style>

Automatically add units and build up style values from arrays

ReactStyleSheets.createGlobalTagStyles({
  p: {
    margin: [10, 'auto']
  }
});

Generates the following styles

<style type="text/css">
  p {
    margin: 10px auto;
  }
</style>

Break up style definitions with nesting

ReactStyleSheets.createGlobalTagStyles({
  p: {
    margin: {
      top: 10,
      bottom: 10
    },
    border: {
      vertical: [1, 'solid', 'black'],
      horizontal: 'none'
    }
  }
});

Generates the following styles

<style type="text/css">
  p {
    margin-top: 10px;
    margin-bottom: 10px;
    border-top: 1px solid black;
    border-bottom: 1px solid black;
    border-right: none;
    border-left: none;
  }
</style>

Utilize CSS keyframe animations

var animations = ReactStyleSheets.createUniqueKeyframeAnimation({
  myAnimation: {
    '0%': {
      opacity: 0;
    },
    '100%': {
      opacity: 1;
    }
  }
});

var classNames = ReactStyleSheets.createUniqueClassStyles({
  myClass: {
    animation: animations.myAnimation
  }
})

Utilize media queries

var classNames = ReactStyleSheets.createUniqueClassStyles({
  myClass: {
    width: '100%',
    '@media all and (min-width: 768px)': {
      width: '50%'
    }
  }
})

Extend styles easily in reusable components

render: function () {
  return (
    <div className={classNames.myClass + ' ' + this.props.className} />
  );
}

Use state selectors like hover, active, disabled, firstChild, etc

ReactStyleSheets.createGlobalTagStyles({
  a: {
    textDecoration: 'none',
    hover: {
      textDecoration: 'underline'
    }
  }
});

Use pseudo element selectors like before, after and selection

ReactStyleSheets.createGlobalTagStyles({
  li: {
    before: {
      content: '">"'
    }
  }
});

Nest state / pseudo element selectors

ReactStyleSheets.createGlobalTagStyles({
  li: {
    firstChild: {
      before: {
        content: '">"'
      }
    }
  }
});

Automatically prefix styles with vendor prefixes

ReactStyleSheets.setOptions({
  vendorPrefixes: {
    transform: ['webkit', 'moz', 'ms', 'o']
  }
});

var classNames = React.createUniqueClassStyles({
  myClass: {
    transform: 'rotate(45deg)'
  }
});

Generates the following styles

<style type="text/css">
  .myClass_obfus {
    -webkit-transform: rotate(45deg);
    -moz-transform: rotate(45deg);
    -ms-transform: rotate(45deg);
    -o-transform: rotate(45deg);
    transform: rotate(45deg);
  }
</style>