Warning
This is an internal project, and is not intended for public use. No support or stability guarantees are provided.
Look up individual type data by name from the nearest TypesDataProvider.
The useType hook is the consumer-side complement to useTypes. While useTypes registers types into a shared context, useType retrieves a single type by name. Together with TypesDataProvider, they form a context-based system for interactive type exploration.
Typical data flow:
TypesDataProvider (wraps the page layout)
↓
useTypes() — registers types and their properties during render
↓
useType("Root") — looks up a single type by name
useTypeProp("Root", "className") — looks up a single property by type and name
↓
TypeRef / TypePropRef component — renders interactive popover
useTypeuseTypePropTypesDataProvider ancestor in the treeundefined when no provider exists or the name is unregistered'use client' (uses React context)'use client';
import { useType } from '@mui/internal-docs-infra/useType';
function MyTypeViewer({ typeName }: { typeName: string }) {
const typeData = useType(typeName);
if (!typeData) {
return <span>Type not found: {typeName}</span>;
}
return (
<div>
<h3>{typeData.meta.name}</h3>
<a href={typeData.href}>Go to documentation</a>
</div>
);
}
The provider must wrap your component tree so that useTypes can register types and useType can read them:
// layout.tsx
import { TypesDataProvider } from '@mui/internal-docs-infra/useType';
export default function Layout({ children }: { children: React.ReactNode }) {
return <TypesDataProvider>{children}</TypesDataProvider>;
}
A common use case is building a TypeRef component that renders an interactive popover when a user clicks on a type name in a code block. The typeRefComponent option in abstractCreateTypes causes the enhanceCodeTypes plugin to emit custom elements instead of plain <a> tags, which are then resolved to your TypeRef component.
'use client';
import { Popover } from '@base-ui/react/popover';
import { useType } from '@mui/internal-docs-infra/useType';
function TypeRef({ href, name, className, children }) {
const typeData = useType(name);
// Fall back to a plain link when the type hasn't been registered
if (!typeData) {
return (
<a href={href} className={className}>
{children}
</a>
);
}
return (
<Popover.Root>
<Popover.Trigger className={className}>{children}</Popover.Trigger>
<Popover.Portal>
<Popover.Positioner sideOffset={8}>
<Popover.Popup>
{/* Render your type documentation here using typeData.meta */}
</Popover.Popup>
</Popover.Positioner>
</Popover.Portal>
</Popover.Root>
);
}
Important: The popover content must not be rendered into the DOM while closed. Type documentation often contains code blocks with nested
TypeRefelements — if each one eagerly renders its own popover content, it creates an infinite render loop. Use a portal (likePopover.Portalabove) or conditionally render the content only when the popover is open.
See Enhance Code Types — typeRefComponent and Abstract Create Types — typeRefComponent for how to wire this up.
The useTypeProp hook looks up a single property from a registered type. This is useful for building TypePropRef components that show property details in a popover:
'use client';
import { useTypeProp } from '@mui/internal-docs-infra/useType';
function TypePropRef({ typeName, propName, href, className, children }) {
const propData = useTypeProp(typeName, propName);
if (!propData) {
return (
<a href={href} className={className}>
{children}
</a>
);
}
return (
<a href={propData.href} className={className}>
{children}
</a>
);
}
The useTypeProp hook returns TypePropData containing the EnhancedProperty and its href anchor (e.g., #root:class-name). Properties are automatically registered by useTypes for:
data.propsdata.parameters and data.optionsPropertiesA client component that collects type data from all useTypes() calls within its subtree.
TypesDataProvider holds a Map<string, TypeData> for types and a Map<string, TypePropData> for propertiesuseTypes() call registers its types via registerTypes and their properties via registerTypePropsAccordion.Root.Props) and any aliases (e.g., the flat export name AccordionRootProps)"Root:className" — the type name and property name joined by :useType(name) reads from the types map; useTypeProp(typeName, propName) reads from the properties mapBoth registerTypes and registerTypeProps perform shallow comparison of their respective entries. If all incoming entries match the existing map, no state update occurs. This prevents re-render cycles when useTypes effects re-fire with the same data.
useTypeHook to look up a single type’s data by name from the nearest TypesDataProvider.
Returns the TypeData for the given name, or undefined if:
TypesDataProvider is present in the tree| Parameter | Type | Description |
|---|---|---|
| name | | The type name to look up (e.g., “Root”, “Trigger”, “AccordionTrigger”) |
TypeData | undefineduseTypePropHook to look up a single type property’s data from the nearest TypesDataProvider.
Returns the TypePropData for the given type and property name, or undefined if:
TypesDataProvider is present in the tree| Parameter | Type | Description |
|---|---|---|
| typeName | | The type name (e.g., “Root”, “Trigger”) |
| propName | | The property name (e.g., “className”, “defaultOpen”) |
TypePropData | undefinedTypesDataProviderProvider that collects type data from all useTypes() calls within its subtree.
This enables useType(name) to look up individual type data anywhere in the tree,
and useTypeProp(typeName, propName) to look up individual properties.
| Prop | Type | Description |
|---|---|---|
| children | |
useTypesDataContextReturns the TypesDataContext value, or undefined if not within a provider.
Used internally by useTypes to register types and by useType to look up types.
TypesDataContextValue | undefinedData for a single type, including the processed type metadata and its anchor href.
type TypeData = {
/** The processed type metadata (component, hook, function, class, or raw) */
meta: EnhancedTypesMeta;
/** The anchor href for navigating to this type's documentation */
href: string;
}Data for a single type property, including the processed property and its anchor href.
type TypePropData = {
/** The processed property metadata */
property: EnhancedProperty;
/** The anchor href for navigating to this property's documentation */
href: string;
}Props passed to the custom typePropRefComponent element.
type TypePropRefProps = {
/** The anchor id (when this is the definition site) */
id?: string;
/** The anchor href (when this is a reference to the definition) */
href?: string;
/** The owner type name (e.g., "Root", "Trigger") */
name: string;
/** The property path (e.g., "className", "open") */
prop: string;
/** Optional CSS class name(s) inherited from syntax highlighting */
className?: string;
/** The rendered text content */
children: React.ReactNode;
}Props passed to the custom typeRefComponent element.
type TypeRefProps = {
/** The anchor href for the type documentation */
href: string;
/** The matched identifier name (e.g., "Trigger", "Accordion.Trigger") */
name: string;
/** Optional CSS class name(s) inherited from the syntax highlighting span */
className?: string;
/** The rendered text content */
children: React.ReactNode;
}useTypes: Hook that registers types into the contextabstractCreateTypes: Factory that creates type documentation componentsenhanceCodeTypes: Rehype plugin that links code identifiers