> For AI agents: the complete Sanity documentation index is available at [https://www.sanity.io/docs/llms.txt](https://www.sanity.io/docs/llms.txt).

# 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

- A frontend that fetches content with stega encoding enabled in draft mode. See [Client setup and stega for visual editing](https://www.sanity.io/docs/visual-editing/visual-editing-client-stega).
- Draft mode, so your server knows when to load Visual Editing. See [Implementing draft mode](https://www.sanity.io/docs/visual-editing/implementing-draft-mode).
- Sanity Studio with the Presentation Tool configured to preview your frontend. See [Configuring the Presentation Tool](https://www.sanity.io/docs/visual-editing/configuring-the-presentation-tool).
- Node.js 20.19 or later.

## 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**

```shell
npm install @sanity/visual-editing-standalone
```

**pnpm**

```shell
pnpm add @sanity/visual-editing-standalone
```

**yarn**

```shell
yarn add @sanity/visual-editing-standalone
```

**bun**

```shell
bun add @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.

**src/visual-editing.ts**

```typescript
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](https://www.sanity.io/docs/visual-editing/visual-editing-overlays).

## 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:

**src/preview.ts**

```typescript
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.

> [!WARNING]
> Run it in the browser only
> `enableVisualEditing()` needs the DOM. If it runs during server-side rendering, the call returns and then fails with `document is not defined`. Call it only from code that runs in the browser.

## 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()`:

**src/data-attributes.ts**

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

export function postImageAttribute(postId: string): string {
  return createDataAttribute({
    id: postId,
    type: 'post',
    path: 'mainImage',
    baseUrl: 'YOUR_STUDIO_URL',
  }).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:

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

Replace `YOUR_STUDIO_URL` 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](https://www.sanity.io/docs/visual-editing/visual-editing-overlays).

## 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:

```html
<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: 'YOUR_STUDIO_URL',
    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](https://www.sanity.io/docs/visual-editing/custom-overlay-components).
- `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

- Control how the preview updates when content changes in the studio with the `refresh` option. See [Overlays and click-to-edit](https://www.sanity.io/docs/visual-editing/visual-editing-overlays).
- Build a full integration with draft mode endpoints and live updates. See [Build a complete visual editing integration](https://www.sanity.io/docs/visual-editing/build-a-visual-editing-integration).
- Fix overlays that don't appear. See [Troubleshooting Visual Editing](https://www.sanity.io/docs/visual-editing/troubleshooting-visual-editing).

