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-css-component

v1.0.2

Published

Injecting CSS via React Components

Downloads

781

Readme

CSS via Components

A single React component to inject CSS with ease. It works with SSR (even using React 16's renderToNodeStream) and client-side rendering out-of-the-box.

Support Me

If you're using Robin Frischmann's packages, please consider supporting his Open Source Work on Patreon.

Installation

# yarn
yarn add react-css-component

# npm
npm i --save react-css-component

Caution: It requires react and prop-types to be present.

Why?

This package was created as a possible solution to a question by Kent C. Dodds. Creating resuable React components in general is pretty easy, but adding styling is not. The simplest way is to just use inline style, which works pretty well for many basic components. Yet, it is very limited. Neither do we have have pseudo classes nor do we have media queries. So, at some point, we need actual CSS. We could include a CSS file, but that would require an additional build step e.g. using Webpack in combination with its css-loader. While this would work, it's not very compelling to enforce a certain build tool, just to use a single component. Alright, how about CSS in JS? Using plain JavaScript to style the component sounds great, as the component is written in JavaScript anyways. But, most CSS in JS solutions are way to big to depend on. They're built for applications, not for independent reusable components. Yet, we can still benefit from writing our CSS in JavaScript. This package is the attempt to provide the smallest universal solution for inlining CSS in JavaScript possible.

How?

Depending on whether universal rendering (server-side rendering with client-side rehydration) or client-side only rendering is used, the implementation uses a different logic.

Universal Rendering

Server Rendering: The UniversalStyle component renders a primitive style DOM element that uses dangerouslySetInnerHTML to inject a CSS string. Client Rehydration: At first, the UniversalStyle component just gets rehydrated to match the server-side markup. But, as soon as it is about to unmount, the style element is copied to the document.head once. This will ensure it's existence just in case any other component using it's CSS is still visible.

Caveat

During server-side rendering, the UniversalStyle component is not able to track it's own occurence, which may result in duplicate style elements. This won't break anything, but also is not optimal. To ensure that each UniversalStyle instance is only rendered once, we need an unique cache on every render. This is achieved by passing a simple cache via React's context feature. Check the StyleCacheProvider component for more information.

Client-Only Rendering

If we only render on the client-side anyways, we can skip that complex flow and just use the ClientStyle. It simply injects a style element into the document.head on instantiation and tracks its occurence using a global cache.

Note: The ClientStyle component won't throw with server-side rendering, but it simply doesn't render styles.

Style

Both UniversalStyle and ClientStyle share the exact same component API. This component is the core component and is used to inject the CSS.

Props

| Parameter | Type | Description | | --- | --- | --- | | css | (string) | A CSS string that is rendered |

Import

// universal rendering
import { UniversalStyle as Style } from 'react-css-component'

// client-only rendering
import { ClientStyle as Style } from 'react-css-component'

Example

const css = `
  .button {
    background-color: darkblue;
    padding: 20px 10px;
    font-size: 16px;
    color: white
  }

  .button:hover {
    background-color: blue
  }
`

// return an array to colocate style and the button
const Button = () => [
  <Style css={css} />
  <button className="button">Click Me!</button>
]

StyleCacheProvider

The StyleCacheProvider is used to prevent duplication. It is not required, but recommended. It should wrap your whole application and is only required once, no matter how many components use the Style component. It does not alter the resulting DOM structure as it simply renders its children. It passes an empty cache object via React's context feature.

Tip: If you're using the UniversalStyle component for a reusable shared component, make sure to inform your users about the caveat of not using this component.

Example

import { StyleCacheProvider } from 'react-css-component'

const App = () => (
  <StyleCacheProvider>
    <Button />
  </StyleCacheProvider>
)

License

react-css-component is licensed under the MIT License. Documentation is licensed under Creative Common License. Created with ♥ by @rofrischmann.