@kleb/templating (0.2.0)
Installation
@kleb:registry=npm install @kleb/templating@0.2.0"@kleb/templating": "0.2.0"About this package
@kleb/templating
Email-focused React template rendering.
import { Body, Head, Html, Tailwind, Text } from "@react-email/components";
import { createEmailTailwindConfig, renderEmailTemplate } from "@kleb/templating";
const theme = createEmailTailwindConfig({
colors: {
background: "#ffffff",
foreground: "#111827",
primary: "#2563eb",
primaryForeground: "#ffffff",
},
});
const WelcomeEmail = ({ name }: { name: string }) => (
<Html>
<Tailwind config={theme}>
<Head />
<Body className="bg-background text-foreground">
<Text>Hello {name}</Text>
</Body>
</Tailwind>
</Html>
);
const rendered = await renderEmailTemplate(WelcomeEmail, { name: "Ada" });
Theme values should be resolved colors. Do not pass CSS variables directly to
email templates. React Email's pixel-based Tailwind preset is included by
default; pass { pixelBasedPreset: false } to disable it.
Use placeholder rendering for subjects, text bodies, or small snippets:
import { renderHtmlTemplateString, renderTemplateString } from "@kleb/templating";
const subject = renderTemplateString("Welcome, {{user.name}}", {
user: { name: "Ada" },
});
const html = renderHtmlTemplateString("<p>Hello {{user.name}}</p>", {
user: { name: "<Ada>" },
});
Missing placeholders throw by default. Pass { missing: "keep" } or
{ missing: "empty" } when that behavior is explicitly desired.
Use a registry for reusable email templates:
import { createEmailTemplateRegistry, defineEmailTemplate } from "@kleb/templating";
const registry = createEmailTemplateRegistry([
defineEmailTemplate({
id: "welcome",
component: WelcomeEmail,
subject: "Welcome {{name}}",
preview: ({ name }) => `Hello ${name}`,
sampleProps: { name: "Ada" },
}),
]);
const rendered = await registry.render("welcome", { name: "Ada" });
render/get are prop-unchecked: Props is a caller-supplied type parameter
with no link from a template id to its props type, and get's internal cast
is not verified at runtime. Passing the wrong Props for a given id compiles
and only fails (or silently misbehaves) at render time; use validateProps on
the definition if you need a runtime guarantee.
Markdown content can be wrapped as an email template with
createMarkdownEmailTemplate.
Use the config integration when email theme tokens should be loaded through
@kleb/config:
import { kConfig } from "@kleb/config/server";
import { emailThemeConfig } from "@kleb/templating/config";
const loaded = kConfig("app.json").section("emailTheme", emailThemeConfig()).load();
Dependencies
Dependencies
| ID | Version |
|---|---|
| @react-email/components | ^1.0.12 |
| @react-email/render | ^2.0.10 |
Development dependencies
| ID | Version |
|---|---|
| @types/react | 19.2.17 |
| @types/react-dom | 19.2.3 |
| react | 19.2.7 |
| react-dom | 19.2.7 |
Peer dependencies
| ID | Version |
|---|---|
| @kleb/config | ^0.5.0 |
| react | ^18.0 || ^19.0 || ^19.0.0-rc |
| react-dom | ^18.0 || ^19.0 || ^19.0.0-rc |