Skip to content

Add slots API #984

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 95 commits into
base: main
Choose a base branch
from
Open

Add slots API #984

wants to merge 95 commits into from

Conversation

chrisvxd
Copy link
Member

@chrisvxd chrisvxd commented Apr 8, 2025

Introduce the slots API, the main feature to achieve #255. The ticket should likely not be closed until the migration strategy is determined, which may be part of a follow-on.

API

Slots are like DropZones, but as a field, with the data stored on the prop.

{
  fields: {
    content: { type: "slot" }
  },
  render: ({ content: Content }) => (
    <div>< Content  /></div>
  )
}

The data format follows the standard Puck data format for content, and supports all standard component APIs including defaultProps and resolveData:

{
  defaultProps: {
    children: [
      {
        type: "Heading",
        props: {
          id: "123456789",
          title: "I'm inside a slot"
        }
      }
    ]
  }
}

This unlocked powerful patterns like the new Template demo component which allows you to save and load pre-configured templates of components.

This model is deterministic, making it possible easily convert it into other formats (like XML), or render in third party environments (even outside of React). For example, you could use react-from-json:

import React from "react";
import ReactFromJSON from "react-from-json";

const entry = {
  type: "Foo",
  props: {
    children: [
      {
        type: "Bar",
        props: {
          baz: "Hello, world",
        },
      },
    ],
  },
};

const mapping = {
  Foo: ({ children }) => (
    <div>
      <div>{children}</div>
    </div>
  ),
  Bar: ({ baz }) => <span>{baz}</span>,
};

const Example = () => {
  return <ReactFromJSON entry={entry} mapping={mapping} />;
};

Drive-by features

Migration

This change is backwards compatible, but it does deprecate DropZones. The PR includes a migration guide.

Part 2 (ticket TBD) of the deprecation will involve the following breaking changes, and be implemented before v1:

  • Complete removal of DropZones and zones
  • Migration of content to a child prop on root
  • Deprecation of the RSC bundle

Remaining tasks

  • Root fields not showing
  • Empty arrays pollute index
  • Tests
  • Permissions
  • Store index in history
  • Consider removing performance warning
  • Consider stripping index from public APIs (history, anything else)
  • Docs
  • Remove or tidy debug plugin
  • Add deprecation notice to DropZone
  • Follow ons
    • Consider rendering mode (interactive vs preview) to slot field
    • Add DropZones to Slots support to “migrate()” helper
    • Disable drag of all slot children during resolving
    • Add API to lock DropZone, used for templates
    • Expose slot helper utils, possibly as separate library (walkTree, mapSlots, randomizeIds), etc
    • Create part 2 migration ticket

Closes #255, closes #963, closes #950, closes #862

Copy link

vercel bot commented Apr 8, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
puck-demo ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 21, 2025 2:02pm
puck-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 21, 2025 2:02pm

@chrisvxd chrisvxd force-pushed the 255-more-portable-data-payload branch from 1602e07 to cbd976f Compare April 11, 2025 15:20
@chrisvxd chrisvxd force-pushed the 255-more-portable-data-payload branch from cbd976f to 44fd9de Compare April 11, 2025 15:22
@chrisvxd chrisvxd mentioned this pull request Apr 12, 2025
5 tasks
@chrisvxd chrisvxd force-pushed the 255-more-portable-data-payload branch from 8b95960 to 550a20e Compare April 12, 2025 17:49
@chrisvxd chrisvxd force-pushed the 255-more-portable-data-payload branch from 1849030 to b9f84b3 Compare April 13, 2025 14:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants