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

anesthetic

v0.4.0

Published

Nest CSS rules inside custom properties

Downloads

15

Readme

a{nest}hetic

Nest CSS rules inside custom properties

About

Anesthetic is a small tool that does one thing - allows CSS authors to nest CSS rules inside other CSS rules in a way that meets the following criteria:

  1. 100% valid CSS syntax
  2. Works in browsers today
  3. No conflict with native CSS nesting syntax
  4. Future-proof

How does it work?

The technique is to put the declaration list for the rule you would like to nest as the value of a custom property, and to name the custom property in a special way. Here's how we'll think about the naming of these properties

"--" <custom-name> <selector> ":" <block> [ ";" ]
  • every custom property starts with a double dash --
  • <custom-name> is part of a valid CSS property name you choose to namespace your nested rules and keep them easily identifiable amongst all your custom properties
  • selector is a CSS selector list, with any special characters escaped by a backslash \
  • <block> is a {-block containing the declaration list of the nested rule.
  • ; semicolon is the delimiter that is required to separate multiple properties in a declaration list

Here's an example using - as the custom name:

/* nested */
a {
  color: red;
  ---\ b: {
    color: blue;
  };
}

/* expanded */
a {
  color: red;
}
a b {
  color: blue;
}

Here in this example, the first part of the custom property ---\ b made up of the first two dashes plus our custom name (-) is replaced by the selector of the original rule, so ---\ b becomes a b.

If you work with a different custom name, like nest-, you can process the same rules written like this:

a {
  color: red;
  --nest-\ b: {
    color: blue;
  };
}

Usage

This package is available on npm and is delivered in two formats:

  • index.cjs.js is a CommonJS module for use with Node and CommonJS bundlers
  • index.js is an ES module for use with Deno, browsers, and ES module bundlers

Below are some of the ways you can consume and use this package.

Using anesthetic via npx without installing anything

$ npx anesthetic 'a { color: red; ---\ b: { color: blue; }; }'
$ npx anesthetic path/to/stylesheet.css

Using as an ES module with Deno or a browser

import anesthetic from 'https://unpkg.com/anesthetic'

console.log(
  anesthetic(`
    a {
      color: red;
      ---\\ b: {
        color: blue;
      };
    }
  `)
)

Using as a CommonJS module with Node

const anesthetic = require('anesthetic/index.cjs.js')

console.log(
  anesthetic(`
    a {
      color: red;
      ---\\ b: {
        color: blue;
      };
    }
  `)
)

Command-line usage with Node or Deno

Expanding a string

To expand a string, supply a string to the CLI script as the first argument

$ node cli/node.js 'a { color: red; ---\ b: { color: blue; }; }'
$ deno cli/deno.js 'a { color: red; ---\ b: { color: blue; }; }'

Expanding a file

To expand a stylesheet, supply a pathname to the CLI script as the first argument:

$ node cli/node.js path/to/stylesheet.css
$ deno --allow-read cli/deno.js path/to/stylesheet.css

You can run npm link if you want to use cli/node.js on your system as the command anesthetic

Options

anesthetic(string, propertyName)
  • string is a string containing a CSS stylesheet to expand
  • propertyName is a part of a custom property name to namespace your nested rules. The default value if omitted is -.

Examples

Nesting rules

/* nested */
a {
  color: red;
  ---\ b: {
    color: green;
    ---\ c: {
      color: blue;
    };
  };
}

/* expanded */
a {
  color: red;
}
a b {
  color: green;
}
a b c {
  color: blue;
}

Using selector lists

/* nested */
a, b, c {
  ---\ d\,\ e\,\ f: {
    color: lime;
  };
}

/* expanded */
a d,
a e,
a f,
b d,
b e,
b f,
c d,
c e,
c f {
  color: lime;
}

To see more demos, check out the tests in examples/

More Info