Skip to content

Commit

Permalink
clients/packages/checkout: prevent init() from wiring the click eve…
Browse files Browse the repository at this point in the history
…nt listener several times

For example, in case of React strict mode!
  • Loading branch information
frankie567 committed Oct 23, 2024
1 parent 7fe2e83 commit 4bb3bb3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
5 changes: 5 additions & 0 deletions clients/.changeset/fuzzy-maps-smoke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@polar-sh/checkout': patch
---

prevent `init()` from wiring the click event listener several times
39 changes: 25 additions & 14 deletions clients/packages/checkout/src/embed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class EmbedCheckout {
private iframe: HTMLIFrameElement
private backdrop: HTMLDivElement

constructor(iframe: HTMLIFrameElement, backdrop: HTMLDivElement) {
public constructor(iframe: HTMLIFrameElement, backdrop: HTMLDivElement) {
this.iframe = iframe
this.backdrop = backdrop
}
Expand All @@ -49,7 +49,7 @@ class EmbedCheckout {
* Send a embed checkout event to the parent window.
* @param message
*/
static postMessage(message: EmbedCheckoutMessage): void {
public static postMessage(message: EmbedCheckoutMessage): void {
window.parent.postMessage({ ...message, type: POLAR_CHECKOUT_EVENT }, '*')
}

Expand All @@ -62,7 +62,7 @@ class EmbedCheckout {
* @returns A promise that resolves to an instance of EmbedCheckout.
* The promise resolves when the embedded checkout is fully loaded.
*/
static async create(
public static async create(
url: string,
theme?: 'light' | 'dark',
): Promise<EmbedCheckout> {
Expand Down Expand Up @@ -167,19 +167,17 @@ class EmbedCheckout {
* <a href="https://buy.polar.sh/polar_cl_123" data-polar-checkout data-polar-checkout-theme="dark">Checkout</a>
* ```
*/
static init(): void {
public static init(): void {
const checkoutElements = document.querySelectorAll('[data-polar-checkout]')
checkoutElements.forEach((checkoutElement) => {
checkoutElement.addEventListener('click', (e) => {
e.preventDefault()
const url =
checkoutElement.getAttribute('href') ||
(checkoutElement.getAttribute('data-polar-checkout') as string)
const theme = checkoutElement.getAttribute(
'data-polar-checkout-theme',
) as 'light' | 'dark' | undefined
EmbedCheckout.create(url, theme)
})
checkoutElement.removeEventListener(
'click',
EmbedCheckout.checkoutElementClickHandler,
)
checkoutElement.addEventListener(
'click',
EmbedCheckout.checkoutElementClickHandler,
)
})
}

Expand All @@ -191,6 +189,19 @@ class EmbedCheckout {
document.body.removeChild(this.backdrop)
document.body.classList.remove('polar-no-scroll')
}

private static async checkoutElementClickHandler(e: Event) {
e.preventDefault()
const checkoutElement = e.target as HTMLElement
const url =
checkoutElement.getAttribute('href') ||
(checkoutElement.getAttribute('data-polar-checkout') as string)
const theme = checkoutElement.getAttribute('data-polar-checkout-theme') as
| 'light'
| 'dark'
| undefined
EmbedCheckout.create(url, theme)
}
}

declare global {
Expand Down

0 comments on commit 4bb3bb3

Please sign in to comment.