html-email-for-react
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

HTML Email for React

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

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.

  1. Write emails as HTML (or use any number of great starter templates)
  2. 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.

Readme

Keywords

none

Package Sidebar

Install

npm i html-email-for-react

Weekly Downloads

3

Version

1.0.1

License

MIT

Unpacked Size

136 kB

Total Files

19

Last publish

Collaborators

  • 00salmon
  • locrian
  • jayden-chan
  • zachharrison
  • logvol
  • lisq
  • johanventer
  • dereksiemens
  • squatch-noahwc
  • ianhitchcock