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

@nberlette/clsx

v0.3.1

Published

Type-safe utility for constructing conditional class names in TypeScript or JavaScript.

Downloads

14

Readme

@nick/clsx

This is a utility for constructing conditional class names in JavaScript and TypeScript from a set of mixed inputs. It's based on the popular clsx package by Luke Edwards, re-written and enhanced by Nicholas Berlette in 100% TypeScript using modern language features.

In addition to being a drop-in replacement for the original, this package also introduces an advanced type-level implementation of the clsx function. It leverages nearly identical logic as the runtime function, at a purely type level.

That type-level implementation affords you with a compile-time preview of the className strings that clsx expects to generate at runtime, without ever leaving your editor. This is quite useful for catching typos and errors before they occur.

Usage

📦 Install

This package is distributed through JSR, NPM, and Deno.

It can be installed using your package manager of choice:

deno add @nick/clsx
npx jsr add @nick/clsx
bun add @nberlette/clsx
# or
pnpm add @nberlette/clsx
# or
yarn add @nberlette/clsx
# or
npm install @nberlette/clsx

📜 Import

The package exports a single function, clsx, which can be imported as follows:

import { clsx } from "@nick/clsx";

const className = clsx("foo", "bar", "baz");

If you're using the NPM package:

import { clsx } from "@nberlette/clsx";

const className = clsx("foo", "bar", "baz");

Or, if you're importing from deno.land:

import { clsx } from "https://deno.land/x/clsx/mod.ts";

const className = clsx("foo", "bar", "baz");

API

clsx

Constructs a composite className string from a given set of mixed inputs.

Signature

function clsx<T extends ClassInputs>(...classes: T): Clsx<T>;

Parameters

| Name | Info | | :------------ | :---------------------------------------- | | classes | The class names to compile into a string. |

Returns

| Type | Info | | :----------------------------------- | :--------------------------------------------------- | | Clsx<T, string> | Composite className string generated from the input. |


Clsx<T, Fallback>

type Clsx<T, Fallback = string> = T extends ClassInputs
  ? T["length"] extends 0 ? Fallback
  : IsValidwInput<
    T,
    MergeValues<T, Fallback> extends infer S ? S extends "" ? Fallback : S
      : Fallback,
    Fallback
  >
  : Fallback;

The type-level equivalent of the clsx function, which is used to render a compile-time preview of the className string expected to be generated.

For your convenience, an optional Fallback type parameter can be specified, which will be used in an event where a suitable type cannot be inferred. The default fallback type is the generic string type, since the clsx function will always return a string at runtime.

Type Parameters

| Name | Extends | Default | | :--------- | :------ | :------- | | T | -- | -- | | Fallback | -- | string |


Examples

import { clsx } from "@nick/clsx";

const cn = clsx("foo", "bar", "baz");
//     ^? const cn: "foo bar baz"

const cn2 = clsx("foo", { bar: true, baz: false });
//     ^? const cn2: "foo bar"

const cn3 = clsx("nested", ["deep", { no: null }, ["yuh"]]);
//     ^? const cn3: "nested deep yuh"
import { clsx } from "@nick/clsx";

const dark = matchMedia("(prefers-color-scheme: dark)").matches;

const bgs = ["bg-white", "bg-black"] as const;

// This type is correctly inferred as a union of two possible class names:
const cn = clsx("w-1/2", "h-full", bgs[+dark]);
//     ^? const cn: "w-1/2 h-full bg-white" | "w-1/2 h-full bg-black"

For more examples, refer to the test suite.


Further Reading

🧑🏽‍💻 Contributing

This project is open-source, and I welcome contributions of any kind. Feel free to open an issue or pull request in the GitHub Repository if you have any suggestions, bug reports, or feature requests.

🐛 Bugs and Issues

If you encounter any bugs or unexpected behavior, please open an issue in the GitHub Repository so it can be addressed promptly. Thank you!


MIT © Nicholas Berlette. All rights reserved.
GitHub · JSR · NPM · Deno · Docs