import "capsomer/tokens.css";
import "capsomer/chart-builder.css";
import { ChartBuilder } from "capsomer/react/chart-builder";
Usage
import { ChartBuilder } from "capsomer/react/chart-builder";
<ChartBuilder
onInsert={(block) => editor.insertAtCursor(block)}
// The family sites' check: Prelum's editorialPack with strictCharts, loaded when needed.
check={async (block, drawable) => {
const m = await import("./chart-check.client");
return { findings: m.checkCharts(block), suggestion: m.suggestFinding(drawable) };
}}
preview={{ action: "/p/site/e/item/preview", source: (block) => withFrontmatter(block) }}
/>;
Examples
With the app's check, drafted finding and a chart drawn here
<p class="cap-muted">Open it, paste <code>month,visits</code> and two rows, and the columns, the check and the chart follow. The inserted block shows below.</p>
<div data-mount="full"></div>
API reference
ChartBuilder
Prop
Type
Required
Does
onInsert
(block: string) => void
Yes
Puts the finished block in the text, at the cursor.
types
readonly ChartType[]
The chart types offered, first one chosen. Default: Prelum's bar, line, area, dot.
write
(fields: ChartFields) => string
The block for the fields, in the site's syntax. Default: Prelum's :::chart directive.
The app's chart check (Prelum's under strictCharts on the family sites), run on the block as written and, for the suggestion, on one with a stand-in finding so it can be drawn.
The columns chosen after the data changed: a choice that is still a column is kept; otherwise x is the first column and y every numeric column after it.
chartReady
(f: Pick<ChartFields, "x" | "y" | "csv">): boolean
Whether the fields make a chart: two columns or more, an x, and at least one y that is not x.
chartDirective
(f: ChartFields): string
Prelum's chart block. An empty title or caption is left out; the alt is always written, so a blank one is flagged by Prelum's required-alt rule rather than silently missing.
Parts and data attributes
Class
Is
.cap-chart-builder
the root
.cap-chart-builder-check
a part
.cap-chart-builder-csv
a part
.cap-chart-builder-findings
a part
.cap-chart-builder-note
a part
.cap-chart-builder-preview
a part
.cap-chart-builder-suggestion
a part
.cap-chart-builder-y
a part
State
Read from
:empty
the platform
Accessibility
Key
Does
Enter or Space (Insert chart)
Opens the dialog, focus inside it
Tab, Shift+Tab
Moves through the fields, kept inside the dialog
Left arrow, Right arrow (chart type)
Chooses the type
Space (a y column)
Adds or removes it
Enter (Use as the finding)
Puts the drafted finding in the Finding box
Enter (Insert chart)
Inserts the block, closes, focus back on the opening button
Esc
Closes without inserting, focus back on the opening button
The dialog is the shared Dialog: modal, named by its title, described by its lead, focus kept inside and returned to the button.
Every field is a Field: label above, help as its description, Required marked. The y columns are a fieldset with a legend.
The check's result is a status region, so a finding arriving is said once. The drafted finding is a named group.
The site preview frame is titled ("The chart as the site renders it"); the drawn chart is the chart frame's, with its own title, finding and table.
Insert's refusal is the button's disabledReason, read as its description.