[V2] rgba function should behave like Sass rgba function
The rgba function that you created expects
${rgba(255, 205, 100, 0.7)} or
${rgba({ red: 255, green: 205, blue: 100, alpha: 0.7 })};
when we use it in styled-components.
I don't know, my approach might be wrong but If I use your rgba function like ${rgba('#000', 0.1)} in order to get the same css rgba(0, 0, 0, 0.1) when I use rgba function in Sass, it would be better.
I use this function to get the result that I mentioned.
const hexToRgb = (hex) => {
// http://stackoverflow.com/a/5624139
var shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
hex = hex.replace(shorthandRegex, function (m, r, g, b) {
return r + r + g + g + b + b;
});
var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result ? {
r: parseInt(result[1], 16),
g: parseInt(result[2], 16),
b: parseInt(result[3], 16),
} : null;
};
const rgba = (hex, alpha) => {
const color = hexToRgb(hex);
return `rgba(${color.r}, ${color.g}, ${color.b}, ${alpha})`;
};
export const Header = styled.div `
box-shadow: 0 0 5px ${rgba('#000', 0.1)};
`;
I wonder your ideas about it.
I look forward to use Polished in a real project. Thanks
Interesting case. The functionality is already covered by https://polished.js.org/docs/#transparentize
To be honest I'm not sure. I thought this would not be needed, but if this is a very common case for Sass users we might want to consider supporting it.
Thanks for sharing your thoughts Nik,
I've been asking many people around me for 2 weeks. the most common built-in functions in sass in my circle are that 1) rgba 2) lighten, darken
Additionally, I've investigated the most popular front-end libraries use Sass, if I compare usage of rgba(#hex, alpha) or transparentize(alpha, #hex) in that libraries
| Library | rgba usage | transparentize usage |
|---|---|---|
| Bootstrap | >3+ | 0 |
| Bourbon | >3+ | 0 |
| Foundation | >3+ | 0 |
| Ionic | >3+ | 0 |
| Materialize | 0 | 1 |
| Bulma | >3+ | 0 |
Personally, I always preferred to use rgba rather than transparentize due to these reason
- It is simple just 4 letters
- Easy to write while setting colors #34d instead of rgba(51, 68, 221, 1)
- Easy to remember hex codes
- When collecting colors on Sketch or Photoshop, easy to copy hex code and give the alpha channel
- It is the same experience on Sketch or Photoshop like opacity. with Transparentize, you need to subtract (1 - opacity) to get the same result that you see on design applications.
I do see where this would be useful, what we would need to sort out though is how this jives with what RGBA currently does for us. Would the API become a bit too confusing if it supported both this use case as well as what it currently does?
@bhough shouldn't be too crazy to do since we can match for the amount of arguments
I'm still be undecided if we want to encourage a simpler API with the explicit function or opt in for this special case …
@tolgaergin thanks a ton for the research! 🙌
@nikgraf agreed, meant more confusing as it seems to be doing two different things using the same method. If that pattern is already established, it may not be too bad to just implement it as opt in, while leaving transparentize in place.
question is: do we want this now or wait a bit longer until more people ask for it?
In the interest of getting a release out soon(ish), I would vote for us to wait. If we lacked a method for doing this it would be different, but with tranparentize, at worst it's a minor inconvenience.
sounds good!
@bhough Max was thinking about launching at React London. I tried to reach you via private message on Twitter.
Personally I always use rgba like this; rgba(#fff, .5). I think this should be supported.
I hope the polished can support multiple function like ${rgba('#000', 0.1)} or ${rgba('#cbcbcb', 0.1)} or ${rgba(200, 200, 200, 0.1)} or ${rgba(green, 0.1)}. Thank you!
@nikgraf @mxstbr I think at this point there is enough demand to warrant implementing this. Any objections?
Just left a comment on the PR. We are going to merge this asap.
@nikgraf , @bhough Just wanted to say thanks for getting this feature added in! You guys do some great work. 😃👏
didn't mention me @alocke12992 😭 😄
Found another issue with rgba and gradient functions. This cause an error: background-image: linear-gradient(to right, ${rgba('#DE6262', .2)}, ${rgba('#FFB88C', .2)});
Use this instead
export const linearGradient = string => {
return linear-gradient(${string})
}
Usage example:
background-image: ${linearGradient(to right, ${rgba('#DE6262', .2)}, ${rgba('#FFB88C', .2)})};
I hope someone will find this solution usefull.