-
Notifications
You must be signed in to change notification settings - Fork 24k
/
PointerEventsExample.js
468 lines (441 loc) · 13.6 KB
/
PointerEventsExample.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
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @flow
*/
'use strict';
const React = require('react');
const {StyleSheet, Text, View} = require('react-native');
type ExampleBoxComponentProps = $ReadOnly<{|
onLog: (msg: string) => void,
|}>;
type ExampleBoxProps = $ReadOnly<{|
Component: React.ComponentType<ExampleBoxComponentProps>,
|}>;
type ExampleBoxState = $ReadOnly<{|
log: string[],
|}>;
class ExampleBox extends React.Component<ExampleBoxProps, ExampleBoxState> {
state: ExampleBoxState = {
log: [],
};
handleLog = (msg: string) => {
// $FlowFixMe
this.state.log = this.state.log.concat([msg]);
};
flushReactChanges = () => {
this.forceUpdate();
};
/**
* Capture phase of bubbling to append separator before any of the bubbling
* happens.
*/
handleTouchCapture = () => {
// $FlowFixMe
this.state.log = this.state.log.concat(['---']);
};
render(): React.Node {
const {Component} = this.props;
return (
<View>
<View
onTouchEndCapture={this.handleTouchCapture}
onTouchStart={this.flushReactChanges}>
<Component onLog={this.handleLog} />
</View>
<View style={styles.logBox}>
<DemoText style={styles.logText}>
{this.state.log.join('\n')}
</DemoText>
</View>
</View>
);
}
}
class NoneExample extends React.Component<$FlowFixMeProps> {
render(): React.Node {
return (
<View
onTouchStart={() => this.props.onLog('A unspecified touched')}
style={styles.box}>
<DemoText style={styles.text}>A: unspecified</DemoText>
<View
pointerEvents="none"
onTouchStart={() => this.props.onLog('B none touched')}
style={[styles.box, styles.boxPassedThrough]}>
<DemoText style={[styles.text, styles.textPassedThrough]}>
B: none
</DemoText>
<View
onTouchStart={() => this.props.onLog('C unspecified touched')}
style={[styles.box, styles.boxPassedThrough]}>
<DemoText style={[styles.text, styles.textPassedThrough]}>
C: unspecified
</DemoText>
</View>
</View>
</View>
);
}
}
class NoneStyleExample extends React.Component<$FlowFixMeProps> {
render(): React.Node {
return (
<View
onTouchStart={() => this.props.onLog('A unspecified touched')}
style={styles.box}>
<DemoText style={styles.text}>A: unspecified</DemoText>
<View
onTouchStart={() => this.props.onLog('B none touched')}
style={[
styles.box,
styles.boxPassedThrough,
styles.pointerEventNone,
]}>
<DemoText style={[styles.text, styles.textPassedThrough]}>
B: none
</DemoText>
<View
onTouchStart={() => this.props.onLog('C unspecified touched')}
style={[styles.box, styles.boxPassedThrough]}>
<DemoText style={[styles.text, styles.textPassedThrough]}>
C: unspecified
</DemoText>
</View>
</View>
</View>
);
}
}
/**
* Special demo text that makes itself untouchable so that it doesn't destroy
* the experiment and confuse the output.
*/
class DemoText extends React.Component<$FlowFixMeProps> {
render(): React.Node {
return (
<View pointerEvents="none">
<Text style={this.props.style}>{this.props.children}</Text>
</View>
);
}
}
class BoxNoneExample extends React.Component<$FlowFixMeProps> {
render(): React.Node {
return (
<View
onTouchStart={() => this.props.onLog('A unspecified touched')}
style={styles.box}>
<DemoText style={styles.text}>A: unspecified</DemoText>
<View
pointerEvents="box-none"
onTouchStart={() => this.props.onLog('B box-none touched')}
style={[styles.box, styles.boxPassedThrough]}>
<DemoText style={[styles.text, styles.textPassedThrough]}>
B: box-none
</DemoText>
<View
onTouchStart={() => this.props.onLog('C unspecified touched')}
style={styles.box}>
<DemoText style={styles.text}>C: unspecified</DemoText>
</View>
<View
pointerEvents="auto"
onTouchStart={() =>
this.props.onLog('C explicitly unspecified touched')
}
style={[styles.box]}>
<DemoText style={[styles.text]}>C: explicitly unspecified</DemoText>
</View>
</View>
</View>
);
}
}
class BoxNoneStyleExample extends React.Component<$FlowFixMeProps> {
render(): React.Node {
return (
<View
onTouchStart={() => this.props.onLog('A unspecified touched')}
style={styles.box}>
<DemoText style={styles.text}>A: unspecified</DemoText>
<View
onTouchStart={() => this.props.onLog('B box-none touched')}
style={[
styles.box,
styles.boxPassedThrough,
styles.pointerEventBoxNone,
]}>
<DemoText style={[styles.text, styles.textPassedThrough]}>
B: box-none
</DemoText>
<View
onTouchStart={() => this.props.onLog('C unspecified touched')}
style={styles.box}>
<DemoText style={styles.text}>C: unspecified</DemoText>
</View>
<View
onTouchStart={() =>
this.props.onLog('C explicitly unspecified touched')
}
style={[styles.box, styles.pointerEventAuto]}>
<DemoText style={[styles.text]}>C: explicitly unspecified</DemoText>
</View>
</View>
</View>
);
}
}
class BoxOnlyExample extends React.Component<$FlowFixMeProps> {
render(): React.Node {
return (
<View
onTouchStart={() => this.props.onLog('A unspecified touched')}
style={styles.box}>
<DemoText style={styles.text}>A: unspecified</DemoText>
<View
pointerEvents="box-only"
onTouchStart={() => this.props.onLog('B box-only touched')}
style={styles.box}>
<DemoText style={styles.text}>B: box-only</DemoText>
<View
onTouchStart={() => this.props.onLog('C unspecified touched')}
style={[styles.box, styles.boxPassedThrough]}>
<DemoText style={[styles.text, styles.textPassedThrough]}>
C: unspecified
</DemoText>
</View>
<View
pointerEvents="auto"
onTouchStart={() =>
this.props.onLog('C explicitly unspecified touched')
}
style={[styles.box, styles.boxPassedThrough]}>
<DemoText style={[styles.text, styles.textPassedThrough]}>
C: explicitly unspecified
</DemoText>
</View>
</View>
</View>
);
}
}
class BoxOnlyStyleExample extends React.Component<$FlowFixMeProps> {
render(): React.Node {
return (
<View
onTouchStart={() => this.props.onLog('A unspecified touched')}
style={styles.box}>
<DemoText style={styles.text}>A: unspecified</DemoText>
<View
onTouchStart={() => this.props.onLog('B box-only touched')}
style={[styles.box, styles.pointerEventBoxOnly]}>
<DemoText style={styles.text}>B: box-only</DemoText>
<View
onTouchStart={() => this.props.onLog('C unspecified touched')}
style={[styles.box, styles.boxPassedThrough]}>
<DemoText style={[styles.text, styles.textPassedThrough]}>
C: unspecified
</DemoText>
</View>
<View
onTouchStart={() =>
this.props.onLog('C explicitly unspecified touched')
}
style={[
styles.box,
styles.boxPassedThrough,
styles.pointerEventAuto,
]}>
<DemoText style={[styles.text, styles.textPassedThrough]}>
C: explicitly unspecified
</DemoText>
</View>
</View>
</View>
);
}
}
type OverflowExampleProps = $ReadOnly<{|
overflow: 'hidden' | 'visible',
onLog: (msg: string) => void,
|}>;
class OverflowExample extends React.Component<OverflowExampleProps> {
render(): React.Node {
const {overflow} = this.props;
return (
<View
onTouchStart={() => this.props.onLog(`A overflow ${overflow} touched`)}
style={[
styles.box,
styles.boxWithOverflowSet,
{overflow: this.props.overflow},
]}>
<DemoText style={styles.text}>A: overflow: {overflow}</DemoText>
<View
onTouchStart={() => this.props.onLog('B overflowing touched')}
style={[styles.box, styles.boxOverflowing]}>
<DemoText style={styles.text}>B: overflowing</DemoText>
</View>
<View
onTouchStart={() => this.props.onLog('C fully outside touched')}
style={[styles.box, styles.boxFullyOutside]}>
<DemoText style={styles.text}>C: fully outside</DemoText>
<View
onTouchStart={() =>
this.props.onLog('D fully outside child touched')
}
style={[styles.box, styles.boxFullyOutsideChild]}>
<DemoText style={styles.text}>D: child of fully outside</DemoText>
</View>
</View>
</View>
);
}
}
class OverflowVisibleExample extends React.Component<ExampleBoxComponentProps> {
render(): React.Node {
return <OverflowExample {...this.props} overflow="visible" />;
}
}
class OverflowHiddenExample extends React.Component<ExampleBoxComponentProps> {
render(): React.Node {
return <OverflowExample {...this.props} overflow="hidden" />;
}
}
type ExampleClass = {
Component: React.ComponentType<any>,
title: string,
description: string,
...
};
const exampleClasses: Array<ExampleClass> = [
{
Component: NoneExample,
title: '`none`',
description:
'`none` causes touch events on the container and its child components to pass through to the parent container.',
},
{
Component: NoneStyleExample,
title: '`none` style',
description:
'`none` causes touch events on the container and its child components to pass through to the parent container.',
},
{
Component: BoxNoneExample,
title: '`box-none`',
description:
'`box-none` causes touch events on the container to pass through and will only detect touch events on its child components.',
},
{
Component: BoxNoneStyleExample,
title: '`box-none` style',
description:
'`box-none` causes touch events on the container to pass through and will only detect touch events on its child components.',
},
{
Component: BoxOnlyExample,
title: '`box-only`',
description:
"`box-only` causes touch events on the container's child components to pass through and will only detect touch events on the container itself.",
},
{
Component: BoxOnlyStyleExample,
title: '`box-only` style',
description:
"`box-only` causes touch events on the container's child components to pass through and will only detect touch events on the container itself.",
},
{
Component: OverflowVisibleExample,
title: '`overflow: visible`',
description:
'`overflow: visible` style should allow subelements that are outside of the parent box to be touchable. Tapping the parts of Box B outside Box A should print "B touched" and "A touched", and tapping Box C should also print "C touched" and "A touched".',
},
{
Component: OverflowHiddenExample,
title: '`overflow: hidden`',
description:
'`overflow: hidden` style should only allow subelements within the parent box to be touchable. Tapping just below Box A (where Box B would otherwise extend if it weren\'t cut off) should not trigger any touches or messages. Touching Box D (inside the bounds) should print "D touched" and "A touched".',
},
];
const infoToExample = (info: ExampleClass) => {
return {
title: info.title,
description: info.description,
render: function () {
return <ExampleBox key={info.title} Component={info.Component} />;
},
};
};
const styles = StyleSheet.create({
text: {
fontSize: 10,
color: '#5577cc',
},
textPassedThrough: {
color: '#88aadd',
},
box: {
backgroundColor: '#aaccff',
borderWidth: 1,
borderColor: '#7799cc',
padding: 10,
margin: 5,
},
boxPassedThrough: {
borderColor: '#99bbee',
},
boxWithOverflowSet: {
paddingBottom: 40,
marginBottom: 50,
},
boxOverflowing: {
position: 'absolute',
top: 30,
paddingBottom: 40,
},
boxFullyOutside: {
position: 'absolute',
left: 200,
top: 65,
},
boxFullyOutsideChild: {
position: 'absolute',
left: 0,
top: -65,
width: 100,
},
logText: {
fontSize: 9,
},
logBox: {
padding: 20,
margin: 10,
borderWidth: 0.5,
borderColor: '#f0f0f0',
backgroundColor: '#f9f9f9',
},
pointerEventBoxNone: {
pointerEvents: 'box-none',
},
pointerEventBoxOnly: {
pointerEvents: 'box-only',
},
pointerEventNone: {
pointerEvents: 'none',
},
pointerEventAuto: {
pointerEvents: 'auto',
},
});
exports.framework = 'React';
exports.title = 'Pointer Events';
exports.category = 'Basic';
exports.description = ('Demonstrates the use of the pointerEvents prop of a ' +
'View to control how touches should be handled.': string);
exports.examples = (exampleClasses.map(infoToExample): Array<any>);