Solid Primitives Props

# @solid-primitives/props [![size](https://img.shields.io/badge/size-1.35_kB-blue?style=for-the-badge)](https://bundlephobia.com/package/@solid-primitives/props) [![size](https://img.shields.io/npm/v/@solid-primitives/props?style=for-the-badge)](https://www.npmjs.com/package/@solid-primitives/props) [![stage](https://img.shields.io/endpoint?style=for-the-badge&url=https%3A%2F%2Fraw.githubusercontent.com%2Fsolidjs-community%2Fsolid-primitives%2Fmain%2Fassets%2Fbadges%2Fstage-3.json)](https://github.com/solidjs-community/solid-primitives#contribution-process) [![tested with vitest](https://img.shields.io/badge/tested_with-vitest-6E9F18?style=for-the-badge&logo=vitest)](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 ; } 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
{JSON.stringify({ ...filtered }, null, 2)}
); } render(() => , document.getElementById("app")!); ``` An alternative primitive to Solid's `omit` that splits props lazily (per-read) — the predicate is not evaluated upfront, so the set of included keys can change dynamically. The `predicate` is run for every property read lazily — any signal accessed within the `predicate` will be tracked, and `predicate` re-executed if changed. ### How to use it Params: - `props` — The props object to filter. - `predicate` — A function that returns `true` if the property should be included in the filtered object. Returns A new props object with only the properties that match the predicate. ```tsx import { filterProps } from "@solid-primitives/props"; const MyComponent = props => { const dataProps = filterProps(props, key => key.startsWith("data-")); return
; }; ``` ### `createPropsPredicate` Creates a predicate function that can be used to filter props by the prop name dynamically. The provided `predicate` function get's wrapped with a cache layer to prevent unnecessary re-evaluation. If one property is requested multiple times, the `predicate` will only be evaluated once. The cache is only cleared when the keys of the props object change. _(when spreading props from a signal)_ This also means that any signal accessed within the `predicate` won't be tracked. ```tsx import { filterProps, createPropsPredicate } from "@solid-primitives/props"; const MyComponent = props => { const predicate = createPropsPredicate(props, key => key.startsWith("data-")); const dataProps = filterProps(props, predicate); return
; }; ``` ## `partitionProps` Splits a props object into two reactive views: one containing only the keys that match the predicate, and one containing the rest. Both views are lazy proxies — the predicate runs per property read, not eagerly. ```tsx repl import { render } from "@solidjs/web"; import { createSignal } from "solid-js"; import { partitionProps } from "@solid-primitives/props"; type BadgeProps = { label: string; color: string; title?: string; id?: string }; function Badge(props: BadgeProps) { const [own, html] = partitionProps(props, key => key === "label" || key === "color"); return ( {own.label} (forwarded: {Object.keys(html).join(", ")}) ); } function Demo() { const [ok, setOk] = createSignal(true); return ( <>

); } render(() => , document.getElementById("app")!); ``` ```tsx import { partitionProps } from "@solid-primitives/props"; const MyButton = (props: ButtonProps & JSX.HTMLAttributes) => { const [ownProps, htmlProps] = partitionProps(props, key => ["label", "variant", "size"].includes(key as string), ); return ; }; ``` For an expensive predicate, pass a [`createPropsPredicate`](#createpropspredicate) result to share a single cache across both views: ```tsx const pred = createPropsPredicate(props, key => expensiveCheck(key)); const [ownProps, htmlProps] = partitionProps(props, pred); ``` ## Changelog See [CHANGELOG.md](./CHANGELOG.md)