base-ui icon indicating copy to clipboard operation
base-ui copied to clipboard

mergeProps: support custom className merging

Open rikbrown opened this issue 1 year ago • 2 comments

Feature request

Summary

Right now mergeProps just concatenates classNames.

Tailwind users often use tailwind-merge or similar to intelligently merge tailwind classnames, which do not work on "left-to-right" precedence order.

It would be advantageous to be able to create a custom version of mergeProps that uses a provided className merging strategy.

Motivation

Increased ergonomics w/ Tailwind

rikbrown avatar Sep 06 '25 20:09 rikbrown

+1 vote

daonham avatar Dec 12 '25 04:12 daonham

I made this helper in the meantime, to also handle the state callback:

function twMergeCallback<Args extends readonly unknown[]>(
	...classNameInputs: ReadonlyArray<
		ClassNameValue | ((...args: Args) => ClassNameValue)
	>
) {
	return (...args: Args) =>
		twMerge(
			classNameInputs.map((input) =>
				input instanceof Function ? input(...args) : input,
			),
		)
}
import { Button } from '@base-ui/react'
function CustomButton(props: Button.Props) {
	return (
		<Button
			{...props}
			className={twMergeCallback("bg-blue-600 p-4 rounded", props.className)}
		/>
	)
}

itsMapleLeaf avatar Feb 07 '26 23:02 itsMapleLeaf