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(puck context):added iframe ref to the puck context #652

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions packages/core/components/AutoFrame/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from "react";
import hash from "object-hash";
import { createPortal } from "react-dom";
import { appContext } from "../Puck/context";

const styleSelector = 'style, link[rel="stylesheet"]';

Expand Down Expand Up @@ -326,6 +327,7 @@ function AutoFrame({
const [ctx, setCtx] = useState<AutoFrameContext>({});
const ref = useRef<HTMLIFrameElement>(null);
const [mountTarget, setMountTarget] = useState<HTMLElement | null>();
const { setIframe, iframe } = useContext(appContext);

useEffect(() => {
if (ref.current) {
Expand All @@ -334,6 +336,11 @@ function AutoFrame({
window: ref.current.contentWindow || undefined,
});

setIframe({
...iframe,
ref: ref,
});

setMountTarget(ref.current.contentDocument?.getElementById("frame-root"));
}
}, [ref, loaded]);
Expand Down
2 changes: 2 additions & 0 deletions packages/core/components/Puck/context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export type AppContext<
status: Status;
setStatus: (status: Status) => void;
iframe: IframeConfig;
setIframe: (iframe: IframeConfig) => void;
safariFallbackMode?: boolean;
globalPermissions?: Partial<Permissions>;
selectedItem?: G["UserData"]["content"][0];
Expand Down Expand Up @@ -105,6 +106,7 @@ const defaultContext: AppContext = {
status: "LOADING",
setStatus: () => null,
iframe: {},
setIframe: () => null,
safariFallbackMode: false,
globalPermissions: {},
getPermissions: () => ({}),
Expand Down
7 changes: 4 additions & 3 deletions packages/core/components/Puck/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,17 @@ export function Puck<
headerTitle?: string;
headerPath?: string;
viewports?: Viewports;
iframe?: IframeConfig;
iframe?: Omit<IframeConfig, "ref">;
dnd?: {
disableAutoScroll?: boolean;
};
initialHistory?: InitialHistory;
}) {
const iframe: IframeConfig = {
const [iframe, setIframe] = useState<IframeConfig>({
enabled: true,
waitForStyles: true,
..._iframe,
};
});

const [generatedAppState] = useState<G["UserAppState"]>(() => {
const initial = { ...defaultAppState.ui, ...initialUi };
Expand Down Expand Up @@ -415,6 +415,7 @@ export function Puck<
history,
viewports,
iframe,
setIframe,
globalPermissions: {
delete: true,
drag: true,
Expand Down
3 changes: 3 additions & 0 deletions packages/core/lib/use-puck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const usePuck = <UserConfig extends Config = Config>() => {
config,
history,
dispatch,
iframe,
selectedItem: currentItem,
getPermissions,
refreshPermissions,
Expand All @@ -30,5 +31,7 @@ export const usePuck = <UserConfig extends Config = Config>() => {
historyStore: history.historyStore,
},
selectedItem: currentItem || null,
/** @description Iframe ref if enabled otherwise returns undefined */
iframeRef: iframe.ref,
};
};
1 change: 1 addition & 0 deletions packages/core/types/API/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export type Permissions = {
export type IframeConfig = {
enabled?: boolean;
waitForStyles?: boolean;
ref?: React.RefObject<HTMLIFrameElement>;
};

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