Styling
Quick start
chartStyleRules() returns the CSS a chart needs for its layout, its states and its default colors.
flowDiagramStyleRules() returns the same for a diagram. Add both to the page once:
import { chartStyleRules, flowDiagramStyleRules } from '@tiptap-pro/extension-diagrams'
const style = document.createElement('style')
style.textContent = chartStyleRules() + flowDiagramStyleRules()
document.head.append(style)Custom properties
Every CSS rule reads a custom property that has a default value, listed on
Classes, data attributes and custom properties. Set the properties on .tiptap, or
on one .tiptap-chart, instead of overriding the rules.
For dark colors of your own, set properties again under a dark selector. A class change needs
refresh():
.tiptap {
--tt-chart-series-1: #6a00f5;
--tt-chart-selected-outline: 2px solid #6a00f5;
}
.dark .tiptap {
--tt-chart-background: #1d1d23;
--tt-chart-label-color: #c4c4ce;
--tt-chart-grid-color: #2c2c35;
}Changing colors from script
A chart reads its colors and fonts when it is drawn. After you change them from script, call
editor.storage.diagrams.refresh(), which draws every chart and diagram on screen again. An
exported file uses the values that apply when it is exported.
Styles
A style is a named set of colors and fonts that a chart's Chart Spec, its settings, can name:
editor.commands.updateChartSpec({ spec: { style: 'ocean' } })Give a chart a built-in style, or change a style and see every chart that names it change:
The document stores only the style's id. Styles apply to charts only, and a diagram's look comes from its source and the custom properties.
What a style holds
A style is { id, label, palette, appearance? }. The chart stores the id, and the user sees the
label. The palette holds hex colors for the data series, such as the bars. The optional
appearance may set the colors background, axisColor, gridColor, labelColor and
titleColor, fontFamily, and border, a boolean for the frame.
Which value wins
The chart's own palette, background and border win over the style. What neither sets comes
from the custom properties. An id that is not registered is ignored, and the chart
looks as if it named no style.
Built-in styles
The package exports the built-in styles as CHART_STYLES: standard, ocean, sunset, mono,
pastel, high-contrast, corporate, print, neon and earth. Each label is its id in title
case, such as High Contrast. print is on white with a frame, and neon is on a dark background.
Your own styles
The styles option adds styles, and one with the id of a built-in style
replaces it:
Diagrams.configure({ styles: [{ id: 'brand', label: 'Brand', palette: ['#0b5fff', '#ff6b00'] }] })These members read the styles, with defaultChartTheme from /core as a theme:
| Where | What |
|---|---|
editor.storage.diagrams.styles | Every style |
describeCapabilities().styles | Every style as { id, label, palette }, for an agent |
editor.storage.diagrams.styleChart(spec, theme, scheme?) | The { spec, theme } a chart is drawn with, for a preview of your own |
Dark color scheme
The default colors are light-dark() pairs, declared on :where(.tiptap) so any value you set
wins. A chart or a diagram uses the color scheme it inherits from the page: color-scheme: dark on
the page turns the defaults dark, and color-scheme: light dark follows the system.
Switch the page between light and dark and watch charts and diagrams follow:
How a dark color is derived
Each dark default is its light color with the lightness turned around and the hue kept.
lightDarkColor gives your own color the same dark variant:
import { lightDarkColor } from '@tiptap-pro/extension-diagrams'
editor.view.dom.style.setProperty('--tt-chart-series-1', lightDarkColor('#0b5fff'))
editor.storage.diagrams.refresh()When the scheme changes
With the colorScheme option at its default, 'auto', charts and
diagrams are drawn again when the system's scheme changes. When a class on <html> changes it, call
editor.storage.diagrams.refresh(). colorScheme: 'light' keeps them light.
Exported files and Word documents are light. For a dark file, pass scheme: 'dark' to
export. That needs the editor in a page: without one, such as on a server, the file
stays light.
Styles in the dark
The colors of a style turn dark by the same rule, except in a style with a dark
background of its own, such as neon. A palette or a background that the Chart Spec sets itself
stays the same in both schemes.
styleChart(spec, theme, scheme) takes 'light' (the default), 'dark' or 'auto'. 'auto'
returns light-dark() pairs, which the page then resolves.
Browser support
light-dark() and relative colors need Chrome and Edge 123, Safari 18 or Firefox 128.
Color helpers
Code that draws outside the page's CSS, such as your own Renderer or a canvas, needs hex values, not
light-dark(). A Renderer gets its colors resolved that way. These functions resolve your own:
| Function | From | Returns |
|---|---|---|
darkColor(color) | @tiptap-pro/extension-diagrams | The dark rule as CSS, oklch(from <color> calc(0.95 - 0.75 * l) c h) |
lightDarkColor(color) | @tiptap-pro/extension-diagrams | The light-dark() pair, for colors of your own |
resolveColors(element, colors, scheme?) | @tiptap-pro/extension-diagrams/theme | Colors of your own, resolved against the element |
resolveChartColors(element, { spec, theme }, scheme?) | @tiptap-pro/extension-diagrams/theme | What styleChart returns, resolved |