-
Notifications
You must be signed in to change notification settings - Fork 486
Expand file tree
/
Copy pathRootView.tsx
More file actions
255 lines (213 loc) · 8.33 KB
/
RootView.tsx
File metadata and controls
255 lines (213 loc) · 8.33 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/**
* RootView.tsx
*
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT license.
*
* The top-most view that's used for proper layering or modals and popups.
*/
import * as React from 'react';
import * as RN from 'react-native';
import { SubscriptionToken } from 'subscribableevent';
import AppConfig from '../common/AppConfig';
import { Types } from '../common/Interfaces';
import UserInterface from '../native-common/UserInterface';
import Accessibility from './Accessibility';
import AccessibilityUtil from './AccessibilityUtil';
import App from './App';
import FrontLayerViewManager from './FrontLayerViewManager';
import { isEqual } from './utils/lodashMini';
import MainViewStore from './MainViewStore';
import Styles from './Styles';
// Fields should be prefixed with 'reactxp' to help avoid naming collisions.
// All fields should be removed from this.props before passing downwards.
interface BaseRootViewProps {
reactxp_rootViewId?: string;
}
interface RootViewPropsWithMainViewType extends BaseRootViewProps {
reactxp_mainViewType: string;
}
interface RootViewState {
mainView?: any;
contextWrapper?: (rootView: React.ReactElement<any>) => React.ReactElement<any>;
announcementText?: string;
}
const _styles = {
rootViewStyle: Styles.createViewStyle ({
flex: 1,
alignItems: 'stretch',
overflow: 'visible',
}),
liveRegionContainer: Styles.createViewStyle({
position: 'absolute',
opacity: 0,
top: -30,
bottom: 0,
left: 0,
right: 0,
height: 30,
}),
};
// Abstract RootView class which handles rendering, front layer view changes and announcement
// changes. Subclasses must set the mainView state value.
abstract class BaseRootView<P extends BaseRootViewProps> extends React.Component<P, RootViewState> {
private _frontLayerViewChangedSubscription: SubscriptionToken | undefined;
private _newAnnouncementEventChangedSubscription: SubscriptionToken | undefined;
private _memoryWarningEventSubscription: SubscriptionToken | undefined;
protected _mainViewProps: {};
protected _rootViewId?: string | null;
protected abstract _getPropsForMainView(): {};
constructor(props: P) {
super(props);
this._mainViewProps = this._getPropsForMainView();
}
UNSAFE_componentWillMount(): void {
this._frontLayerViewChangedSubscription = FrontLayerViewManager.event_changed.subscribe(() => {
// Setting empty state will trigger a render.
this.setState({});
});
// Update announcement text.
this._newAnnouncementEventChangedSubscription =
Accessibility.newAnnouncementReadyEvent.subscribe(announcement => {
this.setState({
announcementText: announcement,
});
});
this._memoryWarningEventSubscription = App.memoryWarningEvent.subscribe(() => {
FrontLayerViewManager.releaseCachedPopups();
this.forceUpdate();
});
}
componentDidMount(): void {
if (this._rootViewId) {
UserInterface.notifyRootViewInstanceCreated(this._rootViewId, RN.findNodeHandle(this)!);
}
}
componentWillUnmount(): void {
if (this._rootViewId) {
UserInterface.notifyRootViewInstanceDestroyed(this._rootViewId);
}
if (this._frontLayerViewChangedSubscription) {
this._frontLayerViewChangedSubscription.unsubscribe();
this._frontLayerViewChangedSubscription = undefined;
}
if (this._newAnnouncementEventChangedSubscription) {
this._newAnnouncementEventChangedSubscription.unsubscribe();
this._newAnnouncementEventChangedSubscription = undefined;
}
if (this._memoryWarningEventSubscription) {
this._memoryWarningEventSubscription.unsubscribe();
this._memoryWarningEventSubscription = undefined;
}
}
render() {
const modalLayerView = FrontLayerViewManager.getModalLayerView(this._rootViewId);
const popupLayerView = FrontLayerViewManager.getPopupLayerView(this._rootViewId);
const isActivePopup = FrontLayerViewManager.isPopupActiveFor(this._rootViewId);
const announcerView = this._renderAnnouncerView();
// When showing a modal/popup we want to hide the mainView shown behind from an accessibility
// standpoint to ensure that it won't get the focus and the screen reader's attention.
const importantForAccessibility = (modalLayerView || isActivePopup) ?
AccessibilityUtil.importantForAccessibilityToString(Types.ImportantForAccessibility.NoHideDescendants) :
undefined; // default
const content = (
<RN.Animated.View style={ _styles.rootViewStyle }>
<RN.View
style={ _styles.rootViewStyle as RN.StyleProp<RN.ViewStyle> }
importantForAccessibility={ importantForAccessibility }
>
{ this.state.mainView }
</RN.View>
{ modalLayerView }
{ popupLayerView }
{ announcerView }
</RN.Animated.View>
);
const maybeContextWrappedContent =
this.state.contextWrapper === undefined
? content
: this.state.contextWrapper(content);
return this.renderTopView(maybeContextWrappedContent);
}
protected _renderAnnouncerView(): JSX.Element {
return (
<RN.View
style={ _styles.liveRegionContainer as RN.StyleProp<RN.ViewStyle> }
accessibilityLabel={ this.state.announcementText }
accessibilityLiveRegion={ AccessibilityUtil.accessibilityLiveRegionToString(Types.AccessibilityLiveRegion.Assertive) }
/>
);
}
renderTopView(content: JSX.Element): JSX.Element {
return content;
}
}
// BaseRootView implementation that uses MainStore to set the main view.
class RootViewUsingStore extends BaseRootView<BaseRootViewProps> {
private _changeListener = this._onChange.bind(this);
constructor(props: BaseRootViewProps) {
super(props);
this.state = {
mainView: undefined,
announcementText: '',
};
}
UNSAFE_componentWillMount(): void {
super.UNSAFE_componentWillMount();
MainViewStore.subscribe(this._changeListener);
this.setState(this._getStateFromStore());
}
componentWillUnmount(): void {
super.componentWillUnmount();
MainViewStore.unsubscribe(this._changeListener);
}
private _onChange() {
this.setState(this._getStateFromStore());
}
private _getStateFromStore(): RootViewState {
let mainView = MainViewStore.getMainView();
let contextWrapper = MainViewStore.getContextWrapper();
if (mainView && !isEqual(mainView.props, this._mainViewProps)) {
mainView = React.cloneElement(mainView, this._mainViewProps);
}
return {
mainView: mainView,
contextWrapper: contextWrapper,
};
}
protected _getPropsForMainView(): {} {
const { reactxp_rootViewId, ...mainViewProps } = this.props;
return mainViewProps;
}
}
// BaseRootView implementation that uses the value in props to set main view.
class RootViewUsingProps extends BaseRootView<RootViewPropsWithMainViewType> {
constructor(props: RootViewPropsWithMainViewType) {
super(props);
if (!props.reactxp_rootViewId) {
if (AppConfig.isDevelopmentMode()) {
console.warn('Some APIs require a value for reactxp_rootViewId');
}
this._rootViewId = null;
} else {
this._rootViewId = props.reactxp_rootViewId;
}
this.state = {
mainView: React.createElement(props.reactxp_mainViewType, this._mainViewProps),
announcementText: '',
};
}
protected _getPropsForMainView(): {} {
const { reactxp_mainViewType, reactxp_rootViewId, ...mainViewProps } = this.props;
return mainViewProps;
}
}
export {
BaseRootViewProps,
RootViewPropsWithMainViewType,
RootViewState,
BaseRootView,
RootViewUsingStore as RootView,
RootViewUsingProps,
};
export default RootViewUsingStore;