Home
Softono
5

5t3ph

Professional software vendor delivering innovative solutions on the Softono platform. Specialized in both open-source and proprietary software development.

Total Products
2

Software by 5t3ph

a11y-color-tokens
Open Source

a11y-color-tokens

![](https://repository-images.githubusercontent.com/336923950/96da5980-6a67-11eb-8d4c-79f194eba4f8) # 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](https://www.w3.org/WAI/WCAG22/Understanding/contrast-minimum.html). 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](https://www.smashingmagazine.com/2019/11/smashing-podcast-episode-3/). ## What's in the box **Example output**: ```scss // `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 >](https://github.com/5t3ph/a11y-color-tokens/blob/main/sass/_color-tokens.scss)** Alternatively, pass `"css"` as the `tokenOutputFormat` to only output CSS custom properties within the `:root` selector. Additionally, [an optional Markdown document](https://github.com/5t3ph/a11y-color-tokens/blob/main/sass/_color-token-contrast.md) is generated with contrast cross-compatibility between all available color tokens. > Review an example of [using the generated Sass assets >](https://github.com/5t3ph/a11y-color-tokens/blob/main/sass/style.scss) ## Usage Install `a11y-color-tokens` into any project using: ```bash 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: ```js // 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](https://github.com/5t3ph/a11y-color-tokens/blob/main/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. ```json "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](https://github.com/5t3ph/11ty-sass-skeleton) 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: ```bash 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: ```bash a11y-color-tokens --customPropertiesFormat='root' ``` ### Update Sass map and mixin name This is handled by updating the following: ```bash 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. ```bash a11y-color-tokens --tokenPrefix='theme-' ``` ### Prevent CSS custom properties output This is handled with `includeCustomProperties` and can be removed with: ```bash a11y-color-tokens --no-includeCustomProperties ``` ### Remove the `_color-token-contrast.md` documentation This is handled with `compatibilityDocs` and can be removed with: ```bash 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): ```bash a11y-color-tokens --compatibilityDocsPath='docs' ``` ## Colophon and Credits Hi! I'm [Stephanie Eckles - @5t3ph](https://twitter.com/5t3ph) and I've been a front-end focused developer for over a decade. [Check out more of my projects](https://thinkdobecreate.com) including in-depth tutorials to help you upgrade your CSS skills on [ModernCSS.dev](https://moderncss.dev), and my [egghead video lessons](https://5t3ph.dev/egghead) on all kinds of front-end topics. `a11y-color-tokens` relies on the following packages: - [color](https://www.npmjs.com/package/color) - for contrast checking and color model evaluation - [a11ycolor](https://www.npmjs.com/package/a11ycolor) - for finding the nearest contrast-safe match > If you've found this project useful, I'd appreciate ☕️ [a coffee to keep me coding](https://www.buymeacoffee.com/moderncss)!

Design Systems & Tokens
162 Github Stars
11ty-web-component-generator
Open Source

11ty-web-component-generator

![](https://repository-images.githubusercontent.com/303895742/105a8780-0da7-11eb-8c1f-7ca10b6e3713) # ⚡️ 11ty Web Component Generator > [View the demo Hello World component >](https://11ty-web-component-generator.netlify.app/) ## Using the Generator [Eleventy (11ty) is a static site generator](https://www.11ty.dev/docs/) that makes it possible to mix templating languages. More importantly to this generator is that we can customize the _output file type_ and composite a file from _includes_. The web components are generated within a Nunjucks (`.njk`) template that outputs the final `.js` file. This generator works with the inherent features of Eleventy, including that it expects templates and template partials to be placed in `_includes`. This leads to the following file structure to create a web component with styles and additional scripting. First, create the component template file in `src/components/[component-name].njk`. _It is important to kebab-case the file name_ Then within `_includes/components/` create: ```bash [component-name]/ script.js style.css ``` Keep the names of `script.js` and `style.css` so that the `_includes/component.njk` template can successfully include their contents to generate the web component. ## Using a Generated Web Component The final web component will be output within `public/components/[component-name].js` and is ready to be included in another project such as: ```html <script type="module" src="path/to/component-name.js"></script> <component-name></component-name> ``` ### Display a Component Within This Generator Project Create additional pages in this project directly within `src` as Nunjuck (`.njk`) files and add the following frontmatter in addition to any HTML and Nunjuck template tags. ```md title: Page Title component: component-name ``` Then the `page.njk` template will use the `component` value to include the relative path to the web component script. ## Project Scripts - **`npm start`** - run Eleventy on localhost with included Browsersync hot-reload - **`npm run build`** - run only Eleventy for creating a production build of the generator project ## Web Component Resources First a little disclaimer - I am brand new to web components, so I know this doesn't cover all the things you may want to do, or the best way to generically composite them. Submit a PR if you want to help extend this generator! Here are some resources that helped me put together [my first web component](https://github.com/5t3ph/css-webring): - [Encapsulating Style and Structure with Shadow DOM](https://css-tricks.com/encapsulating-style-and-structure-with-shadow-dom/) - [Using custom elements](https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_custom_elements#Working_through_some_simple_examples) - [Creating a Custom Element from Scratch](https://css-tricks.com/creating-a-custom-element-from-scratch/) - [Custom Elements v1: Reusable Web Components](https://developers.google.com/web/fundamentals/web-components/customelements) ## 11ty Resources I'm very fond of 11ty, so here's a list of my other resources: **Learn to build an 11ty site in 20 mins** with my [egghead video course](https://5t3ph.dev/learn-11ty) and see how to add a blog and custom data. **Add auto-generated social media images** by following [my tutorial](https://dev.to/5t3ph/automated-social-sharing-images-with-puppeteer-11ty-and-netlify-22ln) **Explore advanced setup of custom data** through my [tutorial on building a community site](https://css-tricks.com/a-community-driven-site-with-eleventy-building-the-site/) **For a full-featured starter** check out my [11ty Netlify Jumpstart](https://11ty-netlify-jumpstart.netlify.app/) (also works for hosts other than Netlify). **For a featureless Sass starter** grab the template for my [11ty Sass Skeleton](https://github.com/5t3ph/11ty-sass-skeleton)

Web Components & Widgets Static Site Generators
55 Github Stars