Safelist own custom classes #7406
-
Is it true that the 'safelist' configuration option only works with the default Tailwind classes? See the following example: stylesheet.css: @tailwind base;
@tailwind components;
@tailwind utilities;
@layer components {
.icon { @apply inline-block w-5 h-5; }
} tailwind.config.js:
When building my css file the .icon class is being removed unless I actually use the class in my html files. The reason for using a safelist is because i'm using an package for rendering icons (blade-icons) and don't want to include this file in the 'content' section of tailwind.config.js as it's only one specific class. One solution would be to move the .icon class from a layer, but then I won't be able to override properties without using important. Edit: Oops, after reading my own question I noticed I made a mistake.. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 4 replies
-
Hey! Thank you for your question! That should still work, but your configuration is incorrect. The safelist option should be an array. More information: https://tailwindcss.com/docs/content-configuration#safelisting-classes |
Beta Was this translation helpful? Give feedback.
-
@RobinMalfait Can we use a similar approach when it comes to stylesheet.css:
tailwind.config.js:
At the moment, I'm getting... |
Beta Was this translation helpful? Give feedback.
-
Hi everybody, const ResponsiveText = ({fontSize})=>{
return(
<div className={`textScaleFast-[${fontSize}]`}>Responsive text</div>)
}; That config doesn't work: // tailwind.config.js
const safeList = [];
const fontValues = [10, 20, 30, 40];
for (const value of fontValues) {
safeList.push(`textScaleFast-[${value}]`);
}
module.exports = {
safeList,
plugins: [
plugin(({ matchUtilities }) => {
matchUtilities(
{
textScaleFast: (value) => {
const numberValue = Number(value);
const fontSize = `${numberValue - 9.6}px`;
return {
fontSize: `calc(${fontSize} + 3vw)`,
};
},
},
);
}),
],
};
Also hardcoding class names doesn't work: // tailwind.config.js
module.exports = {
safeList: [
"textScaleFast-[10]",
"textScaleFast-[20]",
"textScaleFast-[30]",
"textScaleFast-[40]",
],
plugins: [
plugin(({ matchUtilities }) => {
matchUtilities(
{
textScaleFast: (value) => {
const numberValue = Number(value);
const fontSize = `${numberValue - 9.6}px`;
return {
fontSize: `calc(${fontSize} + 3vw)`,
};
},
},
);
}),
],
};
While hardcoding them in the jsx actually works: // tailwind.config.js
module.exports = {
plugins: [
plugin(({ matchUtilities }) => {
matchUtilities(
{
textScaleFast: (value) => {
const numberValue = Number(value);
const fontSize = `${numberValue - 9.6}px`;
return {
fontSize: `calc(${fontSize} + 3vw)`,
};
},
},
);
}),
],
};
// component.jsx
const safeList = [
"textScaleFast-[10]",
"textScaleFast-[20]",
"textScaleFast-[30]",
"textScaleFast-[40]",
];
const ResponsiveText = ({fontSize})=>{
return(
<div className={`textScaleFast-[${fontSize}]`}>Responsive text</div>)
} The last option might be ok, but I'd like to be able to dynamically generate the classes, based on an array of values like in the first example. Is there a way to achieve that? Is it |
Beta Was this translation helpful? Give feedback.
Hey! Thank you for your question!
Much appreciated! 🙏
That should still work, but your configuration is incorrect. The safelist option should be an array. More information: https://tailwindcss.com/docs/content-configuration#safelisting-classes