Skip to content

Commit 8d701fc

Browse files
committed
feat: add syncHostStyles API to disable iframe style sync
1 parent 6d43ba0 commit 8d701fc

File tree

6 files changed

+36
-8
lines changed

6 files changed

+36
-8
lines changed

apps/demo/app/[...puckPath]/client.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export function Client({ path, isEdit }: { path: string; isEdit: boolean }) {
1717
<Puck<UserConfig>
1818
config={config}
1919
data={data}
20+
iframe={{ syncHostStyles: false }}
2021
onPublish={async (data: Data) => {
2122
localStorage.setItem(key, JSON.stringify(data));
2223
}}

apps/docs/pages/docs/api-reference/components/puck.mdx

+10-3
Original file line numberDiff line numberDiff line change
@@ -173,16 +173,23 @@ export function Editor() {
173173

174174
#### iframe params
175175

176-
| Param | Example | Type | Status |
177-
| --------------------- | ---------------- | ------- | ------ |
178-
| [`enabled`](#enabled) | `enabled: false` | boolean | - |
176+
| Param | Example | Type | Status |
177+
| ----------------------------------- | ----------------------- | ------- | ------ |
178+
| [`enabled`](#enabled) | `enabled: false` | boolean | - |
179+
| [`syncHostStyles`](#syncHostStyles) | `syncHostStyles: false` | boolean | - |
179180

180181
##### `enabled`
181182

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

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

187+
##### `syncHostStyles`
188+
189+
Sync the host styles to the iframe. Defaults to `true`.
190+
191+
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).
192+
186193
### `initialHistory`
187194

188195
Sets the undo/redo Puck history state when using the `usePuck` [history API](/docs/api-reference/functions/use-puck#history).

packages/core/components/AutoFrame/index.tsx

+16-2
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,24 @@ const CopyHostStyles = ({
6060
children,
6161
debug = false,
6262
onStylesLoaded = () => null,
63+
enabled = true,
6364
}: {
6465
children: ReactNode;
6566
debug?: boolean;
6667
onStylesLoaded?: () => void;
68+
enabled?: boolean;
6769
}) => {
6870
const { document: doc, window: win } = useFrame();
6971

7072
useEffect(() => {
7173
if (!win || !doc) {
72-
return () => {};
74+
return;
75+
}
76+
77+
if (!enabled) {
78+
onStylesLoaded();
79+
80+
return;
7381
}
7482

7583
let elements: { original: HTMLElement; mirror: HTMLElement }[] = [];
@@ -303,6 +311,7 @@ export type AutoFrameProps = {
303311
debug?: boolean;
304312
id?: string;
305313
onStylesLoaded?: () => void;
314+
syncHostStyles?: boolean;
306315
};
307316

308317
type AutoFrameContext = {
@@ -320,6 +329,7 @@ function AutoFrame({
320329
debug,
321330
id,
322331
onStylesLoaded,
332+
syncHostStyles = true,
323333
...props
324334
}: AutoFrameProps) {
325335
const [loaded, setLoaded] = useState(false);
@@ -351,7 +361,11 @@ function AutoFrame({
351361
>
352362
<autoFrameContext.Provider value={ctx}>
353363
{loaded && mountTarget && (
354-
<CopyHostStyles debug={debug} onStylesLoaded={onStylesLoaded}>
364+
<CopyHostStyles
365+
debug={debug}
366+
onStylesLoaded={onStylesLoaded}
367+
enabled={syncHostStyles}
368+
>
355369
{createPortal(children, mountTarget)}
356370
</CopyHostStyles>
357371
)}

packages/core/components/Puck/components/Preview/index.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export const Preview = ({ id = "puck-preview" }: { id?: string }) => {
4949
onStylesLoaded={() => {
5050
setStatus("READY");
5151
}}
52+
syncHostStyles={iframe.syncHostStyles}
5253
>
5354
<autoFrameContext.Consumer>
5455
{({ document }) => {

packages/core/components/Puck/index.tsx

+7-3
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,7 @@ export function Puck<UserConfig extends Config = Config>({
7676
headerTitle,
7777
headerPath,
7878
viewports = defaultViewports,
79-
iframe = {
80-
enabled: true,
81-
},
79+
iframe: _iframe,
8280
dnd,
8381
initialHistory,
8482
}: {
@@ -119,6 +117,12 @@ export function Puck<UserConfig extends Config = Config>({
119117
createReducer<UserConfig>({ config, record: historyStore.record, onAction })
120118
);
121119

120+
const iframe: IframeConfig = {
121+
enabled: true,
122+
syncHostStyles: true,
123+
..._iframe,
124+
};
125+
122126
const [initialAppState] = useState<AppState>(() => {
123127
const initial = { ...defaultAppState.ui, ...initialUi };
124128

packages/core/types/IframeConfig.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export type IframeConfig = {
22
enabled?: boolean;
3+
syncHostStyles?: boolean;
34
};

0 commit comments

Comments
 (0)