Skip to content

composite class does not merge with individual class #553

@unional

Description

@unional

Describe the bug

Hi @dcastil, this is sort of both a bug and a feature.

tailwind-merge current behavior for merging composite class with their individual class like this:

const a = twMerge('pl-1 p-0') // 'p-0'
const b = twMerge('p-0 pl-1') // 'p-0 pl-1'

This works fine locally, I believe, even with this:

const b = twMerge('p-0 pl-1') // 'p-0 pl-1'
const c = twMerge('p-0') // 'p-0'

because tailwind will place the .p-0 class before .pl-1 class:

.p-0 { ... }

.pl-1 { ... }

However, it will not work when a Tailwind app consuming a Tailwind lib:

// lib
export const A = ({ className }) => <div className={twMerge('p-0', className)}/>

/* got className='p-0 pl-1' */
export const B = ({ className }) => <A className='pl-1' />

// app
import { A, B } from 'lib'

export const C = () => (
  <div className='p-0'>
    <A />
    <B />
  </div>
)

Since p-0 appears in app, the resulting CSS:

/* from lib */
.p-0 { ... }
.pl-1 { ... }

/* from app */
.p-0 { ... }

This cause B to rendered with p-0 instead of pl-1.

PostCSS plugin postcss-discard-duplicates could not help here because it kept the last duplicate:

/* from lib */
/* .p-0 { ... } -- removed by postcss-discard-duplicates */
.pl-1 { ... }

/* from app */
.p-0 { ... }

One solution I can think of is transform p0 pl-1 to pr-0 pt-0 pb-0 pl-1.
But then tailwind couldn't generate the right classes.

Metadata

Metadata

Assignees

No one assigned

    Labels

    context-v3Related to tailwind-merge v3

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions