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

Use dimension/px transformer instead of custom #736

Merged
merged 6 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
36 changes: 1 addition & 35 deletions packages/design-tokens/scripts/style-dictionary.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,45 +13,11 @@ const {fileHeader, formattedVariables} = StyleDictionary.formatHelpers
// functions to be extracted
// TODO: extract to a separate files

// REGISTER THE CUSTOM TRANFORMS

// find values with px unit
function isPx(value) {
return /[\d.]+px$/.test(value)
}

// transform: px to rem
StyleDictionary.registerTransform({
name: 'pxToRem',
type: 'value',
transformer: token => {
if (isPx(token.value)) {
const baseFontSize = 16
const floatValue = parseFloat(token.value.replace('px', ''))

if (isNaN(floatValue)) {
return token.value
}

if (floatValue === 0) {
return '0'
}

if (token.name.includes('lineHeight')) {
return `${floatValue / baseFontSize}`
}

return `${floatValue / baseFontSize}rem`
}
return token.value
},
})

// REGISTER THE CUSTOM TRANFORM GROUPS

StyleDictionary.registerTransformGroup({
name: 'css',
transforms: ['name/pathToKebabCase', 'pxToRem', 'typography/css'],
transforms: ['name/pathToKebabCase', 'dimension/rem', 'typography/css'],
})

// REGISTER A CUSTOM FORMAT
Expand Down
24 changes: 16 additions & 8 deletions packages/design-tokens/src/tokens/functional/animation/base.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,32 @@
"variant": {
"slideInUp": {
"distance": {
"value": "15px"
"value": "15px",
"$type": "dimension"
}
},
"slideInDown": {
"distance": {
"value": "-15px"
"value": "-15px",
"$type": "dimension"
}
},
"slideInLeft": {
"distance": {
"value": "15px"
"value": "15px",
"$type": "dimension"
}
},
"slideInRight": {
"distance": {
"value": "-15px"
"value": "-15px",
"$type": "dimension"
}
},
"scaleInUp": {
"distance": {
"value": "24px"
"value": "24px",
"$type": "dimension"
},
"start": {
"value": "0.96"
Expand All @@ -62,7 +67,8 @@
},
"scaleInDown": {
"distance": {
"value": "-50px"
"value": "-50px",
"$type": "dimension"
},
"start": {
"value": "0"
Expand All @@ -86,7 +92,8 @@
},
"scaleInRight": {
"distance": {
"value": "-20px"
"value": "-20px",
"$type": "dimension"
},
"start": {
"value": "0.90"
Expand All @@ -97,7 +104,8 @@
},
"scaleInLeft": {
"distance": {
"value": "20px"
"value": "20px",
"$type": "dimension"
},
"start": {
"value": "0.90"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"brand": {
"Card": {
"maxWidth": {
"value": "420px"
"value": "420px",
"$type": "dimension"
},
"animation": {
"duration": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
"value": "var(--base-size-16)",
"responsive": {
"768px": {
"value": "100px"
"value": "100px",
"$type": "dimension"
}
}
},
"spacing": {
"inner": {
"block": {
"value": "14px"
"value": "14px",
"$type": "dimension"
},
"inline": {
"value": "var(--base-size-16)",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"FAQ": {
"maxWidth": {
"list": {
"value": "816px"
"value": "816px",
"$type": "dimension"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"brand": {
"IDE": {
"height": {
"value": "800px"
"value": "800px",
"$type": "dimension"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
},
"container": {
"maxWidth": {
"value": "1280px"
"value": "1280px",
"$type": "dimension"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
"Statistic": {
"spacing": {
"condensed": {
"value": "16px"
"value": "16px",
"$type": "dimension"
},
"normal": {
"value": "24px"
"value": "24px",
"$type": "dimension"
},
"spacious": {
"value": "32px"
"value": "32px",
"$type": "dimension"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"SubNav": {
"width": {
"subMenu": {
"value": "300px"
"value": "300px",
"$type": "dimension"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"value": "44ch"
},
"strokeWidth": {
"value": "2px"
"value": "2px",
"$type": "dimension"
},
"spacing": {
"value": "var(--base-size-16)"
Expand All @@ -18,7 +19,8 @@
"value": "var(--base-size-8)"
},
"spacing": {
"value": "2px"
"value": "2px",
"$type": "dimension"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"value": "rgba(0, 0, 0, 0.75)"
},
"padding": {
"value": "10px"
"value": "10px",
"$type": "dimension"
}
}
},
Expand All @@ -27,21 +28,31 @@
},
"range": {
"height": {
"value": "6px"
"value": "6px",
"$type": "dimension"
},
"borderRadius": {
"value": "3px"
"value": "3px",
"$type": "dimension"
},
"minWidth": {
"value": "75px"
"value": "75px",
"$type": "dimension"
}
},
"transition": {
"value": "all var(--brand-animation-duration-fast) var(--brand-animation-easing-default)"
},
"tooltip": {
"padding": {
"value": "5px 10px"
"block": {
"value": "10px",
"$type": "dimension"
},
"inline": {
"value": "5px",
"$type": "dimension"
}
lukasoppermann marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
},
"full": {
"value": "9999px",
"$type": "dimension",
"comment": "Use this border radius for pill shaped elements"
}
}
Expand Down
3 changes: 2 additions & 1 deletion packages/design-tokens/src/tokens/functional/size/size.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@
},
"large": {
"size": {
"value": "60px"
"value": "60px",
"$type": "dimension"
},
"lineBoxHeight": {
"value": "{base.size.20}"
Expand Down
Loading
Loading