Warning
This is an internal project, and is not intended for public use. No support or stability guarantees are provided.
The abstractCreateDemo function helps you create structured demo factories that work seamlessly with the CodeHighlighter component ecosystem. It provides a standardized way to create demos that combine component previews with code display, making it easier to build consistent documentation interfaces.
Tip
For the underlying architectural rationale (URLs as identity, variant enumeration, precompute, server/client split) see the Built Factories Pattern.
Demo factories created with abstractCreateDemo automatically integrate with:
useDemo: For component + code demosuseCode: For code-only interfacesloadPrecomputedCodeHighlighter: For optimized loadingSet DemoContentFrame alongside DemoContentLoading when loading and interactive
content share a stateful preview. The frame receives the initial loading props and
children, which changes from loading chrome to interactive chrome. Keep the preview
in the frame; in the loading and content components, call useContentFrame() and skip
the preview when framed is true.
The frame must be a Client Component, like DemoContentLoading. It is used only when
DemoContentLoading prepares an initial loading fallback; without one, including
skipFallback, content renders directly and the frame is ignored.
'use client';
function DemoFrame({ component, children }: ContentFrameProps<{}>) {
return (
<section>
{component}
{children}
</section>
);
}
function DemoContentLoading(props: ContentLoadingProps<{}>) {
const fallback = useCodeFallback(props);
const { framed } = useContentFrame();
return (
<div>
{!framed && props.component}
{fallback.code}
</div>
);
}
const createDemo = createDemoFactory({
DemoContent,
DemoContentLoading,
DemoContentFrame: DemoFrame,
});
Import useCodeFallback and useContentFrame from @mui/internal-docs-infra/CodeHighlighter,
and ContentFrameProps and ContentLoadingProps from
@mui/internal-docs-infra/CodeHighlighter/types. DemoContent follows the same pattern.
framed applies only to the CodeHighlighter that owns the frame; a nested
CodeHighlighter without one reports false.
The frame stays under the code and demo controller contexts. Its component identity remains
stable while source data and lazy content load on the client. component follows the
selected variant and its live preview; the other props keep their initial values. Use a
consumer-owned context to publish interactive toolbar and reset state to the frame.
With highlightAfter: 'stream', the frame first renders inside the streamed
placeholder and mounts again when the server finishes loading the source.
At the fundamental level, the loadPrecomputedCodeHighlighter function expects factory functions with these signatures:
// Single component demo
export const createDemo = (url: string, component: React.ComponentType, options?: any) => {
return () => <>Demo Content</>;
}
// Multi-variant demo
export const createDemoWithVariants = (url: string, variants: Record<string, React.ComponentType>, options?: any) => {
return () => <>Demo Content</>;
}
To quickly implement these factory functions, use the abstractCreateDemo utilities:
import 'server-only'; // this should be omitted if using the <CodeProvider> or the pages router
import {
createDemoFactory,
createDemoWithVariantsFactory,
} from '@mui/internal-docs-infra/abstractCreateDemo';
import { DemoContent } from './DemoContent';
import { DemoDataTheme } from '../demo-data/theme';
const demoGlobalData = [DemoDataTheme];
/**
* Creates a demo component for displaying code examples with syntax highlighting.
* @param url Depends on `import.meta.url` to determine the source file location.
* @param component The component to be rendered in the demo.
* @param meta Additional meta for the demo.
*/
export const createDemo = createDemoFactory({
DemoContent,
demoGlobalData,
});
/**
* Creates a demo component for displaying code examples with syntax highlighting.
* A variant is a different implementation style of the same component.
* @param url Depends on `import.meta.url` to determine the source file location.
* @param variants The variants of the component to be rendered in the demo.
* @param meta Additional meta for the demo.
*/
export const createDemoWithVariants = createDemoWithVariantsFactory({
DemoContent,
demoGlobalData,
});
Global dependencies allow you to inject shared code (like providers, themes, or utilities) into all demos. These are passed through the demoGlobalData option:
// docs/src/demo-data/theme/index.ts
import { createDemoGlobalWithVariants } from '@mui/internal-docs-infra/createDemoData';
import type { DemoGlobalData } from '@mui/internal-docs-infra/createDemoData/types';
import { DemoThemeProvider as CssModules } from './css-modules';
export const DemoDataTheme: DemoGlobalData = createDemoGlobalWithVariants(import.meta.url, {
CssModules,
});
// docs/src/demo-data/theme/css-modules/index.ts
import './theme.css';
export function DemoThemeProvider({ children }: { children: React.ReactNode }) {
return children;
}
// docs/src/utils/createDemo.ts
import 'server-only'; // this should be omitted if using the <CodeProvider> or the pages router
import { createDemoFactory } from '@mui/internal-docs-infra/abstractCreateDemo';
import { Demo as DemoContent } from '../components/Demo/Demo';
import { DemoDataTheme } from '../demo-data/theme';
const demoGlobalData = [DemoDataTheme];
export const createDemo = createDemoFactory({
DemoContent,
demoGlobalData, // Available in all demos
});
The globals are automatically merged with demo code and displayed alongside existing demo files.
To make global demo data work properly, use withDocsInfra which automatically includes the demo-data patterns:
// next.config.js
import { withDocsInfra } from '@mui/internal-docs-infra/withDocsInfra';
export default withDocsInfra({
// withDocsInfra automatically includes:
// - './app/**/demos/*/index.ts'
// - './app/**/demos/*/client.ts'
// - './src/demo-data/*/index.ts' (for globals)
// - './src/demo-data/*/client.ts' (for client-side globals)
});
If you need additional patterns, you can add them via additionalDemoPatterns:
export default withDocsInfra({
additionalDemoPatterns: {
index: ['./src/**/snippets/*/index.ts'],
client: ['./src/**/snippets/*/client.ts'],
},
});
| Parameter | Type | Description |
|---|---|---|
| options | | |
| url | | |
| variants | | |
| meta | |
React.ComponentClass<{ collapseToEmpty?: boolean; showCollapsedFocus?: boolean; initialExpanded?: boolean; initialCollapsed?: boolean }, any> & { Title: React.ComponentType } | ((props: { collapseToEmpty?: boolean; showCollapsedFocus?: boolean; initialExpanded?: boolean; initialCollapsed?: boolean }) => React.ReactNode | Promise<React.ReactNode>)useDemo: Hook for managing component + code demosuseCode: Hook for code-only interfacesCodeHighlighter: Main component for code highlightingloadPrecomputedCodeHighlighter: Loader for optimized demos