Skip to content

feat(design-tokens): Add a unit on a token value on demand #588

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export interface TokenTransformOptions {
rootFontSize: number;
};
toUnit?: string;
asUnit?: string;
}

export interface Token {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ export function applyTransformsToValue(value: string|undefined|null, transformOp
}
}

// add unit to unit-less value
if (transformOptions.asUnit) {
return `${value}${transformOptions.asUnit}`;
}

return value;
}

Expand Down
10 changes: 10 additions & 0 deletions plugins/postcss-design-tokens/src/values.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ export function onCSSValue(tokens: Map<string, Token>, result: Result, decl: Dec
transformOptions.toUnit = remainingNodes[i + 1].value;
i++;
}

if (
remainingNodes[i].type === 'word' &&
remainingNodes[i].value.toLowerCase() === 'as' &&
remainingNodes[i + 1] &&
remainingNodes[i + 1].type === 'word'
) {
transformOptions.asUnit = remainingNodes[i + 1].value;
i++;
}
}

node.value = replacement.cssValue(transformOptions);
Expand Down