Skip to content

Conversation

@Marruk
Copy link
Collaborator

@Marruk Marruk commented May 23, 2023

Recently color-mix got supported by all major browsers.

It has the potential to clean up our semantic colour set by basing variants on a smaller set of base colours. Some more context here.

This PR is a POC taking the actionBackground and actionFocus as examples.

I added two utility functions to keep the code cleaner as the syntax of color-mix is a bit long, but those are optional of course.

@glenngijsberts
Copy link
Contributor

CleanShot 2023-05-23 at 15 16 02@2x

Not sure if this is really nice

Comment on lines +1 to +29
/**
* Utility function to apply transparency to a color (variable) using `color-mix`
* @param {string} color - The color to make transparent. Can be a theme variabale, a hex code or "currentColor"
* @param {number} opacity - The desired opacity, a number between 0 and 1
*/
export const transparentize = (color: string, opacity: number) => {
const parsedColor = color === 'currentColor' ? 'currentColor' :
color.charAt(0) === '#' ? color :
color.indexOf('var(--') !== -1 ? color : `var(--${color})`

const parsedOpacity = 100 - (opacity * 100)

return `color-mix(in srgb, ${parsedColor}, transparent ${parsedOpacity}%)`
}

export const transparentGradient = (color: string, direction: string, fromOpacity: number, toOpacity: number) => {
const parsedColor = color === 'currentColor' ? 'currentColor' :
color.charAt(0) === '#' ? color :
color.indexOf('var(--') !== -1 ? color : `var(--${color})`

const parsedFromOpacity = 100 - (fromOpacity * 100)
const parsedToOpacity = 100 - (toOpacity * 100)

return `linear-gradient(
${direction},
color-mix(in srgb, ${parsedColor}, transparent ${parsedFromOpacity}%),
color-mix(in srgb, ${parsedColor}, transparent ${parsedToOpacity}%)
)`
} No newline at end of file
Copy link
Contributor

Choose a reason for hiding this comment

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

I would put them in their own files, import them here and then export them from colors/index.ts.

Comment on lines +1 to +5
/**
* Utility function to apply transparency to a color (variable) using `color-mix`
* @param {string} color - The color to make transparent. Can be a theme variabale, a hex code or "currentColor"
* @param {number} opacity - The desired opacity, a number between 0 and 1
*/
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this should live inside the commit message instead of a comment.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The plus side of this for me was that the tooltip of the function will explain what is needed, which I thought would be helpful for the opacity specifically (there are a number of correct ways to pass that through). Any alternative method for that?

Copy link
Contributor

Choose a reason for hiding this comment

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

I do like it tbh, but since it's not a practice in my codebase it seems a bit inconsistent to me 🤷 but yeah not sure what the others think.

@glenngijsberts
Copy link
Contributor

glenngijsberts commented May 23, 2023

I'm not really sure if we should introduce utility functions like this instead of a set of colors/values defined in the theme 🤔 Could you maybe explain that in your PR description why you would go for this solution?

// Action
--action: var(--earth400);
--actionBackground: var(--earth50);
--actionBackground: ${transparentize(color.action, opacity.statusBackgroundColor)};
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this always have an opacity? It introduces this bug again:

CleanShot 2023-05-23 at 15 27 21@2x

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah, will use a new activeBackground here instead so we can distinguish the two use-cases

@Marruk
Copy link
Collaborator Author

Marruk commented May 23, 2023

Not sure if this is really nice

Went with a quick solution, we can do anything.

I'm not really sure if we should introduce utility functions like this instead of a set of colors/values defined in the theme 🤔 Could you maybe explain that in your PR description why you would go for this solution?

I'm not quite sure what you mean. Are you referring to the removal of actionFocus? Or the usage of helper utility functions instead of color-mix directly?

background-color: transparent;
border-radius: calc(${radius.md} + 1px);
box-shadow: 0 0 0 ${space[4]} ${color.actionFocus};
outline: ${space[4]} solid ${transparentize(color.action, opacity.statusFocusColor)};
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The reason for this change is because color-mix doesn't play nicely with css variables + box-shadow (yet)

@glenngijsberts
Copy link
Contributor

Or the usage of helper utility functions instead of color-mix directly?

@Marruk this indeed!

@Marruk
Copy link
Collaborator Author

Marruk commented May 25, 2023

Or the usage of helper utility functions instead of color-mix directly?

@Marruk this indeed!

Ah, that was just an idea to keep the code a bit cleaner since the syntax of color-mix is quite long. Completely optional though :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants