-
Notifications
You must be signed in to change notification settings - Fork 736
Fix/ loadDesignTokens API #2302
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,10 +49,10 @@ export class Colors { | |
* Load light and dark schemes based on generated design tokens | ||
* @param color - palette color | ||
*/ | ||
loadDesignTokens(color: string) { | ||
loadDesignTokens({primaryColor}: {primaryColor: string}) { | ||
this.loadSchemes({ | ||
light: this.generateLightModeTokens(color), | ||
dark: this.generateDarkModeTokens(color) | ||
light: this.generateDesignTokens({primaryColor}), | ||
dark: this.generateDesignTokens({primaryColor, dark: true}) | ||
}); | ||
} | ||
|
||
|
@@ -220,38 +220,19 @@ export class Colors { | |
return this.shouldSupportDarkMode && Scheme.getSchemeType() === 'dark' ? _.reverse(palette) : palette; | ||
}); | ||
|
||
generateDesignTokens(color: string, mode: 'light' | 'dark' = 'light') { | ||
return mode === 'light' ? this.generateLightModeTokens(color) : this.generateDarkModeTokens(color); | ||
} | ||
|
||
private generateLightModeTokens(color: string) { | ||
const colorPalette: string[] = this.generatePalette(color); | ||
const color30 = colorPalette[2]; | ||
const color50 = colorPalette[4]; | ||
const color70 = colorPalette[6]; | ||
const color80 = colorPalette[7]; | ||
|
||
const mainColor = this.isDark(color) ? color : color30; | ||
|
||
return { | ||
$backgroundPrimaryHeavy: mainColor, | ||
$backgroundPrimaryLight: color80, | ||
$backgroundPrimaryMedium: color70, | ||
$iconPrimary: mainColor, | ||
$iconPrimaryLight: color50, | ||
$textPrimary: mainColor, | ||
$outlinePrimary: mainColor | ||
}; | ||
} | ||
|
||
private generateDarkModeTokens(color: string) { | ||
const colorPalette: string[] = _.reverse(this.generatePalette(color)); | ||
private generateDesignTokens({primaryColor, dark}: {primaryColor: string; dark?: boolean}) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why the parameter here is an object? This method is private so no worries about changing its signature in the future. The parameters could be simply There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
const colorPalette: string[] = dark | ||
? _.reverse(this.generatePalette(primaryColor)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can generate the palette outside of the condition and reverse the array if it is for a dark palette, this way you save the code duplication There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I actually tried it and found it less readable that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree that the palette should be generated in the method, but you can still do it once and only reverse the array if dark = true. For example: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ohh right! |
||
: this.generatePalette(primaryColor); | ||
const color30 = colorPalette[2]; | ||
const color50 = colorPalette[4]; | ||
const color70 = colorPalette[6]; | ||
const color80 = colorPalette[7]; | ||
|
||
const mainColor = this.isDark(color) ? color30 : color; | ||
let mainColor = this.isDark(primaryColor) ? primaryColor : color30; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here - check if isDark() once and apply the changes to mainColor if dark is true (instead of checking isDark() twice in this case). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I rewrote this condition and ended up with this one:
because I want to use color30 only if the color is light in light mode or dark in dark mode, but I'm not sure if it makes the code more readable than that:
WDYT? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I think this is readable:
|
||
if (dark) { | ||
mainColor = this.isDark(primaryColor) ? color30 : primaryColor; | ||
} | ||
|
||
return { | ||
$backgroundPrimaryHeavy: mainColor, | ||
|
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.
Why not make the type a defined type, like
export type TokensOptions: {primaryColor: string}
, and have a parameter likeloadDesignTokens(options: TokensOptions)
? Then you can useoptions?.primaryColor
etc.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.
Done