Safelist pattern for arbitrary values #7908
-
What version of Tailwind CSS are you using? I am trying to whitelist a set of arbitrary values since the classes is set within a CMS. I have tried to whitelist the following pattern:
But when compiling I get the following error:
So the question is: How do I whitelist a set of arbitrary values? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 6 replies
-
Hey! Yeah you can only safelist using patterns against things that Tailwind would generate based on the config, not arbitrary values. In your situation just do this: safelist: [
'top-[0%]',
'top-[10%]',
'top-[20%]',
'top-[30%]',
'top-[40%]',
'top-[50%]',
'top-[60%]',
'top-[70%]',
'top-[80%]',
'top-[90%]',
'top-[100%]',
'left-[0%]',
'left-[10%]',
'left-[20%]',
'left-[30%]',
'left-[40%]',
'left-[50%]',
'left-[60%]',
'left-[70%]',
'left-[80%]',
'left-[90%]',
'left-[100%]',
] You could get clever and generate programmatically if you wanted to as well: safelist: [
...[...Array(10).keys()].flatMap(i => [`top-[${i*10}%]`, `left-[${i*10}%]`])
] |
Beta Was this translation helpful? Give feedback.
-
@adamwathan could
This is mostly for DX experience to avoid having a very long string for pattern. Context: my current issue is I am building a highly dynamic theme system with dynamic components where the classes are only computed at run time, thus I have to let tailwind know in the app where i consume these components which classes to safe list. However, some can be really dynamic like "px-[2rem]" where the value 2rem is not known upfront as the consumer will pass only the value 2 tot he component (so that could be 2 or maybe 0.9 or anything else really) |
Beta Was this translation helpful? Give feedback.
-
Sorry to resurrect this, but there are now options of Makes it a lot more readable than code within the config, in my opinion. |
Beta Was this translation helpful? Give feedback.
Hey! Yeah you can only safelist using patterns against things that Tailwind would generate based on the config, not arbitrary values.
In your situation just do this:
You could get clever and generate programmatically if you wanted to as well: