Gathering detailed insights and metrics for htmr
Gathering detailed insights and metrics for htmr
Gathering detailed insights and metrics for htmr
Gathering detailed insights and metrics for htmr
Simple and lightweight (< 2kB) HTML string to React element conversion library
npm install htmr
Typescript
Module System
Node Version
NPM Version
TypeScript (41.52%)
JavaScript (30.88%)
HTML (27.42%)
Shell (0.18%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
531 Stars
187 Commits
32 Forks
4 Watchers
3 Branches
8 Contributors
Updated on Apr 28, 2025
Latest Version
1.0.2
Package Id
htmr@1.0.2
Unpacked Size
22.64 kB
Size
8.58 kB
File Count
7
NPM Version
8.3.1
Node Version
16.14.0
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
2
1
25
Simple and lightweight (< 2kB) HTML string to react element conversion library
1$ yarn add htmr 2 3# or 4 5$ npm install htmr --save
Use the default export, and pass HTML string.
1import React from 'react'; 2import htmr from 'htmr'; 3 4function HTMLComponent() { 5 return htmr('<p>No more dangerouslySetInnerHTML</p>'); 6}
The API also accepts second argument options
containing few optional fields. Below are their default values:
1const options = { 2 transform: {}, 3 preserveAttributes: [], 4 dangerouslySetChildren: ['style'], 5}; 6htmr(html, options);
transform accepts key value pairs, that will be used to transforms node (key) to custom component (value). You can use it to render specific tag name with custom component. For example: component with predefined styles like styled-components.
1import React from 'react'; 2import htmr from 'htmr'; 3import styled from 'styled-components'; 4 5const Paragraph = styled.p` 6 font-family: Helvetica, Arial, sans-serif; 7 line-height: 1.5; 8`; 9 10const transform = { 11 p: Paragraph, 12 // you can also pass string for native DOM node 13 a: 'span', 14}; 15 16function TransformedHTMLComponent() { 17 // will return <Paragraph><span>{'Custom component'}</span></Paragraph> 18 return htmr('<p><a>Custom component</a></p>', { transform }); 19}
You can also provide default transform using underscore _
as property name.
This can be useful if you want to do string preprocessing (like removing all whitespace), or rendering HTML as native view in react-native:
1import React from 'react'; 2import { Text, View } from 'react-native'; 3 4const transform = { 5 div: View, 6 _: (node, props, children) => { 7 // react-native can't render string without <Text> component 8 // we can test text node by checking component props, text node won't have them 9 if (typeof props === 'undefined') { 10 // use `key` because it's possible that <Text> is rendered 11 // inside array as sibling 12 return <Text key={node}>{node}</Text>; 13 } 14 15 // render unknown tag using <View> 16 // ideally you also filter valid props to <View /> 17 return <View {...props}>{children}</View>; 18 }, 19}; 20 21function NativeHTMLRenderer(props) { 22 return htmr(props.html, { transform }); 23}
By default htmr
will convert HTML attributes to camelCase because that's what React uses. You can override this behavior by passing preserveAttributes
options. Specify array of string / regular expression to test which attributes you want to preserve.
For example you want to make sure ng-if
, v-if
and v-for
to be rendered as is
1htmr(html, { preserveAttributes: ['ng-if', new RegExp('v-')] });
By default htmr
will only render children of style
tag inside dangerouslySetInnerHTML
due to security reason. You can override this behavior by passing array of HTML tags if you want the children of the tag to be rendered dangerously.
1htmr(html, { dangerouslySetChildren: ['code', 'style'] });
Note that if you still want style
tag to be rendered using dangerouslySetInnerHTML
, you still need to include it in the array.
You can also convert HTML string which contains multiple elements. This returns an array, so make sure to wrap the output inside other component such as div, or use React 16.
1import React from 'react'; 2import htmr from 'htmr'; 3 4const html = ` 5 <h1>This string</h1> 6 <p>Contains multiple html tags</p> 7 <p>as sibling</p> 8`; 9 10function ComponentWithSibling() { 11 // if using react 16, simply use the return value because 12 // v16 can render array 13 return htmr(html); 14 // if using react 15 and below, wrap in another component 15 return <div>{htmr(html)}</div>; 16}
If you're using htmr
to transform custom elements in a typescript code, you'll get type error because custom element is not defined as valid property. To work around this, you can define the mapping in a separate object, and typecast as any
while spreading in transform object:
1import { ElementType } from 'react'; 2import { HtmrOptions } from 'htmr'; 3 4const customElementTransform: Record<string, ElementType> = { 5 'virtual-scroller': VirtualScroller, 6}; 7 8const options: HtmrOptions = { 9 transform: { 10 a: Anchor, 11 ...(customElementTransform as any), 12 }, 13}; 14 15htmr(html, options);
This library was initially built to provides easy component mapping between HTML string and React component. It's mainly used to render custom component from HTML string returned from an API. This library prioritize file size and simple API over full HTML conversion coverage and other features like JSX parsing or flexible node traversal.
That's why I've decided to not implement some features (see Trade Off section below). If you feel like you need more features that's not possible using this library, you can check out some related projects below.
onclick=""
etc) are not supported due to unnecessary complexitydiv
inside p
element like <p><div>text</div></p>
dangerouslySetInnerHTML
by default due to security. You can opt in by using dangerouslySetChildren
dangerouslySetInnerHTML
by default. You can also reverse this behavior using same method.HTML to react element:
HTML (page) to react component (file/string):
MIT
0/10
Summary
Cross-Site Scripting in htmr
Affected Versions
< 0.8.7
Patched Versions
0.8.7
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
SAST tool detected but not run on all commits
Details
Reason
Found 2/18 approved changesets -- score normalized to 1
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
31 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-07-07
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More