mergeProps
A utility to merge multiple sets of Svelte props.
mergeProps helps you combine multiple prop objects (for example, internal props + user props) into a single set of props you can spread onto an element.
It behaves like Object.assign (rightmost wins) with a few special cases, so common Svelte patterns work as expected.
How merging works
For most keys (everything except
class,style, event handlers, and attachments), the value from the rightmost object wins:There is no
refto merge. Attachments take its place, and every attachment is kept. They run from left to right:classvalues are combined right-to-left (rightmost first) into an array, which Svelte renders as one class list:stylestrings are joined, so declarations from the rightmost style overwrite earlier ones for the same property. Style objects are ignored.Event handlers are merged and executed right-to-left (rightmost first). Handlers use Svelte's lowercase names, such as
onclick. A camelCase prop such asonValueChangeis a plain prop, so the rightmost one wins.- For DOM events, calling
event.preventDefault()in a handler prevents the handlers to its left, including Sveltery Base's internal logic, from running. It also cancels the browser's default action for the event, aspreventDefault()always does. - For handlers whose first argument is not a DOM event (callbacks with primitive or object values), this mechanism isn't available and all handlers always execute.
- For DOM events, calling
Preventing Sveltery Base's default behavior
When using the render snippet, props are not merged automatically.
You can use mergeProps to combine Sveltery Base's props with your own, and call event.preventDefault() to stop Sveltery Base's internal logic from running:
Passing a function instead of an object
Each argument can be a props object or a function that receives the merged props up to that point (left to right) and returns a props object. This is useful when you need to compute the next props from whatever has already been merged.
Note that the function's return value completely replaces the accumulated props up to that point. If you want to chain event handlers from the previous props, you must call them manually:
API reference
mergeProps
This function accepts up to 6 arguments, each being either a props object or a function that returns a props object.
If you need to merge more than 6 sets of props, use mergePropsN instead.
| Parameter | Type | Description |
|---|---|---|
a | object \| PropsGetter \| null \| undefined | Props object to merge. |
b–f | object \| PropsGetter \| null \| undefined | Props object to merge. Each one overwrites conflicting props from the previous parameters. |
| Returns | T | The merged props. T is the type of the last argument. null and undefined arguments are skipped. |
mergePropsN
This function accepts an array of props objects or functions that return props objects.
It uses the same logic as mergeProps, so use it when you need to merge more than 6 sets of props or an array of any length.
| Parameter | Type | Description |
|---|---|---|
props | readonly (object \| PropsGetter \| null \| undefined)[] | Array of props to merge. |
| Returns | T | The merged props. |