1- import {
2- createBrowserHistory ,
3- History ,
4- Location ,
5- UnregisterCallback ,
6- } from 'history' ;
71import * as React from 'react' ;
82
93import { HistoryModalProps } from './HistoryModal' ;
@@ -18,112 +12,92 @@ class HistoryModal extends React.PureComponent<
1812 HistoryModalState
1913> {
2014 public static defaultProps = {
21- hash : 'mo' ,
22- useHistory : false ,
15+ hash : '#modal-open' ,
2316 } ;
17+ public initialHref : string | null = null ;
2418
25- public unlisten : UnregisterCallback | null = null ;
26- public history : History | null = null ;
2719 public state = {
2820 isVisible : false ,
2921 } ;
3022
3123 public componentDidMount = ( ) => {
32- const { onRequestClose, useHistory } = this . props ;
33-
34- if ( ! useHistory ) return ;
35-
36- this . history = createBrowserHistory ( ) ;
24+ this . initialHref = window . location . href ;
3725
3826 /**
39- * For back button, if use clicks back button it should be interpreted as a close request
27+ * For back button, if use clicks back button it should be interpreted as a close request.
4028 */
41- this . unlisten = this . history . listen ( location => {
42- if ( ! this . isHistoryActive ( location ) ) {
43- this . setState ( ( ) => ( { isVisible : false } ) ) ;
44- if ( onRequestClose ) {
45- onRequestClose ( ) ;
46- }
47- }
48- } ) ;
29+ window . addEventListener ( 'hashchange' , this . handleHashChange , false ) ;
4930 } ;
5031
5132 public componentDidUpdate = ( prevProps : HistoryModalProps ) => {
52- const { visible, useHistory } = this . props ;
53-
54- if ( ! useHistory ) return ;
33+ const { visible } = this . props ;
5534
5635 if (
5736 visible &&
5837 visible !== prevProps . visible &&
59- this . history &&
60- ! this . isHistoryActive ( this . history . location )
38+ history &&
39+ ! this . isHistoryActive ( )
6140 ) {
6241 this . activateHistory ( ) ;
6342 }
6443 } ;
6544
6645 public componentWillUnmount ( ) {
67- if ( this . unlisten ) {
68- this . unlisten ( ) ;
69- }
46+ window . removeEventListener ( 'hashchange' , this . handleHashChange , false ) ;
7047 }
7148
72- public handleRequestClose = ( ) => {
49+ public handleHashChange = ( ) => {
7350 const { onRequestClose } = this . props ;
7451
7552 if ( onRequestClose ) {
7653 onRequestClose ( ) ;
7754 }
7855
79- if ( this . history ) {
80- this . history . replace ( { hash : '' } ) ;
81- }
56+ this . setState ( ( ) => ( { isVisible : false } ) ) ;
8257 } ;
8358
84- public activateHistory = ( ) => {
85- const { hash, qs } = this . props ;
86- if ( ! this . history ) return ;
59+ public handleRequestClose = ( ) => {
60+ const { onRequestClose } = this . props ;
8761
88- if ( qs && qs !== null ) {
89- this . history . push ( { search : qs } ) ;
90- } else if ( hash && hash !== null ) {
91- this . history . push ( { hash } ) ;
62+ if ( onRequestClose ) {
63+ onRequestClose ( ) ;
9264 }
9365
94- this . setState ( ( ) => ( { isVisible : true } ) ) ;
66+ history . replaceState ( null , '' , this . initialHref ) ;
67+ this . setState ( ( ) => ( { isVisible : false } ) ) ;
9568 } ;
9669
97- public isHistoryActive = ( location : Location ) => {
98- const { hash, qs } = this . props ;
70+ public activateHistory = ( ) => {
71+ const { hash } = this . props ;
9972
100- if ( qs && qs !== null ) {
101- return location . search . includes ( qs ) ;
73+ this . setState ( ( ) => ( { isVisible : true } ) ) ;
74+ if ( hash && hash !== null ) {
75+ history . pushState ( null , '' , this . initialHref + hash ) ;
10276 }
77+ } ;
10378
104- return hash === location . hash . substring ( 1 ) ;
79+ public isHistoryActive = ( ) => {
80+ const { hash } = this . props ;
81+
82+ return hash === window . location . hash . substring ( 1 ) ;
10583 } ;
10684
10785 public render ( ) {
108- const {
109- hash,
110- qs,
111- useHistory,
112- visible,
113- onRequestClose,
114- ...modalProps
115- } = this . props ;
86+ const { hash, ...modalProps } = this . props ;
11687 const { isVisible } = this . state ;
11788
11889 return (
11990 < ModalBase
12091 { ...modalProps }
121- // If Modal uses history, use local state and handlers, otherwise forward modal props
122- onRequestClose = { useHistory ? this . handleRequestClose : onRequestClose }
123- visible = { useHistory ? isVisible : visible }
92+ onRequestClose = { this . handleRequestClose }
93+ visible = { isVisible }
12494 />
12595 ) ;
12696 }
12797}
12898
129- export default HistoryModal ;
99+ export default ( { useHistory, ...props } : HistoryModalProps ) => {
100+ if ( useHistory ) return < HistoryModal { ...props } /> ;
101+
102+ return < ModalBase { ...props } /> ;
103+ } ;
0 commit comments