Library

react-scoped-css

A build tool that scopes CSS to each React component. You keep plain className and plain CSS or SCSS. The build hashes class names per file, so styles never clash.

Why I built it

Plain CSS in React is global, so two components using the same class name can clash. CSS Modules and CSS-in-JS fix that, but only if you rewrite how every component is styled. I wanted scoping that leaves the code as it is.

Install from npm Get started View source @dinesh-gamage/react-scoped-css
LibrariesDev toolsv2.0.4MITOpen source
On npm
1 / 3
Features

What it does

01

Write CSS the way you already do

Plain className in JSX and plain selectors in CSS or SCSS. Your source files never change. Only the build output gets the hash.

You write
<button className="btn">
  Save
</button>

.btn { color: blue; }
Build output
<button className="btn-a3f9b2c1">
  Save
</button>

.btn-a3f9b2c1 { color: blue; }
02

Same class names, no collisions

Two components can use the same class names and still look different. Here is how, one step at a time.

  1. 1

    Two cards

    TeamCard and TaskCard look nothing alike. Both use the classes .card and .title.

    TeamCard and TaskCard
  2. 2

    TeamCard

    Its own .tsx and .scss file, side by side.

    TeamCard.tsx
    <div className="card">
      <p className="title">{name}</p>
    </div>
    TeamCard.scss
    .card { padding: 24px; background: #7c3aed; }
    .title { color: #fff; font-size: 20px; }
  3. 3

    TaskCard

    The same class names, with different styles.

    TaskCard.tsx
    <div className="card">
      <p className="title">{title}</p>
    </div>
    TaskCard.scss
    .card { padding: 14px; border-left: 4px solid #2563eb; }
    .title { color: #0f172a; font-size: 15px; }
  4. 4

    The build

    Each file gets its own hash. Your source still says .card. The browser sees two different classes, so the styles never clash.

    Your source next to the built page
03

Leave globals alone

Class names that start with an excluded prefix stay exactly as written. Use it for shared styles and component libraries.

vite.config.ts
scopedCss({ exclude: ['global-', 'shared-'] })
How to use

Get started

Read the full docs
  1. 1

    Install

    Add the package to your project.

    $npm install @dinesh-gamage/react-scoped-css
  2. 2

    Get the config

    The init command prints the snippet for your bundler.

    $npx @dinesh-gamage/react-scoped-css init
  3. 3

    Add it to your bundler

    Vite, Next.js and webpack are supported.

    // vite.config.ts
    import { defineConfig } from 'vite'
    import react from '@vitejs/plugin-react'
    import { scopedCss } from '@dinesh-gamage/react-scoped-css/vite'
    
    export default defineConfig({
      plugins: [react(), scopedCss({ exclude: ['global-'] })],
    })
  4. 4

    Keep each stylesheet next to its component

    Same name, same folder. That is how both files get the same hash.

    src/components/Button/Button.tsxButton.scsssrc/components/Card/Card.tsxCard.scss