Skip to content

Commit

Permalink
feat: add isEditing flag to puck object prop
Browse files Browse the repository at this point in the history
This replaces the legacy undocumented `editMode` prop.
  • Loading branch information
chrisvxd committed May 30, 2024
1 parent 6e97a0e commit 13bb1bd
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 17 deletions.
4 changes: 2 additions & 2 deletions apps/demo/config/root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ export type RootProps = {
title: string;
} & DefaultRootProps;

function Root({ children, editMode }: RootProps) {
function Root({ children, puck }: RootProps) {
return (
<>
<Header editMode={editMode} />
<Header editMode={puck.isEditing} />
{children}
<Footer>
<Footer.List title="Section">
Expand Down
2 changes: 1 addition & 1 deletion apps/docs/components/Preview/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ const ConfigPreviewInner = ({
<div className={getClassNameConfigPreview("preview")}>
{componentConfig.render({
...appState.data["content"][0].props,
puck: { renderDropZone: () => <div /> },
puck: { renderDropZone: () => <div />, isEditing: false },
})}
</div>
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,24 @@ const config = {
};
```

#### Args
#### Render props

| Arg | Example | Type |
| ------- | -------------------------------------------------- | ------ |
| `props` | `{ id: "id", puck: { renderDropZone: DropZone } }` | Object |
| Arg | Example | Type |
| -------------------------------------------- | ------------- | -------- |
| [`id`](#id) | `button-1234` | String |
| [`puck.isEditing`](#puckisediting) | `false` | Boolean |
| [`puck.renderDropZone`](#puckrenderdropzone) | `() => {}` | Function |
| [`...props`](#props) | `{}` | Object |

##### `props.id`
##### `id`

A unique ID generated by Puck for this component. You can optionally apply this, or use your own ID.

##### `props.puck.renderDropZone`
##### `puck.isEditing`

A boolean describing whether or not this component is being rendered in the `<Puck>` component.

##### `puck.renderDropZone`

A render method that creates a [`<DropZone>`](/docs/api-reference/components/drop-zone) for creating nested components. Use this method instead of the `<DropZone>` when implementing React server components.

Expand All @@ -81,10 +88,6 @@ const config = {

The remaining props are provided by the user-defined [fields](#fields).

#### Returns

Returns a ReactNode

## Optional params

### `fields`
Expand Down
10 changes: 8 additions & 2 deletions packages/core/components/DropZone/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { DropZoneProvider, dropZoneContext } from "./context";
import { getZoneId } from "../../lib/get-zone-id";
import { useAppContext } from "../Puck/context";
import { DropZoneProps } from "./types";
import { PuckContext } from "../../types/Config";

const getClassName = getClassNameFactory("DropZone", styles);

Expand Down Expand Up @@ -198,11 +199,16 @@ function DropZoneEdit({ zone, allow, disallow, style }: DropZoneProps) {
{content.map((item, i) => {
const componentId = item.props.id;

const puckProps: PuckContext = {
renderDropZone: DropZone,
isEditing: true,
};

const defaultedProps = {
...config.components[item.type]?.defaultProps,
...item.props,
puck: { renderDropZone: DropZone },
editMode: true,
puck: puckProps,
editMode: true, // DEPRECATED
};

const isSelected =
Expand Down
4 changes: 2 additions & 2 deletions packages/core/components/Puck/components/Preview/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ export const Preview = ({ id = "puck-preview" }: { id?: string }) => {
config.root?.render({
id: "puck-root",
...pageProps,
editMode: true,
puck: { renderDropZone: DropZone },
editMode: true, // DEPRECATED
puck: { renderDropZone: DropZone, isEditing: true },
})
) : (
<>{pageProps.children}</>
Expand Down
1 change: 1 addition & 0 deletions packages/core/components/Render/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export function Render<UserConfig extends Config = Config>({
{...rootProps}
puck={{
renderDropZone: DropZone,
isEditing: false,
}}
title={title}
editMode={false}
Expand Down
1 change: 1 addition & 0 deletions packages/core/components/ServerRender/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export function Render<UserConfig extends Config = Config>({
renderDropZone: ({ zone }: { zone: string }) => (
<DropZoneRender zone={zone} data={data} config={config} />
),
isEditing: false,
}}
title={title}
editMode={false}
Expand Down
1 change: 1 addition & 0 deletions packages/core/types/Config.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export type PuckComponent<Props> = (

export type PuckContext = {
renderDropZone: React.FC<DropZoneProps>;
isEditing: boolean;
};

export type ComponentConfig<
Expand Down

0 comments on commit 13bb1bd

Please sign in to comment.