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

@fashionable/tailwind-plugin-rvars

v0.2.0

Published

utility class to generate responsive css variables

Downloads

2

Readme

Responsive Variables Tailwind Plugin

A Tailwind utility for declaring css custom properties that change across breakpoints

Motivation

Tailwind classes are a great way to build UI components in popular frontend frameworks such as Vue and React. However, often times we want a prop in one of those components to govern some styles, which take the form of Tailwind class lists. Writing out all of the possible classes can be a chore. It gets worse when you want the prop to be dynamic across breakpoints (i.e., accepting { sm: '1rem', lg: '2rem' }) as the value of a prop). In responsive scenarios, you need to list out every value at every breakpoint in order for the final CSS to have everything you need. @fashionable/tailwind-plugin-rvars helps here: use the rvars utility to create responsive css properties that match your Tailwind breakpoints, and remap them to css properties on the fly.

Installation

Install @fashionable/tailwind-plugin-rvars with your favorite package manager:

# with pnpm
pnpm add @fashionable/tailwind-plugin-rvars

# or yarn
yarn add @fashionable/tailwind-plugin-rvars

# or npm
npm install @fashionable/tailwind-plugin-rvars

Then add it to your tailwind.config:

// tailwind.config.cjs

const rvars = require('@fashionable/tailwind-plugin-rvars')

/** @type {import('tailwindcss').Config} */
module.exports = {
  content: ['./**/*.html'],
  theme: {
    extend: {},
  },
  plugins: [rvars({ orderedBreakpoints: ['sm', 'md', 'lg', 'xl'] })],
}

Configuration

The plugin requires an options object to be passed in when adding it to your Tailwind configuration. This object has two options:

orderedBreakpoints

  • Type: string[]
  • Required: true

The orderedBreakpoints array should be the list of breakpoints this plugin should consider for its properties, from smallest to largest.

There are a variety of ways of specifying screens in Tailwind. This plugin makes no attempt to parse pixel values from your config, and instead relies on this array to know which breakpoints to look at.

This plugin only supports min-width breakpoints, and will throw an error upon initialization if a Tailwind breakpoint with a raw or max key is found.

// tailwind.config.cjs

const rvars = require('@fashionable/tailwind-plugin-rvars')

module.exports = {
  // ...
  plugins: [rvars({ orderedBreakpoints: ['sm', 'md', 'lg', 'xl'] })],
}

baseBreakpointName

  • Type: string
  • Required: false
  • Default: 'xs'

The baseBreakpointName is the suffix that will be added to the variable that governs the responsive prop before the first breakpoint applies. In other words, this is the "base case" of the reponsive value.

// tailwind.config.cjs

const rvars = require('@fashionable/tailwind-plugin-rvars')

module.exports = {
  // ...
  plugins: [
    rvars({
      orderedBreakpoints: ['sm', 'md', 'lg', 'xl'],
      baseBreakpointName: 'xs',
    }),
  ],
}

Usage

To declare a responsive variable, use the rvars utility in your Tailwind content. Note that this utility can only accept arbitrary values at the moment, so you'll need to use the [name] syntax:

<div class="rvar-[spacing]">
  <!-- ... -->
</div>

In the above example, there is now a --spacing custom property, as well as custom properties for each breakpoint configured:

  • --spacing-xs ('xs' is the baseBreakpointName)
  • --spacing-sm
  • --spacing-md
  • --spacing-lg

This works well when using the --spacing variable in other utilities with the same arbitrary value syntax:

<!-- A simple <Stack> component, perhaps? -->
<div
  class="flex flex-col rvar-[spacing] gap-[var(--spacing)]"
  style="--spacing-xs: 1rem; --spacing-md: 2rem; --spacing-xl: 3rem;"
>
  <!-- ... -->
</div>

Default Values

By default, no value is set as the default value in the absence of a base variable being assigned. You can customize this by adding the desired default value after a comma in the utility name:

<div class="rvar-[spacing,1rem]">
  <!-- ... -->
</div>

In the above example, if no --spacing-* variables are set, the value of --spacing will be 1rem.