Branchwell

Become a Pro / website · becomeapro.ai / src / components / mdx / mdx-components.tsx

Raw 6.6 KB on 5069836c1e4c4e369ebe86b13dc7d299c5bed8c0
1import type { ReactNode } from "react";
2import { Info, AlertTriangle, Lightbulb, ArrowUpRight } from "lucide-react";
3import { CodeBlock } from "@/components/mdx/code-block";
4import { slugify } from "@/lib/utils";
5
6function textOf(node: ReactNode): string {
7 if (typeof node === "string" || typeof node === "number") return String(node);
8 if (Array.isArray(node)) return node.map(textOf).join("");
9 if (node && typeof node === "object" && "props" in node) {
10 return textOf((node as { props: { children?: ReactNode } }).props.children);
11 }
12 return "";
13}
14
15// Heading ids match the outline built in lib/tutorials.ts
16function H2({ children }: { children?: ReactNode }) {
17 return <h2 id={slugify(textOf(children))}>{children}</h2>;
18}
19function H3({ children }: { children?: ReactNode }) {
20 return <h3 id={slugify(textOf(children))}>{children}</h3>;
21}
22
23// A tutorial video hosted on our own site and stored in Cloudflare R2.
24// `id` is the slug at airesourcepro.com/media/video/<slug>/ (served from R2 by cloudflare-worker.mjs). preload="none" keeps the
25// page light until someone presses play.
26export function OwnedVideo({ id, title = "Video", captions = false }: { id: string; title?: string; captions?: boolean }) {
27 const base = `/media/video/${id}`;
28 return (
29 <div className="not-prose my-8 aspect-video overflow-hidden rounded-xl border border-border bg-black">
30 <video
31 className="h-full w-full"
32 controls
33 playsInline
34 preload="none"
35 poster={`${base}/poster.jpg`}
36 title={title}
37 aria-label={title}
38 >
39 <source src={`${base}/${id}-720p.mp4`} type="video/mp4" media="(max-width: 820px)" />
40 <source src={`${base}/${id}-1080p.mp4`} type="video/mp4" />
41 {captions && <track kind="captions" src={`${base}/captions.vtt`} srcLang="en" label="English" />}
42 </video>
43 </div>
44 );
45}
46
47// Blues and whites only, like the rest of the site
48const CALLOUTS = {
49 note: { icon: Info, label: "Note", color: "var(--brand)" },
50 tip: { icon: Lightbulb, label: "Tip", color: "var(--sky)" },
51 warning: { icon: AlertTriangle, label: "Heads up", color: "var(--cobalt)" },
52};
53
54function Callout({ type = "note", children }: { type?: keyof typeof CALLOUTS; children: ReactNode }) {
55 const { icon: Icon, label, color } = CALLOUTS[type];
56 return (
57 <div
58 className="not-prose my-10 overflow-hidden rounded-2xl border border-[color-mix(in_srgb,var(--c)_35%,transparent)] bg-[color-mix(in_srgb,var(--c)_7%,transparent)]"
59 style={{ "--c": color } as React.CSSProperties}
60 >
61 <div className="flex gap-4 border-l-4 border-[var(--c)] p-5 sm:p-6">
62 <span className="flex h-9 w-9 shrink-0 items-center justify-center rounded-full bg-[color-mix(in_srgb,var(--c)_16%,transparent)] text-[var(--c)]">
63 <Icon className="h-[18px] w-[18px]" />
64 </span>
65 <div className="min-w-0">
66 <p className="font-mono text-[11.5px] font-medium uppercase tracking-[0.08em] text-[var(--c)]">{label}</p>
67 <div className="mt-1.5 text-[16px] leading-relaxed text-[var(--mist)] [&>p+p]:mt-2 [&>p]:m-0 [&_a]:text-link">{children}</div>
68 </div>
69 </div>
70 </div>
71 );
72}
73
74// Numbered steps on a timeline: wrap a run of <Step title="..."> blocks in <Steps>
75function Steps({ children }: { children: ReactNode }) {
76 return <ol className="not-prose relative my-10 space-y-4 [counter-reset:step]">{children}</ol>;
77}
78
79function Step({ title, children }: { title: string; children: ReactNode }) {
80 return (
81 <li className="relative pl-16 [counter-increment:step] before:absolute before:left-0 before:top-4 before:z-[1] before:flex before:h-10 before:w-10 before:items-center before:justify-center before:rounded-full before:border before:border-[var(--tc,var(--brand))] before:bg-background before:font-mono before:text-sm before:text-[var(--tc,var(--brand))] before:content-[counter(step)] after:absolute after:bottom-[-1rem] after:left-5 after:top-14 after:w-px after:bg-border last:after:hidden">
82 <div className="rounded-2xl border border-border bg-card p-5 transition-colors hover:border-[var(--tc,var(--brand))]">
83 <p className="font-display text-xl tracking-[-0.02em]">{title}</p>
84 <div className="mt-1.5 text-[15.5px] leading-relaxed text-[var(--mist)] [&_a]:text-link [&_code]:text-foreground [&>p]:m-0">{children}</div>
85 </div>
86 </li>
87 );
88}
89
90// Curated links to official material, as a grid of cards: wrap a run of <Resource> blocks in <Resources>.
91// `source` credits who wrote it; `kind` says what it is (Docs, Course, Video...);
92// `date` is only for one-off items like talks, so readers can see how recent they are.
93function Resources({ children }: { children: ReactNode }) {
94 return <ul className="not-prose my-8 grid gap-4 sm:grid-cols-2">{children}</ul>;
95}
96
97function Resource({
98 title,
99 href,
100 source,
101 kind,
102 date,
103 children,
104}: {
105 title: string;
106 href: string;
107 source: string;
108 kind: string;
109 date?: string;
110 children: ReactNode;
111}) {
112 const host = new URL(href).hostname.replace(/^www\./, "");
113 return (
114 <li className="flex">
115 <a
116 href={href}
117 target="_blank"
118 rel="noopener noreferrer"
119 className="group flex w-full flex-col rounded-2xl border border-border bg-card p-5 transition duration-300 hover:-translate-y-0.5 hover:border-[var(--tc,var(--brand))] hover:shadow-[0_18px_40px_-20px_var(--card-glow)]"
120 >
121 <span className="flex flex-wrap items-center gap-2 font-mono text-[11px] uppercase tracking-[0.08em] text-[var(--ash)]">
122 <span className="rounded-full bg-[color-mix(in_srgb,var(--tc,var(--brand))_14%,transparent)] px-2.5 py-1 font-medium text-[var(--tc,var(--brand))]">
123 {kind}
124 </span>
125 <span>{source}</span>
126 {date && (
127 <>
128 <span aria-hidden>·</span>
129 <span>{date}</span>
130 </>
131 )}
132 </span>
133 <span className="mt-3 font-display text-[19px] leading-snug tracking-[-0.02em] text-[var(--link)] underline decoration-[var(--link-line)] decoration-1 underline-offset-4 transition-colors group-hover:decoration-current">
134 {title}
135 </span>
136 <div className="mt-2 text-[14.5px] leading-relaxed text-[var(--mist)] [&>p]:m-0">{children}</div>
137 <span className="mt-auto flex items-center gap-1.5 pt-4 font-mono text-xs text-[var(--ash)] transition-colors group-hover:text-[var(--link)]">
138 {host}
139 <ArrowUpRight className="h-3.5 w-3.5 transition-transform group-hover:-translate-y-0.5 group-hover:translate-x-0.5" />
140 </span>
141 </a>
142 </li>
143 );
144}
145
146export const mdxComponents = {
147 h2: H2,
148 h3: H3,
149 pre: CodeBlock,
150 OwnedVideo,
151 Callout,
152 Steps,
153 Step,
154 Resources,
155 Resource,
156};