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

cssed

v3.1.0

Published

🤷‍♂️CSS-in-JS modules that just works

Downloads

37

Readme

cssed

🤷‍♂️ CSS-in-JS modules (via babel plugin):

  • lets you write CSS in JS,
  • plays nicely with Vite/NextJS/Webpack,
  • ⚡️ blazingly fast zero-runtime by accident
  • and zero-configuration by choice

Rationale

Why create another CSS-in-JS library? Well, I tried hard to avoid doing it, but couldn't find one which does what I want. And I don't want much:

  1. Template literals syntax.
  2. Write pure CSS/SASS/stylus but in JS.
  3. Media queries and pseudoclasses support.
  4. Seamless intergration with framework (~~NextJS~~Vite in my case).

More on struggle finding existing solution here

Installation

npm i cssed

Configuration

With Vite

vite.config.js:

import cssedVitePlugin from 'cssed/vite-plugin'

export default {
  plugins: [
    // ...
    cssedVitePlugin()
    // ...
  ]
}

With webpack/next/babel

No configuration required as long as you have enabled module:cssed in your .babelrc:

{
  "plugins": ["module:cssed"]
}

Also, add compilation artifcats to gitignore:

# cssed compilation artifacts
.cssed

Usage

import { css } from 'cssed'
import { dark, light } from './constants'

const styles = css`
  .red {
    color: red;
    background: ${dark};
  }

  .blue {
    color: blue;
    background: ${light};
  }
`

const Box = (props) => (
  <div className={props.isRed ? styles.red : styles.blue}></div>
)

That's it? That's it.

How it works

Babel plugin during compilation will extract content of css function call into a separate .module.css file and will place it in .cssed folder in the root of the project, rebasing URLs in css.

Before:

// index.js – before compilation
import { css } from 'cssed'
const styles = css`
  .red {
    color: red;
  }
`

const Box = (props) => <div className={styles.red}></div>

After compilation with cssed:

// index.js – compiled with cssed
import _a from '../.cssed/[hash].module.css'
const styles = _a

const Box = (props) => <div className={styles.red}></div>

And file [hash].module.css contains extracted css:

/* [hash].module.css */
/* AUTO GENERATED FILE. DO NOT EDIT  */
.red {
  color: red;
}

From here Webpack will handle it as a regular file, which has imported CSS and will process it accordingly to configured pipeline.

Syntax Highlight

Same as for styled-jsx package.

Visual Studio Code Extension

Launch VS Code Quick Open (⌘+P), paste the following command, and press enter.

ext install vscode-styled-jsx

Autocomplete Visual Studio Code Extension

Launch VS Code Quick Open (⌘+P), paste the following command, and press enter.

ext install vscode-styled-jsx-languageserver

Exmaples

check examples for:

Prior art

This would not be possible, without linaria. In fact inability to make it work in NextJS pushed me over the line to create cssed 🙃. They did awesome job making evaluators for template literals. So you can write expressions like these:

import { light } from './theme'

const cls = css`
  .some {
    color: ${light};
    font-size: ${22 + 20};
  }
`

Other notable projects:

  • stylis.js. Pretty much every CSS-in-JS project uses it for compilation. It is ultra fast and realiable, was considering using it, until idea with extracting css into file and letting bundler do all the hard lifting came to mind.
  • css-zero was a good project to learn from. Craig added atomic layer to it, which is overkill for me. Also, syntax was not what I wanted:
// this will not generate `cls.red` and `cls.blue` you would expect
const cls = css`
  .red {
    color: red;
  }
  .blue {
    color: blue;
  }
`
  • styled-jsx as with everything guys at Vercel do, it's tailored to awesome devx. Unfortunately, wasn't fit for my needs. This particular issue was super annoying (codesandbox):
const Header = () => {
  // This will not be styled. It's frustrating as hell.
  const moreItems = (
    <>
      <div className={'item'}></div>
      <div className={'item'}></div>
    </>
  )

  return (
    <>
      <div className={'item'}></div>
      {moreItems}
      <style jsx>
        {`
          .item {
            color: red;
          }
        `}
      </style>
    </>
  )
}
  • astroturf. Another awesome project to learn from. They went different direction and rely heavily on PostCSS. css api while what I need, didn't work in NextJS and also breaks when trying to use exported value like:
import { dark } from '../theme'

// this will throw
const styles = css`
  .red {
    border: 2px solid ${dark};
    padding: 10px;
  }
`
  • csjs and csstag. Two close to what I needed, gave up trying to make them work with NextJS. Also, additional setup required to make it zero-runtime. 👍to both for getting CSS syntax in JS.

Gimmicks:

CSSed is dead-simple CSS-in-JS implementation, without any gimmicks. Here is one if you need it:*

License

MIT