Skip to content

Commit 7621dab

Browse files
committed
Modal -> Portal
1 parent ae17d4f commit 7621dab

5 files changed

Lines changed: 46 additions & 39 deletions

File tree

src/Metro/Spans.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import React, {
66
import styled from 'styled-components'
77
import { Point } from 'leaflet'
88

9-
import Modal from 'components/Modal'
9+
import Portal from 'components/Portal'
1010
import Bezier from 'components/primitives/Bezier'
1111

1212
import * as math from 'utils/math'
@@ -209,7 +209,7 @@ const Spans = ({
209209
})}
210210
</PathsOuter>
211211

212-
<Modal
212+
<Portal
213213
tagName="g"
214214
modalRoot={pathsInnerWrapper}
215215
>
@@ -219,7 +219,7 @@ const Spans = ({
219219
strokeWidth: `${innerStrokeWidth}px`,
220220
})}
221221
/>
222-
</Modal>
222+
</Portal>
223223
</>
224224
)
225225
}

src/Metro/Transfers.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
minBy,
1313
} from 'lodash'
1414

15-
import Modal from 'components/Modal'
15+
import Portal from 'components/Portal'
1616
import TransferReact from 'components/Transfer'
1717

1818
import { getCircumcenter } from 'utils/math'
@@ -201,7 +201,7 @@ const Transfers = ({
201201
)
202202
})}
203203
</TransfersOuter>
204-
<Modal
204+
<Portal
205205
tagName="g"
206206
modalRoot={transfersInnerWrapper}
207207
>
@@ -212,7 +212,7 @@ const Transfers = ({
212212
strokeWidth: `${innerStrokeWidth}px`,
213213
})}
214214
/>
215-
</Modal>
215+
</Portal>
216216
</>
217217
)
218218
}

src/components/Platform.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import comparisonLevel from 'utils/comparisonLevel'
88
import Circle from './primitives/Circle'
99
import Stadium from './primitives/Stadium'
1010

11-
import Modal from './Modal'
11+
import Portal from './Portal'
1212

1313
interface Props {
1414
position: Point | Point[],
@@ -69,7 +69,7 @@ class PlatformReact extends Component<Props> {
6969
display={platforms.every(p => p.type === 'dummy') ? 'none' : undefined}
7070
/>
7171
{dummyParent &&
72-
<Modal
72+
<Portal
7373
tagName="g"
7474
modalRoot={dummyParent}
7575
>
@@ -78,7 +78,7 @@ class PlatformReact extends Component<Props> {
7878
onMouseOver={this.onMouseOver}
7979
onMouseOut={onMouseOut}
8080
/>
81-
</Modal>
81+
</Portal>
8282
}
8383
</>
8484
)
Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
import React, { PureComponent } from 'react'
1+
import {
2+
useRef,
3+
useEffect,
4+
useMemo,
5+
memo,
6+
FC,
7+
} from 'react'
8+
29
import ReactDOM from 'react-dom'
310
import htmlTags from 'html-tags'
411

@@ -12,16 +19,19 @@ interface Props {
1219
modalRoot: Element,
1320
}
1421

15-
class Modal extends PureComponent<Props> {
16-
el: HTMLElement
22+
const Portal: FC<Props> = ({
23+
tagName,
24+
modalRoot,
25+
children,
26+
}) => {
27+
const el = useMemo(() => {
28+
const ns = htmlTags.includes(tagName) && tagName !== 'svg' ? HTML_NAMESPACE : SVG_NAMESPACE
29+
return document.createElementNS(ns, tagName) as HTMLElement
30+
}, [tagName, modalRoot])
1731

18-
constructor(props) {
19-
super(props)
20-
const ns = htmlTags.includes(props.tagName) && props.tagName !== 'svg' ? HTML_NAMESPACE : SVG_NAMESPACE
21-
this.el = document.createElementNS(ns, props.tagName) as HTMLElement
22-
}
32+
const elRef = useRef<Element>(el)
2333

24-
componentDidMount() {
34+
useEffect(() => {
2535
// The portal element is inserted in the DOM tree after
2636
// the Modal's children are mounted, meaning that children
2737
// will be mounted on a detached DOM node. If a child
@@ -30,19 +40,16 @@ class Modal extends PureComponent<Props> {
3040
// DOM node, or uses 'autoFocus' in a descendant, add
3141
// state to Modal and only render the children when Modal
3242
// is inserted in the DOM tree.
33-
this.props.modalRoot.appendChild(this.el)
34-
}
35-
36-
componentWillUnmount() {
37-
this.props.modalRoot.removeChild(this.el)
38-
}
39-
40-
render() {
41-
return ReactDOM.createPortal(
42-
this.props.children,
43-
this.el, // this.props.modalRoot is possible
44-
)
45-
}
43+
modalRoot.appendChild(el)
44+
return () => {
45+
modalRoot.removeChild(el)
46+
}
47+
}, [])
48+
49+
return ReactDOM.createPortal(
50+
children,
51+
elRef.current!, // this.props.modalRoot is possible
52+
)
4653
}
4754

48-
export default Modal
55+
export default memo(Portal) as typeof Portal

src/components/Transfer/index.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import Platform from '../../network/Platform'
1212
import Line from '../primitives/Line'
1313
import Arc from '../primitives/Arc'
1414

15-
import Modal from '../Modal'
15+
import Portal from '../Portal'
1616

1717
import Gradient from './Gradient'
1818

@@ -86,7 +86,7 @@ const TransferReact = ({
8686
})}
8787
/>
8888
{innerParent && transfer.type !== 'osi' &&
89-
<Modal
89+
<Portal
9090
tagName="g"
9191
modalRoot={innerParent}
9292
>
@@ -96,10 +96,10 @@ const TransferReact = ({
9696
b={newEnd}
9797
third={third}
9898
/>
99-
</Modal>
99+
</Portal>
100100
}
101101
{defs &&
102-
<Modal
102+
<Portal
103103
tagName="g"
104104
modalRoot={defs}
105105
>
@@ -111,10 +111,10 @@ const TransferReact = ({
111111
endColor={getPlatformColor(transfer.target)}
112112
fullCircleRadius={fullCircleRadius}
113113
/>
114-
</Modal>
114+
</Portal>
115115
}
116116
{dummyParent &&
117-
<Modal
117+
<Portal
118118
tagName="g"
119119
modalRoot={dummyParent}
120120
>
@@ -126,7 +126,7 @@ const TransferReact = ({
126126
onMouseOver={onMouseOverMem}
127127
onMouseOut={onMouseOut}
128128
/>
129-
</Modal>
129+
</Portal>
130130
}
131131
</>
132132
)

0 commit comments

Comments
 (0)