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

v1.0.0-alpha.5

Published

References design tokens in CSS intelligently based on the current property and selector rules

Downloads

25

Readme

PostCSS Magic Token

NPM Version Build Status Support Chat

A function for referencing design tokens and CSS variables intelligently based on the current property and the current selector.

Usage

To reference the primary color for all h2 elements you would write.

h2 {
    color: tok(--primary);
}

This equals the same as writing...

h2 {
    color: var(--h2-color-primary, var(--heading-color-primary, var(--color-primary, var(--color))));
}

In this example, first it looks for a token called --h2-color-primary, if this is not available it then looks to see if there is a token for --heading-color-primary then --color-primary, and then finally as a fallback --color.

When a specific variant of a token is not available, it checks for the next available variant. Below are some examples (some variations have been removed for simplicity).

:root {
    --component-element-property-keyword: value;
    --component-property-keyword: value;
    --element-property-keyword: value;
    --property-variant-keyword: value;
    --property-keyword: value;
    --property: value;
}

Examples

Default values

.default {
    border: tok();
}

Becomes

/* e.g. --border: 1px solid red; */
.default {
    border: var(--border);
}

Keyword values

.keyword {
    color: tok(--primary);
}

Becomes

/* e.g. --color-primary: red;
        --color: black; */
.keyword {
    color: var(--color-primary, var(--color));
}

Element specific

h2 {
    font-size: tok();
}

Becomes

/* e.g. --h2-font-size: 2.5em;
        --font-size: 1em; */
h2 {
    font-size: var(--h2-font-size, var(--font-size));
}

Component specific

Indicate a component using either of the following formats .Component, Component, com-ponent.

.Component {
    color: tok(--primary);
}

Becomes

/* e.g. --component-color-primary: purple;
        --color-primary: blue;
        --color: black; */
.Component {
    color: var(--component-color-primary, var(--color-primary, var(--color)));
}

Property variants

Some properties can include variants. For example margin and padding include variants for inline and block spacing.

.variants {
    padding: tok(--small);
}

Becomes

/* e.g. --padding-block-small: 1em;
        --padding-inline-small: 0.5em;
        --padding-small: 1em; */
.variants  {
    padding: var(--padding-block-small, var(--padding-small, var(--padding)))
        var(--padding-inline-small, var(--padding-small, var(--padding)))
        var(--padding-block-small, var(--padding-small, var(--padding)))
        var(--padding-inline-small, var(--padding-small, var(--padding)));
}

Usage

Add PostCSS Magic Token to your project:

npm install postcss-magic-token --save-dev

Use PostCSS Magic Token to process your CSS:

const postcssMagicToken = require('postcss-magic-token');

postcssMagicToken.process(YOUR_CSS /*, processOptions, pluginOptions */);

Or use it as a PostCSS plugin:

const postcss = require('postcss');
const postcssMagicToken = require('postcss-magic-token');

postcss([
    postcssMagicToken(/* pluginOptions */)
]).process(YOUR_CSS /*, processOptions */);