Creating Documentation Code Examples
File structure
docs/content/guides/category/feature/
feature.md # The guide page
javascript/example1.ts # Primary source; example1.js is generated
react/example1.tsx # Primary source; example1.jsx is generated
angular/example1.ts # plus example1.html (outer wrapper)
vue/example1.vue # TypeScript SFC (`<script setup lang="ts">`)
Rules
- 25-60 lines per example, one concept each. Number by complexity:
example1basic setup,example2a configuration variation,example3advanced. - Write the
.ts/.tsxfile first. Fromdocs/, generate the JS variant withnpm run docs:code-examples:generate-js -- <path-to-ts-file>(path relative todocs/); generated JS files are overwritten, so change the TS source. Vue has no JS variant. - Use
createSpreadsheetData()or domain-appropriate data (product names, dates, currencies).
Required in every example
- Base import and explicit registration (tree-shaking examples import individual plugins and cell types instead of
registerAllModules()):
```js
import Handsontable from 'handsontable/base';
import { registerAllModules } from 'handsontable/registry';
registerAllModules();
```
- The license key stays a commented-out placeholder, never a key value:
// licenseKey: 'your-license-key', // get a key at https://handsontable.com/pricing/. In JSX it is the first attribute (// licenseKey="your-license-key" // get a key at ...), because the generated.jsxloses a comment that follows the last attribute; in a Vue or HTML template it is an<!-- license-key="your-license-key" (get a key at ...) -->comment above the tag. - Container, typed in the
.tsand.tsxfiles:const container = document.querySelector<HTMLElement>('#example1')!;. An untypedquerySelectorreturnsElement | null, which fails withTS2345in a new file, and it also hides every later error in the settings object from the type check (seedocs/AGENTS.md, "Type-checking the examples"). The generated.jsfile keeps the plaindocument.querySelector('#example1').
React (TSX/JSX)
import { HotTable } from '@handsontable/react-wrapper';
import { registerAllModules } from 'handsontable/registry';
registerAllModules();
const App = () => {
return (
<HotTable
// licenseKey="your-license-key" // get a key at https://handsontable.com/pricing/
data={data}
/>
);
};
Angular
- Standalone components (
standalone: true,imports: [HotTableModule]), class namedAppComponentin every example. app.config.ts(notapp.module.ts) withApplicationConfig,provideZoneChangeDetection({ eventCoalescing: true }), and globalHOT_GLOBAL_CONFIG, which carries the commented-out placeholder// license: 'your-license-key', // get a key at https://handsontable.com/pricing/(never a key value, neverNON_COMMERCIAL_LICENSE). LeavelicenseKeyoff individual<hot-table>bindings.- Template control flow:
@if/@for (x of list; track x.id).
The docs site bootstraps Angular examples with JIT in the browser, which cannot load external files and lacks decorator metadata:
- Inline styles:
styles: ['...']. The example-runner injects CSS globally via the--cssslot, andstyleUrlsfails. - Inline template:
template: \...\`.angular/example1.htmlis the outer wrapper (selector tag) the example-runner consumes, not the component template;templateUrl` fails. - Inject services with
inject(). Constructor DI throwsNG0202. - Put Handsontable hook functions inside
gridSettings, not as template bindings ((afterInit)="handler()"). - Import only the symbols you use; unused imports (
RowObject,ViewChild,NgFor) can cause module resolution errors. - Set template-bound state (such as
hotSettingsread by@if) inngOnInit. Angular 22 components areOnPushby default, and nothing marks the component dirty afterngAfterViewInit, so a write there leaves an@ifgrid blank. If the write needs the view, callmarkForCheck()on an injectedChangeDetectorRefafter it, ordetectChanges()when the view must render synchronously. - Mark the component dirty after any write to template-bound state that happens after an
await, in asetTimeout, or in a Handsontable callback such as a hook or a shortcut action (status text, a disabled button). Angular 22 components areOnPushby default, andNgZone.rundoes not mark them dirty. CallmarkForCheck()on an injectedChangeDetectorRef: outside the zone it schedules its own tick, so a Handsontable callback needs nothing more. KeepdetectChanges()for a view that must render synchronously. Verify by loading the page and waiting, with no interaction.
The .ts file holds app.component.ts and app.config.ts as separate /* file: ... */ sections:
/* file: app.component.ts */
import { Component } from '@angular/core';
import { GridSettings, HotTableModule } from '@handsontable/angular-wrapper';
@Component({
standalone: true,
imports: [HotTableModule],
selector: 'example1-feature-name',
template: `
<div>
<hot-table [data]="data" [settings]="gridSettings"></hot-table>
</div>
`,
})
export class AppComponent {
readonly data = [...];
readonly gridSettings: GridSettings = { ... };
}
/* end-file */
/* file: app.config.ts */
import { ApplicationConfig, provideZoneChangeDetection } from '@angular/core';
import { registerAllModules } from 'handsontable/registry';
import { HOT_GLOBAL_CONFIG, HotGlobalConfig } from '@handsontable/angular-wrapper';
registerAllModules();
export const appConfig: ApplicationConfig = {
providers: [
provideZoneChangeDetection({ eventCoalescing: true }),
{
provide: HOT_GLOBAL_CONFIG,
useValue: {
// license: 'your-license-key', // get a key at https://handsontable.com/pricing/
} as HotGlobalConfig,
},
],
};
/* end-file */
angular/example1.html:
<div>
<example1-feature-name></example1-feature-name>
</div>
Edit on StackBlitz: docs/public/example-tabs.js merges each framework's companion example*.html into the generated app shell (parseDocsExampleHtmlForStackBlitz uses the browser DOMParser to collect style nodes for <head> and drops script nodes from the body fragment; mergeCompanionHtmlForStackBlitz wires it into the StackBlitz template). Examples with no HTML tab keep the default mount markup.
See skill angular-wrapper-dev for the full reference.
Vue 3
Write every new or updated example as a TypeScript SFC: one exampleN.vue per example, Composition API, <script setup lang="ts">. The example-runner (docs/src/scripts/example-runner.ts) loads vue/example*.vue and mounts them with createApp(). When you touch an old split exampleN.js + exampleN.html example, migrate it to a .vue SFC. New examples use the Composition API, not defineComponent with data()/methods.
<script setup lang="ts">
import { ref } from 'vue';
import { HotTable } from '@handsontable/vue3';
import { registerAllModules } from 'handsontable/registry';
import type { GridSettings } from 'handsontable/settings';
registerAllModules();
const hotSettings = ref<GridSettings>({
data: [
['Acme Corp', 'Q1 2025', '$4.2M'],
['Vertex Industries', 'Q1 2025', '$18.7M'],
],
colHeaders: true,
height: 'auto',
// licenseKey: 'your-license-key', // get a key at https://handsontable.com/pricing/
});
</script>
<template>
<div id="example1">
<HotTable :settings="hotSettings" />
</div>
</template>
- Type grid options with
GridSettingsfromhandsontable/settings; add localtypealiases for object rows. - Call
registerAllModules()once at the top level of<script setup>, outsideonMounted. - Import
HotTableandHotColumnfrom@handsontable/vue3; using them in<template>registers them (no globalapp.component()registration). licenseKeygoes inside the settings object.- The root
<div>idmatches the guide's example container (#example1in::: example #example1 :vue3). - Prefer a single
:settingsobject; use individual props only when the guide text highlights one. - Put hooks (
afterChange,beforeDataProviderFetch, etc.) inside the settings object, not as Vue event listeners on<HotTable>. ref()holds reactive state the template or handlers update. Use a plainconstforhotSettingswhen deep reactivity would trigger unwantedupdateSettings()calls (e.g. only a status label changes beside the grid).- Template refs bound via
ref="..."useuseTemplateRef('refName'), neverref(), with the string matching the templaterefattribute exactly:
const hotRef = useTemplateRef<InstanceType<typeof HotTable>>('hotRef'); // <HotTable ref="hotRef" :settings="hotSettings" />
const dropdownRef = useTemplateRef<HTMLDivElement>('dropdownRef'); // <div ref="dropdownRef" class="theme-dropdown">
Access the grid with hotRef.value?.hotInstance, a DOM element with dropdownRef.value.
HotColumnnests inside<HotTable>in<template>, with column options via:settingson each.<style scoped>is allowed for example-only UI; the guide's--cssslot CSS still applies globally.
Presets on the ::: example directive select dependencies: :vue3 (default), :vue3-languages, :vue3-vuex. Match the preset to the feature.
Embedding a Vue SFC (single tab, no --html / --js):
::: example #example1 :vue3
@[code](@/content/guides/category/feature/vue/example1.vue)
:::
See skill vue-wrapper-dev for wrapper behavior.
Embedding in the guide
Embed the files with @[code] inside an ::: example container; syntax in the writing-docs-pages skill.
Checklist
- [ ] TypeScript source written (
.ts/.tsx, or.vuewithlang="ts"); JS variant generated, not hand-written. - [ ] The commented-out
licenseKeyplaceholder (no key value),handsontable/baseimport plus registration. - [ ] 25-60 lines, one concept, realistic data.
- [ ] Every framework variant rendered and looked at: open the page in the docs dev server (
npm --prefix docs run dev, not a full docs build) and check each variant for console load errors, grid width, and styling. A variant you did not open counts as untested. - [ ] Angular variants type-check:
SKIP_LATEST=1 npm run typecheck --prefix docs/angular-type-check(as CI runs it; withoutSKIP_LATESTit also checks againsthandsontable@latest, so an API added on the branch fails locally) (needshandsontableandwrappers/angular-wrapperbuilt first). This is theDocs Angular Type CheckCI job, which fails most often after an example edit.