Skip to content

Commit

Permalink
feat: add syncHostStyles API to disable iframe style sync
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisvxd committed Sep 13, 2024
1 parent 9e7d06f commit 112e9bc
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 8 deletions.
13 changes: 10 additions & 3 deletions apps/docs/pages/docs/api-reference/components/puck.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -173,16 +173,23 @@ export function Editor() {

#### iframe params

| Param | Example | Type | Status |
| --------------------- | ---------------- | ------- | ------ |
| [`enabled`](#enabled) | `enabled: false` | boolean | - |
| Param | Example | Type | Status |
| ----------------------------------- | ----------------------- | ------- | ------ |
| [`enabled`](#enabled) | `enabled: false` | boolean | - |
| [`syncHostStyles`](#syncHostStyles) | `syncHostStyles: false` | boolean | - |

##### `enabled`

Render the Puck preview within iframe. Defaults to `true`.

Disabling iframes will also disable [viewports](#viewports).

##### `syncHostStyles`

Sync the host styles to the iframe. Defaults to `true`.

If this isn't working, you may need to inject styles manually using the iframe [override API](/docs/api-reference/overrides/iframe), or make use of a [plugin](/docs/extending-puck/plugins).

### `initialHistory`

Sets the undo/redo Puck history state when using the `usePuck` [history API](/docs/api-reference/functions/use-puck#history).
Expand Down
18 changes: 16 additions & 2 deletions packages/core/components/AutoFrame/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,24 @@ const CopyHostStyles = ({
children,
debug = false,
onStylesLoaded = () => null,
enabled = true,
}: {
children: ReactNode;
debug?: boolean;
onStylesLoaded?: () => void;
enabled?: boolean;
}) => {
const { document: doc, window: win } = useFrame();

useEffect(() => {
if (!win || !doc) {
return () => {};
return;
}

if (!enabled) {
onStylesLoaded();

return;
}

let elements: { original: HTMLElement; mirror: HTMLElement }[] = [];
Expand Down Expand Up @@ -303,6 +311,7 @@ export type AutoFrameProps = {
debug?: boolean;
id?: string;
onStylesLoaded?: () => void;
syncHostStyles?: boolean;
};

type AutoFrameContext = {
Expand All @@ -320,6 +329,7 @@ function AutoFrame({
debug,
id,
onStylesLoaded,
syncHostStyles = true,
...props
}: AutoFrameProps) {
const [loaded, setLoaded] = useState(false);
Expand Down Expand Up @@ -351,7 +361,11 @@ function AutoFrame({
>
<autoFrameContext.Provider value={ctx}>
{loaded && mountTarget && (
<CopyHostStyles debug={debug} onStylesLoaded={onStylesLoaded}>
<CopyHostStyles
debug={debug}
onStylesLoaded={onStylesLoaded}
enabled={syncHostStyles}
>
{createPortal(children, mountTarget)}
</CopyHostStyles>
)}
Expand Down
1 change: 1 addition & 0 deletions packages/core/components/Puck/components/Preview/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export const Preview = ({ id = "puck-preview" }: { id?: string }) => {
onStylesLoaded={() => {
setStatus("READY");
}}
syncHostStyles={iframe.syncHostStyles}
>
<autoFrameContext.Consumer>
{({ document }) => {
Expand Down
10 changes: 7 additions & 3 deletions packages/core/components/Puck/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,7 @@ export function Puck<
headerTitle,
headerPath,
viewports = defaultViewports,
iframe = {
enabled: true,
},
iframe: _iframe,
dnd,
initialHistory: _initialHistory,
}: {
Expand Down Expand Up @@ -121,6 +119,12 @@ export function Puck<
};
initialHistory?: InitialHistory;
}) {
const iframe: IframeConfig = {
enabled: true,
syncHostStyles: true,
..._iframe,
};

const [generatedAppState] = useState<AppState<UserData>>(() => {
const initial = { ...defaultAppState.ui, ...initialUi };

Expand Down
1 change: 1 addition & 0 deletions packages/core/types/API/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export type Permissions = {

export type IframeConfig = {
enabled?: boolean;
syncHostStyles?: boolean;
};

export type OnAction<UserData extends Data = Data> = (
Expand Down

0 comments on commit 112e9bc

Please sign in to comment.