WordCloud | svelte-wordcloud Skip to content

WordCloud

WordCloud is the root wrapper that provides shared state to all child components. It must be the ancestor of WordCloud3D, WordCloudFlat, and all UI components.

PropTypeDefaultDescription
dataWordItem[][]Array of words to display.
scrollbarOrientation'vertical' | 'horizontal''vertical'Orientation of the built-in depth/zoom scrollbar shared by all child display components.
type WordItem<T extends Record<string, unknown> = {}> = {
word: string;
counts: number;
color?: string; // Per-word override. Falls back to WordCloud's color prop.
} & T;

Extra fields beyond word, counts, and color can be added via the type parameter and are available in the onWordClick callback:

// Define words with a custom `link` field
const words: WordItem<{ link?: string }>[] = [
{ word: 'Svelte', counts: 120, link: 'https://svelte.dev' },
{ word: 'WebGL', counts: 60 },
];
// Access the typed field in the callback
function handleWordClick(word: WordItem<{ link?: string }>) {
if (word.link) window.open(word.link, '_blank');
}

Visual properties can be overridden with CSS custom properties. Set them directly on the <WordCloud> component:

<WordCloud --wc-canvas-gap="8px" --wc-indicator-font-size="12px" ...>
VariableDefaultDescription
--wc-colorcurrentColorPrimary color for words and UI elements (scrollbar, indicator, audio button). Inherits currentColor when unset. Individual words can still override with WordItem.color.
--wc-backgroundtransparentBackground of the canvas + scrollbar area.
--wc-aspect-ratioautoAspect ratio of the canvas. Example: 16 / 9.
--wc-canvas-gap0pxGap between the canvas and the scrollbar column.
--wc-scrollbar-padding8pxPadding of the scrollbar column (inline in horizontal mode, block in vertical mode).
--wc-scrollbar-gap4pxGap between items inside the scrollbar column.
--wc-indicator-font-size10pxFont size of the depth / zoom indicator text.
--wc-thumb-size32pxWidth and height of the scrollbar thumb hit area.
--wc-dot-size7pxDiameter of the visible scrollbar dot.
--wc-dot-colorwc-color @ 70%Color of the scrollbar track dot.
--wc-track-opacity0.3Opacity of the scrollbar track line at rest. Set to 0 to hide until hover.
--wc-thumb-colorwc-dot-colorFill color of the scrollbar thumb dot.
--wc-thumb-color-hoverwc-color @ 15% + #000Fill color of the thumb on hover / focus.
--wc-thumb-ring-color-hoverwc-colorRing color of the thumb on hover / focus.