mergeProps
Merges React prop objects, chaining event handlers and joining classes and styles.
mergeProps merges several sets of React props into one. It is Base UI's mergeProps re-exported through a Dify UI subpath, for composing a part's own props with the props a caller passes.
Usage guidelines
- Use it when a part adds its own props to the caller's: event handlers from both sides keep running, and
classNameandstylecombine instead of one replacing the other. - Pass the caller's props last: for plain props, the rightmost object wins, so the caller can override a default such as
relortype. - Resolve Tailwind conflicts with
cn:mergePropsconcatenates class strings without resolving conflicting utilities. Merge default and caller classes withcnfirst, as in the example below. - Merge refs separately:
refis not merged. Combine refs withuseMergedRefs. - Do not use it for a
renderelement: Base UI already merges a part's props into the element passed torender.useRenderalso merges itspropswith therenderelement.
Each argument accepts React props, a function receiving the previously merged props, or undefined.
The merge rules:
- Plain props follow
Object.assign: the rightmost value overwrites the others. - Event handlers are chained and run right to left, so the rightmost handler runs first. For a React synthetic event, it can call
event.preventBaseUIHandler()to stop the handlers to its left. classNamestrings are concatenated right to left.styleobjects are merged with the rightmost values winning.- A function argument receives the props merged so far and returns new props. It does not run the merge logic, so it chains handlers itself.
refis not merged.
Examples
Link with default attributes
The part's defaults come first and the caller's props last, so a caller-supplied rel or onClick still applies.
API reference
mergeProps
Merges multiple sets of React props. It follows the Object.assign pattern where the rightmost object's fields overwrite the conflicting ones from others. This doesn't apply to event handlers, className and style props.
Event handlers are merged and called in right-to-left order (rightmost handler executes first, leftmost last). For React synthetic events, the rightmost handler can prevent prior (left-positioned) handlers from executing by calling event.preventBaseUIHandler(). For non-synthetic events (custom events with primitive/object values), all handlers always execute without prevention capability.
The className prop is merged by concatenating classes in right-to-left order (rightmost class appears first in the string). The style prop is merged with rightmost styles overwriting the prior ones.
Props can either be provided as objects or as functions that take the previous props as an argument. The function will receive the merged props up to that point (going from left to right): so in the case of (obj1, obj2, fn, obj3), fn will receive the merged props of obj1 and obj2. The function is responsible for chaining event handlers if needed (that is, we don't run the merge logic).
Event handlers returned by the functions are not automatically prevented when preventBaseUIHandler is called. They must check event.baseUIHandlerPrevented themselves and bail out if it's true.
| Parameter | Type | Description |
|---|---|---|
a required | InputProps<T> | Props object to merge. |
b required | InputProps<T> | Props object to merge. The function will overwrite conflicting props from |
Returns
WithBaseUIEvent<React.ComponentPropsWithRef<T>>The merged props.