@tonycodes-ui/mortar 0.12.0 → 0.14.0

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/src/email.tsx ADDED
@@ -0,0 +1,413 @@
1
+ /* Transactional email: @tonycodes-ui/mortar/email. Built on React Email,
2
+ which renders the table layout and inline styles email clients need; these
3
+ components give it Mortar's tokens, brand and copy. Nothing here uses
4
+ Mortar's stylesheet or app components, which email clients cannot run.
5
+ Requires the optional peer dependency @react-email/components. */
6
+
7
+ import {
8
+ createContext,
9
+ useContext,
10
+ type CSSProperties,
11
+ type ReactNode,
12
+ } from "react";
13
+ import {
14
+ Body,
15
+ Button,
16
+ Container,
17
+ Head,
18
+ Heading,
19
+ Html,
20
+ Img,
21
+ Link,
22
+ Preview,
23
+ Text,
24
+ render,
25
+ } from "@react-email/components";
26
+ import { emailTheme, type EmailTheme } from "./email-theme.js";
27
+
28
+ export { emailTheme } from "./email-theme.js";
29
+ export type { EmailTheme } from "./email-theme.js";
30
+
31
+ const ThemeContext = createContext<EmailTheme | undefined>(undefined);
32
+ let neutral: EmailTheme | undefined;
33
+ /** The theme from the enclosing EmailShell, or Mortar's neutral theme. */
34
+ function useTheme(): EmailTheme {
35
+ return useContext(ThemeContext) ?? (neutral ??= emailTheme());
36
+ }
37
+
38
+ /* Links leave the mail client, so only web and mail addresses are allowed. */
39
+ function assertHref(href: string, component: string, mail = false) {
40
+ if (!(mail ? /^(?:https?:|mailto:)/i : /^https?:\/\//i).test(href.trim()))
41
+ throw new Error(
42
+ `Mortar ${component} needs an absolute ${mail ? "http(s) or mailto" : "http(s)"} URL: "${href}"`,
43
+ );
44
+ }
45
+
46
+ /* A one-cell layout table. Outlook for Windows ignores padding, margins and
47
+ backgrounds on tables, so spacing and fills go on the cell. */
48
+ function Cell({
49
+ style,
50
+ children,
51
+ }: {
52
+ style: CSSProperties;
53
+ children: ReactNode;
54
+ }) {
55
+ return (
56
+ <table
57
+ role="presentation"
58
+ width="100%"
59
+ border={0}
60
+ cellPadding={0}
61
+ cellSpacing={0}
62
+ >
63
+ <tbody>
64
+ <tr>
65
+ <td style={style}>{children}</td>
66
+ </tr>
67
+ </tbody>
68
+ </table>
69
+ );
70
+ }
71
+
72
+ export interface EmailLogo {
73
+ /** An absolute URL: the email is read away from your site. */
74
+ src: string;
75
+ alt: string;
76
+ width: number;
77
+ height: number;
78
+ }
79
+
80
+ export interface EmailShellProps {
81
+ /** The inbox preview line shown after the subject. */
82
+ preview: string;
83
+ /** From `emailTheme(brand)`; Mortar's neutral theme by default. */
84
+ theme?: EmailTheme;
85
+ logo?: EmailLogo;
86
+ /** Why the person received the email, and what to do if it wasn't for them. */
87
+ footer?: ReactNode;
88
+ lang?: string;
89
+ dir?: "ltr" | "rtl";
90
+ children: ReactNode;
91
+ }
92
+ /**
93
+ * The whole email: a card up to 600px wide on the sunken backdrop, with an
94
+ * optional logo above it and footer below. It sets the theme for the email
95
+ * components inside, and marks the email light-only so mail apps don't
96
+ * recolour the brand.
97
+ */
98
+ export function EmailShell({
99
+ preview,
100
+ theme: given,
101
+ logo,
102
+ footer,
103
+ lang = "en",
104
+ dir = "ltr",
105
+ children,
106
+ }: EmailShellProps) {
107
+ const theme = given ?? emailTheme();
108
+ if (logo) assertHref(logo.src, "EmailShell logo");
109
+ return (
110
+ <ThemeContext.Provider value={theme}>
111
+ <Html lang={lang} dir={dir}>
112
+ <Head>
113
+ <meta name="color-scheme" content="light" />
114
+ <meta name="supported-color-schemes" content="light" />
115
+ </Head>
116
+ <Preview>{preview}</Preview>
117
+ <Body
118
+ style={{
119
+ margin: 0,
120
+ padding: "32px 12px",
121
+ backgroundColor: theme.sunken,
122
+ color: theme.ink,
123
+ fontFamily: theme.font,
124
+ }}
125
+ >
126
+ <Container style={{ width: "100%", maxWidth: "600px" }}>
127
+ {logo && (
128
+ <Cell style={{ padding: "0 0 24px" }}>
129
+ <Img
130
+ src={logo.src}
131
+ alt={logo.alt}
132
+ width={logo.width}
133
+ height={logo.height}
134
+ />
135
+ </Cell>
136
+ )}
137
+ <Cell
138
+ style={{
139
+ backgroundColor: theme.surface,
140
+ border: `1px solid ${theme.line}`,
141
+ borderRadius: "12px",
142
+ padding: "32px",
143
+ }}
144
+ >
145
+ {children}
146
+ </Cell>
147
+ {footer && (
148
+ <Cell style={{ padding: "24px 32px 0" }}>
149
+ <Text
150
+ style={{
151
+ margin: 0,
152
+ fontFamily: theme.font,
153
+ fontSize: "12px",
154
+ lineHeight: "18px",
155
+ color: theme.muted,
156
+ }}
157
+ >
158
+ {footer}
159
+ </Text>
160
+ </Cell>
161
+ )}
162
+ </Container>
163
+ </Body>
164
+ </Html>
165
+ </ThemeContext.Provider>
166
+ );
167
+ }
168
+
169
+ export interface EmailHeadingProps {
170
+ /** 1 for the email's title (once per email), 2 for a section. */
171
+ level?: 1 | 2;
172
+ children: ReactNode;
173
+ }
174
+ export function EmailHeading({ level = 1, children }: EmailHeadingProps) {
175
+ const theme = useTheme();
176
+ return (
177
+ <Heading
178
+ as={level === 1 ? "h1" : "h2"}
179
+ style={{
180
+ margin: "0 0 16px",
181
+ fontFamily: theme.fontHeading,
182
+ fontSize: level === 1 ? "24px" : "18px",
183
+ lineHeight: level === 1 ? "32px" : "26px",
184
+ fontWeight: 600,
185
+ letterSpacing: "-0.02em",
186
+ color: theme.ink,
187
+ }}
188
+ >
189
+ {children}
190
+ </Heading>
191
+ );
192
+ }
193
+
194
+ export interface EmailTextProps {
195
+ /** `muted` for secondary details such as an expiry time. */
196
+ tone?: "default" | "muted";
197
+ size?: "md" | "sm";
198
+ children: ReactNode;
199
+ }
200
+ /** A paragraph. 16px by default: email is read on phones, at arm's length. */
201
+ export function EmailText({
202
+ tone = "default",
203
+ size = "md",
204
+ children,
205
+ }: EmailTextProps) {
206
+ const theme = useTheme();
207
+ return (
208
+ <Text
209
+ style={{
210
+ margin: "0 0 16px",
211
+ fontFamily: theme.font,
212
+ fontSize: size === "md" ? "16px" : "14px",
213
+ lineHeight: size === "md" ? "24px" : "20px",
214
+ color: tone === "muted" ? theme.muted : theme.ink,
215
+ }}
216
+ >
217
+ {children}
218
+ </Text>
219
+ );
220
+ }
221
+
222
+ export interface EmailButtonProps {
223
+ /** An absolute http(s) URL. */
224
+ href: string;
225
+ children: ReactNode;
226
+ }
227
+ /**
228
+ * The email's main action, on its own line. Padding is applied in a way
229
+ * Outlook for Windows keeps, so the whole button is clickable there too.
230
+ * Follow it with the URL as an EmailLink for clients that block buttons.
231
+ */
232
+ export function EmailButton({ href, children }: EmailButtonProps) {
233
+ const theme = useTheme();
234
+ assertHref(href, "EmailButton");
235
+ return (
236
+ <Cell style={{ padding: "8px 0 24px" }}>
237
+ <Button
238
+ href={href}
239
+ style={{
240
+ display: "inline-block",
241
+ backgroundColor: theme.primary,
242
+ color: theme.onPrimary,
243
+ fontFamily: theme.font,
244
+ fontSize: "16px",
245
+ lineHeight: "24px",
246
+ fontWeight: 600,
247
+ textDecoration: "none",
248
+ padding: "12px 24px",
249
+ borderRadius: "8px",
250
+ }}
251
+ >
252
+ {children}
253
+ </Button>
254
+ </Cell>
255
+ );
256
+ }
257
+
258
+ export interface EmailLinkProps {
259
+ /** An absolute http(s) or mailto URL. */
260
+ href: string;
261
+ /** The URL itself when omitted, for "paste this link" fallbacks. */
262
+ children?: ReactNode;
263
+ }
264
+ export function EmailLink({ href, children }: EmailLinkProps) {
265
+ const theme = useTheme();
266
+ assertHref(href, "EmailLink", true);
267
+ return (
268
+ <Link
269
+ href={href}
270
+ style={{
271
+ color: theme.link,
272
+ textDecoration: "underline",
273
+ wordBreak: "break-all",
274
+ }}
275
+ >
276
+ {children ?? href}
277
+ </Link>
278
+ );
279
+ }
280
+
281
+ export interface EmailCodeProps {
282
+ /** A one-time code, shown large and spaced for reading and copying. */
283
+ code: string;
284
+ }
285
+ export function EmailCode({ code }: EmailCodeProps) {
286
+ const theme = useTheme();
287
+ return (
288
+ <Cell style={{ padding: "8px 0 24px" }}>
289
+ <Cell
290
+ style={{
291
+ backgroundColor: theme.sunken,
292
+ borderRadius: "8px",
293
+ padding: "16px",
294
+ textAlign: "center",
295
+ }}
296
+ >
297
+ <Text
298
+ style={{
299
+ margin: 0,
300
+ fontFamily: theme.mono,
301
+ fontSize: "28px",
302
+ lineHeight: "36px",
303
+ fontWeight: 600,
304
+ letterSpacing: "0.2em",
305
+ color: theme.ink,
306
+ }}
307
+ >
308
+ {code}
309
+ </Text>
310
+ </Cell>
311
+ </Cell>
312
+ );
313
+ }
314
+
315
+ export interface RenderedEmail {
316
+ html: string;
317
+ /** The plain-text alternative: send it with the HTML. */
318
+ text: string;
319
+ }
320
+ /**
321
+ * An email element as HTML and plain text, ready for a sending service
322
+ * (Resend, SES, Postmark…). Keep the HTML under about 100 KB: Gmail clips
323
+ * longer messages.
324
+ */
325
+ export async function renderEmail(element: ReactNode): Promise<RenderedEmail> {
326
+ const [html, text] = await Promise.all([
327
+ render(element),
328
+ render(element, { plainText: true }),
329
+ ]);
330
+ return { html, text };
331
+ }
332
+
333
+ /* Templates */
334
+
335
+ export interface VerifyEmailProps {
336
+ /** Your product, as the person knows it. */
337
+ productName: string;
338
+ /** A one-time code to type in, shown large. */
339
+ code?: string;
340
+ /** A confirmation link, shown as a button. Give `code`, `href` or both. */
341
+ href?: string;
342
+ /** The address being confirmed. */
343
+ recipient?: string;
344
+ /** Preformatted, e.g. "10 minutes". */
345
+ expiresIn?: string;
346
+ theme?: EmailTheme;
347
+ logo?: EmailLogo;
348
+ /** Replaces the default "if this wasn't you" footer. */
349
+ footer?: ReactNode;
350
+ lang?: string;
351
+ }
352
+ /**
353
+ * Confirms an email address with a code, a link or both: the email for the
354
+ * auth flow's verify-email step.
355
+ */
356
+ export function VerifyEmail({
357
+ productName,
358
+ code,
359
+ href,
360
+ recipient,
361
+ expiresIn,
362
+ theme,
363
+ logo,
364
+ footer,
365
+ lang,
366
+ }: VerifyEmailProps) {
367
+ if (!code && !href)
368
+ throw new Error("Mortar VerifyEmail needs a `code`, an `href` or both.");
369
+ const address = recipient ?? "your email address";
370
+ return (
371
+ <EmailShell
372
+ preview={`Confirm your email address for ${productName}`}
373
+ theme={theme}
374
+ logo={logo}
375
+ lang={lang}
376
+ footer={
377
+ footer ??
378
+ `You received this email because this address was used to sign up for ${productName}. If that wasn't you, you can ignore it.`
379
+ }
380
+ >
381
+ <EmailHeading>Confirm your email address</EmailHeading>
382
+ {code && (
383
+ <>
384
+ <EmailText>
385
+ Enter this code in {productName} to confirm {address}:
386
+ </EmailText>
387
+ <EmailCode code={code} />
388
+ </>
389
+ )}
390
+ {href && (
391
+ <>
392
+ <EmailText>
393
+ {code
394
+ ? "Or confirm with one click:"
395
+ : `Confirm ${address} to finish setting up your ${productName} account.`}
396
+ </EmailText>
397
+ <EmailButton href={href}>Confirm email address</EmailButton>
398
+ <EmailText tone="muted" size="sm">
399
+ If the button doesn't work, paste this link into your browser:{" "}
400
+ <EmailLink href={href} />
401
+ </EmailText>
402
+ </>
403
+ )}
404
+ {expiresIn && (
405
+ <EmailText tone="muted" size="sm">
406
+ {code && href
407
+ ? `This code and link expire in ${expiresIn}.`
408
+ : `This ${code ? "code" : "link"} expires in ${expiresIn}.`}
409
+ </EmailText>
410
+ )}
411
+ </EmailShell>
412
+ );
413
+ }
@@ -1128,9 +1128,13 @@
1128
1128
  }
1129
1129
 
1130
1130
  /* == SegmentedControl == */
1131
+ /* Never wider than its container: long or many options scroll inside the
1132
+ control, which keeps the selected option in view (inspection.tsx). */
1131
1133
  .m-segmented {
1132
1134
  display: inline-flex;
1133
1135
  width: fit-content;
1136
+ max-width: 100%;
1137
+ overflow-x: auto;
1134
1138
  background: var(--m-sunken);
1135
1139
  padding: 3px;
1136
1140
  gap: 2px;
@@ -1146,6 +1150,12 @@
1146
1150
  padding: 5px 10px;
1147
1151
  cursor: pointer;
1148
1152
  min-height: max(28px, var(--m-touch-target));
1153
+ flex-shrink: 0;
1154
+ white-space: nowrap;
1155
+ }
1156
+ /* Inside the 3px padding, so the scroll container doesn't clip the ring. */
1157
+ .m-segmented button:focus-visible {
1158
+ outline-offset: 1px;
1149
1159
  }
1150
1160
  .m-segmented [aria-pressed="true"] {
1151
1161
  background: var(--m-surface);
package/src/index.ts CHANGED
@@ -66,6 +66,9 @@ export type {
66
66
  TabNavItem,
67
67
  StackedShellProps,
68
68
  NavbarProps,
69
+ NavbarItem,
70
+ NavbarGroup,
71
+ NavbarTone,
69
72
  BreadcrumbsProps,
70
73
  TabNavProps,
71
74
  ProgressStepsProps,
@@ -1,3 +1,4 @@
1
+ import { useLayoutEffect, useRef } from "react";
1
2
  import { KitLink } from "./navigation.js";
2
3
  import { componentRoot, cx, useComponentName } from "./internal.js";
3
4
  export function SegmentedControl({
@@ -14,8 +15,22 @@ export function SegmentedControl({
14
15
  className?: string;
15
16
  }) {
16
17
  const name = useComponentName("SegmentedControl");
18
+ const group = useRef<HTMLDivElement>(null);
19
+ // When the options scroll (narrow screens), keep the selected one in view,
20
+ // on mount and on change. Only the control scrolls, never the page.
21
+ useLayoutEffect(() => {
22
+ const root = group.current;
23
+ const selected = root?.querySelector<HTMLElement>('[aria-pressed="true"]');
24
+ if (!root || !selected || root.scrollWidth <= root.clientWidth) return;
25
+ const outer = root.getBoundingClientRect();
26
+ const inner = selected.getBoundingClientRect();
27
+ if (inner.left < outer.left) root.scrollLeft -= outer.left - inner.left;
28
+ else if (inner.right > outer.right)
29
+ root.scrollLeft += inner.right - outer.right;
30
+ }, [value]);
17
31
  return (
18
32
  <div
33
+ ref={group}
19
34
  {...componentRoot(name)}
20
35
  className={cx("m-segmented", className)}
21
36
  role="group"
package/src/internal.tsx CHANGED
@@ -14,8 +14,17 @@ import {
14
14
  cloneElement,
15
15
  isValidElement,
16
16
  } from "react";
17
- import { Dialog, DialogPanel, DialogTitle } from "@headlessui/react";
18
- import { KitLink, type ItemGroup } from "./navigation.js";
17
+ import {
18
+ CloseButton,
19
+ Dialog,
20
+ DialogPanel,
21
+ DialogTitle,
22
+ Popover,
23
+ PopoverButton,
24
+ PopoverPanel,
25
+ } from "@headlessui/react";
26
+ import { KitLink, type ItemGroup, type LinkItem } from "./navigation.js";
27
+ import { ChevronDown } from "./icons.js";
19
28
  import { ActionLink, Button, type LinkAction } from "./primitives.js";
20
29
  import { headingTag, type HeadingLevel } from "./heading.js";
21
30
 
@@ -698,3 +707,85 @@ export function focusAfterRemoval(element: HTMLElement) {
698
707
  }
699
708
  });
700
709
  }
710
+
711
+ /** An item in a flyout panel; `current` marks the page you are on. */
712
+ export type FlyoutItem = Pick<
713
+ LinkItem,
714
+ "label" | "href" | "description" | "icon" | "current"
715
+ >;
716
+ /**
717
+ * A labelled group of links in a header that opens an anchored panel
718
+ * (MarketingHeader and Navbar). The group is marked current when one of its
719
+ * items is the current page.
720
+ */
721
+ export function Flyout({
722
+ label,
723
+ items,
724
+ footer = [],
725
+ layout = "list",
726
+ onNavigate,
727
+ }: {
728
+ label: string;
729
+ items: FlyoutItem[];
730
+ footer?: LinkAction[];
731
+ layout?: "list" | "columns" | "full";
732
+ onNavigate: () => void;
733
+ }) {
734
+ const dataLayout = layout === "list" ? undefined : layout;
735
+ const current = items.some((item) => item.current);
736
+ return (
737
+ <Popover
738
+ className="m-flyout"
739
+ data-current={current || undefined}
740
+ data-layout={dataLayout}
741
+ >
742
+ <PopoverButton className="m-flyout-trigger">
743
+ {label}
744
+ <ChevronDown />
745
+ </PopoverButton>
746
+ {/* Anchored panels render in a portal, so headers inside clipped containers never cut them off. */}
747
+ <PopoverPanel
748
+ anchor="bottom"
749
+ className="m-flyout-panel"
750
+ data-layout={dataLayout}
751
+ >
752
+ <ul>
753
+ {items.map((item, index) => (
754
+ <li key={`${item.label}:${item.href}:${index}`}>
755
+ <CloseButton
756
+ as={KitLink}
757
+ href={item.href}
758
+ aria-current={item.current ? "page" : undefined}
759
+ onClick={onNavigate}
760
+ >
761
+ {item.icon && (
762
+ <span className="m-flyout-icon" aria-hidden="true">
763
+ {item.icon}
764
+ </span>
765
+ )}
766
+ <span className="m-flyout-text">
767
+ <strong>{item.label}</strong>
768
+ {item.description && <span>{item.description}</span>}
769
+ </span>
770
+ </CloseButton>
771
+ </li>
772
+ ))}
773
+ </ul>
774
+ {footer.length > 0 && (
775
+ <div className="m-flyout-footer">
776
+ {footer.map((link, index) => (
777
+ <CloseButton
778
+ as={KitLink}
779
+ key={`${link.label}:${link.href}:${index}`}
780
+ href={link.href}
781
+ onClick={onNavigate}
782
+ >
783
+ {link.label}
784
+ </CloseButton>
785
+ ))}
786
+ </div>
787
+ )}
788
+ </PopoverPanel>
789
+ </Popover>
790
+ );
791
+ }
package/src/marketing.tsx CHANGED
@@ -3,17 +3,9 @@ import {
3
3
  KitLink,
4
4
  type IconLink,
5
5
  type ItemGroup,
6
- type LinkItem,
7
6
  } from "./navigation.js";
8
7
  import { useId, useState, type CSSProperties, type ReactNode } from "react";
9
- import {
10
- CloseButton,
11
- DialogPanel,
12
- DialogTitle,
13
- Popover,
14
- PopoverButton,
15
- PopoverPanel,
16
- } from "@headlessui/react";
8
+ import { DialogPanel, DialogTitle } from "@headlessui/react";
17
9
  import { ActionLink, Button, type LinkAction } from "./primitives.js";
18
10
  import { BrandMark, Prose, type SectionTone } from "./layout.js";
19
11
  import { SegmentedControl } from "./inspection.js";
@@ -23,6 +15,8 @@ import { childLevel, headingTag, type HeadingLevel } from "./heading.js";
23
15
  import {
24
16
  ActionLinks,
25
17
  DialogFrame,
18
+ Flyout,
19
+ type FlyoutItem,
26
20
  TermList,
27
21
  TitledIntro,
28
22
  cx,
@@ -32,10 +26,7 @@ import {
32
26
  useDomId,
33
27
  } from "./internal.js";
34
28
 
35
- export type FlyoutItem = Pick<
36
- LinkItem,
37
- "label" | "href" | "description" | "icon"
38
- >;
29
+ export type { FlyoutItem } from "./internal.js";
39
30
  /** A header link, or a labelled group that opens a flyout panel. */
40
31
  export type HeaderLink =
41
32
  | LinkAction
@@ -45,67 +36,6 @@ export type HeaderLink =
45
36
  layout?: "list" | "columns" | "full";
46
37
  });
47
38
 
48
- function Flyout({
49
- label,
50
- items,
51
- footer = [],
52
- layout = "list",
53
- onNavigate,
54
- }: {
55
- label: string;
56
- items: FlyoutItem[];
57
- footer?: LinkAction[];
58
- layout?: "list" | "columns" | "full";
59
- onNavigate: () => void;
60
- }) {
61
- const dataLayout = layout === "list" ? undefined : layout;
62
- return (
63
- <Popover className="m-flyout" data-layout={dataLayout}>
64
- <PopoverButton className="m-flyout-trigger">
65
- {label}
66
- <ChevronDown />
67
- </PopoverButton>
68
- {/* Anchored panels render in a portal, so headers inside clipped containers never cut them off. */}
69
- <PopoverPanel
70
- anchor="bottom"
71
- className="m-flyout-panel"
72
- data-layout={dataLayout}
73
- >
74
- <ul>
75
- {items.map((item, index) => (
76
- <li key={`${item.label}:${item.href}:${index}`}>
77
- <CloseButton as={KitLink} href={item.href} onClick={onNavigate}>
78
- {item.icon && (
79
- <span className="m-flyout-icon" aria-hidden="true">
80
- {item.icon}
81
- </span>
82
- )}
83
- <span className="m-flyout-text">
84
- <strong>{item.label}</strong>
85
- {item.description && <span>{item.description}</span>}
86
- </span>
87
- </CloseButton>
88
- </li>
89
- ))}
90
- </ul>
91
- {footer.length > 0 && (
92
- <div className="m-flyout-footer">
93
- {footer.map((link, index) => (
94
- <CloseButton
95
- as={KitLink}
96
- key={`${link.label}:${link.href}:${index}`}
97
- href={link.href}
98
- onClick={onNavigate}
99
- >
100
- {link.label}
101
- </CloseButton>
102
- ))}
103
- </div>
104
- )}
105
- </PopoverPanel>
106
- </Popover>
107
- );
108
- }
109
39
  export function MarketingHeader({
110
40
  brand,
111
41
  tagline,