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

vanilla-extract-constrained

v0.0.12

Published

A utility for enhancing the vanilla extract primitives with a constrained API.

Downloads

22

Readme

vanilla-extract-constrained

A utility for enhancing the vanilla extract primitives with a constrained API.

Provides the same type safety and conventions of @vanilla-extract/sprinkles (and with a similar API), but with the flexibility to apply it to all compatible vanilla extract primitives, and even outside of vanilla extract.

Try it on StackBlitz

Usage

Here's how you define your constraints:

// constrained.css.ts
import { createStyleResolver, all, multi } from "vanilla-extract-constrained";

export type ConstrainedStyle = Parameters<typeof resolveStyle>[0];

export const resolveStyle = createStyleResolver({
  conditions: {
    default: {},
    hover: { selectors: `&:hover` },
  },
  defaultCondition: "default",
  properties: {
    color: {
      success: "green",
      failure: "red",
    },
    background: ["yellow", "blue"]
    fontSize: all(), // Infers and accepts all default CSS values for this property
    border: multi({ // Allows specifying aliases that will define multiple properties at once
      standard: {
        borderWidth: "1px",
        borderStyle: "solid",
        borderColor: "black",
      },
      thick: {
        borderWidth: "2px",
        borderStyle: "solid",
        borderColor: "black",
      },
    }),
    transition  (...list: Transition[]) {
      // Function properties allow you to safely deal with composite and dynamic values
      // The function should return a valid css value, but may accept ANY argument types.
      return list.map((parts) => parts.join(" ")).join(", ")
    }
  },
  shorthands: {
    bg: ["background"],
  },
});

type Transition = [
  prop: string,
  time: "1s" | "2s",
  easing: "ease-out" | "ease-in"
]

Using this resolveStyle function you can now define new stricter versions of the vanilla extract primitives:

// design-tokens.ts
import * as vanilla from "@vanilla-extract/css";
import { resolveStyle } from "./constrained.css.ts";

// The following functions behave like their vanilla extract counterparts,
// but ensures only your constrained property values are accepted as input.

export const style = (cStyle: ConstrainedStyle) =>
  vanilla.style(resolveStyle(cStyle));

export const globalStyle = (selector: string, cStyle: ConstrainedStyle) =>
  vanilla.globalStyle(selector, resolveStyle(cStyle));

export const keyframes = (keyframedStyles: Record<string, ConstrainedStyle>) =>
  vanilla.keyframes(
    Object.fromEntries(
      Object.entries(keyframedStyles).map(([key, cStyle]) => [
        key,
        resolveAtoms(cStyle),
      ]),
    ),
  );

Use your new vanilla extract functions as usual, but now constrained:

// my-styles.css.ts
import { style, globalStyle, keyframes } from "./design-tokens"

export foo = style({
  background: "invalid", // Errors, must be yellow or blue
  color: "success", // Aliases work just like in sprinkles
  bg: "yellow", // As does shorthands
  fontSize: { // As does conditions
    default: 14,
    hover: 18
  }
  // This is new and unique to vanilla-extract-constrained
  // Your own custom functions parameters type is what decides goes here
  transition: [
    ["font-size", "1s", "ease-out"] // This must match the Transition type (see above)
  ]
});


// The same constraints apply everywhere you used your style resolver!
// (props omitted for brevity)
const bar = globalStyle(...);

const baz = keyframes(...);

Usage at runtime

You can even use it to create constrained and enhanced inline styles:

import { resolveStyle } from "./constrained.css.ts";

// This is a React example, but you could use any framework.
<div
  style={resolveStyle({
    color: "success",
  })}
>
  I am green!
</div>;

Using vanilla-extract-constrained at runtime is opt-in. If you do, it will embed minimal javascript to your bundle to allow inline resolution of your constraints. However if you only use resolveStyle inside .css.ts files, no extra javascript gets bundled.

Constraining recipes

Since @vanilla-extract/recipes isn't as straight forward to constrain as the other vanilla extract primitives out of the box, I've created another library to help with this: vanilla-extract-recipe-factory. With it you can easily create a constrained recipe function:

import { createRecipeFactory } from "vanilla-extract-recipe-factory";
import { resolveStyle } from "./constrained.css.ts";

export const recipe = createRecipeFactory(resolveStyle);

Notes on atomic CSS

vanilla-extract-constrained does not generate atomic CSS like @vanilla-extract/sprinkles does, and instead generates potentially duplicated css across the css classes that gets generated. However, a lot of apps should be okay with this performance trade-off, and some likely even benefit from it, since this approach means you generate less verbose HTML due to the large amount of css class names that atomic css yields.