-
Notifications
You must be signed in to change notification settings - Fork 1
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
feat: resolves the design token values [ALT-171] #195
Conversation
3305738
to
20c3b6d
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work on this!
package-lock.json
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've accidently generated this file before too, but since this repo only uses yarn
, I think you can delete this
yarn.lock
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The changes to this file were possibly generated by npm
. When this happens to me, I delete node_modules directory and regenerate the yarn.lock file using yarn
@@ -26,6 +28,8 @@ const toCSSMediaQuery = ({ query }: Breakpoint): string | undefined => { | |||
return undefined; | |||
}; | |||
|
|||
const AvailableDesignTokenVariables = new Set(['cfPadding', 'cfMargin']); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The uppercase first letter is standard for component names and types/interfaces but uncommon for variable names.
const AvailableDesignTokenVariables = new Set(['cfPadding', 'cfMargin']); | |
const availableDesignTokenVariables = new Set(['cfPadding', 'cfMargin']); |
if (AvailableDesignTokenVariables.has(variableName)) { | ||
if (variableName === 'cfMargin' || variableName === 'cfPadding') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The if statements could be combined
if (AvailableDesignTokenVariables.has(variableName)) { | |
if (variableName === 'cfMargin' || variableName === 'cfPadding') | |
if (AvailableDesignTokenVariables.has(variableName) && ['cfMargin', 'cfPadding'].includes(variableName)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I purposely did this so that when there's other attributes like border or whatever, I will add a separate clause for that so that I can direct that to the getDesignTokenRegistrationForBorder
function. So this is the structure I want for the future even though today, in this PR you can shorten it like this. Hopefully what I wrote makes sense
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (AvailableDesignTokenVariables.has(variableName)) {
if (variableName === 'cfMargin' || variableName === 'cfPadding')
return getDesignTokenRegistrationForSpacing(valuesByBreakpoint[breakpointId]);
if(variableName === 'cfBorder')return
getDesignTokenRegistrationForBorder(valuesByBreakpoint[breakpointId]);
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it, makes sense 👍
If enough values are checked on the inner part, a switch
might be preferable too
const isTemplateStringFormat = (str: string) => { | ||
if (!str || str.length < 3) return false; | ||
return str.charAt(0) === '$' && str.charAt(1) === '{' && str.charAt(str.length - 1) === '}'; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The regex you added to the resolveSpacingDesignToken
method could be used here if you wanted
const isTemplateStringFormat = (str: string) => { | |
if (!str || str.length < 3) return false; | |
return str.charAt(0) === '$' && str.charAt(1) === '{' && str.charAt(str.length - 1) === '}'; | |
}; | |
const isTemplateStringFormat = (str: string) => { | |
const regex = /\$\{([^}]+)\}/g; | |
return regex.test(str); | |
}; |
20c3b6d
to
2b2936a
Compare
Approach
Only do this for margin and padding so we can first agree on the implementation structure and then once agreed we can apply it for all other tokens like direction, background color, border.
Additionally, the strategy is as follows
Use a set to determine which potential variables (cfMargin, cfPadding) could have template strings
If the variable is one of those types which could have a template string, run through a specific parser that will know how to handle the given type in the
designTokenRegistry
file. This way we can map specific parsers to specific variable types which may share or differ in their structure. Therefore as we add a new variable type (ie. border in a separate PR) we can refactor this code to generalize as we go alongPurpose
Stores the design token key instead of the hard coded value, and then is able to resolve the design token key and display the value instead.
TODOs