How to Change Tailwind Styles Based On React Variable #1507
Replies: 11 comments 28 replies
-
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
Thanks @PILO7980 ! I would think these are equivalent. Why does the it work only when there are extra parentheses? 🤔 |
Beta Was this translation helpful? Give feedback.
-
This works, but I really hate the syntax - I know this has nothing to do with tailwind but I hate having to write stuff like that. I really wish there was some :class like there there is in alpine and vue, making it a lot more readable. (I'm also not sure if it's only me but sometimes I have to put my conditional classes at the end instead of at the start, not sure if this is me just being bad at tailwind but it looks even worse when you have a combination). |
Beta Was this translation helpful? Give feedback.
-
You can also use template strings: export const TextInput = ({error, ...props}: TextInputProps) => {
return (
<div className="flex flex-row justify-center">
<input type="text" {...props} className={`w-full p-3 my-3 text-xl border-4 ${error ? "border-red-400" : ""}`} />
</div>
)
} |
Beta Was this translation helpful? Give feedback.
-
Just to complement with another case... I've tried to do something like // color being like red-200
const Area = ({ title, color }: AreaProps) => {
return (
<div className={`h-[50vh] flex items-center justify-center font-sans bg-${color}`}>
<div>{title}</div>
</div>
)
} You had to provide the entire property, like: // bgColor is something like bg-red-200
const Area = ({ title, bgColor }: AreaProps) => {
return (
<div className={`h-[50vh] flex items-center justify-center font-sans ${bgColor}`}>
<div>{title}</div>
</div>
)
} |
Beta Was this translation helpful? Give feedback.
-
Simple solution just use this tool |
Beta Was this translation helpful? Give feedback.
-
You could use the class variance authority library which makes it easy to create component libraries with tailwind. |
Beta Was this translation helpful? Give feedback.
-
Maybe you can achieve what you need using the <img
className="absolute bottom-0"
style={{ transform: `rotate(${needleRotation}deg)` }}
src="image.png"
/> |
Beta Was this translation helpful? Give feedback.
-
Try to use prop-types https://blog.logrocket.com/theming-react-components-tailwind-css/ // src/components/Button.js
import themeProps from "./themeProps";
const Button = ({ children, color, ...rest }) => {
return (
<button className={`rounded-md bg-${color} text-text-base px-3 py-1`} {...rest}>
{children}
</button>
);
};
Button.propTypes = {
...themeProps,
};
Button.defaultProps = {
color: "primary",
};
export default Button; |
Beta Was this translation helpful? Give feedback.
-
I hope you see this -> https://tailwindcss.com/blog/tailwindcss-v3-2#data-attribute-variants |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello! I have a header bar, and I'm trying to have it display / hide when the user click a button a toggles a boolean React component state variable.
This is how I would like it to look when expanded:

Here is my code for the toggle button:
Here is a snippet of the div that I would like to hide:
When I add the "hidden" tailwind class (short for
display: hidden;
) then the expandable part does hide correctly:If I try to modify the classes dynamically based on the React variable though... it doesn't work.
Beta Was this translation helpful? Give feedback.
All reactions