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

dtokens

v0.1.3-beta

Published

Simple design-tokens generator

Downloads

35

Readme

dtokens

Simple design-tokens generator. [beta]

sample

Get Started

  1. Install
npm i -D dtokens
  1. Update package.json file
{
  ...
  "scripts": {
    ...
    "tokengen": "dtokens", // <- add
    ...
  },
  ...
}
  1. Run init command
npm run tokengen init
  1. The command will automatically create the following files at the root of your project directory.
./dtokens.config.ts

./design-tokens /index.ts
                /css.css
                /scss.scss

Usage

CSS in JS

import tokens from '/your-path-to/design-tokens/index'

return (
  <h3 style={{
    fontSize: tokens.fontSize.md,
  }}>
     Header
  </h3>
)

CSS

import "/your-path-to/design-tokens/css.css";

.header {
  font-size: var(--fontSize-md);
}

SCSS

@import "/your-path-to/design-tokens/scss.scss";

.header {
  font-size: $fontSize-md;
}

CSS variable approaches

CSS in JS

import "/your-path-to/design-tokens/css.css";
import tokens from "/your-path-to/design-tokens/index";

return (
  <h3 style={{
    fontSize: tokens.v.fontSize.md, // -> "var(--fontSize-md)"
  }}>
    Header
  </h3>
)

SCSS

@import "/your-path-to/design-tokens/css.css";
@import "/your-path-to/design-tokens/scss.scss";

.header {
  font-size: $v-fontSize-md; // -> var(--fontSize-md)
}

You can use CSS variable as default token value as follows:

defineTokens({
  config: {
    ...
    values: {
      tokensPriority: 'css-var',
      scssPriority: 'css-var',
    }
    ...
  },
  ...
})

Customize Tokens

  1. Change dtokens.config file at the root of your project directory.
// dtokens.config file

import { defineTokens } from "dtokens"

export default defineTokens({
  config: { ... },
  tokens: {
    ...
  },
})
  1. Run tokengen command to regenerate the token files.
npm run tokengen

Configuration Tips

Referencing other values

// dtokens.config file

export default defineTokens({
  tokens: {
    colors: {
      primary: {
        500: '#ff0000',
      }
    },
    text: {
      color: {
        main: '{colors.primary.500}',
      }
    }
  }
})

Token key mapping

// dtokens.config.ts

export default defineTokens({
  config: {
    mapKeys: {
      fontSizes: 'fs',
    }
  },
  tokens: {
    fontSizes: {
      2: '14px',
      3: '16px',
    },
  }
})

Usage

// SCSS
@import "/your-path-to/design-tokens/scss.scss";

.box {
  font-size: $fs-3;
}

Nest Hierarchy Skipping

// dtokens.config file

export default defineTokens({
  tokens: {
    '(colors)': {
      primary: {
        500: '#ff0000',
      }
    },
  }
})

Usage

<div style={{
  color: tokens.primary[500] // instead of `tokens.colors.primary[500]`
}} />

Configure with utilities

// dtokens.config file

import { defineTokens } from 'dtokens'
const { pxToRem, remToPx, scalingFactors, toFontFamily } from 'dtokens/utils'

export default defineTokens({
  tokens: {
    fonts: {
      sans: toFontFamily(['Roboto', 'Helvetica Neue', 'sans-serif']),
      // => "Roboto, 'Helvetica Neue', sans-serif"
    },
    fontSizes: {
      sm: pxToRem(14), // => "0.875rem"
      md: pxToRem(16), // => "1rem"
    },
    sizes: {
      topbarH: remToPx(4), // => "64px"
    },
    spacing: {
      ...scalingFactors([1, 2, 4, 8, 16, 32], {
        scaling: 4, //  variable => pxToRem( variable * 4 )
        unit: 'rem',
      }),
    },
  }
})

Generate color tokens with paletten

// dtokens.config file

import { defineTokens } from 'dtokens'
const { paletten } from 'dtokens/utils'

export default defineTokens({
  tokens: {
    colors: {
      primary: {
        // `paletten` will automatically generate color tokens.
        ...paletten('#ff0000')
      }
    }
  }
})

API

// dtokens.config file

function defineTokens(source: {
  config?: {
    outputs?: { // set output file path. e.g., 'design-tokens/index.ts'
      tsFile?: string
      jsFile?: string
      cssFile?: string
      scssFile?: string
      jsonFile?: string
      jsType?: 'module' | 'require'
    }
    values?: {
      tokensPriority?: 'pure-value' | 'css-var' // default: 'pure-value'
      scssPriority?: 'pure-value' | 'css-var' // default: 'pure-value'
      tokensWithV?: boolean
      scssWithV?: boolean
    }
    cssRules?: {
      prefix?: string // e.g. 'd-'
      separation?: '-' |  '_' | 'auto' | string // default: '-'; 'auto': separation is depend on 'naming'
      naming?: 'unset' | 'kebab' | 'snake' | 'pascal'| 'camel' // default: 'unset'
      decimalPoint?: 'dot' | 'underscore' | 'hyphen' // default: 'dot'
    },
    mapKeys?: {
      spacing?: string
      sizes?: string
      fonts?: string
      fontSizes?: string
      fontWeights?: string
      lineHeights?: string
      letterSpacing?: string
      radii?: string
      shadows?: string
      breakpoints?: string
      colors?: string
      [key: string]: string
    }
  },
  tokens: {
    spacing?: {
      [key: string]: string | Object
    },
    sizes?: {
      [key: string]: string | Object
    },
    fonts?: {
      [key: string]: string | Object
    },
    fontSizes?: {
      [key: string]: string | number | Object
    },
    fontWeights?: {
      [key: string]: string | number | Object
    },
    lineHeights?: {
      [key: string]: string | number | Object
    },
    letterSpacing?: {
      [key: string]: string | number | Object
    },
    radii?: {
      [key: string]: string | number | Object
    },
    shadows?: {
      [key: string]: string | number | Object
    },
    breakpoints?: {
      [key: string]: string | number | Object
    },
    colors?: {
      [key: string]: string | Object
    },
    // - - - -
    // Set any tokens as you like
    [key: string]: {
      [key: string]: string | number | Object
    }
  }
}) 

License

MIT License © otsubocloud