Skip to content

Commit 195d4e7

Browse files
committed
fix: modal without history dependency
1 parent b5e34c0 commit 195d4e7

4 files changed

Lines changed: 41 additions & 75 deletions

File tree

package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
"@expo/vector-icons": "9.0.0",
3939
"exenv": "1.2.2",
4040
"focus-trap": "4.0.2",
41-
"history": "4.7.2",
4241
"react": "16.7.0",
4342
"react-art": "16.7.0",
4443
"react-dom": "16.7.0",
@@ -61,7 +60,6 @@
6160
"@semantic-release/release-notes-generator": "7.1.4",
6261
"@types/exenv": "1.2.0",
6362
"@types/expo__vector-icons": "6.2.3",
64-
"@types/history": "4.7.2",
6563
"@types/jest": "23.3.12",
6664
"@types/react": "16.7.13",
6765
"@types/react-dom": "16.0.11",

src/components/Modal/HistoryModal.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ export interface HistoryModalProps extends ModalBaseProps {
88
/** (Web) Only works when `useHistory` is true. Hash string to append to location */
99
hash?: string | null;
1010
/** (Web) Only works when `useHistory` is true. Same as hash, but using query string. Query string will take precedence over hash @default null */
11-
qs?: string | null;
1211
}
1312

1413
export default ModalBase;
Lines changed: 35 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
import {
2-
createBrowserHistory,
3-
History,
4-
Location,
5-
UnregisterCallback,
6-
} from 'history';
71
import * as React from 'react';
82

93
import { 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+
};

yarn.lock

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,7 +1065,7 @@
10651065
resolved "https://registry.yarnpkg.com/@emotion/weak-memoize/-/weak-memoize-0.2.2.tgz#63985d3d8b02530e0869962f4da09142ee8e200e"
10661066
integrity sha512-n/VQ4mbfr81aqkx/XmVicOLjviMuy02eenSdJY33SVA7S2J42EU0P1H0mOogfYedb3wXA0d/LVtBrgTSm04WEA==
10671067

1068-
"@expo/vector-icons@^9.0.0":
1068+
"@expo/vector-icons@9.0.0":
10691069
version "9.0.0"
10701070
resolved "https://registry.yarnpkg.com/@expo/vector-icons/-/vector-icons-9.0.0.tgz#7f18e21d3edc8b99b76d7d1b8e26b212393e08b3"
10711071
integrity sha512-k5ndrW3oueW5jRDLt3o8iXKmiU+CvvCZPewOvxY7eRMivi8hIr6TkW6tMCGE1vS5fwmXffIkIpKGZkSbX7TxwA==
@@ -1463,11 +1463,6 @@
14631463
"@types/react-native" "*"
14641464
"@types/react-native-vector-icons" "*"
14651465

1466-
"@types/history@^4.7.2":
1467-
version "4.7.2"
1468-
resolved "https://registry.yarnpkg.com/@types/history/-/history-4.7.2.tgz#0e670ea254d559241b6eeb3894f8754991e73220"
1469-
integrity sha512-ui3WwXmjTaY73fOQ3/m3nnajU/Orhi6cEu5rzX+BrAAJxa3eITXZ5ch9suPqtM03OWhAHhPSyBGCN4UKoxO20Q==
1470-
14711466
"@types/jest@23.3.12":
14721467
version "23.3.12"
14731468
resolved "https://registry.yarnpkg.com/@types/jest/-/jest-23.3.12.tgz#7e0ced251fa94c3bc2d1023d4b84b2992fa06376"
@@ -4895,7 +4890,7 @@ execa@^1.0.0:
48954890
signal-exit "^3.0.0"
48964891
strip-eof "^1.0.0"
48974892

4898-
exenv@^1.2.2:
4893+
exenv@1.2.2:
48994894
version "1.2.2"
49004895
resolved "https://registry.yarnpkg.com/exenv/-/exenv-1.2.2.tgz#2ae78e85d9894158670b03d47bec1f03bd91bb9d"
49014896
integrity sha1-KueOhdmJQVhnCwPUe+wfA72Ru50=
@@ -10410,10 +10405,10 @@ react-sizes@^1.0.4:
1041010405
lodash.throttle "^4.1.1"
1041110406
prop-types "^15.6.0"
1041210407

10413-
react-spring@^7.0.0:
10414-
version "7.2.2"
10415-
resolved "https://registry.yarnpkg.com/react-spring/-/react-spring-7.2.2.tgz#dbfb54591574754ed551935ddfc8189da4a73022"
10416-
integrity sha512-0VlSF148jLfOVHuHr1+G5RZY2YB7lwbyeeBtcGMkh4jb7V1/3JMmwwA1vdd2eTgQQsntl/Et6UMgZ80cFbVk6w==
10408+
react-spring@7.0.0:
10409+
version "7.0.0"
10410+
resolved "https://registry.yarnpkg.com/react-spring/-/react-spring-7.0.0.tgz#a19151672176a63938be53b488f57e4bd6c8c900"
10411+
integrity sha512-HvWtCDalaHhQYn6VqYV8GHWI8ZuWCC9AzOpMq1f7GQcsbnbHzPwaoYETT5IFMSxne8n1PN4BE19W98ifkZU4RA==
1041710412
dependencies:
1041810413
"@babel/runtime" "^7.0.0"
1041910414

0 commit comments

Comments
 (0)