Skip to content

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

Merged
merged 3 commits into from
Oct 27, 2022
Merged

Fix/ loadDesignTokens API #2302

merged 3 commits into from
Oct 27, 2022

Conversation

lidord-wix
Copy link
Contributor

Description

Fix Colors.loadDesignTokens API
@Effanuel FYI - we decided to change the API a bit to support adding features in the future easily

Changelog

Fix Colors.loadDesignTokens API

@@ -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}) {
Copy link
Collaborator

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 like loadDesignTokens(options: TokensOptions)? Then you can use options?.primaryColor etc.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


private generateDarkModeTokens(color: string) {
const colorPalette: string[] = _.reverse(this.generatePalette(color));
private generateDesignTokens({primaryColor, dark}: {primaryColor: string; dark?: boolean}) {
Copy link
Collaborator

Choose a reason for hiding this comment

The 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 primaryColor: string and dark: boolean.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

const colorPalette: string[] = _.reverse(this.generatePalette(color));
private generateDesignTokens({primaryColor, dark}: {primaryColor: string; dark?: boolean}) {
const colorPalette: string[] = dark
? _.reverse(this.generatePalette(primaryColor))
Copy link
Collaborator

Choose a reason for hiding this comment

The 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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually tried it and found it less readable that generateDesignTokens() will need to get the primaryColor, the palette, and the dark boolean as parameters. it gets all the logic out of the method.
And I think it's not that bad to call it twice since it happens only once when the app starts.

Copy link
Collaborator

Choose a reason for hiding this comment

The 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:
let colorPalette = this.generatePalette(primaryColor); if (dark) { colorPalette = _.reverse(colorPalette); }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ohh right!
done

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;
Copy link
Collaborator

Choose a reason for hiding this comment

The 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).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I rewrote this condition and ended up with this one:

const mainColor = this.isDark(primaryColor) === !!dark ? color30 : primaryColor

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:

let mainColor = isPrimaryColorDark ? primaryColor : color30;
if (dark) {
  mainColor = isPrimaryColorDark ? color30 : primaryColor;
}

WDYT?

Copy link
Collaborator

@Inbal-Tish Inbal-Tish Oct 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think this is readable:

const isPrimaryColorDark = this.isDark(primaryColor)
let mainColor = isPrimaryColorDark ? primaryColor : color30;
if (dark) {
  mainColor = isPrimaryColorDark ? color30 : primaryColor;
}

@lidord-wix lidord-wix requested a review from Inbal-Tish October 27, 2022 08:42
@Inbal-Tish Inbal-Tish merged commit e6fef55 into master Oct 27, 2022
@ethanshar
Copy link
Collaborator

@lidord-wix
I think we should release this as a hotfix so people won't get a chance to use the previous API

lidord-wix added a commit that referenced this pull request Oct 30, 2022
* fix loadDesignTokens API

* review fixes

* update generateDesignTokens
@lidord-wix
Copy link
Contributor Author

@lidord-wix I think we should release this as a hotfix so people won't get a chance to use the previous API

Done :)
6.22.3

@lidord-wix lidord-wix deleted the fix/loadDesignTokens_API branch November 29, 2022 11:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants