Skip to content

Commit

Permalink
chore(tokens,styles): add token output for the utility API (#4613)
Browse files Browse the repository at this point in the history
  • Loading branch information
alizedebray authored Feb 18, 2025
1 parent ffd1f82 commit 6e3b653
Show file tree
Hide file tree
Showing 15 changed files with 132 additions and 121 deletions.
5 changes: 5 additions & 0 deletions .changeset/clever-years-sparkle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@swisspost/design-system-tokens': minor
---

Added a new output that includes utility tokens as Sass maps, enabling direct iteration to generate utility classes.
5 changes: 5 additions & 0 deletions .changeset/tame-squids-float.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@swisspost/design-system-styles': minor
---

Added a new CSS output file that includes all the utility classes.
4 changes: 2 additions & 2 deletions packages/styles/index.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!doctype html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
Expand All @@ -16,7 +16,7 @@

<!-- Styles -->
<link rel="stylesheet" href="src/layout/index.scss" />
<link rel="stylesheet" href="src/utilities/_index.scss" />
<link rel="stylesheet" href="src/utilities/index.scss" />
<link rel="stylesheet" href="src/elements/_index.scss" />
<link rel="stylesheet" href="src/components/_index.scss" />
<script src="playground.js" defer></script>
Expand Down
1 change: 1 addition & 0 deletions packages/styles/src/tokens/_utilities_formatted.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@forward './temp/utilities-formatted';
35 changes: 0 additions & 35 deletions packages/styles/src/utilities/_functions.scss

This file was deleted.

46 changes: 22 additions & 24 deletions packages/styles/src/utilities/_variables.scss
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
@use '../tokens/utilities' as tokens;

@use './functions' as *;
@use '../tokens/utilities_formatted';

@use '../variables/sizing' as *;
@use '../variables/type';
Expand Down Expand Up @@ -35,19 +33,19 @@ $utilities: (
responsive: true,
property: --post-grid-gutter-x --post-grid-gutter-y,
class: g,
values: from-tokens('spacing', 'gap'),
values: utilities_formatted.$gap,
),
'grid-gutter-x': (
responsive: true,
property: --post-grid-gutter-x,
class: gx,
values: from-tokens('spacing', 'gap'),
values: utilities_formatted.$gap,
),
'grid-gutter-y': (
responsive: true,
property: --post-grid-gutter-y,
class: gy,
values: from-tokens('spacing', 'gap'),
values: utilities_formatted.$gap,
),
'width': (
responsive: true,
Expand Down Expand Up @@ -197,103 +195,103 @@ $utilities: (
responsive: true,
property: margin,
class: m,
values: from-tokens('spacing', 'margin'),
values: utilities_formatted.$margin,
),
'margin-x': (
responsive: true,
property: margin-right margin-left,
class: mx,
values: from-tokens('spacing', 'margin'),
values: utilities_formatted.$margin,
),
'margin-y': (
responsive: true,
property: margin-top margin-bottom,
class: my,
values: from-tokens('spacing', 'margin'),
values: utilities_formatted.$margin,
),
'margin-top': (
responsive: true,
property: margin-top,
class: mt,
values: from-tokens('spacing', 'margin'),
values: utilities_formatted.$margin,
),
'margin-end': (
responsive: true,
property: margin-right,
class: me,
values: from-tokens('spacing', 'margin'),
values: utilities_formatted.$margin,
),
'margin-bottom': (
responsive: true,
property: margin-bottom,
class: mb,
values: from-tokens('spacing', 'margin'),
values: utilities_formatted.$margin,
),
'margin-start': (
responsive: true,
property: margin-left,
class: ms,
values: from-tokens('spacing', 'margin'),
values: utilities_formatted.$margin,
),
'padding': (
responsive: true,
property: padding,
class: p,
values: from-tokens('spacing', 'padding'),
values: utilities_formatted.$padding,
),
'padding-x': (
responsive: true,
property: padding-right padding-left,
class: px,
values: from-tokens('spacing', 'padding'),
values: utilities_formatted.$padding,
),
'padding-y': (
responsive: true,
property: padding-top padding-bottom,
class: py,
values: from-tokens('spacing', 'padding'),
values: utilities_formatted.$padding,
),
'padding-top': (
responsive: true,
property: padding-top,
class: pt,
values: from-tokens('spacing', 'padding'),
values: utilities_formatted.$padding,
),
'padding-end': (
responsive: true,
property: padding-right,
class: pe,
values: from-tokens('spacing', 'padding'),
values: utilities_formatted.$padding,
),
'padding-bottom': (
responsive: true,
property: padding-bottom,
class: pb,
values: from-tokens('spacing', 'padding'),
values: utilities_formatted.$padding,
),
'padding-start': (
responsive: true,
property: padding-left,
class: ps,
values: from-tokens('spacing', 'padding'),
values: utilities_formatted.$padding,
),
'gap': (
responsive: true,
property: gap,
class: gap,
values: from-tokens('spacing', 'gap'),
values: utilities_formatted.$gap,
),
'row-gap': (
responsive: true,
property: row-gap,
class: row-gap,
values: from-tokens('spacing', 'gap'),
values: utilities_formatted.$gap,
),
'column-gap': (
responsive: true,
property: column-gap,
class: column-gap,
values: from-tokens('spacing', 'gap'),
values: utilities_formatted.$gap,
),
'flex': (
responsive: true,
Expand Down Expand Up @@ -511,7 +509,7 @@ $utilities: (
),
'elevation': (
property: box-shadow,
values: from-tokens('elevation', 'elevation'),
values: utilities_formatted.$elevation,
class: elevation,
),
);
Expand Down
File renamed without changes.
34 changes: 0 additions & 34 deletions packages/styles/tests/utilities/functions.test.scss

This file was deleted.

1 change: 1 addition & 0 deletions packages/tokens/_build/configs/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import './all.js';
import './palettes.js';
import './tailwind.js';
import './utilities.js';
5 changes: 2 additions & 3 deletions packages/tokens/_build/configs/palettes.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { expandTypesMap } from '@tokens-studio/sd-transforms';
import { registerConfigMethod } from '../methods.js';

const PALETTE_TOKENSET_NAME = 'palettes';
import { TOKENSET_NAMES } from '../constants.js';

/**
* Registers a config getter method to generate output files for all code relevant tokens in the tokens.json.
*/
registerConfigMethod((tokenSets, { sourcePath, buildPath }) => {
const paletteSet = tokenSets.output[PALETTE_TOKENSET_NAME];
const paletteSet = tokenSets.output[TOKENSET_NAMES.Palettes];

if (!paletteSet) return;

Expand Down
25 changes: 3 additions & 22 deletions packages/tokens/_build/configs/tailwind.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { fileHeader } from 'style-dictionary/utils';
import { TOKENSET_LAYERS, TOKENSET_PREFIX } from '../constants.js';
import { TOKENSET_LAYERS, TOKENSET_NAMES, TOKENSET_PREFIX } from '../constants.js';
import StyleDictionary from '../style-dictionary.js';
import { registerConfigMethod, getTokenValue } from '../methods.js';
import { objectDeepmerge, objectTextoutput } from '../utils/index.js';

const TAILWIND_TOKENSET_NAMES = ['utilities', 'helpers'];
const TAILWIND_TOKENSET_NAMES = [TOKENSET_NAMES.Utilities, TOKENSET_NAMES.Helpers];

/**
* Registers a config getter method to generate output files for tailwind relevant tokens in the tokens.json.
Expand Down Expand Up @@ -32,8 +32,8 @@ registerConfigMethod((tokenSets, { sourcePath, buildPath }) => {
files: [
{
destination: `${name}.tailwind.js`,
filter: 'swisspost/source-tokens-filter',
format: 'swisspost/tailwind-format',
filter: 'swisspost/tailwind-filter',
options: {
outputReferences: true,
},
Expand All @@ -45,25 +45,6 @@ registerConfigMethod((tokenSets, { sourcePath, buildPath }) => {
});
});

/**
* @function StyleDictionary.registerFilter()
* Defines a custom StyleDictionary filter.
*
* @param object {
* name: string,
* filter: (token: TransformedToken, options: Config) => boolean
* }
*
* swisspost/tailwind-filter:
* Used to filter only the component layer tokens defined in the tokensets with the names in TAILWIND_TOKENSET_NAMES.
*/
StyleDictionary.registerFilter({
name: 'swisspost/tailwind-filter',
filter: token => {
return token.filePath.includes('/output/');
},
});

/**
* @function StyleDictionary.registerFormat()
* Defines a custom StyleDictionary format to be used at specific places in the build process.
Expand Down
65 changes: 65 additions & 0 deletions packages/tokens/_build/configs/utilities.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { fileHeader } from 'style-dictionary/utils';
import { TOKENSET_NAMES } from '../constants.js';
import StyleDictionary from '../style-dictionary.js';
import { registerConfigMethod, getTokenValue } from '../methods.js';

/**
* Registers a config method to generate output files for utility tokens.
*/
registerConfigMethod((tokenSets, { sourcePath, buildPath }) => {
const { type, layer, filePath, sets } = tokenSets.output[TOKENSET_NAMES.Utilities];

return {
meta: {
type,
layer,
filePath,
setNames: Object.keys(sets),
},
source: [`${sourcePath}_temp/output/${filePath}`],
include: [`${sourcePath}_temp/source/**/*.json`],
platforms: {
utilities: {
transforms: ['name/kebab'],
buildPath,
files: [
{
destination: `_utilities-formatted.scss`,
filter: 'swisspost/source-tokens-filter',
format: 'swisspost/utility-format',
options: {
outputReferences: true,
},
},
],
},
},
};
});

/**
* Registers the format of the utility output file.
*/
StyleDictionary.registerFormat({
name: 'swisspost/utility-format',
format: async ({ dictionary, options, file }) => {
const header = await fileHeader({ file, commentStyle: 'short' });

const utilityTokens = new Map();

dictionary.allTokens.forEach(token => {
const { subitem, state } = token.attributes;

const previousStates = utilityTokens.get(subitem) ?? [];
const newState = `\n ${state}: ${getTokenValue(options, token)},`;

utilityTokens.set(subitem, [...previousStates, newState]);
});

const utilityMaps = Array.from(utilityTokens.entries()).map(([subitem, values]) => {
return `$${subitem}: (${values.join('')}\n);\n`;
});

return header + utilityMaps.join('\n');
},
});
Loading

0 comments on commit 6e3b653

Please sign in to comment.