-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathindex.js
132 lines (116 loc) · 3.07 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
@flow weak
*/
import React from 'react'; // peer-dependency
import mitt from 'mitt'; // DEPENDENCY #1
import PropTypes from 'prop-types'; // DEPENDENCY #2, sorta
if (!PropTypes) console.warn('<react-native-portal> no PropTypes available');
const oContextTypes = {
portalSub: PropTypes.func,
portalUnsub: PropTypes.func,
portalSet: PropTypes.func,
portalGet: PropTypes.func,
};
export class PortalProvider extends React.Component {
_emitter: *;
static childContextTypes = oContextTypes;
portals = new Map();
getChildContext() {
return {
portalSub: this.portalSub,
portalUnsub: this.portalUnsub,
portalSet: this.portalSet,
portalGet: this.portalGet,
};
}
UNSAFE_componentWillMount() {
this._emitter = new mitt();
}
componentWillUnmount() {
this._emitter = null;
}
// 변경시 통지 요청 등록
portalSub = (name, callback) => {
const emitter = this._emitter;
if (emitter) {
emitter.on(name, callback);
}
};
// 변경시 통지 요청 해제
portalUnsub = (name, callback) => {
const emitter = this._emitter;
if (emitter) {
emitter.off(name, callback);
}
};
// 변경
portalSet = (name, value) => {
this.portals.set(name, value);
if (this._emitter) {
this._emitter.emit(name);
}
};
portalGet = name => this.portals.get(name) || null;
// 변경
render() {
return this.props.children;
}
}
export class BlackPortal extends React.PureComponent {
static contextTypes = oContextTypes;
props: {
name: string,
children?: *,
};
componentDidMount() {
const { name, children } = this.props;
const { portalSet } = this.context;
portalSet && portalSet(name, children);
}
UNSAFE_componentWillReceiveProps(newProps) {
const oldProps = this.props;
const { name, children } = newProps;
const { portalSet } = this.context;
if (oldProps.children != newProps.children) {
portalSet && portalSet(name, children);
}
}
componentWillUnmount() {
const { name } = this.props;
const { portalSet } = this.context;
portalSet && portalSet(name, null);
}
render() {
const { name } = this.props;
return null;
}
}
export class WhitePortal extends React.PureComponent {
static contextTypes = oContextTypes;
props: {
name: string,
children?: *,
childrenProps?: *,
};
UNSAFE_componentWillMount() {
const { name } = this.props;
const { portalSub } = this.context;
portalSub && portalSub(name, this.forceUpdater);
}
componentWillUnmount() {
const { name } = this.props;
const { portalUnsub } = this.context;
portalUnsub && portalUnsub(name, this.forceUpdater);
}
forceUpdater = () => this.forceUpdate();
render() {
const { name, children, childrenProps } = this.props;
const { portalGet } = this.context;
const portalChildren = (portalGet && portalGet(name)) || children;
return (
(childrenProps && portalChildren
? React.cloneElement(React.Children.only(portalChildren), childrenProps)
: portalChildren) || null
);
}
}