Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

react-html-props

Package Overview
Dependencies
Maintainers
1
Versions
45
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-html-props

Convenient TypeScript types for all HTML element React props.

  • 1.0.19
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
34K
increased by4.27%
Maintainers
1
Weekly downloads
 
Created
Source

React HTML Props

Convenient TypeScript types for all HTML element React props.

npm Version

Documentation

Overview

This package includes convenient TypeScript type definitions for all React.HTMLAttributes, for all HTML element types.

For example, this allows you to specify 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 for your components.

Features include:

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

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}>My component</div>;
};

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

Unpacking Children

We can use object destructuring to unpack 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>;
};

Mixing With Custom Props

You will likely have your own props, of course.

In this case, simply extend the props for the element you'd like. For instance, MyComponentProps extends DivProps.

Then use object destructuring to unpack your desired props.

Just follow the example below:

interface KindleOfKittensProps extends DivProps {
  kittenCount: 10;
}

export const KindleOfKittens = ({ children, kittenCount, ...divProps }: KindleOfKittensProps): JSX.Element => {
  return (
    <div {...divProps}>
      <h1>I have a kindle of {kittenCount} kittens</h1>
      <div>{children}</div>
    </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, AnchorProps
areaAreaProps
audioAudioProps
baseBaseProps
bodyBodyProps
brBRProps
buttonButtonProps
canvasCanvasProps
dataDataProps
datalistDataListProps
dialogDialogProps
divDivProps
dlDLProps
embedEmbedProps
fieldsetFieldSetProps
formFormProps
h1H1Props, HeadingProps
h2H2Props, HeadingProps
h3H3Props, HeadingProps
h4H4Props, HeadingProps
h5H5Props, HeadingProps
h6H6Props, HeadingProps
headHeadProps
hrHRProps
htmlHtmlProps
iframeIFrameProps
imgImgProps
inputInputProps
insInsProps, ModProps
delDelProps, ModProps
labelLabelProps
legendLegendProps
liLIProps
linkLinkProps
mapMapProps
metaMetaProps
objectObjectProps
olOLProps
optgroupOptGroupProps
optionOptionProps
pPProps, ParagraphProps
paramParamProps
prePreProps
progressProgressProps
blockquoteBlockQuoteProps, QuoteProps
qQProps, QuoteProps
citeCiteProps, QuoteProps
slotSlotProps
scriptScriptProps
noscriptNoScriptProps, ScriptProps
selectSelectProps
sourceSourceProps
spanSpanProps
styleStyleProps
svgSVGProps
tableTableProps
colColProps, TableColProps
colgroupColGroupProps, TableColProps
tdTDProps, TableDataCellProps
thTHProps, TableHeaderCellProps
trTRProps, TableRowProps
theadTHeadProps, TableSectionProps
tbodyTBodyProps, TableSectionProps
tfootTFootProps, TableSectionProps
templateTemplateProps
textareaTextAreaProps
titleTitleProps
trackTrackProps
ulULProps
videoVideoProps
webviewWebViewProps
Generic HTML ElementElementProps

All types have a WithoutRef option that excludes the React ref field. See below.

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.

Using the div example, DivPropsWithoutRef is defined as the following type:

export type DivPropsWithoutRef = React.HTMLAttributes<HTMLDivElement>;

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

TypeScript

This is a TypeScript project and only relevant to React projects written in TypeScript.

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: 👉⭐

MIT License

Copyright © 2021 Justin Mahar https://github.com/justinmahar

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Keywords

FAQs

Package last updated on 19 May 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc