scorpio-99

payload better preview

by scorpio-99
Publishing workflow
v3
^3.0.0
MIT

Overview

Summary generated from the README

Extends Payload live preview with hover highlighting that labels the block under the cursor and two-way sync between admin block rows and the preview iframe. You register the plugin, add data attributes to block wrappers, and render a client component on draft pages. Nested blocks are supported, and Lexical richText blocks too on Payload 3.81 or later. Works with Payload 3.x.

Features

  • Hover overlay with a label showing block type, index and name
  • Clicking a block row in the admin scrolls the preview to it and flashes a highlight
  • Clicking a block in the preview expands and scrolls to its admin row, including nested ancestors
  • Sync for blocks nested at any depth and for Lexical richText blocks
  • Opt-out of sync on elements with a data attribute
  • Configurable accent color, scroll alignment, offset and custom toggle component

Use cases

  • Help editors locate the matching admin block while clicking through a page in live preview
  • Edit pages built from nested layout blocks and see which block is being changed
  • Debug block structure on a draft page using hover labels

From the author

Better live preview for Payload CMS — hover highlighting with block identification, bi-directional admin/preview sync, and smooth transitions.

Installation

npm i payload-better-preview

16

Stars

1

Forks

1

Open Issues

6 months ago

Last Updated

53

Weekly Downloads

v2.1.3

npm Version

98.6 KB

Unpacked Size

0

Dependencies

GitHub stars

16+2 since Sep 7

Show data
GitHub stars by day (UTC)
DateStars
Dec 6, 202616
Dec 5, 202616
Dec 4, 202616
Dec 3, 202616
Dec 2, 202616
Dec 1, 202616
Nov 30, 202616
Nov 29, 202616
Nov 28, 202616
Nov 27, 202616
Nov 26, 202616
Nov 25, 202616
Nov 24, 202616
Nov 23, 202616
Nov 22, 202616
Nov 21, 202616
Nov 20, 202616
Nov 19, 202616
Nov 18, 202616
Nov 17, 202616
Nov 16, 202617
Nov 15, 202617
Nov 14, 202617
Nov 13, 202617
Nov 12, 202617
Nov 11, 202617
Nov 10, 202617
Nov 9, 202617
Nov 8, 202617
Nov 7, 202617
Nov 6, 202617
Nov 5, 202617
Nov 4, 202616
Nov 3, 202616
Nov 2, 202616
Nov 1, 202616
Oct 31, 202616
Oct 30, 202616
Oct 29, 202616
Oct 28, 202616
Oct 27, 202616
Oct 26, 202616
Oct 25, 202616
Oct 24, 202616
Oct 23, 202616
Oct 22, 202616
Oct 21, 202615
Oct 20, 202615
Oct 19, 202615
Oct 18, 202615
Oct 17, 202615
Oct 16, 202615
Oct 15, 202615
Oct 14, 202615
Oct 13, 202615
Oct 12, 202615
Oct 11, 202614
Oct 10, 202614
Oct 9, 202614
Oct 8, 202614
Oct 7, 202614
Oct 6, 202614
Oct 5, 202614
Oct 4, 202614
Oct 3, 202614
Oct 2, 202614
Oct 1, 202614
Sep 30, 202614
Sep 29, 202614
Sep 28, 202614
Sep 27, 202614
Sep 26, 202614
Sep 25, 202614
Sep 24, 202614
Sep 23, 202614
Sep 22, 202614
Sep 21, 202614
Sep 20, 202614
Sep 19, 202614
Sep 18, 202614
Sep 17, 202614
Sep 16, 202614
Sep 15, 202614
Sep 14, 202614
Sep 13, 202614
Sep 12, 202614
Sep 11, 202614
Sep 10, 202614
Sep 9, 202614
Sep 8, 202614
Sep 7, 202614
npm weekly downloads

53−139 since Sep 7

Show data
npm weekly downloads by day (UTC)
DateWeekly downloads
Dec 6, 202653
Dec 5, 202638
Dec 4, 202641
Dec 3, 202639
Dec 2, 202632
Dec 1, 202643
Nov 30, 202642
Nov 29, 202633
Nov 28, 202631
Nov 27, 202635
Nov 26, 202641
Nov 25, 202646
Nov 24, 202638
Nov 23, 202642
Nov 22, 202655
Nov 21, 202669
Nov 20, 202662
Nov 19, 202662
Nov 18, 202666
Nov 17, 202666
Nov 16, 202670
Nov 15, 202669
Nov 14, 202668
Nov 13, 202665
Nov 12, 202672
Nov 11, 202664
Nov 10, 202667
Nov 9, 202663
Nov 8, 202654
Nov 7, 202637
Nov 6, 202647
Nov 5, 202642
Nov 4, 202642
Nov 3, 202639
Nov 2, 202642
Nov 1, 202646
Oct 31, 202650
Oct 30, 202644
Oct 29, 202633
Oct 28, 202631
Oct 27, 202632
Oct 26, 202630
Oct 25, 202627
Oct 24, 202626
Oct 23, 202619
Oct 22, 202628
Oct 21, 202648
Oct 20, 202649
Oct 19, 202655
Oct 18, 202661
Oct 17, 202664
Oct 16, 202674
Oct 15, 2026100
Oct 14, 202696
Oct 13, 202698
Oct 12, 202696
Oct 11, 202693
Oct 10, 202696
Oct 9, 2026110
Oct 8, 202687
Oct 7, 202681
Oct 6, 202682
Oct 5, 202686
Oct 4, 202688
Oct 3, 202698
Oct 2, 202688
Oct 1, 202699
Sep 30, 2026102
Sep 29, 2026111
Sep 28, 2026113
Sep 27, 2026116
Sep 26, 2026106
Sep 25, 2026120
Sep 24, 2026107
Sep 23, 2026117
Sep 22, 2026122
Sep 21, 2026127
Sep 20, 2026122
Sep 19, 2026121
Sep 18, 2026108
Sep 17, 2026114
Sep 16, 2026105
Sep 15, 202691
Sep 14, 202677
Sep 13, 202691
Sep 12, 2026119
Sep 11, 2026142
Sep 10, 2026159
Sep 9, 2026187
Sep 8, 2026187
Sep 7, 2026192

Each point is npm's rolling 7-day download count.

Health Score

49/100Fair

Composite score based on GitHub activity, stars, npm downloads, publish recency, dependency count, and package size.

Badge for your README

Payload Directory health badge
Markdown
[![Payload Directory](https://payloaddirectory.dev/badge/1164837193-root.svg)](https://payloaddirectory.dev/plugins/1164837193-root)
HTML
<a href="https://payloaddirectory.dev/plugins/1164837193-root"><img src="https://payloaddirectory.dev/badge/1164837193-root.svg" alt="Payload Directory"></a>
Updated Jun 4, 2026Created Feb 23, 2026Published Jun 4, 2026

README

payload-better-preview

npm downloads issues stars

⚠️ Consider using the successor payload-better-editor. It offers the same as this plugin, but also adds a side-by-side overlay with a sidebar of native Payload fields, viewport switching (Desktop / Tablet / Mobile / Responsive / Fullscreen), undo/redo, in-sidebar block actions (move, duplicate, add-below, delete), drag-resizable sidebar, an interact mode for clicks inside the preview, and a BetterEditorSettings global so admins can tune sidebar position, hover colours, and breakpoints live without a re-deploy.

Better live preview for Payload CMS — hover highlighting with block identification, bi-directional admin/preview sync, and smooth transitions.

Found a bug? Please open an issue with steps to reproduce, your Payload version, and a minimal example. PRs welcome too.

Features

Hover Highlighting

Blue overlay marks the block under the cursor with a label badge showing block type, index, and name. Nested blocks get a dashed parent overlay with breadcrumb labels.

Hover Highlighting

Admin → Preview Sync

Click a block row in the admin editor — the preview scrolls to that block and highlights it with a flash effect.

Admin to Preview Sync

Preview → Admin Sync

Click a block in the preview — the admin editor scrolls to the corresponding block row, expands it if collapsed (including all ancestor rows for nested blocks), and highlights it.

Preview to Admin Sync

Other

  • Draft-only — Zero impact on published pages
  • Scroll/Resize tracking — Overlay follows block position smoothly
  • Infinite nesting — Bi-directional sync works at any block nesting depth

Installation

pnpm add payload-better-preview
# or
npm install payload-better-preview

1. Register the plugin

// payload.config.ts
import { betterPreview } from 'payload-better-preview'

export default buildConfig({
  plugins: [
    betterPreview({
      accentColor: '#3b82f6', // optional
      scrollAlign: 'start',   // optional
      scrollOffset: 128,      // optional
    }),
  ],
  admin: {
    livePreview: {
      collections: ['pages'],
      url({ data }) {
        return `/${data.id}`
      },
    },
  },
})

2. Add data attributes to block wrappers

Each rendered block must have three data attributes:

AttributeDescription
data-blockBlock type slug, e.g. "hero", "text"
data-block-index0-based index within the blocks field
data-block-fieldField path — see below
data-block-nameOptional display name shown in the overlay label

The data-block-field value must match the Payload field name so the plugin can map blocks between the admin and the preview:

export function BlockRenderer({ block, index, field }) {
  return (
    <div
      data-block={block.blockType}
      data-block-index={index}
      data-block-field={field}
      data-block-name={block.name} // optional
    >
      {/* block content */}
    </div>
  )
}

export function RenderBlocks({ blocks, field }) {
  return blocks.map((block, index) => (
    <BlockRenderer key={block.id} block={block} index={index} field={field} />
  ))
}
// pass the exact Payload field name
<RenderBlocks blocks={page.content} field="content" />
<RenderBlocks blocks={page.sidebar} field="sidebar" />

3. Render <BetterPreview /> in your page

import { BetterPreview } from 'payload-better-preview/client'

export default async function Page() {
  const { isEnabled: draft } = await draftMode()

  return (
    <>
      {draft && <LivePreviewListener />}
      {draft && <BetterPreview />}
      {/* ... rest of page */}
    </>
  )
}

Nested blocks

For blocks that contain other blocks, construct the field prop by concatenating the parent field, parent index, and child field name with hyphens. This matches the ID pattern Payload generates in the admin and enables sync at any depth:

export function NestedBlockRenderer({ block, index, field }) {
  return (
    <div
      data-block={block.blockType}
      data-block-index={index}
      data-block-field={field}
    >
      <RenderBlocks
        blocks={block.content}
        field={`${field}-${index}-content`}
        {/* e.g. "content-0-content", "content-0-content-1-sidebar" */}
      />
    </div>
  )
}

RichText (Lexical) blocks

Payload's Lexical editor supports blocks embedded directly inside richText fields. The plugin provides full bi-directional sync for these blocks too.

Requires Payload 3.81+. On older versions the Lexical plugin component is rendered outside of the LexicalComposerContext during SSR and throws. The core block sync (layout blocks, hover highlighting, Admin ↔ Preview sync) still works on Payload ^3.0.0 — only the richText-block sync is gated on 3.81+.

1. Register the Lexical feature

Add BetterPreviewLexicalFeature to your Lexical editor config. This injects a client-side plugin that stamps each block in the editor with a data-block-id attribute so the admin can locate them:

// payload.config.ts
import { betterPreview, BetterPreviewLexicalFeature } from 'payload-better-preview'
import { lexicalEditor, BlocksFeature } from '@payloadcms/richtext-lexical'

export default buildConfig({
  plugins: [betterPreview()],
  editor: lexicalEditor({
    features: ({ defaultFeatures }) => [
      ...defaultFeatures,
      BlocksFeature({ blocks: [...] }),
      BetterPreviewLexicalFeature(),
    ],
  }),
})

2. Add data attributes in JSX converters

Add data-block-id (the block's unique ID from node.fields.id) to each block wrapper. The plugin uses this ID to sync between admin and preview:

// jsxConverters.tsx
import { JSXConvertersFunction } from '@payloadcms/richtext-lexical/react'

export const jsxConverters: JSXConvertersFunction = ({ defaultConverters }) => ({
  ...defaultConverters,
  blocks: {
    hero: ({ node }) => (
      <div
        data-block={node.fields.blockType}
        data-block-id={node.fields.id}         // required for richText sync
      >
        {/* block content */}
      </div>
    ),
  },
})

How it works

  • data-block-id identifies each block uniquely across both admin and preview
  • Admin → Preview: clicking a Lexical block in the admin sends scroll-to-richtext-block with the block ID — the preview finds [data-block-id] and scrolls to it
  • Preview → Admin: clicking a block with data-block-id (but no data-block-field) sends focus-richtext-block — the admin finds [data-block-id], expands the collapsible if needed, and scrolls to it

Disabling sync on specific elements

Add data-preview-ignore to any element to prevent clicks on it from triggering scroll sync. Works in both directions — in the preview (blocks rendered on the frontend) and in the admin (custom block components inside the Lexical editor).

Useful for interactive elements like buttons, links, or custom controls inside a block component:

// jsxConverters.tsx — preview side
blocks: {
  cta: ({ node }) => (
    <div
      data-block={node.fields.blockType}
      data-block-id={node.fields.id}
    >
      <h2>{node.fields.title}</h2>
      {/* clicks here will NOT trigger scroll sync */}
      <button data-preview-ignore onClick={...}>
        Buy now
      </button>
    </div>
  ),
},
// Custom block component — admin side (Lexical editor)
export function CtaBlockComponent({ formData }) {
  return (
    <div>
      <p>{formData.title}</p>
      {/* clicks here will NOT send scroll-to-preview message */}
      <button data-preview-ignore onClick={openModal}>
        Edit link
      </button>
    </div>
  )
}

Any click originating inside a [data-preview-ignore] element is ignored by the plugin entirely — hover highlighting and scroll sync are both skipped for that interaction.

Nested blocks inside richText

For blocks that are nested inside a richText block (e.g. a nestedBlock with its own blocks field), sync works the same as regular nested blocks. The plugin automatically detects the parent richText block and scopes the search to avoid duplicate IDs.

Add data-block-field and data-block-index to nested blocks (but NOT to direct richText blocks since they have no field path):

// For the outer richText block (nestedBlock):
<div
  data-block={node.fields.blockType}
  data-block-id={node.fields.id}
>
  <RenderBlocks
    blocks={node.fields.content}
    field="content"               // local field name only (not full path)
    richTextBlockId={node.fields.id}
  />
</div>

// For inner blocks (BlockRenderer receives richTextBlockId from parent):
<div
  data-block={block.blockType}
  data-block-id={block.id}
  data-block-index={index}
  data-block-field={field}        // e.g. "content"
>

Plugin options

OptionTypeDefaultDescription
disabledbooleanfalseDisable the plugin entirely
accentColorstring'#3b82f6'Highlight color for overlays and flash effects in the admin
scrollAlign'start' | 'center' | 'end''start'Block alignment when scrolling in admin
scrollOffsetnumber128Top offset in px when scrolling in admin — accounts for the sticky admin header

<BetterPreview /> props

PropTypeDefaultDescription
accentColorstring'#3b82f6'Highlight color for overlays and flash effects in the preview
scrollAlign'start' | 'center' | 'end''start'Block alignment when scrolling in the preview
scrollOffsetnumber0Top offset in px when scrolling in the preview — useful for fixed headers
showTogglebooleantrueShow the built-in toggle button
toggleComponentReact.ComponentType<ToggleProps>—Replace the built-in toggle with a custom component

Scroll offset in the preview

The preview uses window.scrollTo internally to avoid propagating scroll to the parent admin frame. Because of this, scroll-margin-top has no effect. Use scrollOffset instead:

<BetterPreview scrollOffset={80} />

Custom toggle component

import type { ToggleProps } from 'payload-better-preview/client'

function MyToggle({ enabled, onToggle }: ToggleProps) {
  return (
    <button onClick={onToggle} style={{ position: 'fixed', top: 16, right: 16 }}>
      {enabled ? 'Sync on' : 'Sync off'}
    </button>
  )
}

<BetterPreview toggleComponent={MyToggle} />

How it works

Block fields (standard):

  • Admin → Preview: clicking a block row in the admin sends scroll-to-block with { field, index } to the preview iframe. The preview finds [data-block-field][data-block-index] and scrolls to it.
  • Preview → Admin: clicking a block with data-block-field sends focus-block. The admin expands all ancestor rows and scrolls to the target row.

RichText (Lexical) blocks:

  • Admin → Preview: clicking a Lexical block sends scroll-to-richtext-block with { blockId }. The preview finds [data-block-id] and scrolls to it.
  • Preview → Admin: clicking a block with data-block-id (but no data-block-field) sends focus-richtext-block. The admin finds [data-block-id], expands the collapsible if needed, and scrolls to it.
  • Nested blocks inside richText: clicking sends focus-block with an extra richTextBlockId. The admin opens the parent Lexical block, then scopes the row search within it — avoiding duplicate ID issues when multiple identical blocks exist.

All communication is via window.postMessage. No network requests, no shared state.

<BetterPreview /> is a 'use client' component that renders null (no React DOM output). It injects 3 absolutely-positioned DOM elements into document.body:

  1. Overlay — Primary block highlight (solid blue border)
  2. Parent Overlay — For nested blocks (dashed border, subtle)
  3. Label — Info badge with block type and index

All interaction is handled via event delegation on document, so it survives DOM updates from live preview re-renders.

Contributors

[image]

Similar plugins