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 🙏

© 2026 – Pkg Stats / Ryan Hefner

react-polly

v0.0.2

Published

🛠 **polymorphic** _component factory_ for React ⚛️

Readme

react-polly

🛠 polymorphic component factory for React ⚛️

Bootstrapped with dts-cli

stability-experimental

What it does:

react-polly provides a quick and easy way to create polymorphic components—that is, components that can return different underlying elements based on an as prop passed in by the caller.

Additionally, it handles ref forwarding via React.forwardRef, and it manages merging additional prop types based on the provided as prop.

How to use it:

Note: This assumes you're using TypeScript.

Install: npm install react-polly or yarn add react-poly

polly is a generic function that takes two type arguments and one regular function argument.

The type arguments specify the default element type your component will return, and the prop types that your component will handle.

The main function argument is your component definition.

Note that although you must use the same default element type as the first type argument, and as the default value for your as prop.

import polly from 'react-polly';

type TextProps = {
  color?: "black" | "red" | "blue";
  children: React.ReactNode;
};

// Here we create a polymorphic <Text> component that returns a <span> by default.
const Text = polly<"span", TextProps>(function Text(
  {
    // You must provide the same default value for the `as` prop as you
    // passed to the first generic type argument.
    // You must also rename the `as` prop to an Uppercase word cause React
    // component names must be capitalied.
    as: Component = "span",
    color = "black",
    children,
    ...rest
  },
  ref?
) {
  return (
    <Component
      {...rest}
      ref={ref}
      style={{
        color
      }}
    >
      {children}
    </Component>
  );
});

type YellerProps = {
  children: React.ReactNode;
  exclamationCount: number;
};

// This is a regular component for demo purposes below.
function Yeller({ children, exclamationCount, ...rest }: YellerProps) {
  return (
    <span {...rest}>
      {children}
      {Array.from({ length: exclamationCount })
        .map(() => "!")
        .join("")}
    </span>
  );
}

function Demo() {
  return (
    <div className="app">
      <h1>react-polly</h1>
      {/* This will render a <span> element */}
      <Text>Hello! I'm a polymorphic element.</Text>
      <br />
      {/* This will render a <strong> tag */}
      <Text color="red" as="strong">
        I can render any type of element that you specify via my `as` prop.
      </Text>
      <br />
      {/* This will render a Yeller component */}
      <Text as={Yeller} exclamationCount={10} color="blue">
        I can even render as other React Components
      </Text>
    </div>
  );
}

Appendix

  • Shoutout to Ben Ilegbodu for this blog post which was super useful when writing this library.