Skip to content
Merged
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
7 changes: 5 additions & 2 deletions portal-template/src/components/example-layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ interface LayoutSlots {

const SelectionContext = createContext<ExampleSelection | undefined>(undefined);

/** Fumadocs keeps the example column no taller than the window: each box down to the code passes that height on. */
export const SHRINKS_WITH_COLUMN = 'flex min-h-0 flex-col';

// "Example Response" copies through a button of Fumadocs' own in the card's header, which takes no callback.
const reportResponseCopy = (event: MouseEvent) => {
if (event.target instanceof Element && event.target.closest('button[aria-live]') !== null) reportCodeCopy('json');
Expand Down Expand Up @@ -66,7 +69,7 @@ function ExampleLayout({ selector, usageTabs, responseTabs }: Readonly<LayoutSlo
);
return (
<SelectionContext.Provider value={selection}>
<div className="prose-no-margin">
<div className={`${SHRINKS_WITH_COLUMN} prose-no-margin`}>
{selector === null ? null : <ExampleSelector />}
{usageTabs}
<div className="contents" onClickCapture={reportResponseCopy}>
Expand All @@ -92,7 +95,7 @@ function ExampleSelector() {
}));
return (
<Select items={items} value={selected.id} onValueChange={(id) => id !== null && select(id)}>
<SelectTrigger className="not-prose mb-2">
<SelectTrigger className="not-prose">
<SelectValue />
</SelectTrigger>
<SelectContent>
Expand Down
17 changes: 10 additions & 7 deletions portal-template/src/components/usage-tabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { createCodeUsageGeneratorRegistry } from 'fumadocs-openapi/requests/gene
import { curl } from 'fumadocs-openapi/requests/generators/curl';
import { CodeSample } from '@/lib/code-samples';
import { REPLAY_MARKERS } from '@/lib/replay-markers';
import { useExampleSelection } from './example-layout';
import { SHRINKS_WITH_COLUMN, useExampleSelection } from './example-layout';

interface UsageTab {
id: string;
Expand All @@ -21,6 +21,9 @@ const CURL_USAGE_ID = 'portal-curl';
export const codeUsages = createCodeUsageGeneratorRegistry();
codeUsages.add(CURL_USAGE_ID, curl);

// A sample that shrinks with its tab, and scrolls inside it.
const SHRINKING_BLOCK = { className: SHRINKS_WITH_COLUMN, viewportProps: { className: 'min-h-0' } };

// The registry Fumadocs passes in adds the operation's `x-codeSamples`, which cannot follow the example selector.
export function renderUsageTabs(): ReactNode {
return <UsageTabs />;
Expand All @@ -37,16 +40,16 @@ function UsageTabs() {
];

return (
<CodeBlockTabs groupId="fumadocs_openapi_requests" defaultValue={tabs[0].id}>
<CodeBlockTabsList>
<CodeBlockTabs groupId="fumadocs_openapi_requests" defaultValue={tabs[0].id} className={SHRINKS_WITH_COLUMN}>
<CodeBlockTabsList className="shrink-0">
{tabs.map((tab) => (
<CodeBlockTabsTrigger key={tab.id} value={tab.id}>
{tab.label}
</CodeBlockTabsTrigger>
))}
</CodeBlockTabsList>
{tabs.map((tab) => (
<CodeBlockTab key={tab.id} value={tab.id}>
<CodeBlockTab key={tab.id} value={tab.id} className={SHRINKS_WITH_COLUMN}>
{tab.body}
</CodeBlockTab>
))}
Expand All @@ -62,16 +65,16 @@ function SampleCode({ sample }: Readonly<{ sample: CodeSample }>) {
if (source === undefined) {
return <p className="px-4 py-3 text-sm text-fd-muted-foreground">No {sample.label} sample for this example.</p>;
}
return <CodeBlock lang={sample.lang} code={source} />;
return <CodeBlock lang={sample.lang} code={source} codeblock={SHRINKING_BLOCK} />;
}

// Follows the playground's edits, typed credentials included, so it carries the playground's replay markers.
function CurlCode() {
const { CodeBlock } = useComponents();
const code = useCodeUsage(CURL_USAGE_ID);
return code === undefined ? null : (
<div {...REPLAY_MARKERS}>
<CodeBlock lang={curl.lang} code={code} />
<div {...REPLAY_MARKERS} className={`${REPLAY_MARKERS.className} ${SHRINKS_WITH_COLUMN}`}>
<CodeBlock lang={curl.lang} code={code} codeblock={SHRINKING_BLOCK} />
</div>
);
}
12 changes: 12 additions & 0 deletions test/e2e/portal-build.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,18 @@ const stylesheetOf = (output: DirectoryPath) => {
);
});

it('lets both example cards shrink with their column, which Fumadocs keeps no taller than the window', () => {
const shrinks = '[^>]*[" ]flex min-h-0 flex-col[" ][^>]*>';
// Each box passes the height on: our layout, then the request card's tabs, open tab, marked element and block.
const boxes = new RegExp(
`@4xl:max-h-[^>]*><div${shrinks}[\\s\\S]*?<div${shrinks}<div(?=[^>]*role="tablist")[^>]*[" ]shrink-0[" ]` +
`[\\s\\S]*?<div${shrinks}<div${shrinks}(?:<!--.*?-->)*<figure${shrinks}` +
// The code scrolls in what the block has left.
`(?:(?!</figure>)[\\s\\S])*?<div[^>]*[" ]overflow-auto[" ][^>]*[" ]min-h-0[" ]`
);
expect(read('api/apimatic-calculator/simple-calculator/Calculate/index.html')).to.match(boxes);
});

it('keeps the server-side specification loader out of the browser bundle', () => {
const scripts = fs
.readdirSync(path.join(portal.output().toString(), 'assets'))
Expand Down
Loading