• Stars
    star
    107
  • Rank 323,587 (Top 7 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 5 years ago
  • Updated 12 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Tiny (596B) utility for composing class variants using clsx

onno

Bundle Size Code Coverage Workflow Status License

Tiny (596B) utility for composing class variants using clsx

pnpm add onno

Features

Usage

import { onno } from "onno"

const button = onno({
  variants: {
    size: {
      sm: "h-8 px-1",
      md: "h-10 px-2",
      lg: "h-12 px-3",
    },
    intent: {
      primary: "bg-blue-600 text-white",
      secondary: "bg-gray-200 text-black",
    },
    disabled: "opacity-50",
  },
})

// "h-10 px-2 bg-blue-600 text-white opacity-50"
const classes = button({ size: "md", intent: "primary", disabled: true })

Variants

Define variant names and the classes to be applied to them using the variants config option:

const button = onno({
  variants: {
    // This `boolean` variant is applied when `disabled === true`
    disabled: "access denied", // Classes can be defined as a `string`

    // This `boolean` variant is applied when `hidden === true`
    hidden: ["barely", "visible"], // Classes can also be a `string[]`

    // This `enum` variant is applied when `size === "sm" || "lg"`
    size: {
      sm: ["pretty", "small"], // Here we are using a `string[]` class list
      lg: "really large", // ...and here we are using a `string` class list
    },
  },
})

button() // ""
button({}) // ""
button({ size: "sm" }) // "pretty small"
button({ disabled: true }) // "access denied"
button({ hidden: true, size: "lg" }) // "barely visible really large"

Note that you cannot use className as a variant key since it is reserved for applying additional classes:

const button = onno({
  variants: {
    className: "not allowed", // Error: "className" cannot be used as a variant name
  },
})

Defaults

Default variants can be set using the defaults config option:

const button = onno({
  defaults: {
    hidden: true,
    intent: "secondary",
  },
  variants: {
    hidden: "barely visible",
    intent: {
      primary: "super punchy",
      secondary: "quite bland",
    },
    size: {
      sm: "pretty small",
      lg: "really large",
    },
  },
})

button() // "barely visible quite bland"
button({}) // "barely visible quite bland"
button({ hidden: false }) // "quite bland"
button({ intent: "primary" }) // "barely visible super punchy"
button({ size: "sm" }) // "barely visible quite bland pretty small"

Base Classes

Base classes can be applied using the base config option:

const button = onno({
  base: "solid base", // Can also use a `string[]` class list
  variants: {
    size: {
      sm: "pretty small",
      lg: "really large",
    },
  },
})

button() // "solid base"
button({}) // "solid base"
button({ size: "lg" }) // "solid base really large"

Compound Classes

Apply classes when certain variants are combined using the compounds config option:

const button = onno({
  variants: {
    hidden: "barely visible",
    size: {
      sm: "pretty small",
      md: "kinda normal",
      lg: "really large",
    },
  },
  compounds: [
    {
      size: ["sm", "lg"],
      className: ["compound", "one"], // Applied when `size === "sm" || "lg"`
    },
    {
      size: "md",
      hidden: true,
      className: "compound two", // Applied when `size === "md" && hidden === true`
    },
  ],
})

button() // ""
button({}) // ""
button({ size: "md" }) // "kinda normal"
button({ hidden: true }) // "barely visible"
button({ size: "lg" }) // "really large compound one"
button({ size: "md", hidden: true }) // "barely visible kinda normal compound two"

Additional Classes

Additional classes can be applied using the className option:

const button = onno({
  base: "solid base",
  variants: {
    size: {
      sm: "pretty small",
      lg: "really large",
    },
  },
})

button() // "solid base"
button({ className: "with more" }) // "solid base with more"
button({ className: "with more", size: "sm" }) // "solid base pretty small with more"

Class Composition

Classes are applied in the following order:

  1. base
  2. variants
  3. compounds
  4. className

Under the hood onno uses clsx to build the class list (see clsx docs)

For convenience clsx is exported from onno so you can use it to compose classes:

import { onno, clsx } from "onno"

const button = onno({
  variants: {
    size: {
      sm: "pretty small",
      lg: "really large",
    },
  },
})

clsx("foo", ["bar", { baz: true }], button({ size: "sm" })) // "foo bar baz pretty small"

Note that onno's className option also accepts any clsx.ClassValue so you can do:

import { onno, clsx } from "onno"

const button = onno({
  variants: {
    size: {
      sm: "pretty small",
      lg: "really large",
    },
  },
})

button({ size: "lg", className: ["foo", ["bar"], { baz: true }] }) // "really large foo bar baz"

TypeScript

Use the OnnoProps type to infer variant props from an OnnoFunction

import { onno, type OnnoProps } from "onno"

export const button = onno({
  variants: {
    disabled: "not allowed",
    size: {
      sm: "pretty small",
      lg: "really large",
    },
  },
})

export type ButtonProps = OnnoProps<typeof button>
export type ButtonSizeType = ButtonProps["size"] // "sm" | "lg" | undefined
export type ButtonDisabledType = ButtonProps["disabled"] // boolean | undefined

Note that inferred OnnoProps also include the className option alongside the variants:

export type ButtonClassNameType = ButtonProps["className"] // clsx.ClassValue

By default all variants inferred by OnnoProps are optional. To require one or more variants, pass a union of required variant keys as the second argument to the OnnoProps generic type:

import { onno, type OnnoProps } from "onno"

export const button = onno({
  variants: {
    disabled: "not allowed",
    intent: {
      primary: "super punchy",
      secondary: "quite bland",
    },
    size: {
      sm: "pretty small",
      lg: "really large",
    },
  },
})

// Require both the `intent` and `size` variants
export type ButtonProps = OnnoProps<typeof button, "intent" | "size">

// Error: Property 'intent' is missing in type '{ size: "md" }'
const buttonProps: ButtonProps = { size: "md" }

Tailwind CSS

If you are using the Tailwind CSS VSCode extension, add the following configuration to your workspace .vscode/settings.json file:

{
  "tailwindCSS.experimental.classRegex": [
    ["onno|clsx\\(([^)]*)\\)", "[\"'`]([^\"'`]*)[\"'`]"]
  ]
}

This will enable Tailwind's IntelliSense for both onno and clsx within your project! πŸŽ‰

VSCode Tailwind CSS IntelliSense

License

MIT Β© Matthew Wagerfield

More Repositories

1

parallax

Parallax Engine that reacts to the orientation of a smart device
JavaScript
16,466
star
2

flat-surface-shader

Flat Surface Shader for rendering illuminated triangles
JavaScript
2,466
star
3

cssmixins

CSS3 mixins for STYL, SCSS, SASS & LESS.
513
star
4

nuxt-typescript

TypeScript module for Nuxt
JavaScript
90
star
5

nuxt-stack

Suite of modules and commands for building performant applications with zero configuration
JavaScript
66
star
6

axonometric-projection

2.5D Axonometric Projection Engine
JavaScript
60
star
7

cinnamon

Lean boilerplate for front-end projects
JavaScript
36
star
8

ig-api

Axios wrapper around IG's API
JavaScript
25
star
9

eslint-index

CLI for finding and managing rules in ESLint config files
JavaScript
20
star
10

storybook-tailwind

Storybook with Tailwind 2 JIT and PostCSS 8
JavaScript
19
star
11

vue-fela

Fela plugin for Vue
JavaScript
17
star
12

vue-pwa-installer

Vue interface for installing Progressive Web Apps
JavaScript
14
star
13

cinema4D

Useful resources for Cinema 4D
8
star
14

girder

Powerful utility classes, an evolved mouse, useful display classes, advanced drawing methods, comprehensive video components with plug and play gui, project navigation classes, file loading, third party framework layers, and much, much more!
ActionScript
8
star
15

vue-static-data

Add static data to Vue instances
JavaScript
7
star
16

xrc

React component library written in TypeScript and styled with emotion and onno
TypeScript
7
star
17

nd-physics

Dimension Agnostic Physics Engine
JavaScript
7
star
18

colorgasm

WebAudio Visualiser inspired by Nina Geometrieva's Colorgasm
JavaScript
6
star
19

pinscreen

WebGL Pinscreen
JavaScript
6
star
20

nuxtflix

Nuxt + Netlify + Contentful = Awesome
JavaScript
6
star
21

cursor

Complete list of CSS cursor values (auto, pointer, help, etc.)
5
star
22

inferno-fuse-box

Inferno starter project using FuseBox and TypeScript
JavaScript
2
star
23

playgroup

Collaborative Play using WebRTC
2
star
24

aperture

Depth of Field Calculator
2
star
25

jasmine-custom-matchers

Custom matchers for Jasmine Behaviour Driven Development Framework
JavaScript
2
star
26

danbo

Danbo brought to the web in CSS3D
2
star
27

xrc-framer

XRC FramerX Project & Components
TypeScript
2
star
28

i18next-hmr-hook

i18next HMR React Hook Demo
JavaScript
2
star
29

gulp-spritezero

Gulp plugin for spritezero
JavaScript
1
star
30

vue-tabbing

Tabbing navigation utility for Vue
JavaScript
1
star
31

interface

Reactive User Interface
1
star
32

nuxt-base

Base module for Nuxt.js
JavaScript
1
star
33

next-fetch-from-static

Attempt to statically export pages with dynamic JSON
JavaScript
1
star
34

fela-base

Fela Preset & Rules
TypeScript
1
star
35

framer-boilerplate

Rapid, automated boilerplate project for creating prototypes with Framer
JavaScript
1
star
36

unscene

2D Graphics Library
TypeScript
1
star