-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.tsx
216 lines (201 loc) · 7.49 KB
/
index.tsx
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
import React from 'react';
import { ViewProps, StyleProp, StyleSheet, ViewStyle, Modal as ReactModal, Platform, SafeAreaView, View, ScrollView } from 'react-native';
import { styleReferenceBreaker } from '../../helpers';
import { Overlay } from '../Overlay';
import CloseIcon from '@carbon/icons/es/close/20';
import { modalPresentations } from '../../constants/constants';
import { getColor } from '../../styles/colors';
import { Text } from '../Text';
import { Button } from '../Button';
import WebView from 'react-native-webview';
import type { WebViewSource } from 'react-native-webview/lib/WebViewTypes';
import { defaultText } from '../../constants/defaultText';
import { zIndexes } from '../../styles/z-index';
import { BottomSafeAreaColorOverride } from '../BottomSafeAreaColorOverride';
export type DocumentViewerSource = string | WebViewSource;
export type DocumentViewerProps = {
/** Title of text document */
title: string;
/** Content to render (supports HTML (text or URL) and plain text) */
source?: DocumentViewerSource;
/** Content to render as React Nodes. If set source is not used. Content is automatically wrapped in a ScrollView. */
sourceNode?: React.ReactNode;
/** Force no scroll view for sourceNode */
disableScrollView?: boolean;
/** Callback when the view is dismissed (if not set will not render close option) */
onDismiss?: () => void;
/** Disable padding (useful for loading websites) */
disableContainerPadding?: boolean;
/** On dismiss accessibility or visible text (iOS is visible, Android is accessibility). Defaults to ENGLISH "Done" */
dismissText?: string;
/** Force the view (mostly for teting) to a specific platform */
forceView?: 'ios' | 'android';
/** Navigation footer to load in the document viewer (used mostly by other flows) */
navigationFooter?: React.ReactNode;
/** Render children component into the document viewer. This is useful for rendering a modal within here. */
renderChildComponent?: React.ReactNode;
/** Style to set on the item */
style?: StyleProp<ViewStyle>;
/** Direct props to set on the React Native component (including iOS and Android specific props). Most use cases should not need this. */
componentProps?: ViewProps;
};
/**
* Document viewer mostly for Legal content (like Privacy Policy, Terms and Conditions...).
* But can also be used for web login flows or any popup overlay flow.
*
* If source is a STRING the text will be rendered as plain text. For custom styling create an HTML file with proper styling per guidelines and use WebViewSource.
*/
export class DocumentViewer extends React.Component<DocumentViewerProps> {
private get androidViewType(): boolean {
const { forceView } = this.props;
if (forceView) {
return forceView === 'android';
} else {
return Platform.OS === 'android';
}
}
private get styles() {
return StyleSheet.create({
modal: {
zIndex: zIndexes.document,
},
safeAreaWrapper: {
position: 'relative',
flexGrow: 1,
paddingBottom: 1,
marginBottom: 1,
},
wrapper: {
flexGrow: 1,
},
blurBackground: {
zIndex: zIndexes.behind,
position: 'absolute',
top: 0,
right: 0,
left: 0,
bottom: 0,
flex: 1,
},
baseHeader: {
height: 48,
borderBottomWidth: 1,
backgroundColor: getColor('layer01'),
borderBottomColor: getColor('borderSubtle01'),
borderTopRightRadius: 16,
borderTopLeftRadius: 16,
position: 'relative',
flexDirection: 'row',
},
textAndroid: {
paddingTop: 10,
paddingLeft: 16,
flex: 1,
paddingRight: 20,
},
textIos: {
paddingTop: 13,
textAlign: 'center',
position: 'absolute',
top: 0,
right: 0,
left: 0,
bottom: 0,
zIndex: zIndexes.base,
paddingLeft: 90,
paddingRight: 90,
},
headerActionAndroid: {
alignSelf: 'flex-start',
},
headerActionIos: {
alignSelf: 'flex-start',
zIndex: zIndexes.baseOver,
paddingRight: 16,
},
contentContainer: {
backgroundColor: getColor('background'),
padding: 16,
paddingTop: 32,
paddingBottom: 0,
},
noScrollView: {
flex: 1,
},
webView: {
backgroundColor: 'transparent',
flex: 1,
},
contentView: {
backgroundColor: getColor('background'),
flexGrow: 1,
flex: 1,
},
});
}
private get headerBar(): React.ReactNode {
const { title, dismissText, onDismiss } = this.props;
const baseStyle = styleReferenceBreaker(this.styles.baseHeader);
if (this.androidViewType) {
baseStyle.borderTopRightRadius = undefined;
baseStyle.borderTopLeftRadius = undefined;
} else {
baseStyle.marginTop = 8;
baseStyle.flexDirection = 'row-reverse';
}
return (
<View style={baseStyle}>
{!!onDismiss && <Button textType="heading-compact-02" kind="ghost" overrideColor={this.androidViewType ? getColor('iconPrimary') : undefined} style={this.androidViewType ? this.styles.headerActionAndroid : this.styles.headerActionIos} iconOnlyMode={this.androidViewType} icon={this.androidViewType ? CloseIcon : undefined} text={dismissText || defaultText.done} onPress={onDismiss} />}
<Text style={this.androidViewType ? this.styles.textAndroid : this.styles.textIos} type={this.androidViewType ? 'heading-03' : 'heading-compact-02'} text={title} breakMode="tail" />
</View>
);
}
private get mainView(): React.ReactNode {
const { source, disableContainerPadding, sourceNode, disableScrollView } = this.props;
const containerStyle = styleReferenceBreaker(this.styles.contentContainer);
if (disableContainerPadding) {
containerStyle.paddingRight = 0;
containerStyle.paddingTop = 0;
containerStyle.paddingLeft = 0;
containerStyle.paddingBottom = 0;
}
if (sourceNode) {
if (disableScrollView) {
return <View style={styleReferenceBreaker(this.styles.noScrollView, containerStyle)}>{sourceNode}</View>;
}
return (
<ScrollView style={this.styles.contentView} contentContainerStyle={containerStyle}>
{sourceNode}
</ScrollView>
);
}
if (typeof source === 'string') {
return (
<ScrollView style={this.styles.contentView} contentContainerStyle={containerStyle}>
<Text text={source} />
</ScrollView>
);
} else if (source) {
return <WebView source={source} style={this.styles.webView} containerStyle={containerStyle} />;
} else {
return null;
}
}
render(): React.ReactNode {
const { componentProps, style, navigationFooter, renderChildComponent } = this.props;
return (
<ReactModal style={this.styles.modal} supportedOrientations={modalPresentations} transparent={true} animationType="slide">
<Overlay style={this.styles.blurBackground} />
<BottomSafeAreaColorOverride color={navigationFooter ? getColor('layer01') : getColor('background')} />
<SafeAreaView style={this.styles.safeAreaWrapper}>
<View style={styleReferenceBreaker(this.styles.wrapper, style)} {...(componentProps || {})}>
{this.headerBar}
{this.mainView}
</View>
{!!navigationFooter && navigationFooter}
</SafeAreaView>
{!!renderChildComponent && renderChildComponent}
</ReactModal>
);
}
}