import * as React from 'react'; import { parsePadding } from './utils/parse-padding.js'; import { pxToPt } from './utils/px-to-pt.js'; export type ButtonProps = Readonly>; const maxFontWidth = 5; /** * Computes a msoFontWidth \<= 5 and a count of space characters that, * when applied, end up being as close to `expectedWidth` as possible. */ function computeFontWidthAndSpaceCount(expectedWidth: number) { if (expectedWidth === 0) return [0, 0] as const; let smallestSpaceCount = 0; const computeRequiredFontWidth = () => { if (smallestSpaceCount > 0) { return expectedWidth / smallestSpaceCount / 2; } return Number.POSITIVE_INFINITY; }; while (computeRequiredFontWidth() > maxFontWidth) { smallestSpaceCount++; } return [computeRequiredFontWidth(), smallestSpaceCount] as const; } declare module 'react' { interface CSSProperties { msoPaddingAlt?: string | number | undefined; msoTextRaise?: string | number | undefined; } } export const Button = React.forwardRef( ({ children, style, target = '_blank', ...props }, ref) => { const { paddingTop, paddingRight, paddingBottom, paddingLeft } = parsePadding(style ?? {}); const y = (paddingTop ?? 0) + (paddingBottom ?? 0); const textRaise = pxToPt(y); const [plFontWidth, plSpaceCount] = computeFontWidthAndSpaceCount( paddingLeft ?? 0, ); const [prFontWidth, prSpaceCount] = computeFontWidthAndSpaceCount( paddingRight ?? 0, ); return ( = 500% so we need to add extra spaces accordingly. // // See https://github.com/resend/react-email/issues/1512 for why we do not use letter-spacing instead. __html: ``, }} /> {children} `, }} /> ); }, ); Button.displayName = 'Button';