• Stars
    star
    159
  • Rank 235,916 (Top 5 %)
  • Language
    JavaScript
  • License
    ISC License
  • Created almost 4 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Generate accessible UI colors from your base color tokens.

a11y-color-tokens

Generate accessible complementary text or UI colors as Sass variables and/or CSS custom properties from your base color tokens.

Why do I need this?

While many tools are available to check contrast, but efficiently picking an accessible palette can be time-consuming and frustrating. As someone with way too many side projects, I'll say that color contrast is always something that slows down my workflow. In fact, I built this precisely to speed up my own process!

Additionally, everyone benefits from documentation about color token contrast to ensure tokens are used accessibly.

a11y-color-tokens lets you focus on just selecting the base colors while taking care of generating contrast-safe complementary tones to ensure you meet this important success criterion. A unique feature of this project is that it scales the original color value for a more pleasing visual contrast vs only returning either white or black. (Don't worry - you're able to override the contrast color if needed!)

πŸ’‘ "Tokens" comes from the design system world terminology of "design tokens" which you can learn more about from the original creator, Jina Anne.

What's in the box

Example output:

// `primary` name and value provided in your tokens
$color-primary: rebeccapurple !default;
// `on-primary` name and value generated by a11y-color-tokens
// and guaranteed to have at least 4.5 contrast with `primary`
$color-on-primary: #ceb3e9 !default;
// Also includes a list based on your provided color names
$base-color-tokens: ("primary", "secondary");

The default options generate individual Sass variables, as well as a map of those variables and a mixin that contains the palette as CSS custom properties, ready for you to drop into :root or another location of your choice.

Sass variables and the map include the !default flag as an additional way to extend, scale, and share your tokens.

View the sample default output >

Alternatively, pass "css" as the tokenOutputFormat to only output CSS custom properties within the :root selector.

Additionally, an optional Markdown document is generated with contrast cross-compatibility between all available color tokens.

Review an example of using the generated Sass assets >

Usage

Install a11y-color-tokens into any project using:

npm install a11y-color-tokens --save-dev

You can then add it to your scripts or call it directly from the command line, but first, you must prepare a color tokens file.

Create Color Tokens File

Before the script will work, you will need to prepare your color tokens as a module that exports the tokens array.

The expected format is as follows:

// Example color-tokens.js
module.exports = [
  {
    /*
     * `name` - Required
     * Any string, will be used for color reference
     */
    name: "primary",
    /*
     * `color` - Required
     * Any valid CSS color value
     */
    color: "rgb(56, 84, 230)",
    /*
     * `onColor` - Optional
     * enum: undefined | "[css color value]" | false
     *
     * If undefined, will be generated as relative tone of `color`
     * that meets contrast according to `ratioKey`
     *
     * If a color value provided, will still be checked for contrast
     * and a warning comment added if it doesn't pass
     *
     * Set to `false` to omit generation
     */
    /*
     * `ratioKey` - Optional
     * enum: undefined | "small" (default) | "large"
     *
     * Corresponds to mimimum contrast for either normal text ("small" = 4.5)
     * or large text/user interface components ("large" = 3)
     */
  },
];

View color-tokens.js in the package repo for more full example.

Recommended Setup

Add as a standalone script, and then call prior to your build and start commands to ensure tokens are always fresh.

At minimum, be sure to pass an existing outputDirPath (default: "sass") and point colorTokensPath (default: "color-tokens.js") to your tokens file.

"scripts": {
  "color-tokens": "a11y-color-tokens --outputDirPath='src/sass' --colorTokensPath='_theme/color-tokens.js'",
  "start": "npm-run-all color-tokens [your other scripts]",
  "build": "npm-run-all color-tokens [your other scripts]"
},

Sass processing is not included, you must add that separately. This package is a great companion to my 11ty-sass-skeleton template which is a barebones Eleventy static site.

Config Options

Option Type Default
outputDirPath string "sass"
outputFilename string "_color-tokens.scss"
colorTokensPath string "color-tokens.js"
tokenOutputFormat enum: "sass" | "css" "sass"
sassOutputName string "color-tokens"
tokenPrefix enum: string | boolean "color-"
compatibilityDocs boolean true
compatibilityDocsPath string {outputDirPath}
compatibilityDocsTitle string "Color Token Contrast"
includeCustomProperties boolean true
customPropertiesFormat enum: "mixin" | "root" "mixin"

To set a boolean option to false, format the option as --no-[optionName]

Config Examples

Vanilla CSS output of custom properties

As noted in the intro, the default output is Sass based.

Flip this to output all generated tokens as CSS custom properties within :root with the following:

a11y-color-tokens --tokenOutputFormat='css' --outputFilename='theme-colors.css'

For the CSS-only output, you will need to update outputFilename since the default creates this output as a Sass (.scss) file.

Direct :root Sass output of custom properties

The default creates a mixin containing the CSS custom properties version of the tokens. If you'd rather output them in :root directly, set the following:

a11y-color-tokens --customPropertiesFormat='root'

Update Sass map and mixin name

This is handled by updating the following:

a11y-color-tokens --sassOutputName='colors'

Update or remove the generated token prefix

Change the prefix of color- by setting a new value, or use --no-tokenPrefix to remove token prefixing.

a11y-color-tokens --tokenPrefix='theme-'

Prevent CSS custom properties output

This is handled with includeCustomProperties and can be removed with:

a11y-color-tokens --no-includeCustomProperties

Remove the _color-token-contrast.md documentation

This is handled with compatibilityDocs and can be removed with:

a11y-color-tokens --no-compatibilityDocs

Change output location of _color-token-contrast.md

The default places the docs in the same directory defined for outputDirPath.

To change, supply a new file path (the _ prefix will be removed from the Markdown filename as well):

a11y-color-tokens --compatibilityDocsPath='docs'

Colophon and Credits

Hi! I'm Stephanie Eckles - @5t3ph and I've been a front-end focused developer for over a decade. Check out more of my projects including in-depth tutorials to help you upgrade your CSS skills on ModernCSS.dev, and my egghead video lessons on all kinds of front-end topics.

a11y-color-tokens relies on the following packages:

  • color - for contrast checking and color model evaluation
  • a11ycolor - for finding the nearest contrast-safe match

If you've found this project useful, I'd appreciate β˜•οΈ a coffee to keep me coding!

More Repositories

1

stylestage

A modern CSS showcase styled by community contributions. Add your stylesheet!
CSS
2,130
star
2

smolcss

Minimal snippets for modern CSS layouts and components
Nunjucks
791
star
3

SupportsCSS

Live, in-browser detection of modern CSS support for selectors, features, and at-rules. Applies support-based classes, exposes a results object, and allows custom tests.
JavaScript
270
star
4

11ty-sass-skeleton

Featuring absolutely nothing beyond a base HTML5 template and the essential setup to watch and compile your Sass alongside 11ty.
Nunjucks
241
star
5

11ty-netlify-jumpstart

Quickly launch an 11ty-generated static site. Includes a minimal Sass framework, generated sitemap, and RSS feed.
SCSS
235
star
6

moderncss-dot-dev

Posts from the series "Modern CSS Solutions to Old CSS Problems"
Nunjucks
140
star
7

htmlrecipes

A collection of quick copy HTML snippets for a variety of common scenarios.
Nunjucks
131
star
8

html-sass-jumpstart

Minimal Sass/HTML Template Site - dart sass powered, includes stylelint and prettier, and autoprefix upon build. develop script includes hot-reload via browsersync.
HTML
89
star
9

smol-11ty-starter

Extremely minimal Eleventy starter to kickstart a simple multi-page site / a nearly opinionless foundation to continue building on.
CSS
80
star
10

css-browser-support

Query for CSS brower support data, combined from caniuse and MDN, including version support started and global support percentages.
JavaScript
68
star
11

buttonbuddy

Learn about accessible button contrast then generate your own accessible button color palette
SCSS
67
star
12

11ty-email-generator

Generate simple, responsive email templates, with inlined CSS on build using 11ty.
HTML
65
star
13

eleventy-rss-reader

Create a personal RSS Reader using Eleventy. Review recordings to learn how it was built!
SCSS
65
star
14

11ty-web-component-generator

Use the power of 11ty to generate web components (custom elements).
HTML
55
star
15

eleventy-plugin-template

A starter environment for creating plugins for Eleventy (11ty).
JavaScript
53
star
16

eleventy-plugin-social-images

Create dynamic images sized for social media tags for your Eleventy content.
JavaScript
47
star
17

11ty-twitch-scenes

A slim 11ty + Sass setup to get started creating custom Twitch scenes, including responding to chat commands with ComfyJS.
SCSS
40
star
18

eleventy-plugin-lightningcss

Process CSS in Eleventy (11ty) with LightningCSS to minify, prefix, and add future CSS support.
JavaScript
37
star
19

eleventy-plugin-sass-lightningcss

Compile Sass in Eleventy (11ty) and process it with LightningCSS to minify, prefix, and add future CSS support.
JavaScript
37
star
20

stylestage-sass

A starter for Style Stage submissions with the source Sass from the base theme + BrowserSync.
SCSS
31
star
21

nojson

It's a move considered revolutionary by many...
HTML
30
star
22

11ty-serverless-search

An example of using 11ty Serverless to. create site search using local and external data sources.
JavaScript
28
star
23

odb-generative-svg

A demonstration of returning a generative art SVG via Netlify On-Demand Builders
HTML
27
star
24

11ty-rocks

A collection of Eleventy (11ty) starters, projects, plugins, and resources created by Stephanie Eckles (@5t3ph)
SCSS
27
star
25

11ty-membership-site

An Eleventy theme for offering premium content to members. Requires Supabase and Netlify accounts.
SCSS
24
star
26

gravity-forms-snippets

Applies bootstrap classes to various fields.
PHP
23
star
27

objectfit-focalpoint

Generate the object-position value to capture an image's focal point given a custom aspect-ratio.
JavaScript
20
star
28

11ty-design-system-example

SCSS
19
star
29

howtowebdev

The source files repository for the video series for beginners to web development - "Learn How to Make a Website with Steph" - available on YouTube: https://www.youtube.com/channel/UC8qc2AyBbNmvgIky6236nHA
HTML
17
star
30

eleventy-plugin-emoji-readtime

Eleventy (11ty) plugin that provides a configurable filter to display an estimated read time for Eleventy content, optionally with an emoji visual indicator.
JavaScript
16
star
31

course-accessible-cross-browser-forms

Source code for the egghead course "Accessible Cross-Browser CSS Form Styling". Visit the link to take the course!
HTML
16
star
32

eleventy-plugin-collection-schemas

Enforce a typed frontmatter schema for templates within an Eleventy collection.
JavaScript
16
star
33

lwj-modern-css

Developed for Learn With Jason - a dashboard to showcase lots of modern CSS features!
SCSS
13
star
34

11ty-basics

A barebones demo of basic but essential 11ty features.
HTML
13
star
35

11ty-css-houdini

An Eleventy environment for creating CSS Houdini Paint worklets.
JavaScript
12
star
36

repos-wrapped

A yearly review of your public GitHub repository stats.
JavaScript
11
star
37

course-grid-flexbox-landing-page

CSS
11
star
38

11ty-egghead-collection

HTML
10
star
39

a11y-fails

Find the fails, submit a PR with fixes. See link for live demo.
CSS
10
star
40

eleventy-plugin-css-browser-support

Eleventy filters for decorating inline-code with CSS broswer support data or rendering support tables. Data combined from caniuse and MDN, including version support started and global support percentages.
JavaScript
9
star
41

eleventy-plugin-objectfit-focalpoint

JavaScript
8
star
42

howtowebdev-starter

Starter files for the video course "Learn How to Make a Website with Steph"
JavaScript
8
star
43

eleventy-plugin-open-in-codepen

An Eleventy shortcode to add "Open in CodePen" functionality for code demos.
JavaScript
7
star
44

twitch-scenes-gamified

An Eleventy-based Twitch scenes starter with a chat-controlled game! Includes ComfyJS with command responses, and 6 scenes including guest support.
SCSS
7
star
45

devto-netlify-starter

Template HTML/Sass site that retrieves DEV posts with a Netlify function. Uses node-sass and includes Browsersync for hot reloading. Build styles are minified and autoprefixed.
CSS
6
star
46

11ty-edge-demo

Tiny demo with starter concepts for 11ty Edge.
JavaScript
5
star
47

11ty-serverless-esbuild-test

JavaScript
4
star
48

tdbc

Stephanie Eckles' main site, built with Eleventy.
Nunjucks
4
star
49

pollmychat

A browser source Twitch poll widget created on-demand via the !poll chat command.
JavaScript
3
star
50

howtowebdev-capstone

HTML
3
star
51

course-structure

A template to manage lessons for a related course using yarn workspaces.
HTML
3
star
52

meow-vs-bowwow

An Eleventy starter as built for the following CSS-Tricks tutorial: https://css-tricks.com/a-community-driven-site-with-eleventy-building-the-site/
HTML
3
star
53

egghead-twitch-scenes

Learn how to setup Twitch scenes built with Eleventy in 30 minutes! Includes embedded chat, and chat command responses.
CSS
3
star
54

css-webring

A simple web component that retrieves a selection of CSS blog links with customization available via CSS variables.
JavaScript
3
star
55

cursed-horse

Features built on-stream for Frontend Horse
SCSS
2
star
56

houdini-decorative-barcharts

JavaScript
2
star
57

hydrate-me

Get hydrated by running this solo or appending to a watch type of command for reminders at your chosen minute interval.
JavaScript
2
star
58

11ty-demo-theme

Building live on Twitch! https://twitch.tv/5t3phDev
SCSS
1
star
59

popular-named-css-colors

A compilation of replies to a silly tweet asking about named CSS colors.
Nunjucks
1
star