Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace Theo with Style Dictionary #967

Merged
merged 39 commits into from
Mar 10, 2021
Merged
Show file tree
Hide file tree
Changes from 36 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
e034839
wip
spaceninja Sep 26, 2020
d2b126a
add missing sizing tokens
spaceninja Sep 28, 2020
3213575
begin replacing token variables
spaceninja Sep 28, 2020
28e62bc
update sass variables
spaceninja Sep 28, 2020
0e0362a
update build steps
spaceninja Sep 29, 2020
845d49c
replace token stories, remove theo
spaceninja Sep 29, 2020
467eca2
add watch:tokens script
spaceninja Sep 30, 2020
656c9ed
remove old generated tokens from ignore files
spaceninja Sep 30, 2020
22471b2
fix github actions
spaceninja Sep 30, 2020
1ae658a
Merge branch 'v-next' into feature/style-dictionary
spaceninja Mar 3, 2021
a2f4171
lint fix
spaceninja Mar 3, 2021
9d5e461
fix build
spaceninja Mar 3, 2021
44c49f7
update ci and storybook config
spaceninja Mar 3, 2021
59d88d4
fix storybook config
spaceninja Mar 3, 2021
009feb6
fix aspect ratio boxes
spaceninja Mar 3, 2021
11deeda
removing old token file
spaceninja Mar 3, 2021
7deb164
this was removed intentionally, do not restore it
spaceninja Mar 3, 2021
492f456
update storybook config
spaceninja Mar 4, 2021
462c225
standardize sass @use sort order
spaceninja Mar 4, 2021
59cd335
remove deleted file
spaceninja Mar 4, 2021
1b916dd
move compiled tokens to `compiled/tokens/`
spaceninja Mar 8, 2021
edd2afc
Merge branch 'v-next' into feature/style-dictionary
spaceninja Mar 8, 2021
8676261
move style dictionary config and build script
spaceninja Mar 8, 2021
63abb33
move token watch to gulp
spaceninja Mar 9, 2021
0bd077f
Merge branch 'v-next' into feature/style-dictionary
spaceninja Mar 9, 2021
7d38fb2
update sky-nav to fix build
spaceninja Mar 9, 2021
c07213d
generate category scss files, update stories
spaceninja Mar 10, 2021
749fe83
use color tokens
spaceninja Mar 10, 2021
ecf8dc4
fix typo
spaceninja Mar 10, 2021
c19870d
use breakpoint tokens
spaceninja Mar 10, 2021
0abeaad
use font tokens
spaceninja Mar 10, 2021
bd7eb55
update layout, scale, and opacity tokens
spaceninja Mar 10, 2021
375f657
use motion tokens
spaceninja Mar 10, 2021
94c23e3
use size tokens
spaceninja Mar 10, 2021
e6af0b2
remove generic tokens file
spaceninja Mar 10, 2021
755f0c7
lint fixes
spaceninja Mar 10, 2021
5a4f4ef
fix Prism
spaceninja Mar 10, 2021
8707eb9
fix key error
spaceninja Mar 10, 2021
2d6649d
typo
spaceninja Mar 10, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/dist
/ts-dist
/src/design-tokens/generated
/src/compiled
/storybook-static

# Ignore all files in the prototypes directory except for stories
Expand Down
5 changes: 2 additions & 3 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ module.exports = {
// Specifying this here fixes it.
files: ['*.ts', '*.tsx'],
parser: require.resolve(
// This package has to be resolved from the eslint plugin
// since it isn't installed into the pattern library directly.
'@cloudfour/eslint-plugin/node_modules/@typescript-eslint/parser'
// eslint-disable-next-line @cloudfour/node/no-extraneous-require
'@typescript-eslint/parser'
),
parserOptions: {
project: './tsconfig.json',
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ jobs:
restore-keys: ${{ runner.os }}-node-
- name: Install Dependencies
run: CYPRESS_INSTALL_BINARY=0 npm ci
- name: Run Preprocess
run: npm run preprocess
- name: Run Lint
run: npm run check-lint

Expand Down Expand Up @@ -59,8 +61,6 @@ jobs:
restore-keys: ${{ runner.os }}-node-
- name: Install Dependencies
run: CYPRESS_INSTALL_BINARY=0 npm ci
- name: Run Preprocess
run: npm run preprocess
- name: Run Build
run: npm run build

Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ ts-dist
/src/assets/**/*.twig

# Auto-generated design token stories
/src/design-tokens/generated
/src/compiled

# Dependencies
node_modules
Expand Down
2 changes: 1 addition & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/dist
/ts-dist
/src/design-tokens/generated
/src/compiled
/storybook-static

# Ignore all files in the prototypes directory except for stories
Expand Down
23 changes: 9 additions & 14 deletions .storybook/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@ module.exports = {
// We load the welcome story separately so it will be the first sidebar item.
stories: ['../src/welcome.stories.mdx', '../src/**/*.stories.@(js|mdx)'],
addons: [
'@storybook/addon-docs',
'@storybook/addon-controls',
{
name: '@storybook/addon-essentials',
options: {
actions: false,
backgrounds: false,
},
},
'@storybook/addon-a11y',
'storybook-mobile',
'@storybook/addon-viewport/register',
'storybook-addon-themes',
'storybook-addon-paddings',
'@whitespace/storybook-addon-html',
Expand Down Expand Up @@ -63,23 +67,14 @@ module.exports = {
implementation: require('sass'),
sourceMap: true,
sassOptions: {
importer: [
require('../glob-sass-importer'),
// Import Theo design tokens as SCSS variables
require('../.theo/sass-importer'),
],
importer: [require('../glob-sass-importer')],
},
},
},
],
},
twingLoader,
valLoader,
{
// Import Theo design tokens as JS objects
test: /\.ya?ml$/,
use: resolve(__dirname, '../.theo/webpack-loader.js'),
}
valLoader
);

Object.assign(config.resolve.alias, twingAlias);
Expand Down
17 changes: 10 additions & 7 deletions .storybook/preview.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { Parser } from 'html-to-react';
import { withPaddings } from 'storybook-addon-paddings';
import * as colors from '../src/design-tokens/colors.yml';
import * as breakpoints from '../src/design-tokens/breakpoint.yml';
import { INITIAL_VIEWPORTS } from '@storybook/addon-viewport';
import { ratio } from '../src/design-tokens/modular-scale.yml';
import tokens from '../src/compiled/tokens/js/tokens';
import 'focus-visible';
import '../src/index-with-dependencies.scss';
import './preview.scss';
const breakpoints = tokens.size.breakpoint;

/**
* The parameters for Storybook sorting functions are sparsely documented.
Expand Down Expand Up @@ -61,16 +60,18 @@ for (let i = -3; i <= 6; i++) {
// `toFixed` keeps the values from extending past two decimal points.
// The leading `+` keeps values from having decimal points where they don't
// need them, so `1.00` becomes `1`.
value: `${+Math.pow(ratio, i).toFixed(2)}em`,
value: `${+Math.pow(tokens.number.scale.modular.ratio.value, i).toFixed(
2
)}em`,
});
}

// Create viewports using widths defined in design tokens
const breakpointViewports = Object.keys(breakpoints).map((name) => {
return {
name: `breakpoint.$${name}`,
name: `$${breakpoints[name].name}`,
styles: {
width: breakpoints[name],
width: breakpoints[name].value,
// Account for padding and border around viewport preview
height: 'calc(100% - 20px)',
},
Expand All @@ -82,7 +83,9 @@ const htmlToReactParser = new Parser();

export const parameters = {
// Theme selection from stories
themes: [{ name: 'Dark', class: 't-dark', color: colors.primaryBrand }],
themes: [
{ name: 'Dark', class: 't-dark', color: tokens.color.brand.primary.value },
],
// Sort stories according to preferred top-level settings
options: { storySort },
docs: {
Expand Down
107 changes: 107 additions & 0 deletions .style-dictionary/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
const StyleDictionary = require('style-dictionary');
const _ = require('lodash');

/**
* Custom Transform: Kebab-Case without Category
* Creates a kebab case name without the category at the front.
* This is most useful when filtering a file to only contain tokens from one
* category (e.g., colors.$base-dark-red instead of tokens.color-base-dark-red).
* If you define a prefix on the platform in your config, it will be prepended.
*
* ```js
* // Matches: all
* // Returns:
* "background-button-primary-active"
* "prefix-background-button-primary-active"
* ```
*/
StyleDictionary.registerTransform({
name: 'custom/name/ti/kebab',
type: 'name',
transformer: function (prop, options) {
return _.kebabCase(
[options.prefix].concat(prop.path.slice(1, prop.path.length)).join(' ')
);
},
});

/**
* Custom Transform: Kebab-Case without Category or Type
* Creates a kebab case name without the category or type at the front.
* This is most useful when filtering a file to only contain tokens from one
* category & type (e.g., base-colors.$dark-red instead of tokens.color-base-dark-red).
* If you define a prefix on the platform in your config, it will be prepended.
*
* ```js
* // Matches: all
* // Returns:
* "button-primary-active"
* "prefix-button-primary-active"
* ```
*/
StyleDictionary.registerTransform({
name: 'custom/name/i/kebab',
type: 'name',
transformer: function (prop, options) {
return _.kebabCase(
[options.prefix].concat(prop.path.slice(2, prop.path.length)).join(' ')
);
},
});

/**
* Custom Transform Group: CSS
* This is a modified version of the CSS transform group without the time,
* size, or icon transformations and using our custom CSS color transform.
* We also use it for JSON.
*/
StyleDictionary.registerTransformGroup({
name: 'custom/transform-group/css',
transforms: ['attribute/cti', 'name/cti/kebab', 'color/css'],
});

/**
* Custom Transform Group: CSS by Category
* Same as our custom CSS transform group, but strips the category from
* the token names for files that are limited to a single category.
*/
StyleDictionary.registerTransformGroup({
name: 'custom/transform-group/css-category',
transforms: ['attribute/cti', 'custom/name/ti/kebab', 'color/css'],
});

/**
* Custom Transform Group: CSS by Category
* Same as our custom CSS transform group, but strips the category & type from
* the token names for files that are limited to a single category.
*/
StyleDictionary.registerTransformGroup({
name: 'custom/transform-group/css-category-type',
transforms: ['attribute/cti', 'custom/name/i/kebab', 'color/css'],
});

/**
* Custom Format: JS Flat
* This custom format is based on `json/flat` but modified to return a JS module
* containing a `value` and `comment` (if one exists).
*/
StyleDictionary.registerFormat({
name: 'custom/format/js/flat',
formatter(dictionary) {
const tokens = {};
dictionary.allProperties.forEach((prop) => {
tokens[prop.name] = prop;
});
return `module.exports = ${JSON.stringify(tokens, null, ' ')}`;
},
});

// APPLY THE CONFIGURATION
// IMPORTANT: the registration of custom transforms
// needs to be done _before_ applying the configuration
const StyleDictionaryExtended = StyleDictionary.extend(
__dirname + '/config.js'
);

// BUILD ALL THE PLATFORMS
StyleDictionaryExtended.buildAllPlatforms();
119 changes: 119 additions & 0 deletions .style-dictionary/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
module.exports = {
log: 'warn',
source: ['src/tokens/**/*.+(js|json)'],
platforms: {
// Export all the tokens in a single SCSS file.
scss: {
transformGroup: 'custom/transform-group/css',
buildPath: 'src/compiled/tokens/scss/',
files: [
{
destination: '_tokens.scss',
format: 'scss/variables',
},
],
},
// Export some categories to their own SCSS files for easier importing,
// removing the category from the variable names.
'scss-categories': {
transformGroup: 'custom/transform-group/css-category',
buildPath: 'src/compiled/tokens/scss/',
files: [
{
destination: '_color.scss',
format: 'scss/variables',
filter: { attributes: { category: 'color' } },
},
{
destination: '_ease.scss',
format: 'scss/variables',
filter: { attributes: { category: 'ease' } },
},
{
destination: '_size.scss',
format: 'scss/variables',
filter: { attributes: { category: 'size' } },
},
],
},
// Export some category types to their own SCSS files for easier importing,
// removing the category and type from the variable names.
'scss-category-types': {
transformGroup: 'custom/transform-group/css-category-type',
buildPath: 'src/compiled/tokens/scss/',
files: [
{
destination: '_aspect-ratio.scss',
format: 'scss/variables',
filter: {
attributes: { category: 'number', type: 'aspect_ratio' },
},
},
{
destination: '_breakpoint.scss',
format: 'scss/variables',
filter: {
attributes: { category: 'size', type: 'breakpoint' },
},
},
{
destination: '_brightness.scss',
format: 'scss/variables',
filter: {
attributes: { category: 'number', type: 'brightness' },
},
},
{
destination: '_font-family.scss',
format: 'scss/variables',
filter: { attributes: { category: 'font', type: 'family' } },
},
{
destination: '_font-weight.scss',
format: 'scss/variables',
filter: {
attributes: { category: 'number', type: 'font_weight' },
},
},
{
destination: '_opacity.scss',
format: 'scss/variables',
filter: {
attributes: { category: 'number', type: 'opacity' },
},
},
{
destination: '_scale.scss',
format: 'scss/variables',
filter: {
attributes: { category: 'number', type: 'scale' },
},
},
{
destination: '_transition.scss',
format: 'scss/variables',
filter: {
attributes: { category: 'time', type: 'transition' },
},
},
],
},
js: {
transformGroup: 'custom/transform-group/css',
buildPath: 'src/compiled/tokens/js/',
files: [
// Export all the tokens in a single JS file.
{
destination: 'tokens.js',
format: 'javascript/module',
},
// Export a flattened version of just the size tokens for easier looping.
{
destination: 'size-tokens.js',
format: 'custom/format/js/flat',
filter: { attributes: { category: 'size' } },
},
],
},
},
};
3 changes: 3 additions & 0 deletions .stylelintignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@

# Ignore syntax highlighting examples
/src/vendor/prism/demo/samples/**/*

# Generated files
/src/compiled
Loading