# @solid-primitives/props
[](https://bundlephobia.com/package/@solid-primitives/props)
[](https://www.npmjs.com/package/@solid-primitives/props)
[](https://github.com/solidjs-community/solid-primitives#contribution-process)
[](https://vitest.dev)
Library of primitives focused around component props.
- [`combineProps`](#combineprops) - Reactively merges multiple props objects together while smartly combining some of Solid's JSX/DOM attributes.
- [`combineHandlers`](#combinehandlers) - Chains multiple event handlers into a single handler.
- [`filterProps`](#filterprops) - Create a new props object with only the property names that match the predicate.
- [`partitionProps`](#partitionprops) - Split a props object into two reactive views based on a predicate.
## Installation
```bash
npm install @solid-primitives/props
# or
yarn add @solid-primitives/props
# or
pnpm add @solid-primitives/props
```
## `combineProps`
A helper that reactively merges multiple props objects together while smartly combining some of Solid's JSX/HTML attributes.
```tsx repl
import { render } from "@solidjs/web";
import { createSignal } from "solid-js";
import { combineProps } from "@solid-primitives/props";
function Demo() {
const [log, setLog] = createSignal([]);
const [primary, setPrimary] = createSignal(true);
const push = (msg: string) => setLog(l => [...l, msg]);
const combined = combineProps(
{ class: "btn", style: { padding: "8px 16px" }, onClick: () => push("base onClick") },
{
get class() {
return primary() ? "primary" : "secondary";
},
style: { "font-weight": "bold" },
onClick: () => push("user onClick"),
},
);
return (
<>
class: "{combined.class}"
log: {log().join(", ")}
>
);
}
render(() => , document.getElementById("app")!);
```
Event handlers _(onClick, onclick, onMouseMove, onSomething)_, and refs _(props.ref)_ are chained.
`class`, `className`, and `style` are combined.
For all other props, the last prop object overrides all previous ones. Similarly to Solid's `merge`.
### How to use it
```tsx
import { combineProps } from "@solid-primitives/props";
const MyButton: Component = props => {
// primitives of a lot of headless ui libraries will provide props to spread
const { buttonProps } = createButton();
// they can be combined with user's props easily
const combined = combineProps(props, buttonProps);
return ;
};
// component consumer can provide button props
// they will be combined with those provided by createButton() primitive
;
```
#### Chaining of event listeners
Every function property with `on___` name gets chained. That could potentially include properties that are not actually event-listeners — such as `only` or `once`. Hence you should remove them from the props (with Solid's `omit`).
Chained functions will always return `void`. If you want to get the returned value from a callback, you have to split those props and handle them yourself.
**Warning:** The types for event-listeners often won't correctly represent the values. Chaining is meant only for DOM Events spreading to an element.
```ts
const combined = combineProps(
{
onClick: e => {},
onclick: e => {},
},
{
onClick: [(n, e) => {}, 123],
},
);
// combined.onClick() will call all 3 of the functions above
```
The default order of execution is left-to-right. If you want to change it, you can use an options object as the last argument: (`reverseEventHandlers: true`)
```ts
const combined = combineProps(
// props need to be passed in an array
[{ onClick: () => console.log("parent") }, { onClick: () => console.log("child") }],
{
reverseEventHandlers: true,
},
);
combined.onClick(); // "child" "parent"
```
##### For better reference of how exactly `combineProps` works, see the [TESTS](https://github.com/solidjs-community/solid-primitives/blob/main/packages/props/test/combineProps.test.ts)
### Additional helpers
A couple of lower-lever helpers that power `combineProps`:
#### `stringStyleToObject`
```ts
const styles = stringStyleToObject("margin: 24px; border: 1px solid #121212");
styles; // { margin: "24px", border: "1px solid #121212" }
```
#### `combineStyle`
```ts
const styles = combineStyle("margin: 24px; border: 1px solid #121212", {
margin: "2rem",
padding: "16px",
});
styles; // { margin: "2rem", border: "1px solid #121212", padding: "16px" }
```
## `combineHandlers`
Chains multiple event handlers into a single handler that calls each in order. Handlers that are `null`, `undefined`, or `false` are silently skipped.
```tsx repl
import { render } from "@solidjs/web";
import { createSignal } from "solid-js";
import { combineHandlers } from "@solid-primitives/props";
function CountingButton(props: { onClick?: (e: MouseEvent) => void }) {
const [clicks, setClicks] = createSignal(0);
const internal = () => setClicks(c => c + 1);
return ;
}
function Demo() {
const [parentClicks, setParentClicks] = createSignal(0);
return (
<>
setParentClicks(c => c + 1)} />
parent handler calls: {parentClicks()}
>
);
}
render(() => , document.getElementById("app")!);
```
When used inline in JSX, reads from Solid's reactive props proxy are tracked through the render context automatically — no explicit signal unwrapping is needed. For a standalone signal holding a handler, read it before passing (`handler()`) or wrap the whole call in a `createMemo`.
```tsx
import { combineHandlers } from "@solid-primitives/props";
const MyButton: Component = props => {
// Merge an internal handler with whatever the consumer provides
return