Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"write-translations": "docusaurus write-translations",
"write-heading-ids": "docusaurus write-heading-ids",
"typecheck": "tsc",
"test": "node --test scripts/openapi-display.test.mjs scripts/gen-config-ref.test.mjs scripts/sync-catalog.test.mjs src/components/tutorials/snippetText.test.mjs src/components/tutorials/codeGroupTabs.test.mjs",
"test": "node --test scripts/openapi-display.test.mjs scripts/gen-config-ref.test.mjs scripts/sync-catalog.test.mjs src/components/tutorials/snippetText.test.mjs src/components/tutorials/codeGroupTabs.test.mjs src/components/tutorials/titledChildren.test.mjs src/components/tutorials/cardLink.test.mjs",
"spellcheck": "cspell \"**/*.md\" \"**/*.mdx\" --no-progress"
},
"dependencies": {
Expand Down
60 changes: 60 additions & 0 deletions src/components/tutorials/Accordion.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// A question or a detail the reader opens only when they need it: what a flag does, what the
// output looks like. It keeps a tutorial short without hiding anything. Each item opens and
// closes on its own, drawn with the native details element so it works before the page's scripts
// load; AccordionGroup joins several items into one bordered list. src/theme/MDXComponents/index
// .tsx registers them as <Accordion> and <AccordionGroup>, so tutorial pages use them without an
// import. The chevron's turn is styled in src/css/custom.css.
import React, {type ReactNode} from 'react';
import clsx from 'clsx';

export type AccordionProps = {
/** The headline that stays visible, such as the question the item answers. */
title: string;
/** Start open instead of collapsed. */
defaultOpen?: boolean;
/** What the reader sees when the item is open. */
children?: ReactNode;
/** Classes added to the item. */
className?: string;
};

export default function Accordion({
title,
defaultOpen,
children,
className,
}: AccordionProps): ReactNode {
return (
<details className={clsx('ccf-accordion', className)} open={defaultOpen || undefined}>
<summary className="ccf-accordion-summary">
<span className="min-w-0 font-semibold">{title}</span>
<svg
aria-hidden="true"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth={2}
strokeLinecap="round"
strokeLinejoin="round"
className="ccf-accordion-chevron h-4 w-4 shrink-0 text-[var(--ifm-font-color-secondary)]"
>
<path d="m9 18 6-6-6-6" />
</svg>
</summary>
<div className="ccf-accordion-body">{children}</div>
</details>
);
}

export type AccordionGroupProps = {
/** The items, in reading order: <Accordion> elements. */
children?: ReactNode;
/** Classes added to the group. */
className?: string;
};

export function AccordionGroup({children, className}: AccordionGroupProps): ReactNode {
return (
<div className={clsx('ccf-accordion-group my-4', className)}>{children}</div>
);
}
118 changes: 118 additions & 0 deletions src/components/tutorials/Callout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
// The notes a tutorial makes beside its steps: a caveat before a destructive command, a shortcut
// worth knowing. The type picks the accent colour and the icon, following the five admonitions
// Docusaurus itself ships (note, tip, info, warning, danger). <Note>, <Tip>, <Info>, <Warning>
// and <Danger> are <Callout> with the type already set. src/theme/MDXComponents/index.tsx
// registers all six, so tutorial pages use them without an import. The accent colours live in
// src/css/custom.css, in the site's Infima palette.
import React, {type ReactNode} from 'react';
import clsx from 'clsx';

export type CalloutType = 'note' | 'tip' | 'info' | 'warning' | 'danger';

export type CalloutProps = {
/** The kind of note: picks the accent colour and the icon. `note` by default. */
type?: CalloutType;
/** A bold line before the text, such as the command the caveat is about. */
title?: string;
/** What the reader needs to know. */
children?: ReactNode;
/** Classes added to the callout. */
className?: string;
};

const strokeWidth = 2;
const iconProps = {
'aria-hidden': true,
viewBox: '0 0 24 24',
fill: 'none',
stroke: 'currentColor',
strokeWidth,
strokeLinecap: 'round',
strokeLinejoin: 'round',
className: 'h-5 w-5',
} as const;

const icons: Record<CalloutType, ReactNode> = {
note: (
<svg {...iconProps}>
<path d="M12 20h9" />
<path d="M16.5 3.5a2.12 2.12 0 0 1 3 3L7 19l-4 1 1-4Z" />
</svg>
),
tip: (
<svg {...iconProps}>
<path d="M9 18h6" />
<path d="M10 21h4" />
<path d="M12 3a6 6 0 0 0-4 10.5c.6.5 1 1.4 1 2.5h6c0-1.1.4-2 1-2.5A6 6 0 0 0 12 3Z" />
</svg>
),
info: (
<svg {...iconProps}>
<circle cx="12" cy="12" r="10" />
<path d="M12 16v-4" />
<path d="M12 8h.01" />
</svg>
),
warning: (
<svg {...iconProps}>
<path d="m10.29 3.86-8.18 14A2 2 0 0 0 3.83 21h16.34a2 2 0 0 0 1.72-3.14l-8.18-14a2 2 0 0 0-3.42 0Z" />
<path d="M12 9v4" />
<path d="M12 17h.01" />
</svg>
),
danger: (
<svg {...iconProps}>
<path d="M7.86 2h8.28L22 7.86v8.28L16.14 22H7.86L2 16.14V7.86Z" />
<path d="M12 8v4" />
<path d="M12 16h.01" />
</svg>
),
};

export function Callout({type = 'note', title, children, className}: CalloutProps): ReactNode {
return (
<div
className={clsx(
'ccf-callout',
`ccf-callout--${type}`,
'my-4 flex gap-3 rounded-r-[var(--ifm-global-radius)] border-l-4 px-4 py-3',
className,
)}
>
<span className="mt-0.5 shrink-0 text-[var(--ccf-callout-accent)]">{icons[type]}</span>
<div className="ccf-callout-body min-w-0">
{title ? <div className="font-semibold text-[var(--ifm-heading-color)]">{title}</div> : null}
{children}
</div>
</div>
);
}

export default Callout;

type CalloutContentProps = Omit<CalloutProps, 'type'>;

/** A side note that does not change what the reader does. */
export function Note(props: CalloutContentProps): ReactNode {
return <Callout type="note" {...props} />;
}

/** A shortcut or trick worth knowing. */
export function Tip(props: CalloutContentProps): ReactNode {
return <Callout type="tip" {...props} />;
}

/** Background that helps the reader understand the step. */
export function Info(props: CalloutContentProps): ReactNode {
return <Callout type="info" {...props} />;
}

/** Something easy to get wrong; the step still works if the reader heeds it. */
export function Warning(props: CalloutContentProps): ReactNode {
return <Callout type="warning" {...props} />;
}

/** Harm if ignored: lost data, a revoked credential, a broken deployment. */
export function Danger(props: CalloutContentProps): ReactNode {
return <Callout type="danger" {...props} />;
}
113 changes: 113 additions & 0 deletions src/components/tutorials/Cards.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
// A link shown as a clickable card: the "what to do next" at the end of a tutorial step, or the
// links between tutorials. CardGroup lays several cards out in a responsive grid. An internal
// href (starting with a single /) links through Docusaurus's Link, so navigating does not reload
// the site; any other href opens in a new tab. src/theme/MDXComponents/index.tsx registers them
// as <Card> and <CardGroup>, so tutorial pages use them without an import.
import React, {type ReactNode} from 'react';
import Link from '@docusaurus/Link';
import clsx from 'clsx';
import {isInternalHref} from './cardLink.mjs';

export type CardProps = {
/** The card's headline: where the link leads. */
title: string;
/** Where the card links. Omit it for a card that only holds text. */
href?: string;
/** A short glyph before the title, such as an emoji. The site has no icon set to draw from. */
icon?: string;
/** One or two lines saying what the reader finds there. */
children?: ReactNode;
/** Classes added to the card. */
className?: string;
};

const cardBox = clsx(
'ccf-card group block rounded-[var(--ifm-global-radius)] border p-4 no-underline',
'border-[var(--ifm-color-emphasis-300)] bg-[var(--ifm-background-surface-color)]',
'text-[var(--ifm-font-color-base)] transition-[border-color]',
'hover:border-[var(--ifm-color-primary)] hover:no-underline',
);

export default function Card({title, href, icon, children, className}: CardProps): ReactNode {
const heading = (
<span className="flex items-center gap-2 font-semibold text-[var(--ifm-heading-color)]">
{icon ? (
<span aria-hidden="true" className="shrink-0">
{icon}
</span>
) : null}
<span className="min-w-0">{title}</span>
{href ? (
<svg
aria-hidden="true"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth={2}
strokeLinecap="round"
strokeLinejoin="round"
className="ml-auto h-4 w-4 shrink-0 text-[var(--ifm-font-color-secondary)] transition-transform group-hover:translate-x-0.5"
>
<path d="M5 12h14" />
<path d="m13 6 6 6-6 6" />
</svg>
) : null}
</span>
);
const body = (
<>
{heading}
{children ? (
// A div, not a span: MDX hands the children as <p> elements, and a <p> may not sit
// inside a phrasing-only <span>.
<div className="mt-1 text-[var(--ifm-font-color-secondary)]">{children}</div>
) : null}
</>
);
if (href && isInternalHref(href)) {
return (
<Link to={href} className={clsx(cardBox, className)}>
{body}
</Link>
);
}
if (href) {
return (
<a href={href} target="_blank" rel="noopener noreferrer" className={clsx(cardBox, className)}>
{body}
</a>
);
}
return <div className={clsx(cardBox, className)}>{body}</div>;
}

export type CardGroupProps = {
/** How many columns a wide screen shows; 2 by default. Cards stack into one column below. */
cols?: 1 | 2 | 3 | 4;
/** The cards, in reading order. */
children?: ReactNode;
/** Classes added to the group. */
className?: string;
};

// Written out so Tailwind can see each class: a class name built at runtime would be purged.
const gridCols: Record<number, string> = {
1: 'sm:grid-cols-1',
2: 'sm:grid-cols-2',
3: 'sm:grid-cols-3',
4: 'sm:grid-cols-4',
};

export function CardGroup({cols = 2, children, className}: CardGroupProps): ReactNode {
return (
<div
className={clsx(
'ccf-card-group my-4 grid grid-cols-1 gap-4',
gridCols[cols] ?? gridCols[2],
className,
)}
>
{children}
</div>
);
}
66 changes: 66 additions & 0 deletions src/components/tutorials/Steps.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// A tutorial task broken into numbered stages, read top to bottom. The circle numbers each step
// and the line joins the circles, so the reader sees how far they have come. The content of a
// step is ordinary Markdown, so a step can hold commands (CopyBlock), prose or a callout.
// src/theme/MDXComponents/index.tsx registers them as <Steps> and <Step>, so tutorial pages use
// them without an import. The circle and the connecting line are drawn in src/css/custom.css.
import React, {type ReactElement, type ReactNode} from 'react';
import clsx from 'clsx';
import {titledChildren} from './titledChildren.mjs';

export type StepProps = {
/** The step's headline, in a few words: what the reader does in this step. */
title: string;
/** What the step asks the reader to do. */
children?: ReactNode;
};

/**
* One step of a Steps list. It only carries its headline and its content: the list draws the
* number and the connecting line, so a <Step> left outside a list still reads.
*/
export function Step({title, children}: StepProps): ReactNode {
return (
<>
<div className="font-semibold">{title}</div>
{children}
</>
);
}

export type StepsProps = {
/** The steps, in reading order: <Step> elements, anything else is skipped. */
children?: ReactNode;
/** Classes added to the list. */
className?: string;
};

export default function Steps({children, className}: StepsProps): ReactNode {
// The <Step> children, in order; titledChildren skips the text and whitespace MDX leaves
// between them, and what no number could be given to.
const steps = titledChildren(children) as ReactElement<StepProps>[];
if (steps.length === 0) {
return null;
}
return (
<ol className={clsx('ccf-steps my-6 list-none p-0', className)}>
{steps.map((step, index) => (
<li key={`${step.props.title}:${index}`} className="ccf-step relative flex gap-4 pb-6">
<span
aria-hidden="true"
className={clsx(
'ccf-step-number relative z-[1] flex h-7 w-7 shrink-0 items-center justify-center',
'rounded-full border-2 border-[var(--ifm-color-primary)]',
'bg-[var(--ifm-background-surface-color)] font-semibold',
)}
>
{index + 1}
</span>
<div className="ccf-step-body min-w-0 flex-1">
<div className="font-semibold leading-7">{step.props.title}</div>
{step.props.children}
</div>
</li>
))}
</ol>
);
}
Loading
Loading