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

alias-classname

v0.4.1

Published

Create aliases for class names composition

Downloads

8

Readme

alias-classname

Build Test coverage npm version npm license Size

A small utility for defining aliases for CSS classes, resolve those aliases in order to generate a final classes string with conditional joining.

How It Works

The aliasClassName function returned by this module serves as the main interface. It allows you to define aliases and resolve classes using the registered aliases. This utility handles the process of alias resolution and concatenation of classes.

Highlights

  • Under 1 kB minified & gzipped
  • Typed with TypeScript
  • Fully tested
  • Zero dependencies

Installation

To install simply run:

Yarn:

yarn add alias-classname

NPM:

npm install alias-classname

Usage

  1. Import the aliasClassName function from the module:

    import aliasClassName from "alias-classname";
  2. Define your aliases and resolve class names:

    Basic usage:

    const classes = aliasClassName();
    
    const classname = classes("css", "base:my-class", "(base)__modifier");
    // outputs: "css my-class my-class__modifier"

    With default aliases:

    const classes = aliasClassName("base:component", "mod:container");
    
    const classname = classes("(base)", "body:(base)__(mod)");
    // outputs: "component component__container"
    
    const classname2 = classes("(body)--variant");
    // outputs: "component__container--variant"

    Default aliases can also be composed:

    const classes = aliasClassName(
      "base:component",
      "mod:(base)__container",
      "var:(mod)--variant"
    );
    
    const classname = classes("(base)", "(mod)", "(var)");
    // outputs: "component component__container component__container--variant"

    You're not required to use the BEM naming convention. But if you do, it can be really helpful for that use case.

  3. Debugging:

    You can access the aliases and classes stores for debugging purposes:

    const debugInfo = classes.debug();
    console.log(debugInfo.aliases);
    console.log(debugInfo.classes);

Problem

When working on projects with complex CSS class naming conventions, managing them can become really cumbersome. Especially when you need to apply multiple classes to elements, the resulting code can quickly become hard to read and maintain.

Solution

The aliasClassName simplifies CSS classes management by allowing you to define aliases for longer class names. These aliases can then be easily used to generate the final joined class names string. This is particularly useful when you want to:

  • Maintain cleaner and more readable code by abstracting complex class names behind meaningful aliases.
  • Apply consistent and uniform class names throughout your project without repeating lengthy strings.
  • Dynamically switch or toggle between different class configurations by using aliases as references.

Functions and Methods

  • aliasClassName(...aliases: string[]): (classnames: unknown[]) => string

    This is the main function returned by the module. It takes aliases as arguments and returns a function to resolve class names.

    • (...classnames: unknown[]): string

      This is the returned function that takes an array of strings, including aliases, as arguments and returns the resolved final class name string. It concatenates the class names based on the registered aliases.

    • debug(): { aliases: AliasesStore, classes: ClassesStore }

      This method is a property attached to the returned function that allows you to retrieve the current state of the aliases and classes stores for debugging purposes.

Terminology

  • Aliases: Shorter identifiers that can be used in place of longer class names.

  • Class Names: CSS class names or any string that you want to resolve and concatenate.

Limitations and Considerations

  • Aliases should be provided as strings in the format:
    • "alias:class-name".
  • An alias reference should be provided in the format:
    • "(alias)__class-name".
  • An alias must have been registered before it's referenced.
  • A redefinition of an existing alias will override its current value.
  • Circular references or an alias that doesn't exist are not resolved.
    • "a:(a)" will output "(a)".

Contributing

All contributions are very welcome. You can submit any ideas as pull requests or as GitHub issues. If you'd like to improve code, please feel free!