base-ui
base-ui copied to clipboard
mergeProps: support custom className merging
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
+1 vote
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)}
/>
)
}