• Stars
    star
    169
  • Rank 217,371 (Top 5 %)
  • Language
  • License
    MIT License
  • Created over 3 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Zero-runtime polymorphic component definitions for React

react-polymorphic-types

Zero-runtime polymorphic component definitions for React

npm

Motivation

Being a successor to 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:

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

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

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 is resolved.

With React.forwardRef

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

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

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 };
});

More Repositories

1

otion

Atomic CSS-in-JS with a featherweight runtime
TypeScript
624
star
2

react-hooks

Essential set of React Hooks for convenient Web API consumption and state management.
TypeScript
553
star
3

glaze

CSS-in-JS microlibrary for making design systems approachable with React
TypeScript
413
star
4

react-polymorphic-box

Building blocks for strongly typed polymorphic components in React.
TypeScript
311
star
5

uuidv7

UUIDv7 generator with millisecond precision
TypeScript
73
star
6

knex-orm

Knex-based object-relational mapping for JavaScript.
JavaScript
62
star
7

mental-poker

A purely functional mental poker library, based on the thesis of Choongmin Lee.
JavaScript
61
star
8

style-vendorizer

Tiny CSS vendor prefixer and property alias mapper for runtime styling solutions
TypeScript
56
star
9

keez

Frictionless hotkey handling for browsers
TypeScript
55
star
10

gatsby-starter-modern

DEPRECATED. Please use gatsby-starter-strict instead, see the linked website!
JavaScript
55
star
11

gatsby-starter-strict

A Gatsby starter with strict linting and auto-formatting rules.
TypeScript
41
star
12

material-components-react

Modular and customizable Material Design UI components for React.
JavaScript
32
star
13

PoloniexApi.Net

Application Programming Interface for data transmission and retrieval from Poloniex through the Microsoft .NET Framework
C#
32
star
14

css-homogenizer

Base CSS encouraging proper HTML semantics and the use of custom design tokens
CSS
31
star
15

next-starter-strict

A strict Next.js starter with Tailwind CSS, TypeScript, ESLint and Prettier
TypeScript
27
star
16

react-layout-components

Layout components for React, primarily inspired by Every Layout (https://every-layout.dev/)
TypeScript
26
star
17

elgamal.js

ElGamal cryptosystem for JavaScript.
JavaScript
25
star
18

css-shorthand-expanders

Type-safe functions to expand CSS shorthands into their longhand sub-properties
TypeScript
23
star
19

MoneroGui.Net

Provides a GUI to interact with the Monero Core assemblies.
C#
18
star
20

provably-fair

Tools for creating and verifying provably fair games.
JavaScript
16
star
21

wsx

Programmatically extensible lightweight WebSocket implementation in JavaScript.
JavaScript
13
star
22

MoneroApi.Net

API for data transmission and retrieval from Monero applications through the Microsoft .NET Framework
C#
10
star
23

bookshelf-validate

Validation for the Model objects of Bookshelf.js
JavaScript
8
star
24

polonibox

Easily check the legitimacy of Poloniex trollbox users and prove the existence of conversations, avoiding scammers to gain reputation.
PHP
8
star
25

exigo

Responsive Quiz Application made with React and Prisma Framework.
TypeScript
7
star
26

redux-for-beginners

An opinionated set of good practices to minimize the effort required to set up and maintain a Redux project.
JavaScript
6
star
27

provably-fair-algorithms-whitepaper

Introduction to Provably Fair Gaming Algorithms
TeX
5
star
28

react-typed-inputs

Strongly typed input components for React.
TypeScript
5
star
29

record-like-deep-assign

Recursively assigns enumerable own properties of the given sources to a target object
TypeScript
5
star
30

tailwindcss-inner-border

A Tailwind CSS plugin that provides utilities for creating inner borders with box-shadow.
JavaScript
4
star
31

astro-starter-strict

A strict Astro starter with Tailwind CSS, TypeScript, ESLint and Prettier
Astro
3
star
32

chip8-emulator

A CHIP-8 emulator written in JavaScript for educational purposes.
JavaScript
3
star
33

gatsby-plugin-cesium

A Gatsby plugin to add support for CesiumJS
JavaScript
3
star
34

schdesign-web-workshop

Schönherz Design Stúdió frontend workshop
HTML
3
star
35

monero-prices.js

Wrapper for the API methods of moneropric.es, written in JavaScript.
JavaScript
3
star
36

parcel-starter-modern

A modern Parcel starter aiming for high productivity with mostly vanilla techniques.
HTML
2
star
37

deckster

An open-source JavaScript library for creating card games.
JavaScript
2
star
38

express-route-fs

File system-based approach for handling routes in Express.
JavaScript
2
star
39

podicons

Handwritten SVG icons with compressibility in mind
1
star
40

hmac-rng.js

HMAC-based random number generator written in JavaScript.
JavaScript
1
star
41

gatsby-plugin-preval

A Gatsby plugin for pre-evaluating code at build-time
JavaScript
1
star
42

tailwindcss-bug-via-transparent

`via-transparent` utility broken in Safari
JavaScript
1
star
43

pcg.js

A functional implementation of the PCG family random number generators, written in JavaScript.
JavaScript
1
star