# 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`](https://base-ui.com/react/utils/merge-props) re-exported through a Dify UI subpath, for composing a part's own props with the props a caller passes.

```tsx
import { mergeProps } from '@langgenius/dify-ui/merge-props'
```

## Usage guidelines

- **Use it when a part adds its own props to the caller's**: event handlers from both sides keep running, and `className` and `style` combine 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 `rel` or `type`.
- **Resolve Tailwind conflicts with `cn`**: `mergeProps` concatenates class strings without resolving conflicting utilities. Merge default and caller classes with [`cn`](/utilities/cn.md) first, as in the example below.
- **Merge refs separately**: `ref` is not merged. Combine refs with [`useMergedRefs`](/utilities/use-merged-refs.md).
- **Do not use it for a `render` element**: Base UI already merges a part's props into the element passed to `render`. [`useRender`](/utilities/use-render.md) also merges its `props` with the `render` element.

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.
- `className` strings are concatenated right to left. `style` objects 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.
- `ref` is 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.

```tsx
import { cn } from '@langgenius/dify-ui/cn'
import { mergeProps } from '@langgenius/dify-ui/merge-props'
import { useRender } from '@langgenius/dify-ui/use-render'

function ExternalLink({ className, render, ...props }: useRender.ComponentProps<'a'>) {
  return useRender({
    defaultTagName: 'a',
    render,
    props: mergeProps<'a'>(
      {
        className: cn('text-text-accent hover:underline', className),
        rel: 'noopener noreferrer',
        target: '_blank',
      },
      props,
    ),
  })
}
```

## 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.

```ts
mergeProps<T extends ElementType>(
  a: InputProps<T>,
  b: InputProps<T>,
  c: InputProps<T>,
  d: InputProps<T>,
  e: InputProps<T>,
): WithBaseUIEvent<React.ComponentPropsWithRef<T>>

mergeProps<T extends ElementType>(
  a: InputProps<T>,
  b: InputProps<T>,
  c: InputProps<T>,
  d: InputProps<T>,
): WithBaseUIEvent<React.ComponentPropsWithRef<T>>

mergeProps<T extends ElementType>(
  a: InputProps<T>,
  b: InputProps<T>,
  c: InputProps<T>,
): WithBaseUIEvent<React.ComponentPropsWithRef<T>>

mergeProps<T extends ElementType>(
  a: InputProps<T>,
  b: InputProps<T>,
): WithBaseUIEvent<React.ComponentPropsWithRef<T>>
```

**Shared parameters**

| Parameter | Type            | Description                                                                    |
| --------- | --------------- | ------------------------------------------------------------------------------ |
| `a`       | `InputProps<T>` | Props object to merge.                                                         |
| `b`       | `InputProps<T>` | Props object to merge. The function will overwrite conflicting props from `a`. |

**Returns:** `WithBaseUIEvent<React.ComponentPropsWithRef<T>>`

The merged props.
