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 context prop to support popup version #148

Merged
merged 1 commit into from
May 9, 2024
Merged
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
1 change: 1 addition & 0 deletions public/scripts/commit-button-example.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ <h3>Commit button example</h3>
configId: "testing-80002-0",
productUuid: "086b32-3fcd-00d1-0624-67513e85415c",
sellerId: "138",
context: "iframe",
modalMargin: "2%",
lookAndFeel: "modal",
disabled,
Expand Down
23 changes: 20 additions & 3 deletions src/components/widgets/commitButton/CommitButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ const yupStringOrNumber = yup
export function CommitButton() {
const ref = useRef<ElementRef<"div">>(null);
const [props, setProps] = useState(window.xprops ?? {});
const { renderToSelector, buttonStyle, ...commitWidgetProps } = props;
const { renderToSelector, buttonStyle, context, ...commitWidgetProps } =
props;
useEffect(() => {
if ("xprops" in window && typeof window.xprops.onProps === "function") {
window.xprops.onProps((newProps: typeof props) => {
Expand Down Expand Up @@ -77,6 +78,18 @@ export function CommitButton() {
}
return {} as typeof buttonStyleValidated;
}, [buttonStyle]);
const validatedContext = useMemo(() => {
if (!context) {
return "iframe";
}

if (yup.mixed().oneOf(["iframe", "popup"]).isValidSync(context)) {
return context;
}
throw new Error(
`"context" must be either "iframe" or "popup", "${context}" given`
);
}, [context]);
return (
<>
<GlobalStyle />
Expand All @@ -91,13 +104,17 @@ export function CommitButton() {
CommitWidgetModal({ ...commitWidgetProps, modalMargin }).renderTo(
window.parent,
renderToSelector || "body",
"iframe"
validatedContext
);
}}
onTaglineClick={() => {
PurchaseOverviewModal({
modalMargin
}).renderTo(window.parent, renderToSelector || "body", "iframe");
}).renderTo(
window.parent,
renderToSelector || "body",
validatedContext
);
}}
/>
</>
Expand Down
Loading