Structure your Tailwind CSS classes for readability and maintainability. Instead of long chains of conditional classes with &&
operators, use object syntax with clear naming patterns. This improves code readability and makes maintenance significantly easier.
Instead of this:
className={cn(
"h-9 w-9 p-0 font-normal",
modifiers?.today && "bg-accent text-accent-foreground",
modifiers?.selected && "bg-primary text-primary-foreground hover:bg-primary hover:text-primary-foreground",
modifiers?.outside && "text-muted-foreground opacity-50 pointer-events-none",
// many more conditions
)}
Do this instead:
className={cn({
"h-9 w-9 p-0 font-normal": true,
"bg-accent text-accent-foreground": modifiers?.today,
"bg-primary text-primary-foreground hover:bg-primary hover:text-primary-foreground": modifiers?.selected,
"text-muted-foreground opacity-50 pointer-events-none": modifiers?.outside,
// cleaner and easier to read
})}
Additional recommendations:
touch-manipulation
instead of style=
)rounded-md
instead of inline styles for border radiustext-start
rather than text-left
These practices make your code more maintainable, easier to review, and better for internationalization.
Enter the URL of a public GitHub repository