# react-polymorphic-types

> Zero-runtime polymorphic component definitions for React

Latest version **2.0.0** (published 2021-07-11) · MIT license · 0 weekly downloads

## Install

```sh
npm install react-polymorphic-types
pnpm add react-polymorphic-types
yarn add react-polymorphic-types
bun add react-polymorphic-types
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.0 |
| Published | 2021-07-11 |
| First published | 2020-12-27 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 197 |
| Author | Kristóf Poduszló |
| Maintainers | kripod |
| Keywords | react, polymorphism, as-prop, typescript |

## Links

- npm: https://www.npmjs.com/package/react-polymorphic-types
- Repository: https://github.com/kripod/react-polymorphic-types
- Homepage: https://github.com/kripod/react-polymorphic-types#readme
- Issues: https://github.com/kripod/react-polymorphic-types/issues
- npm.io page: https://npm.io/package/react-polymorphic-types

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 2.0.0 (latest) — 2021-07-11
- 1.1.0 — 2021-04-02
- 1.0.3 — 2020-12-27
- 1.0.2 — 2020-12-27
- 1.0.1 — 2020-12-27
- 1.0.0 — 2020-12-27

## README

# react-polymorphic-types

Zero-runtime polymorphic component definitions for React

[![npm](https://img.shields.io/npm/v/react-polymorphic-types)](https://www.npmjs.com/package/react-polymorphic-types)

## Motivation

Being a successor to [react-polymorphic-box](https://github.com/kripod/react-polymorphic-box), this project offers more accurate typings with less overhead.

## Features

- Automatic code completion, based on the value of the `as` prop
- Static type checking against the associated component’s inferred props
- HTML element name validation

## Usage

A `Heading` component can demonstrate the effectiveness of polymorphism:

```tsx
<Heading color="rebeccapurple">Heading</Heading>
<Heading as="h3">Subheading</Heading>
```

Custom components like the previous one may utilize the package as shown below.

```tsx
import type { PolymorphicPropsWithoutRef } from "react-polymorphic-types";

// An HTML tag or a different React component can be rendered by default
export const HeadingDefaultElement = "h2";

// Component-specific props should be specified separately
export type HeadingOwnProps = {
  color?: string;
};

// Extend own props with others inherited from the underlying element type
// Own props take precedence over the inherited ones
export type HeadingProps<
  T extends React.ElementType = typeof HeadingDefaultElement
> = PolymorphicPropsWithoutRef<HeadingOwnProps, T>;

export function Heading<
  T extends React.ElementType = typeof HeadingDefaultElement
>({ as, color, style, ...restProps }: HeadingProps<T>) {
  const Element: React.ElementType = as || HeadingDefaultElement;
  return <Element style={{ color, ...style }} {...restProps} />;
}
```

---

⚠️ All the additional typings below will be deprecated as soon as [microsoft/TypeScript#30134](https://github.com/microsoft/TypeScript/issues/30134) is resolved.

### With [`React.forwardRef`](https://reactjs.org/docs/react-api.html#reactforwardref)

```tsx
import * as React from "react";
import type {
  PolymorphicForwardRefExoticComponent,
  PolymorphicPropsWithoutRef,
  PolymorphicPropsWithRef
} from "react-polymorphic-types";
import { HeadingDefaultElement, HeadingOwnProps } from "./Heading";

export type HeadingProps<
  T extends React.ElementType = typeof HeadingDefaultElement
> = PolymorphicPropsWithRef<HeadingOwnProps, T>;

export const Heading: PolymorphicForwardRefExoticComponent<
  HeadingOwnProps,
  typeof HeadingDefaultElement
> = React.forwardRef(function Heading<
  T extends React.ElementType = typeof HeadingDefaultElement
>(
  {
    as,
    color,
    style,
    ...restProps
  }: PolymorphicPropsWithoutRef<HeadingOwnProps, T>,
  ref: React.ForwardedRef<Element>
) {
  const Element: React.ElementType = as || HeadingDefaultElement;
  return <Element ref={ref} style={{ color, ...style }} {...restProps} />;
});
```

### With [`React.memo`](https://reactjs.org/docs/react-api.html#reactmemo)

```tsx
import * as React from "react";
import type { PolymorphicMemoExoticComponent } from "react-polymorphic-types";
import { Heading, HeadingDefaultElement, HeadingOwnProps } from "./Heading";

export const MemoizedHeading: PolymorphicMemoExoticComponent<
  HeadingOwnProps,
  typeof HeadingDefaultElement
> = React.memo(Heading);
```

### With [`React.lazy`](https://reactjs.org/docs/react-api.html#reactlazy)

```tsx
import * as React from "react";
import type { PolymorphicLazyExoticComponent } from "react-polymorphic-types";
import type { HeadingDefaultElement, HeadingOwnProps } from "./Heading";

export const LazyHeading: PolymorphicLazyExoticComponent<
  HeadingOwnProps,
  typeof HeadingDefaultElement
> = React.lazy(async () => {
  const { Heading } = await import("./Heading");
  return { default: Heading };
});
```

---
_Source: https://npm.io/package/react-polymorphic-types · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
