-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Allow colors to be defined as closures #1676
Conversation
@hawezo looks good to me but will transparency be polyfillable for IE11? It seems like it won't.
Maybe it's worth supporting 2 modes of variables generation? |
@AndrewBogdanovTSS It should be polyfillable - answered with details on the issue to not pollute this PR. |
Sorry I'm probably being dense here but am I right that the example you gave in the opening comment doesn't actually demonstrate anything that doesn't already work without adding this new functionality? For example if you just did this in your config: // tailwind.config.js
module.exports = {
theme: {
colors: {
primary: '#202020',
},
},
} ...Tailwind already does the necessary conversion to rgba with the opacity property automatically under the hood, so this will work already: <div class="bg-primary bg-opacity-50"></div> Can you give an example of something that doesn't currently work that this feature would enable? Am I right in thinking your actual use case is something more like this: // tailwind.config.js
module.exports = {
theme: {
colors: {
primary: variable => `rgba(var(--color-primary), var(${variable}, 1))`,
},
},
} If you could share your actual use case that would be helpful, thanks. |
Yes sorry, I tried to simplify my example. Your last snippet describes accurately what this feature would allow me to do. I would have the following: :root {
--color-primary: 31,31,31;
} And the following class would be generated thanks to your snippet: .text-primary {
color: rgba(var(--color-primary), var(--text-opacity, 1));
}
.bg-primary {
color: rgba(var(--color-primary), var(--bg-opacity, 1));
} Which would allow to do this: <div class="bg-primary bg-opacity-50"></div>
<div class="text-primary text-opacity-50"></div> The difference being that the primary color can change at runtime thanks to |
I'm good with adding this but want to make sure the API is well-thought out. Specifically the variable we are passing in. Right now we're using positional arguments in this PR which could be regrettable if we ever needed to pass another value through. I think we should pass this through as an object where the user can destructure out the pieces they need, but that means it's important that we come up with a good name for the property since the user will have to type it. I'm also conflicted on if we should pass the variable name through or if we should pass the entire alpha declaration through, like If we passed Can you think of any downside to passing the whole |
Good points. I'd also prefer an object to be passed, and it'll be consistent with the rest of Tailwind. Regarding whether to pass About the name, I can think of the following:
I agree that naming this one might not be the easiest. |
I think |
I did the requested changes. Thank you! |
Awesome thanks! @bradlc any thoughts on this, especially around how it might make your life more difficult with the IntelliSense extension? |
Hey hey, any chance this is going in the next release, @adamwathan? |
@innocenzi I'd like to but need to get the ok from @bradlc that it won't break the intellisense plugin before releasing. |
Eh I bet this is actually going to be totally fine the more I think about it, and it's going to be very lightly used for the first while before people really learn about it so going to merge :) thanks for the feature! |
Ah, thank you! |
Thanks, @adamwathan @innocenzi |
@innocenzi @adamwathan Any idea on how this would work with EDIT: theme() fixed by (#2919) |
This pull request allows colors to be defined using closures. The closure takes an
opacityVariableName
as a parameter, which allows to use the opacity variable's name in the color definition:This allows to take advantage of the new
opacity
utilities. It is especially useful for theming, as seen in the example above.This example will generate the following
text
andbackground
color utilities (and other ones which I omitted for simplicity's sake):This allows us to use any
opacity
utility with our customcolor
implementation:This is actually needed for me to integrate the
opacity
utilities totailwindcss-theming
, but it would benefit anyone who wants to use a custom color implementation using CSS variables.