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

eszett

v0.4.0

Published

Explicit css scopes for react – by using a unique css class per component generated at build time.

Downloads

174

Readme

ß – eszett

Explicit css scopes for react – by using a unique css class per component generated at build time.

TODO

  • [ ] dont transform classNames in files that don't import sz
  • [ ] maybe add a custom style component
import eszett from "eszett";

function Header() {
  return (
    <header>
      <h2>Hello World</h2>
      <p>Subtitle</p>

      <style href={eszett} precedence='eszett'>{`
        .${eszett} {
          header& {
            background: blue;
          }

          h2& {
            color: white;
          }

          p& {
            color: grey;
          }
        }
      `}</style>
    </header>
  );
}

Install

npm install eszett

eszett is an swc plugin – so it should work wherever swc works.

With NextJs

In nextjs you can add it to your next.config.js:

// next.config.js
module.exports = {
  …
  experimental: {
    swcPlugins: [["eszett/swc", {}]]
  }
}

Usage

How it Works

eszett generates a unique id for each react component and gives you two helper methods to use it:

Rewriting classNames:

// this input
import "eszett";
<div className='header' />;

// will be tranformed to:
<div className={'23u00ds-1' + ' ' + 'header'} />;

Access the scope name as variable

// this input
import eszett from "eszett";
console.log(eszett);

// will be transfomed to to:
console.log("23u00ds-1");

sz tagged template literal

// this input
import { sz } from "eszett";
<Link className={sz`header`} />;

// will be tranformed to:
<Link className={'23u00ds-1' + ' ' + `header`} />;

The eszett scope name is generated by hashing the file path of the component and incrementing a counter for each top level function in each file

Together with support for <style> tags in react 19 and css nesting this is all we need to encapsulate our styles inside our components.

Without modern css

If you need to suport older Browsers you could use something like postcss-preset-env or you can just write classic css:

import eszett from "eszett";

function Header() {
  return (
    <header className='header'>
      <h2 className='title'>Hello World</h2>
      <p>Subtitle</p>

      <style href={eszett}>{`
        ${eszett}.header {
          background: blue;
        }

        ${eszett}.title {
          color: white;
        }

        ${eszett}.header p {
          color: grey;
        }
      `}</style>
    </header>
  );
}

Styling children

classNames are only rewritten for native html elements and locally defined elements. If you want to pass the scoped class name down to other components, you can use the sz template literal to do that:

import eszett, { sz } from "eszett";

function PassClassNameToChildren() {
  return (
    <>
      <Link className={sz`link`} href="/home">
        Home
      </Link>
      <style href={eszett}>{`
        ${eszett}.link {
          color: red;
        }
      `}</style>
    </>
  );
}