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 1, 2024
1 parent 6d43ba0 commit 8d701fc
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 8 deletions.
1 change: 1 addition & 0 deletions apps/demo/app/[...puckPath]/client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export function Client({ path, isEdit }: { path: string; isEdit: boolean }) {
<Puck<UserConfig>
config={config}
data={data}
iframe={{ syncHostStyles: false }}
onPublish={async (data: Data) => {
localStorage.setItem(key, JSON.stringify(data));
}}
Expand Down
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 @@ -76,9 +76,7 @@ export function Puck<UserConfig extends Config = Config>({
headerTitle,
headerPath,
viewports = defaultViewports,
iframe = {
enabled: true,
},
iframe: _iframe,
dnd,
initialHistory,
}: {
Expand Down Expand Up @@ -119,6 +117,12 @@ export function Puck<UserConfig extends Config = Config>({
createReducer<UserConfig>({ config, record: historyStore.record, onAction })
);

const iframe: IframeConfig = {
enabled: true,
syncHostStyles: true,
..._iframe,
};

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

Expand Down
1 change: 1 addition & 0 deletions packages/core/types/IframeConfig.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export type IframeConfig = {
enabled?: boolean;
syncHostStyles?: boolean;
};

0 comments on commit 8d701fc

Please sign in to comment.