Skip to content

Commit

Permalink
feat(tokens): use rem sizing for font size
Browse files Browse the repository at this point in the history
  • Loading branch information
akdetrick committed Jun 3, 2024
1 parent 5abab4e commit 06c3036
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
3 changes: 2 additions & 1 deletion tokens/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ const config = {
* Custom properties in a CSS file
*/
css: {
transforms: CSS_TRANSFORM_GROUP,
basePxFontSize: 16,
transforms: [...CSS_TRANSFORM_GROUP, "custom/value/pxToRem"],
buildPath: getBuildPath("css"),
files: [
{
Expand Down
20 changes: 18 additions & 2 deletions tokens/util/transforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const nameTransforms = [
matcher: ({ attributes }) =>
_matchByAttribute(
["theme", "alpha", "space", "elevation"],
attributes.type
attributes.type,
),
transformer: ({ attributes }) => {
const { type, item } = attributes;
Expand Down Expand Up @@ -120,7 +120,7 @@ const valueTransforms = [
transformer: ({ original }) => parseFloat(original.value),
matcher: ({ attributes }) =>
["radius", "size", "alpha", "lineHeight", "space"].some(
(allowedType) => attributes.type === allowedType
(allowedType) => attributes.type === allowedType,
),
},
{
Expand All @@ -133,6 +133,22 @@ const valueTransforms = [
return `${r}, ${g}, ${b}`;
},
},
// transforms a pixel font size to rem based on base font size
// (base font size is configured in `tokens/config.js`)
{
name: "custom/value/pxToRem",
type: "value",
matcher: ({ attributes }) => {
const { category, type } = attributes;
return type === "size" && category === "font";
},
transformer: (token, options) => {
const base = (options && options.basePxFontSize) || 16;
const floatVal = parseFloat(token.value);
if (floatVal === 0) return floatVal;
return `${floatVal / base}rem`;
},
},
];

module.exports = [...nameTransforms, ...valueTransforms];

0 comments on commit 06c3036

Please sign in to comment.