|
| 1 | +import React, { useEffect, useId, useRef, useState } from 'react'; |
| 2 | +import { createPortal } from 'react-dom'; |
| 3 | +import './styles.css'; |
| 4 | + |
| 5 | +type CreemEmbeddedCheckoutProps = { |
| 6 | + checkoutUrl: string; |
| 7 | +}; |
| 8 | + |
| 9 | +const CREEM_ORIGIN = 'https://www.creem.io'; |
| 10 | + |
| 11 | +export default function CreemEmbeddedCheckout({ |
| 12 | + checkoutUrl, |
| 13 | +}: CreemEmbeddedCheckoutProps) { |
| 14 | + const [isOpen, setIsOpen] = useState(false); |
| 15 | + const [shouldLoadCheckout, setShouldLoadCheckout] = useState(false); |
| 16 | + const titleId = useId(); |
| 17 | + const closeButtonRef = useRef<HTMLButtonElement>(null); |
| 18 | + const iframeRef = useRef<HTMLIFrameElement>(null); |
| 19 | + const modalContentRef = useRef<HTMLDivElement>(null); |
| 20 | + |
| 21 | + const loadCheckout = () => { |
| 22 | + setShouldLoadCheckout(true); |
| 23 | + }; |
| 24 | + |
| 25 | + const openCheckout = () => { |
| 26 | + loadCheckout(); |
| 27 | + setIsOpen(true); |
| 28 | + }; |
| 29 | + |
| 30 | + useEffect(() => { |
| 31 | + if (document.querySelector(`link[rel="preconnect"][href="${CREEM_ORIGIN}"]`)) { |
| 32 | + return; |
| 33 | + } |
| 34 | + |
| 35 | + const link = document.createElement('link'); |
| 36 | + link.href = CREEM_ORIGIN; |
| 37 | + link.rel = 'preconnect'; |
| 38 | + link.crossOrigin = 'anonymous'; |
| 39 | + document.head.appendChild(link); |
| 40 | + }, []); |
| 41 | + |
| 42 | + useEffect(() => { |
| 43 | + if (!isOpen) { |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + const previousOverflow = document.body.style.overflow; |
| 48 | + const previousActiveElement = document.activeElement; |
| 49 | + const getFocusableElements = () => |
| 50 | + Array.from( |
| 51 | + modalContentRef.current?.querySelectorAll<HTMLElement>( |
| 52 | + 'button, iframe, [data-focus-sentinel]', |
| 53 | + ) ?? [], |
| 54 | + ).filter( |
| 55 | + (element) => |
| 56 | + !element.hasAttribute('disabled') && |
| 57 | + element.getAttribute('aria-hidden') !== 'true', |
| 58 | + ); |
| 59 | + |
| 60 | + const onKeyDown = (event: KeyboardEvent) => { |
| 61 | + if (event.key === 'Escape') { |
| 62 | + setIsOpen(false); |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + if (event.key !== 'Tab') { |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + if ( |
| 71 | + event.shiftKey && |
| 72 | + document.activeElement === closeButtonRef.current |
| 73 | + ) { |
| 74 | + event.preventDefault(); |
| 75 | + iframeRef.current?.focus(); |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + const focusableElements = getFocusableElements(); |
| 80 | + const firstElement = focusableElements[0]; |
| 81 | + const lastElement = focusableElements[focusableElements.length - 1]; |
| 82 | + |
| 83 | + if (!firstElement || !lastElement) { |
| 84 | + event.preventDefault(); |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + if (event.shiftKey && document.activeElement === firstElement) { |
| 89 | + event.preventDefault(); |
| 90 | + lastElement.focus(); |
| 91 | + return; |
| 92 | + } |
| 93 | + |
| 94 | + if (!event.shiftKey && document.activeElement === lastElement) { |
| 95 | + event.preventDefault(); |
| 96 | + firstElement.focus(); |
| 97 | + } |
| 98 | + }; |
| 99 | + |
| 100 | + document.addEventListener('keydown', onKeyDown); |
| 101 | + document.body.style.overflow = 'hidden'; |
| 102 | + closeButtonRef.current?.focus(); |
| 103 | + |
| 104 | + return () => { |
| 105 | + document.removeEventListener('keydown', onKeyDown); |
| 106 | + document.body.style.overflow = previousOverflow; |
| 107 | + |
| 108 | + if (previousActiveElement instanceof HTMLElement) { |
| 109 | + previousActiveElement.focus(); |
| 110 | + } |
| 111 | + }; |
| 112 | + }, [isOpen]); |
| 113 | + |
| 114 | + return ( |
| 115 | + <section className="creem-embed" aria-labelledby="creem-embed-title"> |
| 116 | + <div className="creem-embed__header"> |
| 117 | + <div> |
| 118 | + <p className="creem-embed__eyebrow">Universal Sign In license</p> |
| 119 | + <h2 id="creem-embed-title">Ready to install Universal Sign In?</h2> |
| 120 | + <p> |
| 121 | + Buy a license, then use the private npm registry setup below to add |
| 122 | + the package to your app. |
| 123 | + </p> |
| 124 | + </div> |
| 125 | + <button |
| 126 | + className="button button--primary creem-embed__button" |
| 127 | + type="button" |
| 128 | + onClick={openCheckout} |
| 129 | + onFocus={loadCheckout} |
| 130 | + onPointerEnter={loadCheckout} |
| 131 | + > |
| 132 | + Buy license |
| 133 | + </button> |
| 134 | + </div> |
| 135 | + {shouldLoadCheckout && |
| 136 | + createPortal( |
| 137 | + <div |
| 138 | + className={`creem-embed-modal ${ |
| 139 | + isOpen ? '' : 'creem-embed-modal--preload' |
| 140 | + }`} |
| 141 | + role={isOpen ? 'dialog' : undefined} |
| 142 | + aria-modal={isOpen ? 'true' : undefined} |
| 143 | + aria-labelledby={isOpen ? titleId : undefined} |
| 144 | + aria-hidden={!isOpen} |
| 145 | + > |
| 146 | + <button |
| 147 | + className="creem-embed-modal__backdrop" |
| 148 | + type="button" |
| 149 | + aria-label="Close embedded checkout" |
| 150 | + onClick={() => setIsOpen(false)} |
| 151 | + /> |
| 152 | + <div className="creem-embed-modal__content" ref={modalContentRef}> |
| 153 | + <div className="creem-embed-modal__header"> |
| 154 | + <div> |
| 155 | + <p className="creem-embed-modal__eyebrow">Secure checkout</p> |
| 156 | + <h2 id={titleId}>Complete your purchase</h2> |
| 157 | + </div> |
| 158 | + <button |
| 159 | + className="creem-embed-modal__close" |
| 160 | + ref={closeButtonRef} |
| 161 | + type="button" |
| 162 | + aria-label="Close embedded checkout" |
| 163 | + onClick={() => setIsOpen(false)} |
| 164 | + /> |
| 165 | + </div> |
| 166 | + <span |
| 167 | + className="creem-embed-modal__sentinel" |
| 168 | + data-focus-sentinel |
| 169 | + tabIndex={0} |
| 170 | + onFocus={() => iframeRef.current?.focus()} |
| 171 | + /> |
| 172 | + <iframe |
| 173 | + className="creem-embed-modal__frame" |
| 174 | + ref={iframeRef} |
| 175 | + src={checkoutUrl} |
| 176 | + title="Universal Sign In checkout" |
| 177 | + allow="payment *; publickey-credentials-get *" |
| 178 | + /> |
| 179 | + <span |
| 180 | + className="creem-embed-modal__sentinel" |
| 181 | + data-focus-sentinel |
| 182 | + tabIndex={0} |
| 183 | + onFocus={() => closeButtonRef.current?.focus()} |
| 184 | + /> |
| 185 | + </div> |
| 186 | + </div>, |
| 187 | + document.body, |
| 188 | + )} |
| 189 | + </section> |
| 190 | + ); |
| 191 | +} |
0 commit comments