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

v0.0.2

Published

Sensible Styles in React

Downloads

11

Readme

WIP. Move along!

React Styles

Circle CI

React Styles looks to implement the best features of CSS using the best of React and JavaScript.

Usage

Thanks to the react-styles-parser package, you can write styles as files besides your component. For example,

+-- components/
|------ Button/
|--------- index.js (where the component logic is located)
|--------- styles.js (where the styles are located)

The styles.js file will look very similar if you're used to writing scss.

// styles.js

module.exports = `
  button {
    background: black;

    clicked {
      background: white;
    }
  }
`;

We name our top level styles as component names, and nested styles are states of the component, such as :hover, :active, :focus, etc.

We can even include media queries as nested styles of a component, for example:

// styles.js

module.exports = `
  button {
    background: black;

    minWidth {
      320 {
        width: 20%;
      }
    }
  }
`;

We can also use variables by declaring them at the top of the file, or importing them at the top of the file, and including them in our styles.js file by writing ${variableName}. This is made possible by JavaScript ES6's new template string feature.

Now, inside of our index.js file, we import our button styles:

// index.js (button component)

// The react-styles! is a webpack loader that is required for
// loading our styles properly
import styles from 'react-styles!./styles';
import styleHelper from 'react-styles';

const Button = React.createClass({
  render () {
    let { button } = styleHelper(styles, this.context);

    return <button style={button}></button>
  }
})

react-styles-loader

react-styles-loader is a package used by Webpack as a loader for styles written in React Styles. For any file that you want to use styles from, you specify the webpack loader before the name of the path to get a raw styles object.

styleHelper(stylesObject, contextObject)

The styleHelper method takes in an object representing your styles and a context that will tell the function the state of the world. This means information like viewport size, or browser being used, can be used in determining what are valid styles when calling the styleHelper.

What gets returned from styleHelper is a style object that you can then grab properties off of and apply to React Components.

Philosophy

React Styles takes on the philosophy that CSS has incredibly useful ideas on how to style components in an application, but implementation details prevented these useful properties from being highly valued. For example, the cascade is a very useful tool that allows developers to have a baseline look throughout their application without having to explicity declare repeated styles.

Unfortunately, you can never opt out of this cascade or change what the cascade is composed of. This ultimately leads to specificity wars and is the birthplace of !important due to its nature.

By harnessing the power of React's context feature, React Styles is able to provide style suggestions for children components that is very similar to the cascade. Children can choose to opt in to these styles, or even pick and choose the styles that they want. More importantly, children reserve the right to override specific styles, most of the base styles, or even create their own style independent of the given context.

React Styles also gives you the ability to change the context at any given component level so that children will receive a new set of suggestions regarding the base styles of components.