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-design-token-function

v1.0.0

Published

A PostCSS plugin to add custom functions for accessing your design tokens.

Downloads

15

Readme

postcss-design-token-function

A PostCSS to add custom functions for accessing your design tokens.

Build status Coverage Status Maintained NPM version Dependency Status Dev Dependency Status Code Climate

Installation

npm install postcss-design-token-function

Usage

In JavaScript, simply pass an object with your design token information along with the name of the function by which you will retrieve that data in your CSS. Values can be simple strings or nested objects, which are useful for cases where you have naturally nested design details (e.g., values for your color palette).

// dependencies
import fs from 'fs';
import postcss from 'postcss';
import designTokenFunction from 'postcss-design-token-function';

// css to be processed
const css = fs.readFileSync('input.css', 'utf8');

// your colors
const colors = {
  white: '#F2F5FF',
  blue: {
    light: '#A2CEFF',
    base: '#5898FF',
  },
};

// process css
var output = postcss()
  .use(designTokenFunction({
    name: 'color',
    data: colors,
  }))
  .process(css)
  .css

And, in CSS, used the color function to reference your color palette. You can use the shade option to reference the nested shades, if provided (omitting a shade will default to using the base key of a nested color):

.foo {
  background: color(white);
  border: 1px solid color(blue, shade: light);
  color: color(blue, base);
}

Which will generate:

.foo {
  background: #F2F5FF;
  border: 1px solid #A2CEFF;
  color: #5898FF;
}

Options

The following is a complete list of the possible options you can pass this plugin:

postcss().use(
  designTokenFunction({
    // the actual function to look for; here, `colour(white)` becomes valid.
    name: 'colour',

    // The design token data to reference in your function calls. Must be an object,
    // optionally with multiple levels of nested objects.
    data: {},

    // the key to look for at any level of nesting when no variation is provided
    // (and one is needed; that is, there is a nested object in your design tokens).
    // By default, this plugin will look for a 'base' key.
    base: 'default',
  })
)

Note that you can generate functions for different design tokens by executing this plugin multiple times with each token's data provided in turn.