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

@cheesebit/classy

v1.1.2

Published

Apply CSS classes or conditional CSS properties based on props more easily.

Downloads

2

Readme

classy

This library helps you to handle assigning style that relies on multiple combinations of props or conditionals.

Coverage Status npm package

What problem do we want to solve here?

Let's suppose you have some good old conditional classes to be applied to an element:

<Button className={`my-button ${disabled ? 'is-disabled' : ''}`}>My Cool Button</Button>

You could write:

import { classy } from '@cheesebit/classy';

<Button
  className={classy('my-button', {
    'is-disabled': disabled,
  })}
>
  My Cool Button
</Button>;

Or, let's say you have a React component with props such as colorScheme, variant, validationStatus, disabled, and so on.

If you dare to style your component conditionally according to these props, you would have to write something like:

  import clsx from 'clsx'

  const { colorScheme, variant, validationStatus, disabled } = props

  <Button
    className={clsx({
      'some class combination a': colorScheme == 'dark' && variant == 'primary',
      'some class combination b': (colorScheme == 'light' || colorScheme == 'dark') && variant == 'secondary' && validationStatus == 'valid',
      'some class combination c': colorScheme == 'dark' || variant == 'terciary',
      'some class combination d': !disabled
    })}
  >
    My Cool Button
  </Button>

Well, what if you could write something like:

  import useClassy from '@cheesebit/classy'

  const { prop, classy } = useClassy(props)
  const { disabled } = props

  <Button
    className={classy({
      'some class combination a': prop({ colorScheme:'dark', variant == 'primary' }),
      'some class combination b': prop({ colorScheme: ['light', 'dark'], variant: 'secondary', validationStatus: 'valid' }),
      'some class combination c': prop([{ colorScheme: 'dark' }, { variant: 'terciary' }],
      'some class combination d': !disabled, // or prop({ disabled: false })
    })}
  >
    My Cool Button
  </Button>

Cool, huh?!

Or if you were using some CSS-in-JS library like styled-components, you would have something like:

import styled from 'styled-components';

const Button = styled.button`
  background: ${({ variant, scheme }) => ({
    '$button-primary-background': variant == 'primary',
    '$button-secondary-background': variant == 'secondary' && scheme == 'light',
    '$button-secondary-dark-background': variant == 'secondary' && scheme == 'dark',
    '$button-terciary-background': variant == 'terciary',
  })};

  border-color: ${({ variant, scheme }) => ({
    '$button-primary-border-color': variant == 'primary',
    '$button-secondary-border-color': variant == 'secondary' && scheme == 'light',
    '$button-secondary-dark-border-color': variant == 'secondary' && scheme == 'dark',
    '$button-terciary-border-color': variant == 'terciary',
  })};

  color: ${({ variant, scheme }) => ({
    '$button-primary-color': variant == 'primary',
    '$button-secondary-color': variant == 'secondary' && scheme == 'light',
    '$button-secondary-dark-color': variant == 'secondary' && scheme == 'dark',
    '$button-terciary-color': variant == 'terciary',
  })};
`;

Well, what about something simpler like:

import { classier, prop } from '@cheesebit/classy';

const Button = styled.button`
  background: ${classier({
    '$button-primary-background': prop({ variant: 'primary' }),
    '$button-secondary-background': prop({ variant: 'secondary', scheme: 'light' }),
    '$button-secondary-dark-background': prop({ variant: 'secondary', scheme: 'dark' }),
    '$button-terciary-background': prop({ variant: 'terciary' }),
  })};

  border-color: ${classier({
    '$button-primary-border-color': prop({ variant: 'primary' }),
    '$button-secondary-border-color': prop({ variant: 'secondary', scheme: 'light' }),
    '$button-secondary-dark-border-color': prop({ variant: 'secondary', scheme: 'dark' }),
    '$button-terciary-border-color': prop({ variant: 'terciary' }),
  })};

  color: ${classier({
    '$button-primary-color': prop({ variant: 'primary' }),
    '$button-secondary-color': prop({ variant: 'secondary', scheme: 'light' }),
    '$button-secondary-dark-color': prop({ variant: 'secondary', scheme: 'dark' }),
    '$button-terciary-color': prop({ variant: 'terciary' }),
  })};
`;

classy gives you more power to represent conditionals and helps you declutter your styling, making it more purposeful.