Skip to content

Commit 29e8364

Browse files
committed
fix: simplify modal
1 parent 195d4e7 commit 29e8364

1 file changed

Lines changed: 11 additions & 32 deletions

File tree

src/components/Modal/HistoryModal.web.tsx

Lines changed: 11 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,12 @@ import * as React from 'react';
33
import { HistoryModalProps } from './HistoryModal';
44
import ModalBase from './ModalBase';
55

6-
export interface HistoryModalState {
7-
isVisible: boolean;
8-
}
9-
10-
class HistoryModal extends React.PureComponent<
11-
HistoryModalProps,
12-
HistoryModalState
13-
> {
6+
class HistoryModal extends React.PureComponent<HistoryModalProps> {
147
public static defaultProps = {
158
hash: '#modal-open',
169
};
17-
public initialHref: string | null = null;
1810

19-
public state = {
20-
isVisible: false,
21-
};
11+
public initialHref: string | null = null;
2212

2313
public componentDidMount = () => {
2414
this.initialHref = window.location.href;
@@ -32,13 +22,8 @@ class HistoryModal extends React.PureComponent<
3222
public componentDidUpdate = (prevProps: HistoryModalProps) => {
3323
const { visible } = this.props;
3424

35-
if (
36-
visible &&
37-
visible !== prevProps.visible &&
38-
history &&
39-
!this.isHistoryActive()
40-
) {
41-
this.activateHistory();
25+
if (visible && visible !== prevProps.visible && !this.isHistoryActive()) {
26+
this.pushHistory();
4227
}
4328
};
4429

@@ -52,10 +37,11 @@ class HistoryModal extends React.PureComponent<
5237
if (onRequestClose) {
5338
onRequestClose();
5439
}
55-
56-
this.setState(() => ({ isVisible: false }));
5740
};
5841

42+
/**
43+
* Relay the callback and also clean up the hash state
44+
*/
5945
public handleRequestClose = () => {
6046
const { onRequestClose } = this.props;
6147

@@ -64,34 +50,27 @@ class HistoryModal extends React.PureComponent<
6450
}
6551

6652
history.replaceState(null, '', this.initialHref);
67-
this.setState(() => ({ isVisible: false }));
6853
};
6954

70-
public activateHistory = () => {
55+
public pushHistory = () => {
7156
const { hash } = this.props;
7257

73-
this.setState(() => ({ isVisible: true }));
74-
if (hash && hash !== null) {
58+
if (hash) {
7559
history.pushState(null, '', this.initialHref + hash);
7660
}
7761
};
7862

7963
public isHistoryActive = () => {
8064
const { hash } = this.props;
8165

82-
return hash === window.location.hash.substring(1);
66+
return hash === window.location.hash;
8367
};
8468

8569
public render() {
8670
const { hash, ...modalProps } = this.props;
87-
const { isVisible } = this.state;
8871

8972
return (
90-
<ModalBase
91-
{...modalProps}
92-
onRequestClose={this.handleRequestClose}
93-
visible={isVisible}
94-
/>
73+
<ModalBase {...modalProps} onRequestClose={this.handleRequestClose} />
9574
);
9675
}
9776
}

0 commit comments

Comments
 (0)