Access processed type information within a TypesContent component using the Props Context Layering pattern.
The useTypes hook provides a consistent interface for accessing type metadata that has been processed during component creation. This follows the Props Context Layering pattern, allowing for future enhancements through context while maintaining the current props-based API.
Key Responsibilities:
Data Flow:
Build Time (Webpack Loader)
──────────────────────────
TypeScript Component
↓
Extract types (typescript-api-extractor)
↓
Format as HAST nodes (syntax highlighting)
↓
Embed in source code as JSON
Runtime (React - First Render)
──────────────────────────────
Import factory with embedded HAST
↓
TypesComponent renders
↓
typesToJsx converts HAST → React.ReactNode (memoized)
↓
TypesContent component renders
Key Points:
typesToJsx converts HAST to React.ReactNode via memoizationuseTypes returns the already-processed types from propsTypes are built using typescript-api-extractor and formatted with syntax highlighting at build time.
This page demonstrates how to use the useTypes hook to extract and display type information from React components.
A simple component that displays a title and optional children.
| Prop | Type | Description |
|---|---|---|
| title | string | The title to display |
| disabled | boolean | Whether the component is disabled |
| children | ReactNode | Child elements |
import { createTypes } from './createTypes';
import { Component } from './Component';
export const TypesCheckbox = createTypes(import.meta.url, Component);
This demo shows how to display data attributes defined in a component using the useTypes hook.
A simple component that displays a title and optional children.
| Prop | Type | Description |
|---|---|---|
| title | string | The title to display |
| disabled | boolean | Whether the component is disabled |
| children | ReactNode | Child elements |
| Data Attribute | Description | Default |
|---|---|---|
| data-type | The type of the component. |
import { createTypes } from './createTypes';
import { Component } from './Component';
export const TypesCheckboxDataAttr = createTypes(import.meta.url, Component);
This demo illustrates how to use the useTypes hook to extract and display type information from a custom React hook.
| Key | Type | Required |
|---|---|---|
| name | string | Yes |
| data | string | Yes |
import { createTypes } from './createTypes';
import { useHook } from './useHook';
export const TypesHook = createTypes(import.meta.url, useHook);
This demo illustrates how to use the useTypes hook to extract and display type information from a custom React hook.
A simple component that displays a title and optional children.
| Prop | Type | Description |
|---|---|---|
| title | string | The title to display |
| disabled | boolean | Whether the component is disabled |
| children | ReactNode | Child elements |
A simple component that displays a title and optional children.
| Prop | Type | Description |
|---|---|---|
| title | string | The title to display |
| disabled | boolean | Whether the component is disabled |
| children | ReactNode | Child elements |
import * as React from 'react';
import { TypesComponentPart, TypesComponentRoot } from './types';
export function TypesComponent() {
return (
<div>
<h3>ComponentRoot</h3>
<TypesComponentRoot />
<h3>ComponentPart</h3>
<TypesComponentPart />
</div>
);
}
This demo illustrates how to use the useTypes hook to extract and display type information from a custom React hook.
A simple component that displays a title and optional children.
| Prop | Type | Description |
|---|---|---|
| onStateChange | ((details: Component.Root.ChangeEventDetails) => void) | Callback fired when the state changes. Receives the event details containing previous and new states. |
| partState | Component.Part.State | Optional state from the Part component. This demonstrates cross-component type references. |
| title | string | The title to display |
| disabled | boolean | Whether the component is disabled |
| children | ReactNode | Child elements |
| Data Attribute | Description | Default |
|---|---|---|
| data-disabled | Present when the component is disabled. | |
| data-title | Present when the component has a title. |
A simple component that displays a title and optional children.
| Prop | Type | Description |
|---|---|---|
| title | string | The title to display |
| disabled | boolean | Whether the component is disabled |
| children | ReactNode | Child elements |
| Data Attribute | Description | Default |
|---|---|---|
| data-disabled | Present when the component is disabled. | |
| data-title | Present when the component has a title. |
import * as React from 'react';
import { TypesComponentPart, TypesComponentRoot } from './types';
export function TypesComponent() {
return (
<div>
<h3>ComponentRoot</h3>
<TypesComponentRoot />
<h3>ComponentPart</h3>
<TypesComponentPart />
</div>
);
}
Types are converted from HAST to React elements during component creation, not at runtime:
From the webpack loader, stored as precomputed data:
interface FormattedProperty {
type: HastRoot; // Syntax-highlighted HAST
description?: HastRoot; // Parsed markdown HAST
example?: HastRoot; // Parsed markdown HAST
default?: HastRoot; // Syntax-highlighted HAST
// ... other fields
}
During the first render, typesToJsx converts HAST to React (memoized for performance):
interface ProcessedProperty {
type: React.ReactNode; // Rendered JSX elements
description?: React.ReactNode; // Rendered markdown
example?: React.ReactNode; // Rendered examples
default?: React.ReactNode; // Rendered defaults
// ... other fields
}
The conversion happens on first render within TypesComponent:
// In your types.ts file
export const TypesButton = createTypes(import.meta.url, Button);
// Inside abstractCreateTypes (simplified):
function TypesComponent(props) {
// Memoize the conversion from HAST to JSX (runs once on first render)
const types = React.useMemo(() => typesToJsx(rawTypes, { components }), []);
return <TypesContent {...props} types={types} />;
}
// In your TypesContent component:
function TypesContent(props) {
const { types } = useTypes(props); // Already processed!
return <div>{types[0].data.props.variant.type}</div>; // React.ReactNode
}
Hook for accessing types props in TypesContent components.
| Parameter | Type | Description |
|---|---|---|
| contentProps | TypesContentProps<{}> |
TypesContentProps<{}>abstractCreateTypes: Factory functions where type processing happenstypesToJsx: Function that converts HAST to React nodesloadPrecomputedTypesMeta: Build-time loader that extracts types as HAST