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

with-polymorphic

v1.0.0

Published

A Higher Order Component for React that enables polymorphic prop patterns.

Downloads

18

Readme

With Polymorphic

github codecov npm npm npm

With Polymorphic (with-polymorphic) is a Higher Order Component for React that enables polymorphic prop patterns. This library is written in TypeScript to provide a well-typed experience.

Table of Contents

Why Use With Polymorphic?

  • Type Safety: The library is written in TypeScript, ensuring a well-typed usage.
  • Flexibility: With polymorphic prop patterns, you can create versatile components that can behave differently based on the passed props.
  • Easy Integration: It's a Higher Order Component that can be easily wrapped around your existing components.

Installation

You can install With Polymorphic (with-polymorphic) in your project using npm:

npm install with-polymorphic

Alternatively, you can use yarn:

yarn add with-polymorphic

Usage

Here's an example illustrating how withPolymorphic can be used:

import React from 'react';
import withPolymorphic from 'with-polymorphic';

interface PolymorphicComponentProps {
  children: React.ReactNode;
  as?: React.ElementType;
}

const PolymorphicComponent = withPolymorphic<'div', PolymorphicComponentProps>(
  ({ as: Component = 'div', children, ...restProps }) => (
    <Component {...restProps}>{children}</Component>
  )
);

Examples

Here are some examples demonstrating both correct and incorrect prop assignments.

Correct Prop Assignment

No additional props assigned. Component will render as expected.

export function CorrectComponent() {
  return <PolymorphicComponent>Polymorphic Component</PolymorphicComponent>;
}

"as" prop set to "a" and "href" prop set to "/example" to render the component as a link.

export function CorrectComponent() {
  return (
    <PolymorphicComponent as="a" href="/example">
      Polymorphic Component
    </PolymorphicComponent>
  );
}

Incorrect Prop Assignment

The component does not handle or recognize the "href" prop, leading to unexpected behavior or errors.

export function IncorrectComponent() {
  return (
    <PolymorphicComponent href="/example">
      Polymorphic Component
    </PolymorphicComponent>
  );
}

The component does not expect the "foo" prop, which may be ignored or cause errors.

export function IncorrectComponent() {
  return (
    <PolymorphicComponent foo="bar">Polymorphic Component</PolymorphicComponent>
  );
}

Usage with ForwardRef

Here's an example demonstrating the usage of withPolymorphic with forwardRef:

import React, { forwardRef } from 'react';
import withPolymorphic from 'with-polymorphic';

interface PolymorphicComponentProps {
  children: React.ReactNode;
  as?: React.ElementType;
}

const PolymorphicComponentWithRef = withPolymorphic<'div', PolymorphicComponentProps>(
  forwardRef<HTMLDivElement, PolymorphicComponentProps>(
    ({ as: Component = 'div', children, ...restProps }, ref) => (
      <Component ref={ref} {...restProps}>{children}</Component>
    )
  )
);

Bug Report

If you encounter any bugs or issues, please report them through the Issues section of our GitHub repository. When reporting an issue, please include as much information as possible, such as the version of the library you're using, what you expected to happen, what actually happened, and steps to reproduce the issue.

Contributing

We welcome contributions to With Polymorphic (with-polymorphic)! If you're interested in contributing, please fork the repository and submit a pull request. If you're not sure where to start, take a look at the open Issues or feel free to submit new ones.

Before submitting a pull request, please make sure that your changes are well-documented and include appropriate test coverage. For more detailed information on contributing, see the CONTRIBUTING.md file.

License

With Polymorphic (with-polymorphic) is licensed under the MIT License.