Visual Editing

Add Visual Editing without React

Add click-to-edit overlays to Vue, Svelte, Astro, or plain JavaScript frontends with @sanity/visual-editing-standalone, without installing React.

Visual Editing overlays render with React, so @sanity/visual-editing requires react, react-dom, and styled-components as peer dependencies. @sanity/visual-editing-standalone compiles React into the package instead. You can add click-to-edit overlays to a frontend built with Vue, Svelte, Astro, or plain JavaScript without installing React.

This guide shows how to install the standalone package, enable overlays in draft mode, sync navigation with the Presentation Tool, and make non-text content editable. If your frontend uses React, use @sanity/visual-editing instead. The standalone package carries its own copy of React, so a React app would ship two.

Prerequisites

Install the package

Install the package in your frontend project. It has no dependencies or peer dependencies, so you don't install React alongside it:

npm install @sanity/visual-editing-standalone

Enable Visual Editing

Create a module that imports the overlay stylesheet and calls enableVisualEditing(). The history option connects your frontend's navigation to the Presentation Tool: the studio follows the editor as they browse the preview, and the preview follows links the editor opens in the studio.

import '@sanity/visual-editing-standalone/styles.css'
import { enableVisualEditing } from '@sanity/visual-editing-standalone'

export function startVisualEditing() {
  return enableVisualEditing({
    history: {
      subscribe: (navigate) => {
        const onPopState = () => navigate({ type: 'pop', url: location.href })
        addEventListener('popstate', onPopState)
        return () => removeEventListener('popstate', onPopState)
      },
      update: (update) => {
        if (update.type === 'push') history.pushState(null, '', update.url)
        if (update.type === 'replace') history.replaceState(null, '', update.url)
        if (update.type === 'pop') history.back()
      },
    },
  })
}

enableVisualEditing() returns a function that removes the overlays and their event listeners. Call it when draft mode ends or when the component that started Visual Editing unmounts.

The stylesheet styles the loading spinner, screen-reader-only text, and label truncation. Overlays still work without it, but those details render unstyled. If your bundler doesn't handle CSS imports, add the stylesheet with a <link> tag instead, as shown in the CDN section of this guide.

The popstate event only fires for back and forward navigation. If your frontend uses a client-side router, also call navigate() from the router's change listener. See the router integration section of Overlays and click-to-edit.

Load Visual Editing only in draft mode

Load Visual Editing only while draft mode is active. This keeps the overlay code out of the pages your visitors see. Create an entry module that starts Visual Editing, and have your server render a <script type="module"> tag for it only on draft mode requests:

import { startVisualEditing } from './visual-editing'

startVisualEditing()

In a component framework, call startVisualEditing() from a client-side lifecycle hook, such as onMounted in Vue or onMount in Svelte. Call the function it returns from the matching teardown hook.

Run it in the browser only

Make non-text content editable

Overlays find string fields through stega encoding. Images, numbers, booleans, and other values that can't carry stega need a data-sanity attribute instead. Generate the attribute value with createDataAttribute():

import { createDataAttribute } from '@sanity/visual-editing-standalone'

export function postImageAttribute(postId: string): string {
  return createDataAttribute({
    id: postId,
    type: 'post',
    path: 'mainImage',
    baseUrl: '',
  }).toString()
}

Then render the value on the element that displays the field. This example builds the HTML with a template literal in your server-side rendering code, where imageUrl and post come from your query:

const imageHtml = `<img src="${imageUrl}" alt="" data-sanity="${postImageAttribute(post._id)}" />`

Replace with your studio's full URL, including any base path. createDataAttribute() doesn't use the DOM, so you can call it on the server while rendering HTML. Importing only createDataAttribute() doesn't load the overlay renderer.

For edit groups, edit targets, and the other attributes overlays recognize, see the content detection section of Overlays and click-to-edit.

Load the package from a CDN

If your site has no build step, load the package as a browser module from esm.sh, and add the stylesheet with a <link> tag. Include these tags only in draft mode responses:

<link rel="stylesheet" href="https://esm.sh/@sanity/visual-editing-standalone@2/styles.css" />
<script type="module">
  import {
    createDataAttribute,
    enableVisualEditing,
  } from 'https://esm.sh/@sanity/visual-editing-standalone@2'

  document.querySelector('h1').dataset.sanity = createDataAttribute({
    baseUrl: '',
    id: 'post-1',
    type: 'post',
    path: 'title',
  }).toString()

  enableVisualEditing()
</script>

In production, replace @2 in both URLs with an exact version so the CDN always serves the same build.

Features not included in the standalone package

The standalone package exports enableVisualEditing() and createDataAttribute(). They accept the same options as in @sanity/visual-editing, with these exceptions:

  • The components and plugins options aren't available, because they take React components. See Custom overlay components and controls.
  • createOverlayController(), the low-level overlay controller, isn't exported.
  • The framework-specific integrations, such as the components for Next.js and SvelteKit, aren't included.

If you need any of these, use @sanity/visual-editing and install its peer dependencies.

Next steps

Was this page helpful?

Sanity logosanity.ioAll Systems Operational© Sanity 2026
Change site theme