API Reference
January 14, 2026 · View on GitHub
Components
<Infographic />
Main component for rendering infographics.
Props
| Prop | Type | Required | Default | Description |
|---|---|---|---|---|
children | string | Conditional* | - | (Recommended) The DSL string as component children. Takes precedence over dsl prop when provided. Must be a valid infographic DSL string. |
dsl | `string \ | DSLObject` | Conditional* | - |
overrides | DSLOverride[] | No | [] | Array of path-value pairs to override DSL values. Only applies when dsl is an object. |
theme | string | No | - | Theme name. Falls back to dsl.theme if not provided. |
palette | Palette | No | - | Color palette. Falls back to dsl.palette if not provided (merged into themeConfig.palette). |
width | `number \ | string` | No | '100%' |
height | `number \ | string` | No | 'auto' |
className | string | No | - | Additional CSS class name. |
editable | boolean | No | false | Enable interactive editing. |
beforeRender | PreRenderHook | No | - | Hook called before rendering to modify DSL. Only applies when dsl is an object. |
afterRender | PostRenderHook | No | - | Hook called after rendering completes. |
onRender | (result: InfographicRenderResult) => void | No | - | Callback when rendering completes. |
onError | (error: InfographicError) => void | No | - | Callback when an error occurs. |
onLoad | (result: InfographicRenderResult) => void | No | - | Callback when all resources are loaded. |
*Either
childrenordslprop must be provided. When both are provided,childrentakes precedence. Usingchildrenwith a template literal is the recommended approach for most use cases.
Ref
The component exposes a ref with the following methods:
toDataURL(options?: ExportOptions): Promise<string>- Export infographic as data URLgetTypes(): string[] | undefined- Get element types in the infographicupdate(options: DSLInput): Promise<void>- Update infographic with new DSLdestroy(): void- Cleanup resources
Example
import { useRef } from 'react';
import { Infographic } from 'infographic-for-react';
function App() {
const ref = useRef(null);
const handleExport = async () => {
const dataURL = await ref.current?.toDataURL({ type: 'svg' });
// ...
};
return (
<Infographic
ref={ref}
dsl={{
data: {
title: 'My Infographic',
items: [
{ label: 'Item 1', value: 100 },
],
},
}}
width={800}
height={600}
onRender={(result) => console.log('Rendered:', result)}
onError={(error) => console.error('Error:', error)}
/>
);
}
<ErrorBoundary />
Error boundary component for catching React errors.
Props
| Prop | Type | Required | Default | Description |
|---|---|---|---|---|
children | string | Yes | - | Child components to wrap. |
fallback | string | ((error: Error, errorInfo: ErrorInfo) => ReactNode) | No | - | Custom fallback UI. |
onError | (error: Error, errorInfo: ErrorInfo) => void | No | - | Callback when an error is caught. |
Example
import { ErrorBoundary, Infographic } from 'infographic-for-react';
function App() {
return (
<ErrorBoundary onError={(error) => console.error(error)}>
<Infographic dsl={dsl} />
</ErrorBoundary>
);
}
Hooks
useInfographic
Hook for managing infographic rendering with full control over the renderer.
Parameters
containerRef: React.RefObject<HTMLElement>- Reference to the container elementprops: InfographicProps- Props for configuring the infographic
Returns
An object with methods:
toDataURL(options?: ExportOptions): Promise<string>getTypes(): string[] | undefinedupdate(options: Partial<InfographicOptions>): voiddestroy(): void
Example
import { useRef, useCallback } from 'react';
import { useInfographic } from 'infographic-for-react';
function App() {
const containerRef = useRef<HTMLDivElement>(null);
const infographic = useInfographic(containerRef, {
dsl: {
data: {
title: 'My Infographic',
items: [
{ label: 'Item 1', value: 100 },
],
},
},
onRender: (result) => console.log('Rendered'),
});
const handleExport = useCallback(async () => {
const dataURL = await infographic.toDataURL({ type: 'svg' });
// ...
}, [infographic]);
return (
<div>
<button onClick={handleExport}>Export</button>
<div ref={containerRef} style={{ width: 600, height: 400 }} />
</div>
);
}
useRenderer
Lower-level hook for direct renderer management.
Parameters
containerRef: React.RefObject<HTMLElement>- Reference to the container element
Returns
An object with methods:
render(options?: Partial<InfographicOptions>): voidupdate(options: Partial<InfographicOptions>): voidtoDataURL(options?: ExportOptions): Promise<string>getTypes(): string[] | undefineddestroy(): voidon(event: string, listener: Function): voidoff(event: string, listener: Function): voidisReady: boolean
Utility Functions
applyOverrides(dsl: DSLObject, overrides: DSLOverride[]): DSLObject
Apply path-value overrides to a DSL object.
Example
import { applyOverrides } from 'infographic-for-react';
const dsl = {
data: {
title: { text: 'Old' }
}
};
const overrides = [
{ path: 'data.title.text', value: 'New Title' }
];
const updated = applyOverrides(dsl, overrides);
// => { data: { title: { text: 'New Title' } } }
mergeDSL(dsl1: DSLObject, dsl2: DSLObject): DSLObject
Deep merge two DSL objects.
Example
import { mergeDSL } from 'infographic-for-react';
const dsl1 = { a: 1, b: { x: 10 } };
const dsl2 = { b: { y: 20 }, c: 3 };
const merged = mergeDSL(dsl1, dsl2);
// => { a: 1, b: { x: 10, y: 20 }, c: 3 }
composeTemplates(options: ComposeTemplateOptions): DSLObject
Compose multiple templates into a single DSL object.
Parameters
templates: DSLObject[]- Array of DSL objects to composeoverrides?: DSLOverride[]- Optional overrides to apply
Example
import { composeTemplates } from 'infographic-for-react';
const headerDSL = { /* header config */ };
const bodyDSL = { /* body config */ };
const footerDSL = { /* footer config */ };
const composed = composeTemplates({
templates: [headerDSL, bodyDSL, footerDSL],
overrides: [{ path: 'some.value', value: 123 }]
});
Types
DSLInput
The DSL input type, which can be either a string or an object.
type DSLInput = string | DSLObject;
- String: Native infographic DSL syntax. More concise, suitable for template-based configurations. When using string DSL,
overridesandbeforeRenderare ignored. - Children (Recommended): String DSL passed via component's
childrenprop. Provides an HTML-like template syntax that is more intuitive and preserves formatting. Use a template literal to wrap the DSL content to preserve newlines and indentation. When using children DSL,overridesandbeforeRenderare ignored, anddslprop is ignored (children takes precedence). - Object: Structured DSL object with full TypeScript type support. Supports
overridesandbeforeRenderhooks for dynamic modifications.
DSLOverride
interface DSLOverride {
path: string;
value: unknown;
}
InfographicError
interface InfographicError {
type: 'syntax' | 'render' | 'runtime';
message: string;
dsl?: string;
details?: string | Error | unknown;
}
InfographicRenderResult
interface InfographicRenderResult {
node: SVGSVGElement;
options: InfographicOptions;
}
PreRenderHook
type PreRenderHook = (dsl: DSLObject) => DSLObject | Promise<DSLObject>;
PostRenderHook
type PostRenderHook = (result: InfographicRenderResult) => void | Promise<void>;