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

postcss-theme-ui

v0.10.0

Published

PostCSS plugin for accessing Theme UI variables

Downloads

147

Readme

NPM Version Build Status Support Chat

PostCSS Theme UI lets you access Theme UI variables in your CSS.

Table of Contents

Setup

Add PostCSS Theme UI to your project:

npm install postcss-theme-ui --save-dev

Use PostCSS Theme UI to process your CSS:

const postcssThemeUI = require("postcss-theme-ui");

postcssThemeUI.process(YOUR_CSS /*, processOptions, theme */);

Or use it as a PostCSS plugin:

const postcss = require("postcss");
const postcssThemeUI = require("postcss-theme-ui");
const theme = require("./theme");

postcss([postcssThemeUI(theme)]).process(YOUR_CSS /*, processOptions */);

PostCSS Theme UI runs in all Node environments, with special instructions for:

| Node | PostCSS CLI | Webpack | Create React App | Gulp | Grunt | | ----------------------- | ------------------------------------- | ----------------------------- | ----------------------------------------------- | ----------------------- | ------------------------- |

Options

PostCSS Theme UI accepts an object formatted based on the System UI Theme Specification. See sample theme object.

Overview

Given the following theme config:

{
  breakpoints: ["40em", "52em", "64em"],
  colors: { text: '#111', primary: '#06c', error: '#c30' },
	fonts: {
		sans: '"IBM Plex Sans", sans-serif',
		serif: '"IBM Plex Serif", serif'
	},
  fontSizes: [12, 14, 16, 20, 24, 32, 48, 64, 72],
  sizes: ["initial", "48rem", "64rem"],
  space: [0, 4, 8, 16, 32, 64, 128, 256, 512]
}

PostCSS Theme UI maps CSS properties to the appropriate theme field. You can view the full prop mapping here. It supports negative values (for certain properties) and conversion of unitless integers to px.

.example {
  color: primary; /* colors.primary */
  font-size: 6; /* fontSizes[6] */
  margin: 0 auto -1; /* space[0] auto -space[1] */
  padding: 0 3 3px; /* space[0] space[3] 3px */
}

/* becomes */

.example {
  color: #06c;
  font-size: 48px;
  margin: 0 auto -4px;
  padding: 0 16px 3px;
}

Responsive Values

It also provides support for array values that map to your breakpoints for convenient responsive styling. You can call the theme function inside arrays, or use null to skip breakpoints. See Caveats for some limitations.

.card {
  border: th(colors.primary) [1, 2]; /* colors.primary [solid 1px, solid 2px] */
  color: primary; /* colors.primary */
  max-width: [0, null, 2]; /* [sizes], `null` is used to skip breakpoints */
  padding: [1, 2]; /* [space] */
}

/* becomes */

.card {
  border: #07c solid 1px;
  color: #07c;
  max-width: initial;
  padding: 4px;
}
@media screen and (min-width: 40em) {
  .card {
    border: #07c solid 2px;
    padding: 8px;
  }
}
@media screen and (min-width: 52em) {
  .card {
    max-width: 64rem;
  }
}

Custom Design Tokens

If you have design tokens currently not on the Theme UI spec, you can access them via the theme() or th() function.

Say, you want to add a gradients field to your tokens:

// theme config
{
  gradients: [
    "linear-gradient(to right, #DD2476, #FF512F)",
    "linear-gradient(to right, #FFF, #6DD5FA, #2980B9)"
  ];
}

Use them by calling the theme() or th() CSS functions.

.awesome-cta {
  background: th(gradients.0);
}

/* becomes */

.awesome-cta {
  background: linear-gradient(to right, #dd2476, #ff512f);
}

Note: Since we're using PostCSS, we can conveniently plug autoprefixer in our toolchain as well.

Caveats

  1. Short hand CSS properties such as font, background, and border will only map to one theme field as defined here. However, You can still access theme variables via the theme function.
.btn {
  border: theme(colors.primary) 1; /* colors.primary borders[1] */
  font: sans theme(fontSizes.1); /* fonts.sans fontSizes[1] */
}

/* becomes */

.btn {
  border: #07c solid 1px;
  font: "IBM Plex Sans", sans-serif 1rem;
}
  1. Responsive Values cannot be nested and can only be balanced when used more than once per property. For example, the following won't work at the moment. 😉
.unsupported {
  margin: [0, 1] [0, 1, 2];
  padding: [0, [1, 2]];
}

.fixed {
  margin: [0, 1, 1] [0, 1, 2];
  padding: 0 [1, 2];
}

.equivalent {
  margin-top: [0, 1];
  margin-right: [0, 1, 2];
  margin-bottom: [0, 1];
  margin-left: [0, 1, 2];
  padding: 0 [1, 2];
}
  1. Editor syntax highlighting is still a work in progress.