Skip to content

Commit

Permalink
feat: add iframe override for style injection
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisvxd committed Aug 30, 2024
1 parent f0655f0 commit 7cac376
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 5 deletions.
1 change: 1 addition & 0 deletions apps/docs/pages/docs/api-reference/overrides.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const overrides = {
- [`fieldTypes`](overrides/field-types): Override each [field type](/docs/api-reference/fields).
- [`header`](overrides/header): Override the header.
- [`headerActions`](overrides/header-actions): Override the header actions. Return a fragment so your items appear inline.
- [`iframe`](overrides/iframe): Override the root of the iframe. Useful for injecting styles.
- [`outline`](overrides/outline): Override the outline.
- [`preview`](overrides/preview): Override the drag-and-drop preview.
- [`puck`](overrides/puck): Override the Puck children. This is the equivalent of passing in [`children`](/docs/api-reference/components/puck#children) to the [`<Puck>`](/docs/api-reference/components/puck) component.
36 changes: 36 additions & 0 deletions apps/docs/pages/docs/api-reference/overrides/iframe.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---
title: iframe
---

# iframe

Override the root of the iframe.

```tsx copy
const overrides = {
iframe: ({ children, document }) => {
useEffect(() => {
if (document) {
document.body.setAttribute("style", "background: hotpink;");
}
}, [document]);

return <>{children}</>;
},
};
```

## Props

| Prop | Example | Type |
| ----------------------- | --------- | --------- |
| [`document`](#document) | `{}` | Document |
| [`children`](#children) | `<div />` | ReactNode |

### `document`

The document of the iframe window.

### `children`

The default node for the iframe.
22 changes: 17 additions & 5 deletions packages/core/components/Puck/components/Preview/index.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import { DropZone } from "../../../DropZone";
import { rootDroppableId } from "../../../../lib/root-droppable-id";
import { ReactNode, useCallback, useRef } from "react";
import { ReactNode, useCallback, useMemo, useRef } from "react";
import { useAppContext } from "../../context";
import AutoFrame from "../../../AutoFrame";
import styles from "./styles.module.css";
import { getClassNameFactory } from "../../../../lib";
import { DefaultRootProps } from "../../../../types/Config";
import { FrameContextConsumer } from "react-frame-component";

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

type PageProps = DefaultRootProps & { children: ReactNode };

export const Preview = ({ id = "puck-preview" }: { id?: string }) => {
const { config, dispatch, state, setStatus, iframe } = useAppContext();
const { config, dispatch, state, setStatus, iframe, overrides } =
useAppContext();

const Page = useCallback<React.FC<PageProps>>(
(pageProps) =>
Expand All @@ -29,6 +31,8 @@ export const Preview = ({ id = "puck-preview" }: { id?: string }) => {
[config.root]
);

const Frame = useMemo(() => overrides.iframe || "div", [overrides]);

// DEPRECATED
const rootProps = state.data.root.props || state.data.root;

Expand All @@ -52,9 +56,17 @@ export const Preview = ({ id = "puck-preview" }: { id?: string }) => {
setStatus("READY");
}}
>
<Page dispatch={dispatch} state={state} {...rootProps}>
<DropZone zone={rootDroppableId} />
</Page>
<FrameContextConsumer>
{({ document }) => {
return (
<Frame document={document}>
<Page dispatch={dispatch} state={state} {...rootProps}>
<DropZone zone={rootDroppableId} />
</Page>
</Frame>
);
}}
</FrameContextConsumer>
</AutoFrame>
) : (
<div id="preview-frame" className={getClassName("frame")}>
Expand Down
1 change: 1 addition & 0 deletions packages/core/types/Overrides.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export type Overrides = OverridesGeneric<{
}>;
components: RenderFunc;
componentItem: RenderFunc<{ children: ReactNode; name: string }>;
iframe: RenderFunc<{ children: ReactNode; document?: Document }>;
outline: RenderFunc;
puck: RenderFunc;
}>;
Expand Down

0 comments on commit 7cac376

Please sign in to comment.