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

@siriwatknp/zero-runtime

v0.0.4

Published

### Variant styles

Downloads

18

Readme

Features

Variant styles

To style component based on finite values (e.g. size, color), use variants styles.

// Ex. 1
styled('button')({
  variants: [
    {
      // matcher as a plain object
      props: {
        size: 'small',
      },

      // CSS as an object
      style: {
        fontSize: 12,
        '&:hover': {
          transform: 'scale(1.1)',
        }
      }
    }
  ],
})

// Ex. 2, accessing to `theme.vars`
styled('button')(({ theme }) => ({
  variants: [
    {
      props: {...},
      style: {
        fontSize: theme.vars.fontSize,
      }
    }
  ],
}))

The props of each variant supports callback syntax to create complex matcher with internal state (ownerState).

// Ex. 1
styled('button')({
  variants: [
    {
      // matcher as a plain object
      props: (ownerState) => !ownerState.disabled,

      // CSS as an object
      style: {
        background: 'tomato',
      }
    }
  ],
})

Each variant style will be created at build time and injected into the component via className when the condition matched.

.hashed-component_variant-1 {
  font-size: 12px;
}
.hashed-component_variant-1:hover {
  transform: scale(1.1);
}

.hashed-component_variant-2 {
  background: tomato;
}
<button className={clsx(ownerState.size === 'small' && 'hashed-component_variant-1', !ownerState.disabled && 'hashed-component_variant-2')}>

::: ⚠️ Warning

ownerState cannot be accessed in the root callback.

// ❌ Error
styled('button')(({ ownerState }) => ({
  variants: [
    {
      props: {...},
      style: {
        fontSize: ownerState.fontSize,
      }
    }
  ],
}))

:::

Dynamic styles

To style component based on infinite values (e.g. width, height, ratio), pass callback as a value to the CSS property.

// Ex. 1
styled('button')({
  width: ({ ownerState }) => ownerState.width,
  height: ({ ownerState }) => {
    if (ownerState.height === 'auto') {
      return 300
    }
    return typeof ownerState.height === 'number' ? `${ownerState.height}px` : 0
  },
})

The dynamic styles will be transpiled into vars object and inject as a prop to the component,

.hashed-component {
  width: var(--hashed-var1);
  height: var(--hashed-var2);
}
// internal implementation
<button className="hashed-component" style={{
  '--hashed-var1': ({ ownerState }) => ownerState.width,
  '--hashed-var2': ({ ownerState }) => {
    if (ownerState.height === 'auto') {
      return 300
    }
    return typeof ownerState.height === 'number' ? `${ownerState.height}px` : 0
  },
}}>

::: ⚠️ Warning

theme cannot be accessed in dynamic styles.

// ❌ Error
styled('button')(({ theme }) => ({
  width: ({ ownerState }) => theme.vars.width,
}))

:::