diff --git a/landing/siwe.react.js b/landing/siwe.react.js --- a/landing/siwe.react.js +++ b/landing/siwe.react.js @@ -57,10 +57,18 @@ } function onWalletConnectModalUpdate(update) { - postMessageToNativeWebView({ - type: 'walletconnect_modal_update', - ...update, - }); + // Conditional is only here for Flow + if (update.state === 'closed') { + postMessageToNativeWebView({ + type: 'walletconnect_modal_update', + ...update, + }); + } else { + postMessageToNativeWebView({ + type: 'walletconnect_modal_update', + ...update, + }); + } } async function signInWithEthereum( diff --git a/landing/walletconnect-hooks.js b/landing/walletconnect-hooks.js --- a/landing/walletconnect-hooks.js +++ b/landing/walletconnect-hooks.js @@ -3,44 +3,117 @@ import invariant from 'invariant'; import * as React from 'react'; -type WalletConnectModalUpdate = { - +state: 'open' | 'closed', -}; +type WalletConnectModalUpdate = + | { +state: 'closed' } + | { +state: 'open', +height: number }; function useMonitorForWalletConnectModal( callback: WalletConnectModalUpdate => mixed, ) { - const newModalAppeared = React.useCallback( - mutationList => { - for (const mutation of mutationList) { - for (const addedNode of mutation.addedNodes) { - if ( - addedNode instanceof HTMLElement && - addedNode.id === 'walletconnect-wrapper' - ) { - callback({ state: 'open' }); - } - } - for (const addedNode of mutation.removedNodes) { - if ( - addedNode instanceof HTMLElement && - addedNode.id === 'walletconnect-wrapper' - ) { - callback({ state: 'closed' }); - } + const [wcShadowRoot, setWCShadowRoot] = React.useState(); + const [wcResizableContainer, setWCResizableContainer] = React.useState(); + + const newShadowRootAppeared = React.useCallback(mutationList => { + for (const mutation of mutationList) { + for (const addedNode of mutation.addedNodes) { + if ( + addedNode instanceof HTMLElement && + addedNode.localName === 'w3m-modal' && + addedNode.shadowRoot + ) { + const { shadowRoot } = addedNode; + // We actually are looking to track an element inside w3m-modal, + // rather than w3m-modal itself. Normally we could pass subtree: true + // to observer.observe, but this doesn't appear to work with a "shadow + // root", so instead we implement a second-layer MutationObserver once + // we see the shadow root. + setWCShadowRoot(shadowRoot); } } - }, - [callback], - ); + } + }, []); React.useEffect(() => { - const observer = new MutationObserver(newModalAppeared); + const observer = new MutationObserver(newShadowRootAppeared); invariant(document.body, 'document.body should be set'); observer.observe(document.body, { childList: true }); return () => { observer.disconnect(); }; - }, [newModalAppeared]); + }, [newShadowRootAppeared]); + + const newModalAppeared = React.useCallback(mutationList => { + // We pass subtree: true to the MutationObserver that calls this function. + // This means we monitor for changes all through the subtree, but if a child + // subtree is added, we only get the root of the subtree in addedNodes. As + // such we need to recursively scan the subtree to try and find the node + // that we're looking for. + const nodesToInspect = new Set(); + const addNodesToInspect = node => { + nodesToInspect.add(node); + for (const childNode of node.childNodes) { + addNodesToInspect(childNode); + } + }; + for (const mutation of mutationList) { + for (const addedNode of mutation.addedNodes) { + addNodesToInspect(addedNode); + } + } + for (const node of nodesToInspect) { + if ( + node instanceof HTMLElement && + node.localName === 'div' && + node.className === 'w3m-container' + ) { + setWCResizableContainer(node); + } + } + }, []); + + React.useEffect(() => { + if (!wcShadowRoot) { + return undefined; + } + + // We can actually skip the MutationObserver below if by the time the React + // state of wcShadowRoot is set, the subtree has already appeared. We store + // wcShadowRoot as React state so that we can properly "clean up" the + // associated observer below in an effect, but it means we have to deal with + // the associated delay in updated React state, which is unpredictable. + const modal = wcShadowRoot.getElementById('w3m-modal'); + if (modal) { + const container = wcShadowRoot.querySelector('div.w3m-container'); + if (container) { + setWCResizableContainer(container); + return undefined; + } + } + + const observer = new MutationObserver(newModalAppeared); + observer.observe(wcShadowRoot, { childList: true, subtree: true }); + return () => { + observer.disconnect(); + }; + }, [wcShadowRoot, newModalAppeared]); + + React.useEffect(() => { + if (!wcResizableContainer) { + return undefined; + } + const resizeObserver = new ResizeObserver(entries => { + const lastEntry = entries[entries.length - 1]; + const { height } = lastEntry.contentRect; + if (height) { + callback({ state: 'open', height }); + } else { + callback({ state: 'closed' }); + } + }); + resizeObserver.observe(wcResizableContainer); + return () => { + resizeObserver.disconnect(); + }; + }, [wcResizableContainer, callback]); } export { useMonitorForWalletConnectModal }; diff --git a/lib/types/siwe-types.js b/lib/types/siwe-types.js --- a/lib/types/siwe-types.js +++ b/lib/types/siwe-types.js @@ -49,7 +49,12 @@ } | { +type: 'walletconnect_modal_update', - +state: 'open' | 'closed', + +state: 'open', + +height: number, + } + | { + +type: 'walletconnect_modal_update', + +state: 'closed', }; export type SIWEMessage = {