npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@solid-primitives/props

v3.1.11

Published

Library of primitives focused around component props.

Downloads

201,460

Readme

@solid-primitives/props

turborepo size size stage

Library of primitives focused around component props.

  • combineProps - Reactively merges multiple props objects together while smartly combining some of Solid's JSX/DOM attributes.
  • filterProps - Create a new props object with only the property names that match the predicate.

Installation

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.

Event handlers (onClick, onclick, onMouseMove, onSomething), and refs (props.ref) are chained.

class, className, classList and style are combined.

For all other props, the last prop object overrides all previous ones. Similarly to Solid's mergeProps.

How to use it

import { combineProps } from "@solid-primitives/props";

const MyButton: Component<ButtonProps> = 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 <button {...combined} />;
};

// component consumer can provide button props
// they will be combined with those provided by createButton() primitive
<MyButton style={{ margin: "24px" }} />;

Chaining of event listeners

Every function/tuple property with on___ name get's 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 splitProps).

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.

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)

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

Additional helpers

A couple of lower-lever helpers that power combineProps:

stringStyleToObject

const styles = stringStyleToObject("margin: 24px; border: 1px solid #121212");
styles; // { margin: "24px", border: "1px solid #121212" }

combineStyle

const styles = combineStyle("margin: 24px; border: 1px solid #121212", {
  margin: "2rem",
  padding: "16px",
});
styles; // { margin: "2rem", border: "1px solid #121212", padding: "16px" }

DEMO

https://codesandbox.io/s/combineprops-demo-ytw247?file=/index.tsx

filterProps

A helper that creates a new props object with only the property names that match the predicate.

An alternative primitive to Solid's splitProps that will split the props eagerly, without letting you change the omitted keys afterwards.

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.

import { filterProps } from "@solid-primitives/props";

const MyComponent = props => {
  const dataProps = filterProps(props, key => key.startsWith("data-"));

  return <div {...dataProps} />;
};

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 singal) This also means that any signal accessed within the predicate won't be tracked.

import { filterProps, createPropsPredicate } from "@solid-primitives/props";

const MyComponent = props => {
  const predicate = createPropsPredicate(props, key => key.startsWith("data-"));
  const dataProps = filterProps(props, predicate);

  return <div {...dataProps} />;
};

Changelog

See CHANGELOG.md