@comet/mail-react 9.0.0-beta.3 → 9.0.0-beta.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +94 -15
- package/lib/__stories__/layout-patterns/AsymmetricTwoColumnLayout.stories.js +2 -1
- package/lib/blocks/pixelImage/HtmlPixelImageBlock.d.ts +11 -0
- package/lib/blocks/pixelImage/HtmlPixelImageBlock.js +18 -0
- package/lib/blocks/pixelImage/MjmlPixelImageBlock.d.ts +9 -0
- package/lib/blocks/pixelImage/MjmlPixelImageBlock.js +15 -0
- package/lib/blocks/pixelImage/__stories__/HtmlPixelImageBlock.stories.d.ts +7 -0
- package/lib/blocks/pixelImage/__stories__/HtmlPixelImageBlock.stories.js +54 -0
- package/lib/blocks/pixelImage/__stories__/MjmlPixelImageBlock.stories.d.ts +7 -0
- package/lib/blocks/pixelImage/__stories__/MjmlPixelImageBlock.stories.js +54 -0
- package/lib/blocks/pixelImage/__stories__/exampleBlockData.d.ts +2 -0
- package/lib/blocks/pixelImage/__stories__/exampleBlockData.js +19 -0
- package/lib/blocks/pixelImage/__tests__/usePixelImageBlockConfig.test.d.ts +1 -0
- package/lib/blocks/pixelImage/__tests__/usePixelImageBlockConfig.test.js +21 -0
- package/lib/blocks/pixelImage/__tests__/usePixelImageBlockData.test.d.ts +1 -0
- package/lib/blocks/pixelImage/__tests__/usePixelImageBlockData.test.js +205 -0
- package/lib/blocks/pixelImage/common.d.ts +18 -0
- package/lib/blocks/pixelImage/common.js +1 -0
- package/lib/blocks/pixelImage/usePixelImageBlockConfig.d.ts +5 -0
- package/lib/blocks/pixelImage/usePixelImageBlockConfig.js +11 -0
- package/lib/blocks/pixelImage/usePixelImageBlockData.d.ts +16 -0
- package/lib/blocks/pixelImage/usePixelImageBlockData.js +80 -0
- package/lib/components/divider/HtmlDivider.d.ts +9 -0
- package/lib/components/divider/HtmlDivider.js +43 -0
- package/lib/components/divider/MjmlDivider.d.ts +7 -0
- package/lib/components/divider/MjmlDivider.js +13 -0
- package/lib/components/divider/__stories__/HtmlDivider.stories.d.ts +10 -0
- package/lib/components/divider/__stories__/HtmlDivider.stories.js +60 -0
- package/lib/components/divider/__stories__/MjmlDivider.stories.d.ts +10 -0
- package/lib/components/divider/__stories__/MjmlDivider.stories.js +60 -0
- package/lib/components/divider/__tests__/HtmlDivider.test.d.ts +1 -0
- package/lib/components/divider/__tests__/HtmlDivider.test.js +144 -0
- package/lib/components/divider/__tests__/MjmlDivider.test.d.ts +1 -0
- package/lib/components/divider/__tests__/MjmlDivider.test.js +43 -0
- package/lib/components/divider/defaultDividerStyles.d.ts +2 -0
- package/lib/components/divider/defaultDividerStyles.js +4 -0
- package/lib/components/divider/dividerProps.d.ts +38 -0
- package/lib/components/divider/dividerProps.js +1 -0
- package/lib/components/divider/generateResponsiveDividerCss.d.ts +8 -0
- package/lib/components/divider/generateResponsiveDividerCss.js +55 -0
- package/lib/components/image/HtmlImage.d.ts +11 -0
- package/lib/components/image/HtmlImage.js +23 -0
- package/lib/components/image/MjmlImage.d.ts +10 -0
- package/lib/components/image/MjmlImage.js +22 -0
- package/lib/components/image/__stories__/HtmlImage.stories.d.ts +7 -0
- package/lib/components/image/__stories__/HtmlImage.stories.js +32 -0
- package/lib/components/image/__stories__/MjmlImage.stories.d.ts +7 -0
- package/lib/components/image/__stories__/MjmlImage.stories.js +32 -0
- package/lib/components/mailRoot/MjmlMailRoot.d.ts +13 -4
- package/lib/components/mailRoot/MjmlMailRoot.js +10 -5
- package/lib/components/text/textStyles.d.ts +1 -3
- package/lib/components/text/textStyles.js +1 -1
- package/lib/config/ConfigProvider.d.ts +43 -0
- package/lib/config/ConfigProvider.js +16 -0
- package/lib/config/ConfigProvider.test.d.ts +8 -0
- package/lib/config/ConfigProvider.test.js +30 -0
- package/lib/index.d.ts +15 -2
- package/lib/index.js +8 -1
- package/lib/server/renderMailHtml.test.js +10 -1
- package/lib/theme/createTheme.d.ts +2 -1
- package/lib/theme/createTheme.js +1 -0
- package/lib/theme/defaultTheme.js +2 -0
- package/lib/theme/themeTypes.d.ts +41 -0
- package/package.json +13 -9
package/README.md
CHANGED
|
@@ -1,26 +1,105 @@
|
|
|
1
1
|
# @comet/mail-react
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Single-package solution for building HTML emails with React and MJML. Consumers install only `@comet/mail-react` (plus `react`) — never `@faire/mjml-react` directly.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
The package:
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
- re-exports all `@faire/mjml-react` MJML components (with prop types renamed to drop the `I` prefix);
|
|
8
|
+
- extends some of them with extra props and features;
|
|
9
|
+
- adds new components and utilities — block factories (`BlocksBlock`, `ListBlock`, `OneOfBlock`, `OptionalBlock`) for rendering Comet CMS block data, and a `css()` tagged template literal for IDE syntax highlighting.
|
|
8
10
|
|
|
9
|
-
|
|
11
|
+
Some components also ship in an `Html` variant for use inside MJML ending tags. These variants are additive — separate components alongside the `Mjml` versions, with their own API.
|
|
10
12
|
|
|
11
|
-
|
|
12
|
-
- Open the `packages/mail-react/` directory directly in the IDE, as OpenSpec does not currently support monorepos
|
|
13
|
+
## Internal development
|
|
13
14
|
|
|
14
|
-
|
|
15
|
+
We extend `@faire/mjml-react` rather than fork it. A few rules keep that working:
|
|
15
16
|
|
|
16
|
-
-
|
|
17
|
-
-
|
|
18
|
-
-
|
|
19
|
-
-
|
|
17
|
+
- **Additive only.** A custom component must work everywhere the base did. Only add props — never remove or rename them, and don't require new providers or context. For theme access, prefer `useOptionalTheme()` over `useTheme()`.
|
|
18
|
+
- **Wrap, don't reimplement.** Custom components delegate to `@faire/mjml-react`. Less to maintain, and we stay close to upstream behaviour.
|
|
19
|
+
- **One export per name.** When we ship a custom version, it replaces the re-export. Consumers should never need to import from `@faire/mjml-react` directly.
|
|
20
|
+
- **Public-facing documentation doesn't reference `@faire/mjml-react`.** TSDoc, story descriptions, and changeset entries describe behavior in terms of this package only. Internal places (feature READMEs, commit messages) can mention upstream when it adds maintainer context.
|
|
20
21
|
|
|
21
|
-
|
|
22
|
+
### Styling
|
|
22
23
|
|
|
23
|
-
|
|
24
|
+
- **Inline first.** Components must render correctly without any `<style>` block — clients like Outlook ignore them.
|
|
25
|
+
- **Media queries** via `registerStyles` are progressive enhancement for mobile, where modern CSS (flex, grid) is fine. Use `theme.breakpoints.*.belowMediaQuery` (max-width queries) to target viewports below a breakpoint.
|
|
26
|
+
- **BEM, camelCase blocks.** Block `mjmlSection`, element `mjmlSection__item`, modifier `mjmlSection--indented`. Every component applies its block class and merges any consumer-provided `className` with `clsx`: `clsx("mjmlSection", className)`.
|
|
24
27
|
|
|
25
|
-
|
|
26
|
-
|
|
28
|
+
### Theme variants
|
|
29
|
+
|
|
30
|
+
Themed components expose their styling through the theme: a flat base entry and an optional, consumer-extensible variants map.
|
|
31
|
+
|
|
32
|
+
- **Base theme entry covers the unstyled use.** Components render from `theme.<component>` when no variant is picked. The `variant` prop is optional; the package ships no built-in variants.
|
|
33
|
+
- **Variants are declared by the consumer.** Variant names are added via TypeScript module augmentation against the variant-name type for that component. A `defaultVariant` on the theme entry makes one of them the implicit pick.
|
|
34
|
+
- **Responsive values appear only inside variants.** Base entries use plain types per property. Variant entries can declare a `ResponsiveValue<T>` — a `{ default, ...overrides }` shape with override keys drawn from `theme.breakpoints`.
|
|
35
|
+
|
|
36
|
+
### File layout
|
|
37
|
+
|
|
38
|
+
- **Location.** Custom components live in `src/components/<concern>/` (e.g. `src/components/section/MjmlSection.tsx`).
|
|
39
|
+
- **File order.** Imports → types/props → component → styles. `registerStyles` calls go below the component.
|
|
40
|
+
- **Module format.** ESM only (`type: module`). Use `.js` extensions in imports.
|
|
41
|
+
- **TSDoc.** Short TSDoc — one line where possible — on exported components and props.
|
|
42
|
+
|
|
43
|
+
### Storybook
|
|
44
|
+
|
|
45
|
+
Every custom component has a story in `src/components/<concern>/__stories__/<Component>.stories.tsx`. Wrap non-section stories in `<MjmlSection indent>` to show a realistic indented layout.
|
|
46
|
+
|
|
47
|
+
Placeholder images use the `picsum.photos` seed URL pattern — `https://picsum.photos/seed/<seed>/<width>/<height>` — so the same seed renders the same image on every build.
|
|
48
|
+
|
|
49
|
+
### Changesets
|
|
50
|
+
|
|
51
|
+
When a custom component replaces a re-export, describe the change against the previous re-export — the added props, features, or behavior. Consumers don't need to know the internal component is new; they only see what's different in the API they use.
|
|
52
|
+
|
|
53
|
+
## Internal documentation per feature
|
|
54
|
+
|
|
55
|
+
Features substantial enough to live in their own directory should have a `README.md` that describes the current state and how the feature changes it — and, when a maintainer might assume otherwise, the boundaries it deliberately doesn't cross. These READMEs are internal — written for the people (and agents) maintaining the code, not for consumers. End-user usage docs live elsewhere (see [Consumer-facing documentation](#consumer-facing-documentation)).
|
|
56
|
+
|
|
57
|
+
### What is a feature
|
|
58
|
+
|
|
59
|
+
A feature is any self-contained unit of behavior worth describing on its own — a component (`InlineLink`), a utility (`css` helper), an addon (the Storybook addon), or the package itself. Features nest: this README documents `@comet/mail-react` as a feature, and the components inside it are features in their own right.
|
|
60
|
+
|
|
61
|
+
A feature README describes **only its own feature**. It does not describe parent features that contain it, nor sub-features it contains — each of those has its own README.
|
|
62
|
+
|
|
63
|
+
### Where they live
|
|
64
|
+
|
|
65
|
+
A feature that warrants a README is organized as a directory, with the README at the directory root (e.g. `src/components/inline-link/README.md`). Small features that live as a single file inside a parent don't need their own README — they're just part of the parent. Promote a file to a directory at the same time you give it a README.
|
|
66
|
+
|
|
67
|
+
### What goes in a feature README
|
|
68
|
+
|
|
69
|
+
**Title.** Use the exact identifier when the feature is a single component or function (e.g. `MjmlSection`); otherwise use a sentence-case name (e.g. _Inline link_).
|
|
70
|
+
|
|
71
|
+
Two sections, in order. Only the intro is required.
|
|
72
|
+
|
|
73
|
+
1. **Intro.** One short paragraph: the current state, and how the feature changes it. Describe the current state as plain facts — what's there, what's required, what's missing — rather than framing it as "a problem the feature solves". Maintainers read this to work on the code, not to be sold on the feature's existence.
|
|
74
|
+
2. **Non-goals** (optional). Things the feature deliberately doesn't do — only when a reader would reasonably assume it does, typically because the feature's name, its domain, or a sibling feature suggests so. The test: would a maintainer look here for this and be surprised it's missing? If not, leave it out. Skip the obvious; skip future work; skip rejected alternatives — those belong in the commit that made the choice.
|
|
75
|
+
|
|
76
|
+
Write each bullet as a noun phrase naming the thing not done (`Not a heading component`, `No CSS variables`). Add a single follow-up sentence only when the reader needs to be redirected to the alternative or told why.
|
|
77
|
+
|
|
78
|
+
Other sections should be rare — only when content is durable, feature-specific, and doesn't fit the two above. Specifically not warranted: no Architecture (the code shows it), no Design decisions (commits carry them), no Usage (consumer docs), no Dependencies (imports show them; non-obvious cross-feature coupling belongs in the intro).
|
|
79
|
+
|
|
80
|
+
**Express the rule, not the code.** Every line should say something the code doesn't. Don't restate type signatures, prop lists, formulas, or control flow — the code already shows those.
|
|
81
|
+
|
|
82
|
+
**Length.** Simple feature → one paragraph. Complex feature → one screen, no scrolling. Past a screen and you're probably duplicating commit history or describing what the code shows.
|
|
83
|
+
|
|
84
|
+
### Template
|
|
85
|
+
|
|
86
|
+
```md
|
|
87
|
+
# <feature-name>
|
|
88
|
+
|
|
89
|
+
<One short paragraph: the current state, and how the feature changes it.>
|
|
90
|
+
|
|
91
|
+
## Non-goals <!-- only if any -->
|
|
92
|
+
|
|
93
|
+
- <Noun phrase naming what the feature deliberately doesn't do.> <Optional follow-up sentence pointing to the alternative or stating the rationale.>
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
### Living documents
|
|
97
|
+
|
|
98
|
+
Feature READMEs (this one included) should stay current. Update them in the same PR as any change that makes them inaccurate or adds context worth recording.
|
|
99
|
+
|
|
100
|
+
## Consumer-facing documentation
|
|
101
|
+
|
|
102
|
+
- Docs: [docs/docs/3-features-modules/13-building-html-emails/](../../docs/docs/3-features-modules/13-building-html-emails/)
|
|
103
|
+
- Agent skill: [skills/comet-mail-react/SKILL.md](../../skills/comet-mail-react/SKILL.md)
|
|
104
|
+
|
|
105
|
+
When a change here affects usage patterns, component APIs, or styling conventions, update these alongside the library change.
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
2
|
-
import { MjmlColumn,
|
|
2
|
+
import { MjmlColumn, MjmlSpacer, MjmlWrapper } from "@faire/mjml-react";
|
|
3
|
+
import { MjmlImage } from "../../components/image/MjmlImage.js";
|
|
3
4
|
import { MjmlSection } from "../../components/section/MjmlSection.js";
|
|
4
5
|
import { MjmlText } from "../../components/text/MjmlText.js";
|
|
5
6
|
import { registerStyles } from "../../styles/registerStyles.js";
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { ComponentProps, ReactNode } from "react";
|
|
2
|
+
import type { PixelImageBlockBaseProps } from "./common.js";
|
|
3
|
+
export type HtmlPixelImageBlockProps = Omit<ComponentProps<"img">, "src" | "width" | "height"> & PixelImageBlockBaseProps;
|
|
4
|
+
/**
|
|
5
|
+
* Renders a pixel-image from the DAM as a raw `<img>` tag.
|
|
6
|
+
*
|
|
7
|
+
* Use within raw HTML context — HTML-only emails or
|
|
8
|
+
* [MJML ending tags](https://documentation.mjml.io/#ending-tags) like `MjmlRaw`.
|
|
9
|
+
* For MJML context, use `MjmlPixelImageBlock`.
|
|
10
|
+
*/
|
|
11
|
+
export declare function HtmlPixelImageBlock({ data, width, largestPossibleRenderWidth, aspectRatio, className, ...imgProps }: HtmlPixelImageBlockProps): ReactNode;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import clsx from "clsx";
|
|
3
|
+
import { HtmlImage } from "../../components/image/HtmlImage.js";
|
|
4
|
+
import { usePixelImageBlockData } from "./usePixelImageBlockData.js";
|
|
5
|
+
/**
|
|
6
|
+
* Renders a pixel-image from the DAM as a raw `<img>` tag.
|
|
7
|
+
*
|
|
8
|
+
* Use within raw HTML context — HTML-only emails or
|
|
9
|
+
* [MJML ending tags](https://documentation.mjml.io/#ending-tags) like `MjmlRaw`.
|
|
10
|
+
* For MJML context, use `MjmlPixelImageBlock`.
|
|
11
|
+
*/
|
|
12
|
+
export function HtmlPixelImageBlock({ data, width, largestPossibleRenderWidth, aspectRatio, className, ...imgProps }) {
|
|
13
|
+
const imageData = usePixelImageBlockData({ data, defaultRenderWidth: width, largestPossibleRenderWidth, aspectRatio });
|
|
14
|
+
if (!imageData) {
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
return (_jsx(HtmlImage, { src: imageData.imageUrl, width: imageData.defaultRenderWidth, height: imageData.desktopImageHeight, alt: imageData.alt, title: imageData.title, className: clsx("htmlPixelImageBlock", className), ...imgProps }));
|
|
18
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { ReactNode } from "react";
|
|
2
|
+
import { type MjmlImageProps } from "../../components/image/MjmlImage.js";
|
|
3
|
+
import type { PixelImageBlockBaseProps } from "./common.js";
|
|
4
|
+
export type MjmlPixelImageBlockProps = Omit<MjmlImageProps, "src" | "width" | "height"> & PixelImageBlockBaseProps;
|
|
5
|
+
/**
|
|
6
|
+
* Renders a pixel-image from the DAM as `MjmlImage`. Must be placed within an
|
|
7
|
+
* `MjmlColumn`. For raw HTML context, use `HtmlPixelImageBlock`.
|
|
8
|
+
*/
|
|
9
|
+
export declare function MjmlPixelImageBlock({ data, width, largestPossibleRenderWidth, aspectRatio, className, ...imageProps }: MjmlPixelImageBlockProps): ReactNode;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import clsx from "clsx";
|
|
3
|
+
import { MjmlImage } from "../../components/image/MjmlImage.js";
|
|
4
|
+
import { usePixelImageBlockData } from "./usePixelImageBlockData.js";
|
|
5
|
+
/**
|
|
6
|
+
* Renders a pixel-image from the DAM as `MjmlImage`. Must be placed within an
|
|
7
|
+
* `MjmlColumn`. For raw HTML context, use `HtmlPixelImageBlock`.
|
|
8
|
+
*/
|
|
9
|
+
export function MjmlPixelImageBlock({ data, width, largestPossibleRenderWidth, aspectRatio, className, ...imageProps }) {
|
|
10
|
+
const imageData = usePixelImageBlockData({ data, defaultRenderWidth: width, largestPossibleRenderWidth, aspectRatio });
|
|
11
|
+
if (!imageData) {
|
|
12
|
+
return null;
|
|
13
|
+
}
|
|
14
|
+
return (_jsx(MjmlImage, { src: imageData.imageUrl, width: imageData.defaultRenderWidth, height: imageData.desktopImageHeight, alt: imageData.alt, title: imageData.title, className: clsx("mjmlPixelImageBlock", className), ...imageProps }));
|
|
15
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { Meta, StoryObj } from "@storybook/react-vite";
|
|
2
|
+
import { HtmlPixelImageBlock } from "../HtmlPixelImageBlock.js";
|
|
3
|
+
type Story = StoryObj<typeof HtmlPixelImageBlock>;
|
|
4
|
+
declare const config: Meta<typeof HtmlPixelImageBlock>;
|
|
5
|
+
export default config;
|
|
6
|
+
export declare const Default: Story;
|
|
7
|
+
export declare const AspectRatioOverride: Story;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { MjmlColumn, MjmlRaw } from "@faire/mjml-react";
|
|
3
|
+
import { MjmlMailRoot } from "../../../components/mailRoot/MjmlMailRoot.js";
|
|
4
|
+
import { MjmlSection } from "../../../components/section/MjmlSection.js";
|
|
5
|
+
import { HtmlPixelImageBlock } from "../HtmlPixelImageBlock.js";
|
|
6
|
+
import { exampleBlockData } from "./exampleBlockData.js";
|
|
7
|
+
const config = {
|
|
8
|
+
title: "Components/Blocks/HtmlPixelImageBlock",
|
|
9
|
+
component: HtmlPixelImageBlock,
|
|
10
|
+
tags: ["autodocs"],
|
|
11
|
+
parameters: {
|
|
12
|
+
mailRoot: false,
|
|
13
|
+
docs: {
|
|
14
|
+
description: {
|
|
15
|
+
component: "Renders a pixel-image from the DAM as a raw `<img>` tag, for use in HTML-only emails or inside MJML ending tags such as `MjmlRaw`.\n\n_Note: this story may fail to load the actual image when the API fixtures don't include the referenced DAM file._",
|
|
16
|
+
},
|
|
17
|
+
},
|
|
18
|
+
},
|
|
19
|
+
argTypes: {
|
|
20
|
+
data: { control: false },
|
|
21
|
+
aspectRatio: {
|
|
22
|
+
control: "select",
|
|
23
|
+
options: ["inherit", "16x9", "4x3", "3x2", "3x1", "2x1", "1x1", "1x2", "1x3", "2x3", "3x4", "9x16"],
|
|
24
|
+
},
|
|
25
|
+
},
|
|
26
|
+
args: {
|
|
27
|
+
data: exampleBlockData,
|
|
28
|
+
width: 600,
|
|
29
|
+
},
|
|
30
|
+
};
|
|
31
|
+
export default config;
|
|
32
|
+
export const Default = {
|
|
33
|
+
render: (args) => (_jsx(MjmlMailRoot, { config: {
|
|
34
|
+
pixelImageBlock: {
|
|
35
|
+
validSizes: [640, 750, 828, 1080, 1200, 1920, 2048, 2560, 3200, 3840],
|
|
36
|
+
baseUrl: "",
|
|
37
|
+
},
|
|
38
|
+
}, children: _jsx(MjmlSection, { indent: true, children: _jsx(MjmlColumn, { children: _jsx(MjmlRaw, { children: _jsx(HtmlPixelImageBlock, { ...args }) }) }) }) })),
|
|
39
|
+
};
|
|
40
|
+
export const AspectRatioOverride = {
|
|
41
|
+
parameters: {
|
|
42
|
+
docs: {
|
|
43
|
+
description: {
|
|
44
|
+
story: "Renders the same DAM image at its native aspect ratio and overridden to `16x9`, demonstrating how the `aspectRatio` prop reframes the rendered image without changing the source DAM record.",
|
|
45
|
+
},
|
|
46
|
+
},
|
|
47
|
+
},
|
|
48
|
+
render: (args) => (_jsx(MjmlMailRoot, { config: {
|
|
49
|
+
pixelImageBlock: {
|
|
50
|
+
validSizes: [640, 750, 828, 1080, 1200, 1920, 2048, 2560, 3200, 3840],
|
|
51
|
+
baseUrl: "",
|
|
52
|
+
},
|
|
53
|
+
}, children: _jsx(MjmlSection, { indent: true, children: _jsx(MjmlColumn, { children: _jsxs(MjmlRaw, { children: [_jsx(HtmlPixelImageBlock, { ...args }), _jsx(HtmlPixelImageBlock, { ...args, aspectRatio: "16x9" })] }) }) }) })),
|
|
54
|
+
};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { Meta, StoryObj } from "@storybook/react-vite";
|
|
2
|
+
import { MjmlPixelImageBlock } from "../MjmlPixelImageBlock.js";
|
|
3
|
+
type Story = StoryObj<typeof MjmlPixelImageBlock>;
|
|
4
|
+
declare const config: Meta<typeof MjmlPixelImageBlock>;
|
|
5
|
+
export default config;
|
|
6
|
+
export declare const Default: Story;
|
|
7
|
+
export declare const AspectRatioOverride: Story;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { MjmlColumn } from "@faire/mjml-react";
|
|
3
|
+
import { MjmlMailRoot } from "../../../components/mailRoot/MjmlMailRoot.js";
|
|
4
|
+
import { MjmlSection } from "../../../components/section/MjmlSection.js";
|
|
5
|
+
import { MjmlPixelImageBlock } from "../MjmlPixelImageBlock.js";
|
|
6
|
+
import { exampleBlockData } from "./exampleBlockData.js";
|
|
7
|
+
const config = {
|
|
8
|
+
title: "Components/Blocks/MjmlPixelImageBlock",
|
|
9
|
+
component: MjmlPixelImageBlock,
|
|
10
|
+
tags: ["autodocs"],
|
|
11
|
+
parameters: {
|
|
12
|
+
mailRoot: false,
|
|
13
|
+
docs: {
|
|
14
|
+
description: {
|
|
15
|
+
component: "Renders a pixel-image from the DAM as `MjmlImage`. Must be placed within an `MjmlColumn`.\n\n_Note: this story may fail to load the actual image when the API fixtures don't include the referenced DAM file._",
|
|
16
|
+
},
|
|
17
|
+
},
|
|
18
|
+
},
|
|
19
|
+
argTypes: {
|
|
20
|
+
data: { control: false },
|
|
21
|
+
aspectRatio: {
|
|
22
|
+
control: "select",
|
|
23
|
+
options: ["inherit", "16x9", "4x3", "3x2", "3x1", "2x1", "1x1", "1x2", "1x3", "2x3", "3x4", "9x16"],
|
|
24
|
+
},
|
|
25
|
+
},
|
|
26
|
+
args: {
|
|
27
|
+
data: exampleBlockData,
|
|
28
|
+
width: 600,
|
|
29
|
+
},
|
|
30
|
+
};
|
|
31
|
+
export default config;
|
|
32
|
+
export const Default = {
|
|
33
|
+
render: (args) => (_jsx(MjmlMailRoot, { config: {
|
|
34
|
+
pixelImageBlock: {
|
|
35
|
+
validSizes: [640, 750, 828, 1080, 1200, 1920, 2048, 2560, 3200, 3840],
|
|
36
|
+
baseUrl: "",
|
|
37
|
+
},
|
|
38
|
+
}, children: _jsx(MjmlSection, { indent: true, children: _jsx(MjmlColumn, { children: _jsx(MjmlPixelImageBlock, { ...args }) }) }) })),
|
|
39
|
+
};
|
|
40
|
+
export const AspectRatioOverride = {
|
|
41
|
+
parameters: {
|
|
42
|
+
docs: {
|
|
43
|
+
description: {
|
|
44
|
+
story: "Renders the same DAM image at its native aspect ratio and overridden to `16x9`, demonstrating how the `aspectRatio` prop reframes the rendered image without changing the source DAM record.",
|
|
45
|
+
},
|
|
46
|
+
},
|
|
47
|
+
},
|
|
48
|
+
render: (args) => (_jsx(MjmlMailRoot, { config: {
|
|
49
|
+
pixelImageBlock: {
|
|
50
|
+
validSizes: [640, 750, 828, 1080, 1200, 1920, 2048, 2560, 3200, 3840],
|
|
51
|
+
baseUrl: "",
|
|
52
|
+
},
|
|
53
|
+
}, children: _jsx(MjmlSection, { indent: true, children: _jsxs(MjmlColumn, { children: [_jsx(MjmlPixelImageBlock, { ...args }), _jsx(MjmlPixelImageBlock, { ...args, aspectRatio: "16x9" })] }) }) })),
|
|
54
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export const exampleBlockData = {
|
|
2
|
+
damFile: {
|
|
3
|
+
id: "example-image",
|
|
4
|
+
name: "example-image.jpg",
|
|
5
|
+
size: 1000000,
|
|
6
|
+
mimetype: "image/jpeg",
|
|
7
|
+
contentHash: "example-hash",
|
|
8
|
+
archived: false,
|
|
9
|
+
scope: { domain: "at" },
|
|
10
|
+
fileUrl: "https://picsum.photos/seed/comet-pixel-image/1000/1000",
|
|
11
|
+
image: {
|
|
12
|
+
width: 1000,
|
|
13
|
+
height: 1000,
|
|
14
|
+
cropArea: { focalPoint: "SMART" },
|
|
15
|
+
dominantColor: "#000000",
|
|
16
|
+
},
|
|
17
|
+
},
|
|
18
|
+
urlTemplate: "https://picsum.photos/seed/comet-pixel-image/$resizeWidth/$resizeHeight",
|
|
19
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { renderToStaticMarkup } from "react-dom/server";
|
|
3
|
+
import { describe, expect, it } from "vitest";
|
|
4
|
+
import { ConfigProvider } from "../../../config/ConfigProvider.js";
|
|
5
|
+
import { usePixelImageBlockConfig } from "../usePixelImageBlockConfig.js";
|
|
6
|
+
function ConfigProbe() {
|
|
7
|
+
usePixelImageBlockConfig();
|
|
8
|
+
return null;
|
|
9
|
+
}
|
|
10
|
+
describe("usePixelImageBlockConfig", () => {
|
|
11
|
+
it("throws when config.pixelImageBlock is unset", () => {
|
|
12
|
+
expect(() => renderToStaticMarkup(_jsx(ConfigProbe, {}))).toThrowError(/`pixelImageBlock` must be set/);
|
|
13
|
+
});
|
|
14
|
+
it("error message points at MjmlMailRoot and ConfigProvider", () => {
|
|
15
|
+
expect(() => renderToStaticMarkup(_jsx(ConfigProbe, {}))).toThrowError(/MjmlMailRoot/);
|
|
16
|
+
expect(() => renderToStaticMarkup(_jsx(ConfigProbe, {}))).toThrowError(/ConfigProvider/);
|
|
17
|
+
});
|
|
18
|
+
it("does not throw when config.pixelImageBlock is provided", () => {
|
|
19
|
+
expect(() => renderToStaticMarkup(_jsx(ConfigProvider, { config: { pixelImageBlock: { validSizes: [640, 1280], baseUrl: "http://localhost:3000" } }, children: _jsx(ConfigProbe, {}) }))).not.toThrow();
|
|
20
|
+
});
|
|
21
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { renderToStaticMarkup } from "react-dom/server";
|
|
3
|
+
import { describe, expect, it } from "vitest";
|
|
4
|
+
import { ConfigProvider } from "../../../config/ConfigProvider.js";
|
|
5
|
+
import { createTheme } from "../../../theme/createTheme.js";
|
|
6
|
+
import { ThemeProvider } from "../../../theme/ThemeProvider.js";
|
|
7
|
+
import { usePixelImageBlockData } from "../usePixelImageBlockData.js";
|
|
8
|
+
function captureUsePixelImageData({ data, defaultRenderWidth, largestPossibleRenderWidth, aspectRatio, config }) {
|
|
9
|
+
const captured = { value: undefined };
|
|
10
|
+
function Probe() {
|
|
11
|
+
captured.value = usePixelImageBlockData({ data, defaultRenderWidth, largestPossibleRenderWidth, aspectRatio });
|
|
12
|
+
return null;
|
|
13
|
+
}
|
|
14
|
+
renderToStaticMarkup(_jsx(ThemeProvider, { theme: createTheme(), children: _jsx(ConfigProvider, { config: config, children: _jsx(Probe, {}) }) }));
|
|
15
|
+
if (captured.value === undefined) {
|
|
16
|
+
throw new Error("Probe did not run");
|
|
17
|
+
}
|
|
18
|
+
return captured.value;
|
|
19
|
+
}
|
|
20
|
+
function expectNonNull(value) {
|
|
21
|
+
if (value === null) {
|
|
22
|
+
throw new Error("Expected non-null result from usePixelImageBlockData");
|
|
23
|
+
}
|
|
24
|
+
return value;
|
|
25
|
+
}
|
|
26
|
+
const validSizes = [320, 640, 1280, 2048];
|
|
27
|
+
const baseUrl = "http://localhost:3000";
|
|
28
|
+
const config = { pixelImageBlock: { validSizes, baseUrl } };
|
|
29
|
+
const smartUrlTemplate = "/dam/images/abc/resize:$resizeWidth:$resizeHeight/photo.jpg";
|
|
30
|
+
const smartImageData = {
|
|
31
|
+
damFile: {
|
|
32
|
+
id: "id-1",
|
|
33
|
+
name: "photo.jpg",
|
|
34
|
+
size: 1000,
|
|
35
|
+
mimetype: "image/jpeg",
|
|
36
|
+
contentHash: "hash",
|
|
37
|
+
archived: false,
|
|
38
|
+
fileUrl: "/dam/files/id-1.jpg",
|
|
39
|
+
image: {
|
|
40
|
+
width: 4000,
|
|
41
|
+
height: 2000,
|
|
42
|
+
cropArea: { focalPoint: "SMART" },
|
|
43
|
+
},
|
|
44
|
+
},
|
|
45
|
+
urlTemplate: smartUrlTemplate,
|
|
46
|
+
};
|
|
47
|
+
describe("usePixelImageBlockData — width selection", () => {
|
|
48
|
+
it("picks the smallest validSizes entry >= defaultRenderWidth × 2", () => {
|
|
49
|
+
const result = expectNonNull(captureUsePixelImageData({ data: smartImageData, defaultRenderWidth: 200, config }));
|
|
50
|
+
expect(result.imageUrl).toContain(":640:");
|
|
51
|
+
});
|
|
52
|
+
it("picks an exact validSizes match when present", () => {
|
|
53
|
+
const result = expectNonNull(captureUsePixelImageData({ data: smartImageData, defaultRenderWidth: 320, config }));
|
|
54
|
+
expect(result.imageUrl).toContain(":640:");
|
|
55
|
+
});
|
|
56
|
+
it("falls back to the largest validSizes entry when none qualifies", () => {
|
|
57
|
+
const result = expectNonNull(captureUsePixelImageData({ data: smartImageData, defaultRenderWidth: 2000, config }));
|
|
58
|
+
expect(result.imageUrl).toContain(":2048:");
|
|
59
|
+
});
|
|
60
|
+
it("uses largestPossibleRenderWidth × 2 when defaultRenderWidth equals largestPossibleRenderWidth", () => {
|
|
61
|
+
const result = expectNonNull(captureUsePixelImageData({
|
|
62
|
+
data: smartImageData,
|
|
63
|
+
defaultRenderWidth: 600,
|
|
64
|
+
largestPossibleRenderWidth: 600,
|
|
65
|
+
config,
|
|
66
|
+
}));
|
|
67
|
+
expect(result.imageUrl).toContain(":1200:");
|
|
68
|
+
});
|
|
69
|
+
it("defaults largestPossibleRenderWidth to theme.sizes.bodyWidth (600) and triggers DPR-2 fixed path at width 600", () => {
|
|
70
|
+
const result = expectNonNull(captureUsePixelImageData({ data: smartImageData, defaultRenderWidth: 600, config }));
|
|
71
|
+
expect(result.imageUrl).toContain(":1200:");
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
describe("usePixelImageBlockData — URL prefixing", () => {
|
|
75
|
+
it("prefixes a relative URL with config.pixelImageBlock.baseUrl", () => {
|
|
76
|
+
const result = expectNonNull(captureUsePixelImageData({ data: smartImageData, defaultRenderWidth: 200, config }));
|
|
77
|
+
expect(result.imageUrl.startsWith(`${baseUrl}/dam/images/abc/`)).toBe(true);
|
|
78
|
+
});
|
|
79
|
+
it("does not prefix an absolute URL", () => {
|
|
80
|
+
const absoluteData = {
|
|
81
|
+
...smartImageData,
|
|
82
|
+
urlTemplate: "https://cdn.example.com/$resizeWidth/$resizeHeight/photo.jpg",
|
|
83
|
+
};
|
|
84
|
+
const result = expectNonNull(captureUsePixelImageData({ data: absoluteData, defaultRenderWidth: 200, config }));
|
|
85
|
+
expect(result.imageUrl.startsWith("https://cdn.example.com/")).toBe(true);
|
|
86
|
+
expect(result.imageUrl.includes(baseUrl)).toBe(false);
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
describe("usePixelImageBlockData — aspect ratio", () => {
|
|
90
|
+
it("uses the natural image aspect ratio for SMART crop areas", () => {
|
|
91
|
+
const result = expectNonNull(captureUsePixelImageData({ data: smartImageData, defaultRenderWidth: 200, config }));
|
|
92
|
+
expect(result.desktopImageHeight).toBe(100);
|
|
93
|
+
expect(result.imageUrl).toContain(":640:320/");
|
|
94
|
+
});
|
|
95
|
+
it("derives aspect ratio from crop dimensions for non-SMART crop areas", () => {
|
|
96
|
+
const nonSmartData = {
|
|
97
|
+
damFile: {
|
|
98
|
+
id: "id-2",
|
|
99
|
+
name: "photo2.jpg",
|
|
100
|
+
size: 1000,
|
|
101
|
+
mimetype: "image/jpeg",
|
|
102
|
+
contentHash: "hash",
|
|
103
|
+
archived: false,
|
|
104
|
+
fileUrl: "/dam/files/id-2.jpg",
|
|
105
|
+
image: {
|
|
106
|
+
width: 4000,
|
|
107
|
+
height: 2000,
|
|
108
|
+
cropArea: { focalPoint: "CENTER", width: 50, height: 100, x: 25, y: 0 },
|
|
109
|
+
},
|
|
110
|
+
},
|
|
111
|
+
urlTemplate: smartUrlTemplate,
|
|
112
|
+
};
|
|
113
|
+
const result = expectNonNull(captureUsePixelImageData({ data: nonSmartData, defaultRenderWidth: 200, config }));
|
|
114
|
+
expect(result.desktopImageHeight).toBe(200);
|
|
115
|
+
expect(result.imageUrl).toContain(":640:640/");
|
|
116
|
+
});
|
|
117
|
+
it("throws when crop dimensions are missing on a non-SMART crop area", () => {
|
|
118
|
+
const malformedData = {
|
|
119
|
+
damFile: {
|
|
120
|
+
id: "id-3",
|
|
121
|
+
name: "photo3.jpg",
|
|
122
|
+
size: 1000,
|
|
123
|
+
mimetype: "image/jpeg",
|
|
124
|
+
contentHash: "hash",
|
|
125
|
+
archived: false,
|
|
126
|
+
fileUrl: "/dam/files/id-3.jpg",
|
|
127
|
+
image: {
|
|
128
|
+
width: 4000,
|
|
129
|
+
height: 2000,
|
|
130
|
+
cropArea: { focalPoint: "CENTER" },
|
|
131
|
+
},
|
|
132
|
+
},
|
|
133
|
+
urlTemplate: smartUrlTemplate,
|
|
134
|
+
};
|
|
135
|
+
expect(() => captureUsePixelImageData({ data: malformedData, defaultRenderWidth: 200, config })).toThrow(/Missing crop dimensions/);
|
|
136
|
+
});
|
|
137
|
+
});
|
|
138
|
+
describe("usePixelImageBlockData — aspectRatio override", () => {
|
|
139
|
+
it("uses a numeric aspectRatio in place of the cropArea-derived ratio", () => {
|
|
140
|
+
const result = expectNonNull(captureUsePixelImageData({ data: smartImageData, defaultRenderWidth: 200, aspectRatio: 16 / 9, config }));
|
|
141
|
+
expect(result.imageUrl).toContain(":640:360/");
|
|
142
|
+
expect(result.desktopImageHeight).toBe(113);
|
|
143
|
+
});
|
|
144
|
+
it.each([
|
|
145
|
+
["WxH", "16x9"],
|
|
146
|
+
["W:H", "16:9"],
|
|
147
|
+
["W/H", "16/9"],
|
|
148
|
+
])("parses string aspectRatio in %s form", (_label, value) => {
|
|
149
|
+
const result = expectNonNull(captureUsePixelImageData({ data: smartImageData, defaultRenderWidth: 200, aspectRatio: value, config }));
|
|
150
|
+
expect(result.imageUrl).toContain(":640:360/");
|
|
151
|
+
expect(result.desktopImageHeight).toBe(113);
|
|
152
|
+
});
|
|
153
|
+
it("treats a single-token string as `width / 1`", () => {
|
|
154
|
+
const result = expectNonNull(captureUsePixelImageData({ data: smartImageData, defaultRenderWidth: 200, aspectRatio: "2", config }));
|
|
155
|
+
expect(result.imageUrl).toContain(":640:320/");
|
|
156
|
+
expect(result.desktopImageHeight).toBe(100);
|
|
157
|
+
});
|
|
158
|
+
it("overrides a non-SMART cropArea ratio", () => {
|
|
159
|
+
const nonSmartData = {
|
|
160
|
+
damFile: {
|
|
161
|
+
id: "id-override",
|
|
162
|
+
name: "photo.jpg",
|
|
163
|
+
size: 1000,
|
|
164
|
+
mimetype: "image/jpeg",
|
|
165
|
+
contentHash: "hash",
|
|
166
|
+
archived: false,
|
|
167
|
+
fileUrl: "/dam/files/id-override.jpg",
|
|
168
|
+
image: {
|
|
169
|
+
width: 4000,
|
|
170
|
+
height: 2000,
|
|
171
|
+
cropArea: { focalPoint: "CENTER", width: 50, height: 100, x: 25, y: 0 },
|
|
172
|
+
},
|
|
173
|
+
},
|
|
174
|
+
urlTemplate: smartUrlTemplate,
|
|
175
|
+
};
|
|
176
|
+
const result = expectNonNull(captureUsePixelImageData({ data: nonSmartData, defaultRenderWidth: 200, aspectRatio: "16x9", config }));
|
|
177
|
+
expect(result.imageUrl).toContain(":640:360/");
|
|
178
|
+
expect(result.desktopImageHeight).toBe(113);
|
|
179
|
+
});
|
|
180
|
+
it("throws when the aspectRatio string is malformed", () => {
|
|
181
|
+
expect(() => captureUsePixelImageData({ data: smartImageData, defaultRenderWidth: 200, aspectRatio: "not-a-ratio", config })).toThrow(/An error occurred while parsing the aspect ratio: not-a-ratio/);
|
|
182
|
+
});
|
|
183
|
+
});
|
|
184
|
+
describe("usePixelImageBlockData — incomplete data", () => {
|
|
185
|
+
it("returns null when damFile is absent", () => {
|
|
186
|
+
const result = captureUsePixelImageData({ data: { urlTemplate: smartUrlTemplate }, defaultRenderWidth: 200, config });
|
|
187
|
+
expect(result).toBeNull();
|
|
188
|
+
});
|
|
189
|
+
it("returns null when damFile.image is absent", () => {
|
|
190
|
+
const noImageData = {
|
|
191
|
+
damFile: {
|
|
192
|
+
id: "id-4",
|
|
193
|
+
name: "no-image.jpg",
|
|
194
|
+
size: 0,
|
|
195
|
+
mimetype: "image/jpeg",
|
|
196
|
+
contentHash: "hash",
|
|
197
|
+
archived: false,
|
|
198
|
+
fileUrl: "/dam/files/id-4.jpg",
|
|
199
|
+
},
|
|
200
|
+
urlTemplate: smartUrlTemplate,
|
|
201
|
+
};
|
|
202
|
+
const result = captureUsePixelImageData({ data: noImageData, defaultRenderWidth: 200, config });
|
|
203
|
+
expect(result).toBeNull();
|
|
204
|
+
});
|
|
205
|
+
});
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { PixelImageBlockData } from "../../blocks.generated.js";
|
|
2
|
+
export type PixelImageBlockBaseProps = {
|
|
3
|
+
/** The block data to render. */
|
|
4
|
+
data: PixelImageBlockData;
|
|
5
|
+
/** Width at which the image is rendered, in the default/desktop breakpoint. */
|
|
6
|
+
width: number;
|
|
7
|
+
/**
|
|
8
|
+
* Largest possible width the image can be rendered at across breakpoints.
|
|
9
|
+
* Defaults to `theme.sizes.bodyWidth`. Use this when the image can stretch
|
|
10
|
+
* wider on a narrower breakpoint than its desktop render width.
|
|
11
|
+
*/
|
|
12
|
+
largestPossibleRenderWidth?: number;
|
|
13
|
+
/**
|
|
14
|
+
* Aspect ratio for the rendered image.
|
|
15
|
+
* @example "16x9"
|
|
16
|
+
*/
|
|
17
|
+
aspectRatio?: number | string;
|
|
18
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { useConfig } from "../../config/ConfigProvider.js";
|
|
2
|
+
/**
|
|
3
|
+
* Reads `config.pixelImageBlock` from the configuration context and returns it narrowed to non-null.
|
|
4
|
+
*/
|
|
5
|
+
export function usePixelImageBlockConfig() {
|
|
6
|
+
const { pixelImageBlock } = useConfig();
|
|
7
|
+
if (!pixelImageBlock) {
|
|
8
|
+
throw new Error("`pixelImageBlock` must be set in `config` on `MjmlMailRoot` or `ConfigProvider` to use the pixel-image configuration.");
|
|
9
|
+
}
|
|
10
|
+
return pixelImageBlock;
|
|
11
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { PixelImageBlockData as PixelImageBlockSourceData } from "../../blocks.generated.js";
|
|
2
|
+
interface UsePixelImageBlockDataProps {
|
|
3
|
+
data: PixelImageBlockSourceData;
|
|
4
|
+
defaultRenderWidth: number;
|
|
5
|
+
largestPossibleRenderWidth?: number;
|
|
6
|
+
aspectRatio?: number | string;
|
|
7
|
+
}
|
|
8
|
+
interface PixelImageBlockData {
|
|
9
|
+
imageUrl: string;
|
|
10
|
+
defaultRenderWidth: number;
|
|
11
|
+
desktopImageHeight: number;
|
|
12
|
+
alt: string | undefined;
|
|
13
|
+
title: string | undefined;
|
|
14
|
+
}
|
|
15
|
+
export declare function usePixelImageBlockData({ data: { damFile, cropArea, urlTemplate }, defaultRenderWidth, largestPossibleRenderWidth: passedLargestPossibleRenderWidth, aspectRatio: passedAspectRatio, }: UsePixelImageBlockDataProps): PixelImageBlockData | null;
|
|
16
|
+
export {};
|