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

@kurasu/variants

v0.2.2

Published

A package for generating classes based on the provided variants.

Downloads

1

Readme

Kurasu (クラス) - Variants

Introduction

Kurasu Variants is a simple and powerful library tailored for managing class-based variants in your JavaScript or TypeScript projects. It allows you to define and manipulate CSS class names dynamically based on various conditions, making it easier to handle styling variations in a clean and maintainable way.

Key Features

  • 🛠️ Class Variants Management: Define variants for different styling properties and dynamically generate class name based on previously provided classes.
  • 🔧 Default Variants: Set default values for your variants to ensure consistent styling across your components
  • 🎨 Compound Variants: Create compound variants that combine multiple variant conditions to apply specific class names.
  • 🧹 Clean Output: Ensures that duplicate class names are removed, providing clean and efficient class strings.
  • 🏷️ Class Handling: Includes clsx-like utility for handling class strings effortlessly.
  • 🧩 Type Safety: Built with TypeScript to ensure type safety and reduce errors.
  • Lightweight: Minimal footprint, ensuring your project stays lean and fast.

Getting started

Installation

To use Kurasu Variants in your project, you can install it as a dependency with npm, yarn, pnpm or bun:

bun add -D @kurasu/variants

Note: Use a runtime of your choice

Example Usage

example.tsx

import { Variants, type VariantsType  } from "@kurasu/variants";

const variants = new Variants({
  base: 'base-class',
  variants: {
    size: {
      small: 'small-size',
      large: 'large-size'
    },
    color: {
      light: 'light-color',
      dark: 'dark-color'
    }
  },
  default: {
    size: 'small',
    color: 'light'
  },
  compoundVariants: {
    ({ size, color }) => {
      if (size === 'large' && color === 'dark') {
        return 'large-dark-class'
      }

      return ''
    }
  }
})

type Size = keyof VariantType<typeof variants.getVariants>['size']
type Color = keyof VariantType<typeof variants.getVariants>['color']

// In this example a JSX component
export const Example = (
  { size, color }: { size: Size, color: Color }
) => {
  return (
    <div className={variants.forgeClasses({ size, color })} />
  )
}

main.ts

import { Example } from "./example";

const Main = () => {
  return <Example size="large" color="dark" />;
};

// Class output of Example: "large-size dark-color large-dark-class"

API

Variants Class

new Variants(config: {
  base: string;
  variants: VariantsMap;
  default: DefaultMap<VariantsMap>;
  compoundVariants?: CompoundVariant<VariantsMap>[];
})
  • base: a base class string that will always be included.
  • variants: an object defining the different variant options.
  • default: an object defining the default values for the variants.
  • compoundVariants (optional): an array of functions that return class names based on specific variant conditions.

VariantsMap

Defines the structure of the variants object.

type VariantsMap = {
  [key: string]: { [key: string]: string };
};

DefaultMap

Maps each variant key to one of its possible values, setting default values for the variants.

type DefaultMap<V extends VariantsMap> = {
  [K in keyof V]: keyof V[K];
};

VariantOptions

Defines the optional variant values passed to the forgeClasses method.

type VariantOptions<V extends VariantsMap> = {
  [K in keyof V]?: keyof V[K];
};

VariantsType

Extracts the type of the variants object returned by the getVariants method in the Variants class.

export type VariantsType<T> = T extends () => infer R ? R : never;

CompoundVariant

A function type that takes variant options and returns a class name string or undefined.

type CompoundVariant<V extends VariantsMap> = (options: {
  [K in keyof V]: keyof V[K];
}) => string | undefined;

Methods

forgeClasses

Generates the class string based on the provided variant options, removing any duplicate class names.

forgeClasses(options?: VariantOptions<VariantsMap>): string

Utility Functions

cn

A utility function to concatenate class names, ensuring duplicates are removed.

export declare function cn(...inputs: ClassValue[]): string;

Acknowledgements

Kurasu Variants is heavily inspired by libraries such as Stitches, Tailwind-Variants and CVA.

Main difference between this library and above is that Kurasu Variants is created in OOP and supports custom functions as compoundVariants.

In the future I plan on adding more features that would expand the experience as well as creating functional API for people that don't really like OOP.

License

Kurasu Variants is licensed under the MIT License.