在 React 中只需写一次代码。从同一组件树中渲染电子邮件、网页和 PDF。
Unlayer Elements
Write once in React. Render emails, web pages, and PDFs from the same component tree.
Unlayer Elements is an open-source React component library that lets you build content once and render it across three output modes:
Elements is built and maintained by the team behind Unlayer, the content creation platform used by thousands of companies to power email, page, and document experiences inside their products.
| Tool | Web | ||
|---|---|---|---|
| React Email | ✅ | ❌ | ❌ |
| MJML | ✅ | ❌ | ❌ |
| PDF libraries | ❌ | ❌ | ✅ |
| Elements | ✅ | ✅ | ✅ |
Elements is designed for teams that need to generate email, web, and document experiences from a single React codebase.
npm install @unlayer/react-elementsimport {
Email, Row, Column, ColumnLayouts,
Heading, Paragraph, Button, renderToHtml
} from '@unlayer/react-elements';
function WelcomeEmail() {
return (
Welcome!
Get Started
);
}
// Render to a complete HTML document ( to ) —
// email-client-safe shell included, no React hydration markers
const html = renderToHtml();Email mode is designed for the major clients listed throughout this repo:
Email clients do not share a modern browser CSS baseline. To keep layout behavior stable across major inboxes (especially Outlook's Word-based renderer), `` renders with table-oriented HTML and MSO-specific patterns instead of web-style div/flex layouts.
Repository evidence:
[if mso]): packages/react/src/golden-template.test.tsxValidation references:
Elements includes a strict CSP gate that runs the built package under:
node --disallow-code-generation-from-stringsThe gate's purpose is to fail if import/render paths evaluate strings (eval / new Function), including behavior coming from pinned dependencies.
test:csp): packages/react/package.jsonInterpretation guidance:
test:csp run is evidence that the exercised import/render paths are compatible with CSP configurations that disallow 'unsafe-eval'.test:csp is failing in CI or your local environment, treat strict CSP compatibility as unresolved for that run and investigate the reported failure.Build content once and render it as email, web, or document. Share components, styling, and content across all three output modes from the same React codebase.
Works with React, Next.js, Remix, and Server Components. Use familiar JSX patterns and existing frontend workflows without learning a new templating language.
Generates email-safe HTML, responsive web HTML, and print-ready HTML for PDF generation — optimized output for each destination without maintaining separate implementations.
Export Unlayer-compatible design JSON with renderToJson() for round-tripping between code and the visual editor. Ideal for teams that want the flexibility of code alongside visual editing workflows.
Built with TypeScript from the ground up — full type definitions, autocomplete for components and props, and safer development with better IDE support.
renderToHtml() generates a complete, production-ready HTML document with no React hydration markers, no framework artifacts, and no client-side JavaScript required. Need to compose the document yourself? renderToHtmlParts() returns { head, body } chunks.
~12KB gzipped (under 60KB ESM), tree-shakeable, with zero client-side JavaScript required. Designed for performance-sensitive applications and server-side rendering environments.
Build and maintain order confirmations, password resets, and receipts from the same React codebase that powers your application.
Generate invoices, contracts, and reports server-side without maintaining a separate PDF system.
Render content from a CMS, API, or database into emails, web pages, and documents using a shared component model.
Publish the same content as an email campaign, a web archive, and a public landing page from a single source of truth.
| Component | Description |
|---|---|
| `` | Root wrapper — email-safe HTML (tables for Outlook/Gmail) |
| `` | Root wrapper — responsive web (div + flexbox) |
| `` | Root wrapper — print/PDF optimized |
| `` | Layout container with column layout support |
| `` | Column inside a Row |
| `` | CTA button with hover states and links |
| `` | Heading (h1–h4) |
| `` | Rich text with plain text or HTML content |
| `` | Responsive image with alt text |
| `` | Horizontal separator |
| `` | Social media icon links |
| `` | Navigation menu |
| `` | Data table |
| `` | YouTube/Vimeo embed |
| `` | Custom HTML passthrough |
Components follow a strict hierarchy:
← sets render mode
← layout container
← must match layout column count
← content components
…For the full API reference, component props, design patterns, and common mistakes, see the React package documentation.
| Package | Description | Published |
|---|---|---|
@unlayer/react-elements |
React components and renderers | |
@unlayer-internal/shared-elements |
Framework-agnostic shared logic | Internal |
@unlayer/elements-demo |
Demo application | — |
Runnable projects you can clone and experiment with:
| Example | Description |
|---|---|
examples/nextjs-app-router |
Next.js 15 App Router — renders an email with renderToHtml() in a Server Component, previews it at /email-preview, and serves the raw HTML from a Route Handler |
# Prerequisites: Node.js (see .nvmrc), pnpm 9+
# Install dependencies
pnpm install
# Build all packages
pnpm build
# Run tests
pnpm test
# Run tests with coverage
pnpm test:coverage
# Launch Storybook
pnpm storybook:hubElements generates its HTML with the same battle-tested engine that powers exports in the Unlayer editor, an engine used in production by thousands of companies for years. The engine version is pinned per release, and every component's output is locked down by snapshot tests, a golden-template test, and browser-level gates (see below), so upgrades cannot silently change the HTML you ship. Releases follow semantic versioning and each one is published with release notes, so any change to the API or output is visible before you upgrade.
Every release must pass, in CI:
role="presentation" on layout tables). The suite includes a negative control that must fail, proving the gate can catch regressions rather than just pass vacuously.The email mode emits the same table-based, Outlook-safe HTML patterns as Unlayer's editor exports, which land in real inboxes every day. Our templates are tested on Litmus and Email on Acid across major email clients, including Outlook, Gmail, Yahoo, and Apple Mail. For repository-scoped compatibility evidence and caveats, see Email Client Compatibility Notes.
Yes, you can generate PDF files in two ways: use Unlayer's PDF export service to get a finished PDF with a single API call, or pass the print-optimized HTML from `` to any HTML-to-PDF library. Pagination (page breaks, margins, and headers and footers) is controlled with standard CSS print rules plus your renderer's options.
Yes. The package is MIT-licensed, makes no network calls, requires no API key or account, and has no runtime dependency on Unlayer's platform. renderToHtml() runs entirely in your own process, and the output is plain, static HTML that you own outright. The platform integrations (design-JSON round-tripping via renderToJson() and Custom Tools shared with the visual editor) are strictly opt-in: valuable if you also use the Unlayer builder, ignorable if you don't.
Yes. Elements is fully compatible with the Unlayer editor, the drag-and-drop builder for emails, pages, and documents that you can embed in your own product. renderToJson() exports your component tree as Unlayer design JSON that loads directly into the editor, so developers can build temp
暂无开放 Issues,或尚未同步最近议题。