Skip to Content
Delmare Digital
Guides

Copying and pasting blocks

Copy a block with everything inside it and paste it elsewhere on the page, into an empty slot, or on another page.

Any block can be copied, with everything inside it, and pasted somewhere else: further down the page, into an empty column or slot, or on another page. Needs payload-puck 0.12.1 or later.

In the editor

Select a block and use its action bar:

  • Copy block copies the block, its settings (including whether it's shown or hidden) and every block inside it.
  • Paste after puts the copied block right after the selected one, in the same place.
  • Paste inside puts it into one of the selected block's slots, after anything already there. It works on empty slots, so this is how you fill an empty column, and on slots inside list and group fields. A block with several slots opens a menu of them, for example Paste into Left or Paste into Tabs 1 › Content; use the arrow keys, Home and End to move through it and Escape to close it.

The paste buttons appear once something is copied. The pasted block is selected, one undo removes it, and your onChange receives it as a single change. Its resolveData runs with the insert trigger, as when you add a block from the sidebar.

A block holding something that can't be saved as JSON (a function, a React element) can't be copied; the editor says so. Values that saving would drop or convert are copied the same way: undefined is left out and a date becomes its ISO string.

Keyboard: with a block selected, Cmd+C / Ctrl+C copies it and Cmd+V / Ctrl+V pastes after it. In a text field, or with text selected, they copy and paste text as usual.

Across pages, and what's stored

The copied block is kept in your browser, so it's still there after you open another page, and your other editor tabs on the site share it. Copy a block on one page, open another, select a block and paste.

What is stored, where, and for how long:

  • What: one record holding the copied block (its type, its settings including visibility, and every block inside it, as the JSON saving the page would store), when it expires, and a scope saying whose it is. It is stored unencrypted.
  • Where: in the browser's localStorage, under payload-puck:block-clipboard. Nothing is sent to the server.
  • Whose: the signed-in Payload user's. The scope is a SHA-256 hash of the user id (plus your own scope, below), and a copy belonging to another user or scope reads as an empty clipboard. The hash only keeps the id out of plain view; it isn't a secret, since ids are easy to guess.
  • For how long: 24 hours, after which it can't be pasted and is deleted the next time the editor reads it.
  • Signing out doesn't delete it. Payload's Log out first navigates away from the page, so the copy stays in this browser until a different user (or, in the admin's Edit button and list, no one) opens the editor, a Puck page's edit view, or the Pages list with the Puck column showing, or until it expires. Until then no one else can paste it through the editor, but it sits unencrypted in the browser's storage. If the user signs out in another tab while one of those is open, it's deleted as soon as Payload tells that tab the user signed out (recent Payload versions do this), otherwise on that tab's next navigation or refresh. This needs no new admin component, so your import map needs no update.
  • Without a signed-in user (an editor on your own page outside the admin), or when the browser has no Web Crypto crypto.subtle (an admin served over plain http), the copy is kept in sessionStorage instead and lasts only for that tab. It is keyed by a short, non-secret hash of the user id, so the next user to sign in to that tab doesn't see it (unless their two ids collide on that 32-bit hash, about a 1 in 4 billion chance).
  • When the browser won't store data (some private windows), copy and paste still work within the same tab.

The copy is a snapshot of the page content at the time, including unpublished drafts, so treat a shared computer the way you would any signed-in admin session.

Separating tenants or sites

If one admin serves several tenants or sites on the same domain, pass your own scope so a block copied in one can't be pasted in another. It's combined with the user, not a replacement for it:

<PuckEditor copyPaste={{ scope: tenant.id }} />

When a paste is refused

The editor checks the copied block against where it's going, in this order. The first refusal stops the paste, nothing changes, and a short message says why:

  1. The copy can be read and every block type is known. A copy from a different editor version or a malformed copy can't be read; copy it again. Every block type, including those inside the copied block, must exist in this editor's config.
  2. Your blockClipboard.canPaste hooks allow it. The hooks run only after the whole copy is readable and its types are known. If a hook refuses, its message is shown before any generic slot or permission text.
  3. The slots allow it. The destination slot's allow and disallow lists are checked first, then the slots inside the pasted block. These lists apply to pasting the same way they apply to dragging.
  4. You may add that block, under Puck's insert permission.

Copy block isn't offered on blocks Puck's duplicate permission forbids, since a copy is a duplicate made elsewhere. Paste inside isn't offered on blocks you may not edit.

Refusing a paste from your own blocks

A block type can refuse pastes with blockClipboard.canPaste. It's asked for each block the paste would land inside, nearest first, so a container can refuse something pasted anywhere beneath it, and then for the pasted block's own type. Return true to allow the paste, or a short message to refuse it; the first message stops the paste and is shown in the editor.

Hooks are asked after the copy is read and its block types are found, and before the slot lists and the insert permission (see When a paste is refused), so a hook's message is the one shown. When every hook returns true, the slot lists and insert permission still apply.

Tabs.tsx
import type { ComponentConfig } from '@puckeditor/core'
import type { CanPasteArgs } from '@delmaredigital/payload-puck/editor'

const containsTabs = (block: CanPasteArgs['block']): boolean =>
  block.type === 'Tabs' ||
  Object.values(block.props).some(
    (value) => Array.isArray(value) && value.some((child) => child?.type && containsTabs(child)),
  )

export const Tabs: ComponentConfig = {
  fields: { first: { type: 'slot' }, second: { type: 'slot' } },
  blockClipboard: {
    // No Tabs anywhere inside a Tabs block
    canPaste: ({ block, destination }) =>
      destination.ancestors.some((ancestor) => ancestor.type === 'Tabs') && containsTabs(block)
        ? "Tabs can't go inside other tabs."
        : true,
  },
  render: () => null,
}

The hook receives:

Prop

Type

The hook runs in the browser and only decides what the editor allows. A hook that throws refuses the paste.

Known limits

  • If a container's resolveData is still running when you paste into it, its result can replace the container's content and drop the pasted block. Puck's own insert has the same race.
  • Slots come from a block type's fields. Puck doesn't treat a slot added only by resolveFields as a slot, so neither do copy, paste or Reset to defaults.

Turning it off

In the admin editor, set it on the plugin:

payload.config.ts
createPuckPlugin({ copyPaste: false })

On your own editor page, set it on PuckEditor:

<PuckEditor copyPaste={false} />

That removes the action bar buttons and the keyboard shortcuts, nothing is stored, and the plugin's admin Edit button and list stop checking for a leftover copy. The plugin also honors copyPaste: false set directly in your config's custom.puck. Copy and paste are on by default. The plugin option also takes a scope: createPuckPlugin({ copyPaste: { scope: 'site-a' } }).

Edit this page on GitHub

On this page