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

babel-plugin-react-html-attrs

v3.0.5

Published

Babel plugin which transforms HTML and SVG attributes on JSX host elements into React-compatible attributes

Downloads

46,342

Readme

babel-plugin-react-html-attrs

Travis npm package

As of v3 this plugin only transforms host elements (JSX elements with lowercase tag names).

Stick with babel-plugin-react-html-attrs@2 if you just want to transform classclassName and forhtmlFor on all JSX elements.

This plugin's goal is to allow you to copy and paste HTML and SVG verbatim into your React components, by:

  • Transforming HTML attribute names into React-compatible DOM property names:

    <label class="label" for="input"><label className="label" htmlFor="input">

  • Transforming numeric HTML attribute values into numeric JSX expressions:

    <td colspan="2"><td colSpan={2}>

  • Removing values from boolean HTML attributes, as "the presence of a boolean attribute on an element represents the true value":

    <input checked="checked"><input checked>

Installation & Usage

Install the plugin for Babel >= 6:

npm install --save-dev babel-plugin-react-html-attrs

Edit your .babelrc to include react-html-attrs as a plugin:

{
  "plugins": ["react-html-attrs"]
}

Babel Configuration for XML Namespaces in JSX

To allow use of XML namespaces in JSX for SVG, you will have to configure the preset or plugin you're using with {"throwIfNamespace": false}, e.g.:

{
  "presets": [["@babel/preset-react", { "throwIfNamespace": false }]]
}

ESLint Configuration

If you're using eslint-plugin-react's' no-unknown-property rule, you can configure it to ignore usage of HTML attributes in your JSX like so:

TypeScript Support ⛈

Unfortunately, it doesn't seem to be possible to provide full TypeScript support for JSX which takes advantage of this plugin's transformations, as it's not currently possible to override host element attribute types in the interface definitions provided by @types/react.

This Pull Request to DefinitelyTyped is for a @types/babel-plugin-react-html-attrs type definition which forks @types/react to add support for missing HTML attributes and to allow all numeric and boolean attributes to be strings as per HTML, but even if approved and merged, this will likely break if you try to use it with other type definitions dependent on @types/react.

In the meantime, here's a partial type definition file (for HTML attributes whose name doesn't match their DOM property name, including the all-important class and for) you can drop into your typings/ when using this plugin:

Caveats

Read facebook/react#4433 for the reasoning behind why React treats these attributes the way it does and the potential gotchas which await you if you use this plugin (an old version of the JSX transformer used to perform a similar transformation).

Attribution

Testing setup cloned from gaearon/babel-plugin-react-transform.

MIT Licensed