Visual Editor
Live-editing patterns for SvelteKit — LocalessDocument, the localessSync store, and how the sync script gets loaded.
Visual Editor
Set enableSync: true in localessInit() to load the Visual Editor sync script (sync-v1.js). It's a no-op outside the Localess Visual Editor iframe.
With LocalessDocument
<LocalessDocument> wraps <LocalessComponent> and subscribes to input/change events automatically when enableSync is active, re-rendering with the updated content in place. It doesn't fetch content — pass the full Content object as document:
<script lang="ts">
import { getLocaless, LocalessDocument } from '@localess/svelte';
const content = await getLocaless().getContentBySlug('home');
</script>
<LocalessDocument document={content} />Props:
| Prop | Type | Required | Description |
|---|---|---|---|
document | Content<T> | Yes | Full content response object (from getContentBySlug/getContentById) |
Renders an inline error message if
document.datais missing. PreferLocalessDocumentoverLocalessComponentfor the top-level content of a page when Visual Editor sync should apply; useLocalessComponentdirectly for nested blocks within an already-synced tree.
With the localessSync store
For custom integrations that manage their own content state instead of using LocalessDocument, subscribe to the Visual Editor bridge directly via the localessSync store — a Readable that emits the latest matching event:
<script lang="ts">
import { localessSync } from '@localess/svelte';
const latest = localessSync(['input', 'change']);
</script>
<p>{$latest?.data}</p>No-ops (never emits) when enableSync was not set in localessInit, or outside the Visual Editor iframe. Unlike @localess/react, there's no exported isSyncEnabled/localessSyncOn helper — the localessSync store is the only public entry point into the sync bridge.
Available events:
| Event | When |
|---|---|
input | User is typing in a field (real-time preview) |
change | Field value confirmed |
save | Content saved |
publish | Content published |
unpublish | Content unpublished |
pong | Editor heartbeat response |
enterSchema | Editor cursor enters a schema block |
hoverSchema | Editor cursor hovers over a schema block |
leaveSchema | Editor cursor leaves a schema block |