Replies: 1 comment 10 replies
-
Hey @MrEfrem! 👋 I didn't implement the object syntax because I think it's easier to read code using the Long explanationWhen I read a collection of conditionally applied classes, I'm always interested in the condition first to know whether I even need to think about the classes or not and only afterwards I want to deal with the actual classes. The object notation turns that around and increases the cognitive load because I need to skip the string with all the classes to get to the condition and then return back to the string again. Let me show you what I mean on a real example (formatted with Prettier), first using an object. clsx({
'px-8 sticky grid items-center grid-flow-col grid-cols-1 transition cursor-pointer bg-clip-padding border-y border-white hover:bg-grey-700-opaque-5':
true,
'bg-white': !transparentWhenFloaty || position !== Position.Floaty,
invisible: isInvisible,
// Fixes sticky position bug in Chrome. More info: <private link>
'before:absolute before:left-0 before:-top-px before:w-full before:h-px before:bg-white':
position === Position.StickyTop,
'before:absolute before:left-0 before:-bottom-px before:w-full before:h-px before:bg-white':
position === Position.StickyBottom,
'border-t-grey-700/10': position === Position.StickyBottom,
'border-b-grey-700/10': position !== Position.StickyBottom,
}) Let's say I need to fix something at this DOM node when the component is in the StickyBottom position and want to check all the classes being applied in this state. Here are some aspects I don't like about this syntax:
Now let's take a look at the same code using the logical clsx(
'px-8 sticky grid items-center grid-flow-col grid-cols-1 transition cursor-pointer bg-clip-padding border-y border-white hover:bg-grey-700-opaque-5',
(!transparentWhenFloaty || position !== Position.Floaty) && 'bg-white',
isInvisible && 'invisible',
// Fixes sticky position bug in Chrome. More info: <private link>
position === Position.StickyTop &&
'before:absolute before:left-0 before:-top-px before:w-full before:h-px before:bg-white',
position === Position.StickyBottom &&
'before:absolute before:left-0 before:-bottom-px before:w-full before:h-px before:bg-white',
position === Position.StickyBottom ? 'border-t-grey-700/10' : 'border-b-grey-700/10'
) Thinking about the three points mentioned:
ConclusionI think the object syntax shouldn't be used to apply Tailwind classes conditionally and therefore I didn't add support for it in tailwind-merge. Because of this I built a But if you have a use case where you think the object syntax makes more sense than Related: #121 (reply in thread), #124 |
Beta Was this translation helpful? Give feedback.
-
classnames
,clxs
libraries support very useful object syntax to conditionally apply rules.Would it be possible to implement the same syntax here? Then we will be able to use only one library for class names managing.
Beta Was this translation helpful? Give feedback.
All reactions