Skip to content

Slate Integration

WriteTrack includes a first-party Slate integration, compatible with Slate v0.94+.

Install WriteTrack alongside Slate:

Terminal window
npm i writetrack slate slate-react
import { useEffect, useMemo, useRef } from 'react';
import { createEditor } from 'slate';
import { Slate, Editable, withReact, ReactEditor } from 'slate-react';
import { createWriteTrackSlate } from 'writetrack/slate';
function Editor() {
const editor = useMemo(() => withReact(createEditor()), []);
const handleRef = useRef(null);
useEffect(() => {
const domNode = ReactEditor.toDOMNode(editor, editor);
const handle = createWriteTrackSlate(editor, domNode, {
license: 'your-license-key',
});
handleRef.current = handle;
return () => handle.destroy();
}, [editor]);
return (
<Slate editor={editor} initialValue={initialValue}>
<Editable />
</Slate>
);
}

Pass options as the third argument:

const handle = createWriteTrackSlate(editor, domNode, {
license: 'your-license-key',
userId: 'user-123',
contentId: 'document-456',
metadata: { formName: 'signup' },
autoStart: true, // default
});
OptionTypeDefaultDescription
licensestringundefinedWriteTrack license key
userIdstringundefinedUser identifier included in metadata
contentIdstringundefinedContent identifier included in metadata
metadataRecord<string, unknown>undefinedAdditional metadata
autoStartbooleantrueStart tracking when created
wasmUrlstringundefinedURL to WASM binary for analysis
persistbooleanfalseEnable IndexedDB session persistence (requires contentId)
// Get the tracker instance
const tracker = handle.tracker;
// Get typing data
const data = tracker?.getData();
// Check tracking status
const isTracking = handle.isTracking;

Slate does not have a built-in destroy lifecycle, so destroy() must be called manually — typically in a React useEffect cleanup:

useEffect(() => {
const handle = createWriteTrackSlate(editor, domNode, options);
return () => handle.destroy();
}, [editor]);
import type {
WriteTrackSlateOptions,
WriteTrackSlateHandle,
} from 'writetrack/slate';