Skip to content
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

Generate rules at compile that you know you will use dynamically later #511

Closed
d11z opened this issue Jan 17, 2022 · 6 comments
Closed

Generate rules at compile that you know you will use dynamically later #511

d11z opened this issue Jan 17, 2022 · 6 comments

Comments

@d11z
Copy link

d11z commented Jan 17, 2022

I have a <Button> component with a prop I call intent: 'primary' | 'secondary' | 'success' | 'danger' | 'warning' | 'info' which I'd like to use to determine the background color of the button. I feel like I'm using a quite naive approach by using a switch (intent) to return strings like "red-500" or "transparent" which get added to the markup like so:

<button class="btn bg-{getBackgroundColor(intent)}" {disabled} class:bg-opacity-50={disabled}>
  <slot/>
</button>

This works only for background colors that were previously statically generated. My workaround for this is stacking all the classes that I want available at runtime onto a random <div class="hidden ...">. This seems like a smelly solution to me; I was wondering if it was a common enough use case to add a config property to define which classes will always be available.

Maybe I am missing something though, and there is a more straightforward way to accomplish what I want. Otherwise, I'm a huge fan of unocss so far 👍.

@chu121su12
Copy link
Member

There's undocumented safelist feature that do exactly just this.

Playground

@d11z
Copy link
Author

d11z commented Jan 22, 2022

Are there are plans to support regexes in the safelist?

https://tailwindcss.com/docs/content-configuration#using-regular-expressions

@chu121su12
Copy link
Member

chu121su12 commented Jan 22, 2022

Lightly reading the source, what I catch is that tailwind's implementation requires a default safelist tokens to be matched against. Also they have extensive config you can reverse into a working class list, so it is pre-generate-able.

https://github.com/tailwindlabs/tailwindcss/blob/bab689ca8284265f51d5b66a1f3f1007850d97c1/src/lib/setupContextUtils.js#L670-L709 (line 696)

Since unocss' preset is mostly regex-based, It's quite difficult to pre-generate all the possible classes to be matched against.

One api I know to do this is in php faker. You can try to open a PR for js-based implementation.

@antfu
Copy link
Member

antfu commented Jan 22, 2022

Are there are plans to support regexes in the safelist?

There will be infinite possiblity to match a dynamic classes. Regex in safelist is not possible to support. You can use an inline function to generate the combinations you need.

@im-Qarch
Copy link

Are there are plans to support regexes in the safelist?

There will be infinite possiblity to match a dynamic classes. Regex in safelist is not possible to support. You can use an inline function to generate the combinations you need.

what if i want to set all of classes in safelist ?

@maelp
Copy link

maelp commented Nov 1, 2023

@im-Qarch I think the issue here is that there is no "all classes", because Uno creates class "on demand", so instead you have to tell explicitely the ones you want to generate, eg

["red", "green", "purple"].map((color) => `bg-${color}-500`)

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

No branches or pull requests

5 participants