🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
DemoInstallSign in
Socket

html-email-for-react

Package Overview
Dependencies
Maintainers
11
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

html-email-for-react

An HTML processor that makes it easy to enrich HTML templates with rich React components

1.0.1
latest
npm
Version published
Maintainers
11
Created
Source

HTML Email for React

An HTML processor that makes it easy to enrich HTML templates with rich React components.

  • Sanitizes dirty HTML templates (dompurify)
  • Inlines CSS (juice)
  • Support "Web Component" style React components (html-react-parser)

Philosophy

The problem with writing good emails is combining the static parts and the dynamic parts.

The best part about emails is the HTML. Stop getting rid of it, and start building on it.

  • Write emails as HTML (or use any number of great starter templates)
  • Enhance the dynamic parts with React

Example

import React from "react";
import { render, useData } from "html-email-for-react";

const html = `
<html>
  <body>
    <my-component></my-component>
  </body>
</html>
`;

function MyComponent() {
  const { name } = useData();
  return <div>Hello {name}</div>;
}
// NOTE: Html components are CASE-InSeNsItIvE and React names are case sensitive
// So use lowercase component names (see customElements.define)
const components = {
  "my-component": MyComponent,
};
const data = {
  name: "Jorghan",
};
console.log(render(html, { components, data }));
/*
Returns:

<html>
  <body>
    <div>Hello Jorghan</div>
  </body>
</html>
*/

The great part of this is that it brings the portable web-component method of building components and allows you to build dynamic components using React.

More Examples

Inspiration

Inspired by StencilJS. I'd love to use Stencil components in emails directly, but email HTML is fickle and doesn't support custom web components.

Why not MJML?

MJML is a a great language, but it does too much and too little.

Too Much:

  • Requires the entire template to be written in MJML
  • Doesn't support progressive enhancement of templates by mixing in MJML into HTML

Too little:

  • Doesn't support passing data into nested components.
  • Requires a separate template language (Vue, Liquid, Handlebars, etc.) to render dynamic elements

Benefits of React HTML for Email

  • Uses the existing HTML tooling ecosystem.
  • Single system for rich components (React). Good for React-focused teams.
  • Modular: Render sub-templates and sub-components for parts of an email.

FAQs

Package last updated on 21 Apr 2023

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