Canica

canica/email

Email templates (server/emails.tsx): email() and the React Email components they are written with.

Functions and values

email renderBody toPlainText

email functionsrc/email/index.tsx

Email templates (server/emails.tsx): typed props, a subject from them, sample props, and the body as React Email components. Canica draws the rest: the app's name and icon above, and below "You're getting this because you use · Unsubscribe", which a template cannot remove. The plain-text version is made from the HTML.

import { email, Text, Button } from "canica/email"
import { string } from "canica/server"

export const emails = {
  listShared: email(
    { props: { listName: string(), by: string() }, subject: ({ listName }) => `${listName} was shared with you`, preview: { listName: "Groceries", by: "Ana" } },
    ({ listName, by }) => <Text>{by} shared {listName} with you.</Text>,
  ),
}
function email<const F extends ArgsFields>(
  config: {
    /** The props' fields (checked on every send, like mutation args). */
    readonly props: F
    /** The subject line (one line; at most 200 characters are kept). */
    readonly subject: (props: ArgsOf<F>) => string
    /** Sample props, for the dev outbox, `canica emails`, the storyboard and tests. */
    readonly preview: ArgsOf<F>
  },
  /** The body (only the body: no <Html>, <Head> or <Body>). */
  component: (props: ArgsOf<F>) => ReactElement,
): EmailDef<ArgsOf<F>>

renderBody functionsrc/email/index.tsx

The body as HTML and as plain text (links as "text (url)"; images skipped).

const renderBody = (element: ReactElement): { html: string; text: string } => …

toPlainText functionsrc/email/index.tsx

const toPlainText = (html: string): string => …

Types

EmailDef

EmailDef interfacesrc/server/app.ts

An email template (email({ props, subject, preview }, (props) => <Text>…</Text>) from canica/email). The component is only the body: Canica adds the app's name and icon above it and the unsubscribe footer below.

interface EmailDef<P = any> {
  readonly kind: "email"
  /** The props' fields (validated on every send, like mutation args). */
  readonly props: ArgsFields
  /** The subject line, from the props. */
  readonly subject: (props: any) => string
  /** Sample props: the dev outbox, `canica emails` and the storyboard render the template with them. */
  readonly preview: Readonly<Record<string, unknown>>
  /** The body: a React Email element from the props. */
  readonly component: (props: any) => unknown
  /** @internal Renders the body to HTML and plain text (set by `email()`, which brings the renderer). */
  readonly "~render": (props: any) => { readonly html: string; readonly text: string }
  readonly "~types"?: { readonly props: P }
}

Re-exported

From react-email, as they are: Button, CodeBlock, CodeInline, Column, Container, Heading, Hr, Img, Link, Markdown, Preview, Row, Section, Text, ButtonProps, ColumnProps, ContainerProps, HeadingProps, HrProps, ImgProps, LinkProps, RowProps, SectionProps, TextProps.