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

v0.5.7

Published

Scoped styles for React components

Downloads

546

Readme

Scoped Styles for React

NPM

Get your CSS classes scoped by component directory

How it's different from CSS Modules?

In CSS Modules you have to manually import and assign classes

import styles from './button.styl';

const Button = () => (
  <button className={styles.foo}>Press Me</button>
);

React Scoped Styles doesn't require to change the conventional styling workflow. You still assign your classes with plain strings.

import './button.styl';

const Button = () => (
  <button className="foo">Press Me</button>
);

Installation

npm i react-scoped-styles

Usage

The module assumes that component file and its styles are in the same directory. This is sample for Stylus. The same applies for Sass and others.

+-- button
   +-- Button.tsx
   +-- button.styl

Button.tsx

  import React from 'react';
  import './button.styl';

  const Button = () => (
    <button className="foo">Press Me</button>
  );

  export default Button;

button.styl

.foo
    border none
    padding 10px 30px
    color white
    background-color darkslateblue

This will be rendered to

<button class="button-c65bae6565-foo">Press Me</button>
.button-c65bae6565-foo {
  border: none;
  padding: 10px 30px;
  color: #fff;
  background-color: #483d8b;
}

Getting started

The module exposes two loaders both for componenets and styles.
Append the script-loader after it has been transpiled by TypeScript or Babel.
And style-loader should be after the preprocessor loader and before the css-loader.

webpack.config.js

module.exports = {
  module: {
    rules: [
      // TypeScript
      {
        test: /\.tsx?$/,
        use: [
          'react-scoped-styles/script-loader',
          'awesome-typescript-loader'
        ]
      },
      // Babel
      {
        test: /\.jsx?$/,
        exclude: /node_modules/,
        use: [
          'react-scoped-styles/script-loader',
          {
            loader: 'babel-loader',
            options: {
              presets: ['@babel/preset-env', '@babel/preset-react']
            }
          }
        ]
      },
      // Stylus
      {
        test: /\.styl$/,
        use: [
          'style-loader',
          'css-loader',
          'react-scoped-styles/style-loader',
          'stylus-loader'
        ]
      },
      // Sass
      {
        test: /\.scss$/,
        use: [
          'style-loader',
          'css-loader',
          'react-scoped-styles/style-loader',
          'sass-loader'
        ]
      }
    ]
  }
};

Globals

To use global styles you can pass globalsPrefix options to both loaders and prefix your classes with it.
(app is applied by default)

const scopedStylesOptions = {
  globalsPrefix: 'app'
};


{
  loader: 'react-scoped-styles/script-loader',
  options: scopedStylesOptions
}
// ...
{
  loader: 'react-scoped-styles/style-loader',
  options: scopedStylesOptions
}

Thus classes with app- prefix will be ignored.

const Button = () => (
    <button className="foo app-global-class">Press Me</button>
);
.foo
    border none
    padding 10px 30px
    color white
    background-color darkslateblue

.app-global-class
    background-color purple

Becomes

<button class="button-c65bae6565-foo app-global-class">Press Me</button>
.button-c65bae6565-foo {
  border: none;
  padding: 10px 30px;
  color: #fff;
  background-color: #483d8b;
}
.app-global-class {
  background-color: #800080;
}

Conditional classes

To use conditional classnames you can use the classes function.
Note that the classnames should be inline

import React, { useState } from 'react';
import { classes } from 'react-scoped-styles';
import './sidebar.styl';

export const SideBar = () => {
    const [open, setOpen] = useState(false);

    return (
        <div className={classes([open, 'open'], 'sidebar')}>
            ...
        </div>
    )
};

API

classes (
    ...([boolean, string] | string)[]
) => string;

classes function accepts arrays of [condition, className] pairs, and class-name strings or default classes.

<div className={classes('default', [true, 'applied'], 'another-one', [false, 'not-applied'])} />

All classes should be INLINE, this won't work

const someClass = 'some';
const someCondition = true;

<div className={classes([someCondition, someClass])} />