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 🙏

© 2025 – Pkg Stats / Ryan Hefner

yocss

v0.5.1

Published

> A zero-dependency, 1k CSS-in-JS library for purists.

Downloads

30

Readme

YoCSS

A zero-dependency, 1k CSS-in-JS library for purists.

The goal of this was to learn the ins and outs of CSS in JS intricacies, but happy to evolve and maintain it. Since it's in the early stages, it's experimental, but if you have any ideas, suggestions or want to contribute, please do!

This reflects latest master. For docs on specific versions, refer to their corresponding tags.

Install

npm install yocss

Usage

YoCSS is all about objects. Do whatever you want with an object and just pass it off to css(). It will then take your object, convert it to a CSS rule and insert it into a global stylesheet, returning the resulting className for the set of rules as a string.

import css from 'yocss';

const className = css({
  backgroundColor: 'black',
  color: 'white'
});

React et al

You can take this return value and do whatever you want with it. You may be using React:

import css from 'yocss';
import React from 'react';

const Div = props => <div {...props} className={css({
  backgroundColor: 'black',
  color: 'white'
})} />

You can even create a very simplistic function to stamp out primitives for you:

import css from 'yocss';
import React from 'react';

const styled = (Type, styles) => props => <Type {...props} className={css(styles)} />;
const Div = styled('div', {
  backgroundColor: 'black',
  color: 'white'
});

And you can even respond to props:

import css from 'yocss';
import React from 'react';

const styled = (Type, styles) =>
  props =>
    <Type
      {...props}
      className={css(typeof styles === 'function' ? styles(props) : styles)}
    />;

const Div = styled('div', props => ({
  backgroundColor: props.dark ? 'black' : 'white',
  color: props.dark ? 'white' : 'black'
}));

Shadow DOM

The css() function returns a string and inserts styles to the head. Calling raw() does the same thing except it doesn't insert styles to the head. You can use this with the value() function to return the CSS value of the specified classes and put content inside of the <style> tag in the shadow root.

import { raw, names, value } from 'yocss';

const styles = [{
  backgroundColor: 'black'
}, {
  color: 'white'
}].map(raw);

class Test extends HTMLElement {
  connectedCallback() {
    this.attachShadow({ mode: 'open' }).innerHTML = `
      <style>${value(...styles)}</style>
      <div class="${names(...styles)}">Woot!</div>
    `;
  }
}

customElements.define('x-test', Test);

Class name format

YoCSS scopes styles using a class name format of _${suffix} where ${suffix} is a number unique to that set of CSS rules. So, wherever you see something like ._0, it's representing the scoping class or a selector for it.

Nesting

Nesting works similarly to other libraries.

css({
  // ._0 .link
  link: {},

  // ._0 .link
  ' .link': {},

  // ._0 link
  ' link': {},

  // ._0 >.link
  ' >.link': {}

  // ._0.link
  '&.link': {}

  // tag._0
  '&tag': {}
});

However, if you do that, the selectors you use are only scoped to the class name generated by css(). You can combine css() and raw() to scope descendant:

const nested1 = raw({
  backgroundColor: 'black'
});
const nested2 = raw({
  color: 'white'
});

const className = css({
  [nested1]: rules(nested1),
  [`> ${nested2}`]: rules(nested2)
});

Rules that are contained in separate blocks, but eventually end up as the same selector, will be merged into the same set of rules. For example:

css({
  link: { key1: 'val1' },
  ' .link': { key2: 'val2' }
});

Would merge into:

{
  '._0 .link': {
    key1: 'val1',
    key2: 'val2'
  }
}

Global styles

Globals styles can be specified by prefixing your selector with * selector syntax where selector is the selector you want to use globally.

css({
  '* body': {
    fontFamily: 'Helvetica'
  }
})

This only works as a prefix and since * already has special meaning as selecting everything, if you want to do so, simply use **.

css({
  '**': {
    fontFamily: 'Helvetica'
  }
});