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

class-c

v0.0.10

Published

className helper

Downloads

199

Readme

class-c

Chainable class name helper with first class CSS Module support

Installation

$ npm install class-c

Usage

Basic

c uses the tagged template syntax

import c from "class-c";

c`a b`; // => "a b"

// Extra whitespace will be removed
c`   a     b    `; // => "a b"
c`
  a
  b
`; // => "a b"

// Calling with a string can be useful for class forwarding, see CSS Modules section
c("a b"); // => "a b"

Chaining

You can chain as many scopes as you'd like

c`a`.c`b c`.c`d`; // => "a b c d"

// This is more useful when used with CSS Modules, see CSS Modules section for more details
const styles = {
  a: "scoped-a",
  b: "scoped-b",
};

c(styles)`a b`.c`a b`; // => "scoped-a scoped-b a b"

Conditionals

Falsey values will be omitted, condition-by-class mapping is also supported. This can be useful with object shorthand. This also supports function conditions.

c`a ${condition && b}`; // => condition ? "a b" : "a"

c`a ${{ b: false, c: true }}`; // => "a c"

const focused = true;
const highlighted = false;
const open = true;

c`${{ focused, highlighted, open }}`; // => "focused open"

// Function conditions
const focused = () => true;
const highlighted = () => false;
const open = () => true;

c`${{ focused, highlighted, open }}`; // => "focused open"

CSS Modules

Calling c with a styles object will create a scoped context

const styles = {
  a: "scoped-a",
  b: "scoped-b",
};

c(styles)`a b`; // => "scoped-a scoped-b"

Chaining

Chaining will reset context which can be useful for forwarding classes

c(styles)`a b`.c`a b`; // => "scoped-a scoped-b a b"

// Forwarding classes
const someExternalClasses = "external-a external-b";
c(styles)`a b`.c(someExternalClasses); // => "scoped-a scoped-b external-a external-b"

Many references

If you need to use a context in many places, it can be convenient to make a new helper

const cs = c(styles);

cs`a`; // => "scoped-a"

// Chaining will still reset context
cs`b`.c`c`; // => "scoped-b"

Multiple modules/styles objects

const otherStyles = {
  a: "other-a",
  b: "other-b",
};

c(styles)`a b`.c(otherStyles)`a b`; // => "scoped-a scoped-b other-a other-b"
// Non existant classes will be removed
c(styles)`a nonexistant b`; // => "scoped-a scoped-b"

Prefixing and suffixing

const styles = {
  variant_a: "scoped-variant_a",
  variant_b: "scoped-variant_b",
};

const variant: "a" | "b" = "a";

c(styles)`variant_${variant}`; // => "scoped-variant_a"

const suffixStyles = {
  a_suffix: "scoped-a_suffix",
  b_suffix: "scoped-b_suffix",
};

c(suffixStyles)`${variant}_suffix`; // => "scoped-variant_a"