Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add syncHostStyles API to disable iframe style sync #593

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This API won't work until we can isolate the Puck styles from the required styles. Setting syncHostStyles to false will prevent any Preview styles from loading, making the preview unusable.

### `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
Loading