diff --git a/packages/comment/.eslintrc.js b/packages/comment/.eslintrc.js new file mode 100644 index 0000000000..d750d83f71 --- /dev/null +++ b/packages/comment/.eslintrc.js @@ -0,0 +1,3 @@ +module.exports = { + extends: ["custom/react-internal"], +}; diff --git a/packages/comment/license.md b/packages/comment/license.md new file mode 100644 index 0000000000..5058f53ebb --- /dev/null +++ b/packages/comment/license.md @@ -0,0 +1,7 @@ +Copyright 2024 Plus Five Five, Inc + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/packages/comment/package.json b/packages/comment/package.json new file mode 100644 index 0000000000..e715f199ee --- /dev/null +++ b/packages/comment/package.json @@ -0,0 +1,45 @@ +{ + "name": "@react-email/comment", + "version": "0.0.1", + "description": "HTML comment components", + "sideEffects": false, + "main": "./dist/index.js", + "module": "./dist/index.mjs", + "types": "./dist/index.d.ts", + "files": [ + "dist/**" + ], + "exports": { + ".": { + "import": { + "types": "./dist/index.d.mts", + "default": "./dist/index.mjs" + }, + "require": { + "types": "./dist/index.d.ts", + "default": "./dist/index.js" + } + } + }, + "license": "MIT", + "scripts": { + "build": "tsup src/index.ts --format esm,cjs --dts --external react", + "clean": "rm -rf dist", + "dev": "tsup src/index.ts --format esm,cjs --dts --external react --watch", + "lint": "eslint .", + "test:watch": "vitest", + "test": "vitest run" + }, + "peerDependencies": { + "react": "^18.0 || ^19.0 || ^19.0.0-rc" + }, + "devDependencies": { + "@react-email/render": "workspace:*", + "eslint-config-custom": "workspace:*", + "tsconfig": "workspace:*", + "typescript": "5.1.6" + }, + "publishConfig": { + "access": "public" + } +} diff --git a/packages/comment/src/comment.spec.tsx b/packages/comment/src/comment.spec.tsx new file mode 100644 index 0000000000..1d909ed14a --- /dev/null +++ b/packages/comment/src/comment.spec.tsx @@ -0,0 +1,11 @@ +import { render } from "@react-email/render"; +import { Comment } from "./index"; + +describe(" component", () => { + it("renders correctly", async () => { + const actualOutput = await render(Lorem ipsum); + expect(actualOutput).toMatchInlineSnapshot( + '""', + ); + }); +}); diff --git a/packages/comment/src/comment.tsx b/packages/comment/src/comment.tsx new file mode 100644 index 0000000000..10f21eee01 --- /dev/null +++ b/packages/comment/src/comment.tsx @@ -0,0 +1,10 @@ +import * as React from "react"; +import { childrenPropsDecider, CommentPlaceholder } from "./shared/utils"; + +export type CommentProps = Required; + +export const Comment: React.FC = ({ children }) => ( + +); + +Comment.displayName = "Comment"; diff --git a/packages/comment/src/index.ts b/packages/comment/src/index.ts new file mode 100644 index 0000000000..12ddb4a287 --- /dev/null +++ b/packages/comment/src/index.ts @@ -0,0 +1,9 @@ +export * from "./comment"; + +export * from "./mso"; + +export * from "./non-mso"; + +export * from "./mso-ghost"; + +export * from "./non-mso-ghost"; diff --git a/packages/comment/src/mso-ghost.tsx b/packages/comment/src/mso-ghost.tsx new file mode 100644 index 0000000000..d400099f5b --- /dev/null +++ b/packages/comment/src/mso-ghost.tsx @@ -0,0 +1,38 @@ +import * as React from "react"; +import { + conditionPropsDecider, + MsoPlaceholderCloser, + MsoPlaceholderOpener, +} from "./shared/utils"; +import type { MsoConditionalVersion, MsoVersion } from "./shared/mso"; + +type Version = MsoVersion | MsoConditionalVersion; + +export type MsoGhostProps = Required & { + version?: Version | Version[]; + ghostWrapper: (children: React.ReactNode) => React.ReactElement; +}; + +export const MsoGhost: React.FC = ({ + children, + version, + ghostWrapper, +}) => { + const conditionProps = conditionPropsDecider(version); + + return ( + <> + + {ghostWrapper( + <> + + {children} + + , + )} + + + ); +}; + +MsoGhost.displayName = "MsoGhost"; diff --git a/packages/comment/src/mso.spec.tsx b/packages/comment/src/mso.spec.tsx new file mode 100644 index 0000000000..aee6280b01 --- /dev/null +++ b/packages/comment/src/mso.spec.tsx @@ -0,0 +1,25 @@ +import { render } from "@react-email/render"; +import { Mso } from "./index"; + +describe(" component", () => { + it("renders correctly", async () => { + const actualOutput = await render(Lorem ipsum); + expect(actualOutput).toMatchInlineSnapshot( + '""', + ); + }); + + it("renders correctly with complex children", async () => { + const actualOutput = await render( + +
+ Lorem + ipsum +
+
, + ); + expect(actualOutput).toMatchInlineSnapshot( + '""', + ); + }); +}); diff --git a/packages/comment/src/mso.tsx b/packages/comment/src/mso.tsx new file mode 100644 index 0000000000..c1c733436c --- /dev/null +++ b/packages/comment/src/mso.tsx @@ -0,0 +1,22 @@ +import * as React from "react"; +import { + childrenPropsDecider, + conditionPropsDecider, + MsoPlaceholder, +} from "./shared/utils"; +import type { MsoConditionalVersion, MsoVersion } from "./shared/mso"; + +type Version = MsoVersion | MsoConditionalVersion; + +export type MsoProps = Required & { + version?: Version | Version[]; +}; + +export const Mso: React.FC = ({ children, version }) => ( + +); + +Mso.displayName = "Mso"; diff --git a/packages/comment/src/non-mso-ghost.tsx b/packages/comment/src/non-mso-ghost.tsx new file mode 100644 index 0000000000..3138ef3901 --- /dev/null +++ b/packages/comment/src/non-mso-ghost.tsx @@ -0,0 +1,36 @@ +import * as React from "react"; +import { + conditionPropsDecider, + NonMsoCloserPlaceholder, + NonMsoOpenerPlaceholder, +} from "./shared/utils"; +import type { MsoVersion } from "./shared/mso"; + +export type NonMsoGhostProps = Required & { + version?: MsoVersion | MsoVersion[]; + ghostWrapper: (children: React.ReactNode) => React.ReactElement; +}; + +export const NonMsoGhost: React.FC = ({ + children, + version, + ghostWrapper, +}) => { + const conditionProps = conditionPropsDecider(version); + + return ( + <> + + {ghostWrapper( + <> + + {children} + + , + )} + + + ); +}; + +NonMsoGhost.displayName = "NonMsoGhost"; diff --git a/packages/comment/src/non-mso.tsx b/packages/comment/src/non-mso.tsx new file mode 100644 index 0000000000..63c503a3e8 --- /dev/null +++ b/packages/comment/src/non-mso.tsx @@ -0,0 +1,21 @@ +import * as React from "react"; +import { + conditionPropsDecider, + NonMsoCloserPlaceholder, + NonMsoOpenerPlaceholder, +} from "./shared/utils"; +import type { MsoVersion } from "./shared/mso"; + +export type NonMsoProps = Required & { + version?: MsoVersion | MsoVersion[]; +}; + +export const NonMso: React.FC = ({ children, version }) => ( + <> + + {children} + + +); + +NonMso.displayName = "NonMso"; diff --git a/packages/comment/src/shared/mso.ts b/packages/comment/src/shared/mso.ts new file mode 100644 index 0000000000..227a9b46bd --- /dev/null +++ b/packages/comment/src/shared/mso.ts @@ -0,0 +1,7 @@ +export type MsoVersion = "9" | "10" | "11" | "12" | "14" | "15" | "16"; + +type MsoCondition = "gt" | "lt" | "gte" | "lte"; + +export type MsoConditionalVersion = + | MsoVersion + | `${MsoCondition} ${MsoVersion}`; diff --git a/packages/comment/src/shared/utils/index.ts b/packages/comment/src/shared/utils/index.ts new file mode 100644 index 0000000000..da3da19f2b --- /dev/null +++ b/packages/comment/src/shared/utils/index.ts @@ -0,0 +1,166 @@ +type Opener = (condition: string | undefined) => string; + +interface CommentTemplateData { + opener?: Opener; + closer?: string; + name: Capitalize; +} + +const MSO_CONDITION_ARGUMENT_KEY = "a"; + +const VERSION_SEPARATOR = ","; + +type ChildrenProps = React.PropsWithChildren< + Pick, "dangerouslySetInnerHTML"> +>; + +type ConditionProps = Partial< + Record +>; + +type ComponentPlaceholder = React.FC; + +const generateCommentUtilities = ( + ...args: T +) => { + const alphabet = "abcdefghijklmnopqrstuvwxyz"; + + const placeholders = {} as Record< + { + [index in keyof T]: + | `${T[index]["name"]}Placeholder` + | (T[index]["closer"] extends string + ? T[index]["opener"] extends Opener + ? `${T[index]["name"]}Placeholder${"Opener" | "Closer"}` + : never + : never); + }[number], + ComponentPlaceholder + >; + + const commentReplacerMap = new Map< + string, + (comment: string, condition: string | undefined) => string + >(); + + let alphabetIndex = 0; + + args.forEach(({ name, opener, closer }) => { + const letter = alphabet[alphabetIndex++]; + + placeholders[`${name as T[number]["name"]}Placeholder`] = + `${letter}-` as unknown as ComponentPlaceholder; + + if (opener && closer) { + const openCommentLetter = alphabet[alphabetIndex++]; + + const closeCommentLetter = alphabet[alphabetIndex++]; + + placeholders[`${name}PlaceholderOpener` as keyof typeof placeholders] = + `${openCommentLetter}-` as unknown as ComponentPlaceholder; + + placeholders[`${name}PlaceholderCloser` as keyof typeof placeholders] = + `${closeCommentLetter}-` as unknown as ComponentPlaceholder; + + commentReplacerMap.set( + letter, + (comment, condition) => + ``, + ); + + commentReplacerMap.set( + openCommentLetter, + (_, condition) => ``); + } else if (opener) { + commentReplacerMap.set( + letter, + (comment, condition) => ``, + ); + } else if (closer) { + commentReplacerMap.set(letter, () => ``); + } else { + commentReplacerMap.set(letter, (comment) => ``); + } + }); + + const placeholderRegex = new RegExp( + `<([${alphabet[0]}-${ + alphabet[alphabetIndex - 1] + }])-(?: ${MSO_CONDITION_ARGUMENT_KEY}="(.*?)")?>(.*?)<\\/\\1->`, + "g", + ); + + return { + placeholders, + replaceCommentPlaceholders: (html: string) => + html.replace( + placeholderRegex, + (_, tag: string, condition: string | undefined, comment: string) => + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + commentReplacerMap.get(tag)!(comment, condition), + ), + }; +}; + +const { placeholders, replaceCommentPlaceholders } = generateCommentUtilities( + { + name: "NonMsoOpener", + opener: (version) => + version + ? `[if ${version + .split(VERSION_SEPARATOR) + .map((item) => `!mso ${item}`) + .join(" | ")}]> + data + ? `[if ${data + .split(VERSION_SEPARATOR) + .map((item) => { + const t = item.split(" "); + + return t.length === 1 ? `mso ${t[0]}` : `${t[0]} mso ${t[1]}`; + }) + .join(" | ")}]>` + : "[if mso]>", + closer: " + typeof children === "string" + ? { dangerouslySetInnerHTML: { __html: children } } + : { children }; + +export const conditionPropsDecider = ( + data: string | string[] | undefined, +): ConditionProps => + data + ? { + [MSO_CONDITION_ARGUMENT_KEY]: + typeof data !== "object" ? data : data.join(VERSION_SEPARATOR), + } + : {}; diff --git a/packages/comment/tsconfig.json b/packages/comment/tsconfig.json new file mode 100644 index 0000000000..cd6c94d6e8 --- /dev/null +++ b/packages/comment/tsconfig.json @@ -0,0 +1,5 @@ +{ + "extends": "tsconfig/react-library.json", + "include": ["."], + "exclude": ["dist", "build", "node_modules"] +} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 3b29377b7e..17e725ea8d 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -381,6 +381,25 @@ importers: specifier: 5.1.6 version: 5.1.6 + packages/comment: + dependencies: + react: + specifier: ^18.0 || ^19.0 || ^19.0.0-rc + version: 18.3.1 + devDependencies: + '@react-email/render': + specifier: workspace:* + version: link:../render + eslint-config-custom: + specifier: workspace:* + version: link:../eslint-config-custom + tsconfig: + specifier: workspace:* + version: link:../tsconfig + typescript: + specifier: 5.1.6 + version: 5.1.6 + packages/components: dependencies: '@react-email/body': @@ -9046,7 +9065,7 @@ snapshots: '@types/webpack@5.28.5(@swc/core@1.3.101(@swc/helpers@0.5.12))(esbuild@0.19.11)': dependencies: - '@types/node': 20.16.1 + '@types/node': 18.18.0 tapable: 2.2.1 webpack: 5.91.0(@swc/core@1.3.101(@swc/helpers@0.5.12))(esbuild@0.19.11) transitivePeerDependencies: @@ -9644,7 +9663,7 @@ snapshots: caniuse-lite: 1.0.30001605 fraction.js: 4.3.7 normalize-range: 0.1.2 - picocolors: 1.0.1 + picocolors: 1.0.0 postcss: 8.4.39 postcss-value-parser: 4.2.0