Unmount component when closing

This commit is contained in:
Steve Klebanoff
2019-03-21 14:43:31 +01:00
parent 91500501ce
commit 50f5002b71

View File

@@ -65,21 +65,34 @@ const validateInstantRenderConfig = (config: ZeroExInstantConfig, selector: stri
assert.isString('selector', selector); assert.isString('selector', selector);
}; };
let injectedDiv: HTMLDivElement | undefined;
let parentElement: Element | undefined;
export const unrender = () => {
if (!injectedDiv) {
return;
}
ReactDOM.unmountComponentAtNode(injectedDiv);
if (parentElement) {
parentElement.removeChild(injectedDiv);
}
};
// Render instant and return a callback that allows you to remove it from the DOM. // Render instant and return a callback that allows you to remove it from the DOM.
const renderInstant = (config: ZeroExInstantConfig, selector: string) => { const renderInstant = (config: ZeroExInstantConfig, selector: string) => {
const appendToIfExists = document.querySelector(selector); const appendToIfExists = document.querySelector(selector);
assert.assert(!_.isNull(appendToIfExists), `Could not find div with selector: ${selector}`); assert.assert(!_.isNull(appendToIfExists), `Could not find div with selector: ${selector}`);
const appendTo = appendToIfExists as Element; parentElement = appendToIfExists as Element;
const injectedDiv = document.createElement('div'); injectedDiv = document.createElement('div');
injectedDiv.setAttribute('id', INJECTED_DIV_ID); injectedDiv.setAttribute('id', INJECTED_DIV_ID);
injectedDiv.setAttribute('class', INJECTED_DIV_CLASS); injectedDiv.setAttribute('class', INJECTED_DIV_CLASS);
appendTo.appendChild(injectedDiv); parentElement.appendChild(injectedDiv);
const closeInstant = () => { const closeInstant = () => {
analytics.trackInstantClosed(); analytics.trackInstantClosed();
if (!_.isUndefined(config.onClose)) { if (!_.isUndefined(config.onClose)) {
config.onClose(); config.onClose();
} }
appendTo.removeChild(injectedDiv); unrender();
}; };
const instantOverlayProps = { const instantOverlayProps = {
...config, ...config,