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

responsive-recipes

v0.1.2

Published

Create responsive variant styles, powered by Vanilla Extract. Inspired by Stitches, Recipes, and CVA.

Downloads

1,158

Readme

Responsive recipes

Responsive recipes is built on top of Vanilla Extract. It provides variant based styling, including responsive variants, something that was missing in packages like Recipes and cva.

This package assumes you have Vanilla Extract installed and configured.

Installation

npm i responsive-recipes

Getting started

To get started, create a recipe function that will have default conditions for which every responsive variant will get generated. Similar to Sprinkles, it will need an initialCondition for calling singular string values on responsive variants.

import { createRecipe } from 'responsive-recipes';

const recipe = createRecipe({
  defaultConditions: {
    initial: {},
    sm: { '@media': '(min-width: 380px)' },
    lg: { '@media': '(min-width: 1024px)' }
  },
  initialCondition: 'initial'
});

We can use this recipe function to generate a piece of statically extracted CSS, powered by Vanilla Extract. Like regular recipes it can take an optional set of base styles, variants, compoundVariants, defaultVariants.

But responsive-recipes also takes in responsiveVariants and inlineVariants. Every responsive variant or inline variant, by default, will get generated for the conditions provided in defaultConditions.

const stack = recipe({
  base: {
    display: 'flex'
  },
  variants: {
    isFullHeight: {
      true: {
        height: '100%'
      },
      false: {
        height: 'auto'
      }
    }
  },
  responsiveVariants: {
    direction: {
      row: {
        flexDirection: 'row'
      },
      column: {
        flexDirection: 'column'
      }
    }
  },
  inlineVariants: {
    width: { property: 'width' },
    height: { property: 'height' }
  },
  compoundVariants: {
    variants: {
      direction: 'row',
      isFullHeight: true
    },
    style: {
      backgroundColor: 'green'
    }
  },
  defaultVariants: {
    isFullHeight: false,
    direction: 'row'
  }
});

Runtime

In runtime code you can use the recipe to retrieve classNames:

const { className } = stack({
  isFullHeight: true,
  direction: { initial: 'column', lg: 'row' },
  width: { initial: '100%', sm: '50%', lg: '25%' }
});

Responsive variants

Responsive variants get generated for each set condition in your defaultConditions configuration.

The initialCondition is used as a fallback for responsive variants

Whenever you call a responsive variant with a singular value it will select the className that belongs to the media query defined in the initialCondition.

const { className } = stack({ direction: 'row' });
// Is equal to stack({ direction: { initial: 'row' }});

Custom conditions

If you need to override the defaultConditions set in createRecipe on a per recipe basis, you can add a conditions property with an initialCondition to your recipe. Responsive variants in this recipe will only be generated for these conditions.

const hidden = recipe({
  conditions: {
    initial: {},
    sm: { '@media': '(min-width: 380px)' },
    smMax: { '@media': '(max-width: 379px)' },
    lg: { '@media': '(min-width: 1024px)' },
    lgMax: { '@media': '(max-width: 1023px)' }
  },
  initialCondition: 'initial',
  responsiveVariants: {
    hide: {
      true: {
        display: 'none'
      },
      false: {
        display: 'block'
      }
    }
  }
});

Inline variants

Inline variants are special variants that allow any value and are coupled to a CSS property. These variants are very powerful, because they allow you to define any value, without having to explicitly define a variant for every single value. These are useful for CSS properties like width, top, left, etc. Inline variants are responsive by default. Inline variants also work with compoundVariants for granular control, and they can also have a defaultVariant.

Whenever you define inlineVariants your recipe will also return a style object that needs to added onto your DOM element (or component) in order to apply the variant:

const { className, style } = stack({ width: { initial: '100%', sm: '50%' } });

<div className={className} style={style}>
  Stack
</div>;

Compound variants

A compound variant is a variant that kicks in when a combination of variants, responsive variants and inline variants are active. These account for conditions as well. If we take our initial stack recipe:

const className = stack({
  isFullHeight: true,
  direction: { initial: 'column', lg: 'row' }
});

isFullHeight is a regular variant. Regular variants are valid for all conditions. That means that the compoundVariant combination will kick in from the lg breakpoint and the backgroundColor will become green.

Default variants

Variants will fall back to the default variants if they're not defined in the runtime.

Type helpers

GetVariants is a type helper that you can use to infer the type of variants that you can use in interfaces for your components.

import { GetVariants } from 'responsive-recipes';

type Variants = GetVariants<typeof stack>;

Get base className

Often, when using globalStyle in Vanilla Extract, you want to target the base className. You can retrieve it using the classNames.base getter:

const baseClassName = stack.classNames.base;

Variant definitions

For documentation purposes it might come in handy to get a list of all variants and their definitions. You can do this by calling variantDefinitions getters:

const variants = stack.variantDefinitions.variants;
const responsiveVariants = stack.variantDefinitions.responsiveVariants;
const inlineVariants = stack.variantDefinitions.inlineVariants;

A variant definition object has the following shape:

type VariantDefinition = {
  values: string[];
  defaultValue: string | undefined;
};