From 9a53a29b1f421cdea190b6175dec437ec6d76a39 Mon Sep 17 00:00:00 2001 From: fragosti Date: Fri, 16 Nov 2018 15:41:21 -0800 Subject: feat: allow integrator to disable pushstate logic --- packages/instant/src/index.umd.ts | 54 +++++++++++++++++++++------------------ 1 file changed, 29 insertions(+), 25 deletions(-) (limited to 'packages/instant/src') diff --git a/packages/instant/src/index.umd.ts b/packages/instant/src/index.umd.ts index fda21478f..381c9094b 100644 --- a/packages/instant/src/index.umd.ts +++ b/packages/instant/src/index.umd.ts @@ -8,7 +8,7 @@ import { assert } from './util/assert'; import { util } from './util/util'; export interface ZeroExInstantConfig extends ZeroExInstantOverlayProps { - shouldUseHistoryApi?: boolean; + shouldDisablePushToHistory?: boolean; } export const render = (config: ZeroExInstantConfig, selector: string = DEFAULT_ZERO_EX_CONTAINER_SELECTOR) => { @@ -50,36 +50,40 @@ export const render = (config: ZeroExInstantConfig, selector: string = DEFAULT_Z injectedDiv.setAttribute('id', INJECTED_DIV_ID); injectedDiv.setAttribute('class', INJECTED_DIV_CLASS); appendTo.appendChild(injectedDiv); + const close = () => appendTo.removeChild(injectedDiv); const instantOverlayProps = { ...config, - onClose: () => window.history.back(), + // If we are using the history API, just go back to close + onClose: () => (config.shouldDisablePushToHistory ? close() : window.history.back()), }; ReactDOM.render(React.createElement(ZeroExInstantOverlay, instantOverlayProps), injectedDiv); - const close = () => appendTo.removeChild(injectedDiv); return close; }; - // Before we render, push to history saying that instant is showing for this part of the history. - window.history.pushState({ zeroExInstantShowing: true }, '0x Instant'); - let closeInstant = renderInstant(); + if (config.shouldDisablePushToHistory) { + renderInstant(); + } else { + // Before we render, push to history saying that instant is showing for this part of the history. + window.history.pushState({ zeroExInstantShowing: true }, '0x Instant'); + let closeInstant = renderInstant(); - let prevOnPopState = util.boundNoop; - if (window.onpopstate) { - prevOnPopState = window.onpopstate.bind(window); - } - window.onpopstate = (e: PopStateEvent) => { - // Don't override integrators handler. - prevOnPopState(e); - // e.state represents the new state - if (e.state && e.state.zeroExInstantShowing) { - // The user pressed fowards, so re-render instant. - closeInstant = renderInstant(); - } else { - // User pressed back, so close instant. - closeInstant(); - delete window.onpopstate; - if (!_.isUndefined(config.onClose)) { - config.onClose(); - } + let prevOnPopState = util.boundNoop; + if (window.onpopstate) { + prevOnPopState = window.onpopstate.bind(window); } - }; + window.onpopstate = (e: PopStateEvent) => { + // Don't override integrators handler. + prevOnPopState(e); + // e.state represents the new state + if (e.state && e.state.zeroExInstantShowing) { + // The user pressed fowards, so re-render instant. + closeInstant = renderInstant(); + } else { + // User pressed back, so close instant. + closeInstant(); + if (!_.isUndefined(config.onClose)) { + config.onClose(); + } + } + }; + } }; -- cgit v1.2.3