2.0.9 • Published 3 months ago

react-html-props v2.0.9

Weekly downloads
-
License
MIT
Repository
github
Last release
3 months ago

Documentation

Read the official documentation.

Overview

This package includes convenient TypeScript type definitions for all React HTML props.

For example, this allows you to use the type DivProps instead of:

React.DetailedHTMLProps<React.HTMLAttributes<HTMLDivElement>, HTMLDivElement>

...Because nobody wants to type all of that. 😁

Using these types makes it easy to support all standard HTML props, such as style and className, in your own components.

Features include:

  • 🧩 TypeScript types for all React HTML props
    • Easily use types for HTML props with simple names like DivProps.
  • 🧠 Easy to remember
    • All types start with the HTML element name, so you'll never end up scratching your head.
  • 👍 Optional types without React ref
    • Where needed, use WithoutRef types for props that don't inherit ref from React.DetailedHTMLProps.

Donate

If this project helped you, please consider buying me a coffee or sponsoring me. Your support is much appreciated!

 

Table of Contents

Installation

npm i --save-dev react-html-props

Quick Start

Let's use div as an example since it's the most common.

You can use DivProps to support all props for div in your own components.

import { DivProps } from "react-html-props";

const MyComponent = (props: DivProps) => {
  return <div {...props}>{props.children}</div>;
};

Note: DivProps is equivalent to React.DetailedHTMLProps<React.HTMLAttributes<HTMLDivElement>, HTMLDivElement>

In this example, we're using className, style, and onClick on our own component since it supports all div props:

const render = () => (
  <MyComponent
    className="text-center"
    style={{ background: 'blue', color: 'white' }}
    onClick={() => console.log('Get schwifty!')}
  >
    Show me what you got
  </MyComponent>
);

Types are available for all HTML props. See below for a table containing all supported types.

Unpacking Props

We can use object destructuring and the spread operator to unpack props, such as children, from the rest of an element's props.

Using div as an example again:

import { DivProps } from "react-html-props";

export const MyComponent = ({ children, ...divProps }: DivProps): JSX.Element => {
  return <div {...divProps}>{children}</div>;
};

Extending HTML Props

You can extend all HTML props to add your own.

Then use object destructuring to unpack and use your own props.

Just follow the example below:

interface KindleOfKittensProps extends DivProps {
  kittenCount: 10;
}

export const KindleOfKittens = ({ kittenCount, ...divProps }: KindleOfKittensProps): JSX.Element => {
  return (
    <div {...divProps}>
      <h1>I have a kindle of {kittenCount} kittens</h1>
    </div>
  );
};

(Yes, a group of kittens is called a "kindle")

Included HTML Element Props

The goal was to make it as easy to use each HTML element's props as possible, so the types for all props begin with the exact HTML element.

For example, the type for the p element's props is PProps.

In some cases there are multiple types available, such as with headings h1, h2, h3, etc. The props for these elements can be referenced either as H1Props, H2Props, H3Props, etc, or simply as HeadingProps. See the table below for more.

You can import any of the following types:

HTML ElementProps Type To Use
aAProps
abbrAbbrProps
addressAddressProps
areaAreaProps
articleArticleProps
asideAsideProps
audioAudioProps
bBProps
baseBaseProps
bdiBDIProps
bdoBDOProps
blockquoteBlockQuoteProps
bodyBodyProps
brBRProps
buttonButtonProps
canvasCanvasProps
captionCaptionProps
citeCiteProps
codeCodeProps
colColProps
colgroupColGroupProps
dataDataProps
datalistDataListProps
ddDDProps
delDelProps
detailsDetailsProps
dfnDfnProps
dialogDialogProps
divDivProps
dlDLProps
dtDTProps
emEmProps
embedEmbedProps
fieldsetFieldSetProps
figcaptionFigCaptionProps
figureFigureProps
footerFooterProps
formFormProps
h1H1Props, HeadingProps
h2H2Props, HeadingProps
h3H3Props, HeadingProps
h4H4Props, HeadingProps
h5H5Props, HeadingProps
h6H6Props, HeadingProps
headHeadProps
headerHeaderProps
hgroupHGroupProps
hrHRProps
htmlHtmlProps
iIProps
iframeIFrameProps
imgImgProps
inputInputProps
insInsProps
kbdKbdProps
labelLabelProps
legendLegendProps
liLIProps
linkLinkProps
mainMainProps
mapMapProps
markMarkProps
menuMenuProps
metaMetaProps
meterMeterProps
navNavProps
noscriptNoScriptProps
objectObjectProps
olOLProps
optgroupOptGroupProps
optionOptionProps
outputOutputProps
pPProps
paramParamProps
picturePictureProps
prePreProps
progressProgressProps
qQProps
rpRPProps
rtRTProps
rubyRubyProps
sSProps
sampSampProps
scriptScriptProps
sectionSectionProps
selectSelectProps
slotSlotProps
smallSmallProps
sourceSourceProps
spanSpanProps
strongStrongProps
styleStyleProps
subSubProps
summarySummaryProps
supSupProps
svgSVGProps
tableTableProps
tbodyTBodyProps, TableSectionProps
tdTDProps
templateTemplateProps
textareaTextAreaProps
tfootTFootProps, TableSectionProps
thTHProps
theadTHeadProps, TableSectionProps
timeTimeProps
titleTitleProps
trTRProps
trackTrackProps
uUProps
ulULProps
varVarProps
videoVideoProps
wbrWBRProps
webviewWebViewProps
Generic HTML ElementElementProps

For any elements not listed above, use the generic ElementProps.

Props Without Ref

You may need props that exclude the ref field inherited from React.DetailedHTMLProps.

For this, all types have a WithoutRef option.

For example, you can use DivPropsWithoutRef for a div without a React ref. DivPropsWithoutRef is equivalent to React.HTMLAttributes<HTMLDivElement>.

A ref may not always be desirable, so it remains optional to give you flexibility. For instance, components returned by styled-components may not support React's ref type.

TypeScript

Type definitions have been included for TypeScript support.

Icon Attribution

Favicon by Twemoji.

Contributing

Open source software is awesome and so are you. 😎

Feel free to submit a pull request for bugs or additions, and make sure to update tests as appropriate. If you find a mistake in the docs, send a PR! Even the smallest changes help.

For major changes, open an issue first to discuss what you'd like to change.

⭐ Found It Helpful? Star It!

If you found this project helpful, let the community know by giving it a star: 👉⭐

License

See LICENSE.md.

2.0.7

3 months ago

2.0.6

3 months ago

2.0.9

3 months ago

2.0.8

3 months ago

2.0.5

3 months ago

2.0.4

3 months ago

2.0.3

10 months ago

2.0.2

1 year ago

2.0.1

1 year ago

1.0.36

1 year ago

1.0.35

1 year ago

1.0.34

1 year ago

1.0.33

2 years ago

1.0.32

3 years ago

1.0.31

3 years ago

1.0.30

3 years ago

1.0.29

3 years ago

1.0.28

3 years ago

1.0.27

3 years ago

1.0.26

3 years ago

1.0.25

3 years ago

1.0.24

3 years ago

1.0.23

3 years ago

1.0.22

3 years ago

1.0.21

3 years ago

1.0.20

3 years ago

1.0.19

3 years ago

1.0.18

3 years ago

1.0.17

3 years ago

1.0.16

3 years ago

1.0.15

3 years ago

1.0.14

3 years ago

1.0.13

3 years ago

1.0.12

3 years ago

1.0.11

3 years ago

1.0.10

3 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago