-
|
What version of Tailwind CSS are you using? What build tool (or framework if it abstracts the build tool) are you using? What version of Node.js are you using? What browser are you using? What operating system are you using? Hi, I'm trying to make a utility function to turn responsive props into tailwind classes, but the responsive class doesn't work. Here is the utility function import { WithResponsiveType } from "../types/Responsive.type";
// into tailwind responsive prefix
const withBreakpoints = (
value: WithResponsiveType<string>, // value to check
valueMap: Record<string, string>, // An object to map props values to a different tailwind classes
) => {
const classes: string[] = [];
if (value === "") return "";
if (typeof value === "object") {
for (const [bp, val] of Object.entries(value)) {
const className = bp === "initial" ? `${valueMap[val]}` : `${bp}:${valueMap[val]}`;
classes.push(className);
}
}
if (typeof value === "string") {
const className = `${valueMap[value]}`;
classes.push(className);
}
return classes.join(" ");
};
export default withBreakpoints;Here is the mapValue: export const FLEX_DIRECTION = {
row: "flex-row",
"row-reverse": "flex-row-reverse",
col: "flex-col",
"col-reverse": "flex-col-reverse",
};Here is my component const Flex = React.forwardRef<FlexElement, FlexProps>((props, ref) => {
const { children, className, direction = "", ...rest } = props;
const flex = ["flex", withBreakpoints(direction, FLEX_DIRECTION)];
return (
<div ref={ref} className={cnx(flex, className)} {...rest}>
{children}
</div>
);
});Here is the usage of the component with responsive props: <Flex direction={{ initial: "row", md: "col" }}>
<div className="w-10 h-10 bg-red-800"></div>
<div className="w-10 h-10 bg-red-800 md:bg-blue-800"></div>
<div className="w-10 h-10 bg-red-800"></div>
</Flex>Basically it just turns '{ initial: "row", md: "col" }' into 'flex-row md:flex-col'. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
As per the documentation:
Thus, the way you're trying to construct the class names fundamentally clashes with how Tailwind looks for class names. A work-around is to add the classes you'd like to generate to the Furthermore, the initial value "works" because the default value for |
Beta Was this translation helpful? Give feedback.
As per the documentation: