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

v0.0.12

Published

React style sheet is a react library for styling your components. You create a stylesheet component by specifiying the css, and the stylesheet renders its children with the appropriate styles.

Downloads

28

Readme

React-style-sheet

React style sheet is a react library for styling your components. You create a stylesheet component by specifiying the css, and the stylesheet renders its children with the appropriate styles.

Install

yarn add react-style-sheet

Basic Usage

import Style from 'react-style-sheet';

const Stylesheet = Style`
	button {
		color: lightskyblue;
	}
`;

Creates a Stylesheet component for you to use inside a render.

const Component = props => {
	return (
		<Stylesheet>
			<button>Push me</button>
		</Stylesheet>
	);
};

The Stylesheet component renders it children with the appropriate styles. In this case, the button would have a lightskyblue color. This would effectively be the same as if the component rendered

<button style={{ color: 'lightskyblue' }}>Push me</button>

Target your own components

You can also target your own components in the stylesheet.

const Stylesheet = Style`
	MyFancyButton {
		color: lightskyblue;
	}
`;

<Stylesheet>
	<MyFancyButton>Push me</MyFancyButton>
</Stylesheet>;

More useful stylesheets

React-style-sheet supports ids, classes, nested selectors, attributes, combinators, ... for targeting the children of a stylesheet component.

IDs & Classes

const Stylesheet = Style`
	#someID.aClass {
		color: lightskyblue;
	}
`;

<Stylesheet>
	<button>I have no style</button>
	<button id="someID" className="aClass">
		I have style
	</button>
</Stylesheet>;

Attributes

const Stylesheet = Style`
	MyFancyLink[href] {
		color: lightskyblue;
	}
`;

<Stylesheet>
	<MyFancyLink href="https://github.com/">Github</MyFancyLink>
</Stylesheet>;

Nesting

const Style = Stylesheet`
	> div {
		h1 + button {
			color: lightskyblue;
		}

		button:nth-child(3n+1) {
			background-color: hotpink;
		}
	}
`;

Cascade

React-style-sheet figures out which elements needs which styles. The styles are applied to the elements themselves, and are not added to the global scope. This means styles only affect the elements from the render that uses the stylesheet. It does not cascade into other components.

If you have, for example, a MyButton component that renders a button tag:

const MyButton = props => {
	return <button>push me</button>;
};

And you render this MyButton component within a stylesheet like this:

const Stylesheet = Style`
	button {
		color: lightskyblue;
	}
`;

const Component = props => {
	return (
		<Stylesheet>
			<MyButton />
		</Stylesheet>
	);
};

There is no child of stylesheet that is a 'button' element, so the button style rule is not added to any of the stylesheets' children. Anything rendered inside the MyButton component does not suffer from cascading styles.

If however your stylesheet was defined as:

const Stylesheet = Style`
	MyButton {
		color: lightskyblue;
	}
`;

The corresponding style will be passed to the MyButton component as a style attribute. It would have effectively been rendering the same as if the component was defined like:

const Component = props => {
	return <MyButton style={{ color: 'lightskyblue' }} />;
};

The component MyButton decides what to do with the style attribute. The component is in full control of its rendering, and no global styles or cascade are messing with the render of MyButton.