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: 4 comments 7 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.
-
I recently encountered the same issue, and my use case involves dynamically updating class name in code. I discovered that that using CSS variables is an effective solution. The reason this works is that the literal string Applying the Concept to Specific ScenariosTaking @shivasaisagar18 's example:
This is similarly to @damianof's case:
We can use a placeholder like
This approach allows us to adjust the grid column widths dynamically using JavaScript. Here is the complete code example in react: import type React from "react"
import { useState } from "react"
interface CustomGridProps extends React.HTMLAttributes<HTMLDivElement> {
firstColWidth?: string
secondColWidth?: string
}
export function CustomGrid({
className,
children,
firstColWidth = "1fr",
secondColWidth = "2fr",
...props
}: CustomGridProps) {
return (
<div
className={`grid grid-cols-[var(--custom-grid-cols-property)] ${className}`}
style={
{
"--custom-grid-cols-property": `${firstColWidth} ${secondColWidth}`,
} as React.CSSProperties
}
{...props}
>
{children}
</div>
)
} DemoThe result demonstrates how the grid column widths can be updated dynamically at runtime using the provided props. Screenity.video.-.Jan.23.2025.webm |
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: