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

v3.0.1

Published

Write scoped, autoprefixed styles declaratively in React

Downloads

22,922

Readme

react-style-tag

Write styles declaratively in React

Table of contents

Installation

$ npm i react-style-tag --save

Usage

// ES2015
import { Style } from 'react-style-tag';

// CommonJS
const Style = require('react-style-tag').Style;

Implementation

import React, { Component } from 'react';

import { Style } from 'react-style-tag';

function App() {
  return (
    <div>
      <h1 className="foo">Bar</h1>

      <Style>{`
          .foo {
            color: red;

            &:hover {
              background-color: gray;
            }

            @media print {
              color: black;
            }
          }
        `}</Style>
    </div>
  );
}

Summary

react-style-tag creates a React component that will inject a <style> tag into the document's head with the styles that you pass as the text content of the tag. Notice above that the styles are wrapped in {`and`}, which create a template literal string. Internally, react-style-tag parses this text and applies all necessary prefixes via stylis. All valid CSS is able to be used (@media, @font-face, you name it), and you can use nesting via the use of the & reference to the parent selector.

The style tag that is injected into the head will be automatically mounted whenever the component it is rendered in is mounted, and will be automatically unmounted whenever the component it is rendered in is unmounted.

Scoped Styles

There is an additional utility provided that can help to scope your styles in the vein of CSS Modules, and this is hashKeys. This function accepts an array of keys to hash, and returns a map of the keys to their hashed values.

import { hashKeys, Style } from 'react-style-tag';

const { foo, bar } = hashKeys(['foo', 'bar']);

function App() {
  return (
    <div>
      <div className={foo}>My text is red due to the scoped style of foo.</div>

      <div className={bar}>
        My text is green due to the scoped style of bar.
      </div>

      <div className="baz">My text is blue due to the global style of baz.</div>

      <Style>{`
          .${foo} {
            color: red;
          }

          .${bar} {
            color: green;
          }

          .baz {
            color: blue;
          }
        `}</Style>
    </div>
  );
}

Notice you can easily mix both scoped and global styles, and for mental mapping the scoped styles all follow the format scoped__{key}__{hash}, for example scoped__test__3769397038. The hashes are uniquely based on each execution of hashKeys, so the implementation can either be Component-specific (if defined outside the class) or instance-specific (if defined inside the class, on componentDidMount for example).

Props

Naturally you can pass all standard attributes (id, name, etc.) and they will be passed to the <style> tag, but there are a few additional props that are specific to the component.

hasSourceMap

boolean, defaults to false in production, true otherwise

If set to true, it will render a <link> tag instead of a <style> tag, which allows source referencing in browser DevTools. This is similar to the way that webpack handles styles via its style-loader.

The use of sourcemaps require the use of Blob, which is supported in IE10+, Safari 6.1+, and all other modern browsers (Chrome, Firefox, etc.). If you browser does not support Blob and you want to use sourcemaps, you should include a polyfill. Recommended is blob-polyfill.

Make sure this import occurs prior to the import of react-style-tag to ensure blob support is present.

isMinified

boolean, defaults to true in production, false otherwise

If set to false, it will pretty-print the rendered CSS text. This can be helpful in development for readability of styles.

isPrefixed

boolean, defaults to true

If set to false, it will prevent stylis from applying vendor prefixes to the CSS.

Global Options

All of the props available are also available as global options for all instances that can be set with the setGlobalOptions method:

import { setGlobalOptions } from 'react-style-tag';

setGlobalOptions({
  hasSourceMap: true,
  isMinified: true,
  isPrefixed: false,
});

Development

Standard stuff, clone the repo and npm i to get the dependencies. npm scripts available:

  • build => run rollup to build dist files
  • dev => run webpack dev server to run example app / playground
  • dist => runs build and build:minified
  • lint => run ESLint against all files in the src folder
  • lint:fix => runs lint with --fix
  • prepublishOnly => run lint, typecheck, clean, test, transpile:es, transpile:lib, and dist
  • test => run jest test functions with NODE_ENV=test
  • test:coverage => run test, but with coverage checker
  • test:watch => run test, but with persistent watcher
  • transpile:lib => run babel against all files in src to create files in lib
  • transpile:es => run babel against all files in src to create files in es, preserving ES2015 modules (for pkg.module)
  • typecheck => check for TypeScript errors