-
Notifications
You must be signed in to change notification settings - Fork 171
Expand file tree
/
Copy pathCalloutTest.tsx
More file actions
501 lines (438 loc) · 20.6 KB
/
CalloutTest.tsx
File metadata and controls
501 lines (438 loc) · 20.6 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
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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
import * as React from 'react';
import type { KeyboardMetrics, Text as RNText } from 'react-native';
import { View, Switch, ScrollView, Platform } from 'react-native';
import { Button, Callout, Separator, Pressable, StealthButton, TextV1 as Text } from '@fluentui/react-native';
import type { IFocusable, RestoreFocusEvent, DismissBehaviors, ICalloutProps } from '@fluentui/react-native';
import { E2ECalloutTest } from './CalloutE2ETest';
import { CALLOUT_TESTPAGE } from '../../../../E2E/src/Callout/consts';
import { MenuPicker } from '../Common/MenuPicker';
import { fluentTesterStyles } from '../Common/styles';
import type { TestSection, PlatformStatus } from '../Test';
import { Test } from '../Test';
const StandardCallout: React.FunctionComponent = () => {
const [showStandardCallout, setShowStandardCallout] = React.useState(false);
const [isStandardCalloutVisible, setIsStandardCalloutVisible] = React.useState(false);
const [openCalloutOnHoverAnchor, setOpenCalloutOnHoverAnchor] = React.useState(false);
const [calloutHovered, setCalloutHovered] = React.useState(false);
const [shouldSetInitialFocus, setShouldSetInitialFocus] = React.useState(true);
const onInitialFocusChange = React.useCallback((value: boolean) => setShouldSetInitialFocus(value), []);
const [customRestoreFocus, setCustomRestoreFocus] = React.useState(true);
const onRestoreFocusChange = React.useCallback((value) => setCustomRestoreFocus(value), []);
const [isBeakVisible, setIsBeakVisible] = React.useState(false);
const onIsBeakVisibleChange = React.useCallback((value) => setIsBeakVisible(value), []);
const [preventDismissOnKeyDown, setPreventDismissOnKeyDown] = React.useState(false);
const [preventDismissOnClickOutside, setPreventDismissOnClickOutside] = React.useState(false);
const [calloutDismissBehaviors, setDismissBehaviors] = React.useState<DismissBehaviors[]>([]);
const onPreventDismissOnKeyDownChange = React.useCallback(
(value) => {
setPreventDismissOnKeyDown(value);
if (value) {
setDismissBehaviors(calloutDismissBehaviors.concat('preventDismissOnKeyDown'));
} else {
const newDismissBehaviors = calloutDismissBehaviors.filter((value) => {
value != 'preventDismissOnKeyDown';
});
setDismissBehaviors(newDismissBehaviors);
}
},
[calloutDismissBehaviors],
);
const onPreventDismissOnClickOutsideChange = React.useCallback(
(value) => {
setPreventDismissOnClickOutside(value);
if (value) {
setDismissBehaviors(calloutDismissBehaviors.concat('preventDismissOnClickOutside'));
} else {
const newDismissBehaviors = calloutDismissBehaviors.filter((value) => {
value != 'preventDismissOnClickOutside';
});
setDismissBehaviors(newDismissBehaviors);
}
},
[calloutDismissBehaviors],
);
const textRef = React.useRef<RNText>(null);
const textRefInner1 = React.useRef<RNText>(null);
const textRefInner2 = React.useRef<RNText>(null);
const redTargetRef = React.useRef<View>(null);
const blueTargetRef = React.useRef<View>(null);
const greenTargetRef = React.useRef<View>(null);
const decoyBtn1Ref = React.useRef<IFocusable>(null);
const decoyBtn2Ref = React.useRef<IFocusable>(null);
const [anchorRefIndex, setAnchorRefIndex] = React.useState(0);
const anchorRefCycle = [redTargetRef, greenTargetRef, blueTargetRef, textRef, textRefInner1, textRefInner2];
const [anchorRef, setAnchorRef] = React.useState<React.RefObject<View> | React.RefObject<RNText> | string | undefined>(anchorRefCycle[0]);
const [hoveredTargetsCount, setHoveredTargetsCount] = React.useState(0);
const [displayCountHoveredTargets, setDisplayCountHoveredTargets] = React.useState(0);
const anchorRefsInfo: { hoverCount: number; isCurrentAnchor: boolean[] } = React.useMemo(() => {
return {
hoverCount: displayCountHoveredTargets,
isCurrentAnchor: [
openCalloutOnHoverAnchor && isStandardCalloutVisible && anchorRef == redTargetRef,
openCalloutOnHoverAnchor && isStandardCalloutVisible && anchorRef == greenTargetRef,
openCalloutOnHoverAnchor && isStandardCalloutVisible && anchorRef == blueTargetRef,
],
};
}, [anchorRef, displayCountHoveredTargets, isStandardCalloutVisible, openCalloutOnHoverAnchor]);
// Use a timer to update the targets count and avoid jittery updates of counts on-screen
React.useLayoutEffect(() => {
const timer = setTimeout(() => {
setDisplayCountHoveredTargets(hoveredTargetsCount);
}, 50);
return () => clearTimeout(timer);
}, [hoveredTargetsCount]);
React.useLayoutEffect(() => {
// The callout may be dismissed by multiple mechanisms, depending on its active invocation
// mechanisms. In each of those dismissal routes, we use this function to reset the callout's
// state universally.
if (!showStandardCallout) {
setIsStandardCalloutVisible(false);
setCalloutHovered(false);
}
}, [showStandardCallout]);
const toggleShowStandardCallout = React.useCallback(() => {
setShowStandardCallout(!showStandardCallout);
}, [showStandardCallout]);
// Set up a LayoutEffect for hover callout mechanics including:
// the toggle switch for hover callout behavior
// multiple hover targets (i.e. the callout and its anchor)
// a dismissal timer for closing the callout with no targets hovered
React.useLayoutEffect(() => {
if (!openCalloutOnHoverAnchor) {
setShowStandardCallout(false);
} else if (openCalloutOnHoverAnchor && hoveredTargetsCount > 0) {
setShowStandardCallout(true);
} else {
const timer = setTimeout(() => {
setShowStandardCallout(false);
}, 2000);
return () => clearTimeout(timer);
}
return undefined;
}, [openCalloutOnHoverAnchor, hoveredTargetsCount]);
const updateCalloutTargetsHoverState = React.useCallback(
(show: boolean, hoverAnchorRef: React.MutableRefObject<View>) => {
if (hoverAnchorRef !== undefined && hoverAnchorRef !== anchorRef) {
return;
}
setHoveredTargetsCount(hoveredTargetsCount + (show ? 1 : -1));
},
[anchorRef, hoveredTargetsCount, setHoveredTargetsCount],
);
const calloutHoveredCallback = React.useCallback(
(isHovered: boolean) => {
updateCalloutTargetsHoverState(isHovered, undefined);
setCalloutHovered(isHovered);
},
[updateCalloutTargetsHoverState],
);
const toggleCalloutRef = React.useCallback(() => {
// Cycle the target ref between the RGB target views
setAnchorRefIndex((anchorRefIndex + 1) % anchorRefCycle.length);
setAnchorRef(anchorRefCycle[anchorRefIndex]);
}, [anchorRef, anchorRefIndex]);
const switchTargetRefOrRect = React.useCallback(() => {
// Switch between RGB views or a fixed anchor
setAnchorRef(anchorRef === redTargetRef || anchorRef === greenTargetRef || anchorRef == blueTargetRef ? undefined : redTargetRef);
}, [anchorRef]);
React.useEffect(() => {
if (!showStandardCallout && anchorRef === undefined) {
setAnchorRef(redTargetRef);
}
}, [anchorRef, setAnchorRef, showStandardCallout]);
const onShowStandardCallout = React.useCallback(() => {
setIsStandardCalloutVisible(true);
}, []);
const onDismissStandardCallout = React.useCallback(() => {
// setting the internal state to false will instigate unmounting the
// zombie Callout control.
setShowStandardCallout(false);
}, []);
const onRestoreFocusStandardCallout = React.useCallback(
(restoreFocusEvent: RestoreFocusEvent) => {
if (restoreFocusEvent?.nativeEvent?.containsFocus) {
decoyBtn1Ref?.current?.focus?.();
} else {
decoyBtn2Ref?.current?.focus?.();
}
},
[decoyBtn1Ref, decoyBtn2Ref],
);
const colorDefault: string = 'default';
const colorSelections: string[] = [colorDefault, 'red', 'green', 'blue'];
const menuPickerColorCollection = colorSelections.map((color) => {
return {
label: color,
value: color,
};
});
const [selectedBackgroundColor, setSelectedBackgroundColor] = React.useState<string | undefined>(undefined);
const [selectedBorderColor, setSelectedBorderColor] = React.useState<string | undefined>(undefined);
const borderWidthDefault: string = Platform.OS === 'macos' ? '0' : '1';
const borderWidthSelections: string[] = ['0', '1', '2', '4', '10'];
const menuPickerBorderWidthCollection = borderWidthSelections.map((width) => {
return {
label: width,
value: width,
};
});
const borderRadiusDefault: string = '5';
const borderRadiusSelections: string[] = ['0', '5', '7', '15'];
const menuPickerBorderRadiusCollection = borderRadiusSelections.map((width) => {
return {
label: width,
value: width,
};
});
const [selectedBorderWidth, setSelectedBorderWidth] = React.useState<string | undefined>(undefined);
const [selectedBorderRadius, setSelectedBorderRadius] = React.useState<string | undefined>(undefined);
const [showScrollViewCallout, setShowScrollViewCalout] = React.useState(false);
const [scrollviewContents, setScrollviewContents] = React.useState([1, 2, 3]);
const removeButton = React.useCallback(() => {
const tempArray = scrollviewContents;
tempArray.pop();
setScrollviewContents([...tempArray]);
}, [setScrollviewContents, scrollviewContents]);
const addButton = React.useCallback(() => {
setScrollviewContents((arr) => [...arr, 1]);
}, [setScrollviewContents]);
const calloutTargetOrAnchor: Partial<ICalloutProps> = {};
if (anchorRef) {
calloutTargetOrAnchor.target = anchorRef;
} else {
calloutTargetOrAnchor.anchorRect = { screenX: 50, screenY: 50, width: 1, height: 1 };
}
return (
<View>
<View style={{ flexDirection: 'row', paddingVertical: 5 }}>
<View style={{ flexDirection: 'column', paddingHorizontal: 5 }}>
<View style={{ flexDirection: 'row' }}>
<Switch value={openCalloutOnHoverAnchor} onValueChange={setOpenCalloutOnHoverAnchor} />
<Text>Show Callout On Hover Anchor</Text>
</View>
<View style={{ flexDirection: 'row' }}>
<Switch value={shouldSetInitialFocus} onValueChange={onInitialFocusChange} />
<Text>Set Initial Focus</Text>
</View>
<View style={{ flexDirection: 'row' }}>
<Switch value={customRestoreFocus} onValueChange={onRestoreFocusChange} />
<Text>Customize Restore Focus</Text>
</View>
<View style={{ flexDirection: 'row' }}>
<Switch value={isBeakVisible} onValueChange={onIsBeakVisibleChange} />
<Text>Beak Visible</Text>
</View>
<View style={{ flexDirection: 'row' }}>
<Switch value={preventDismissOnKeyDown} onValueChange={onPreventDismissOnKeyDownChange} />
<Text>Prevent Dismiss On Key Down</Text>
</View>
<View style={{ flexDirection: 'row' }}>
<Switch value={preventDismissOnClickOutside} onValueChange={onPreventDismissOnClickOutsideChange} />
<Text>Prevent Dismiss On Click Outside</Text>
</View>
<View style={{ flexDirection: 'row' }}>
<Switch value={showScrollViewCallout} onValueChange={setShowScrollViewCalout} />
<Text>Enable ScrollView Callout</Text>
</View>
<MenuPicker
prompt="Background Color"
selected={selectedBackgroundColor || colorDefault}
onChange={(color) => setSelectedBackgroundColor(color === colorDefault ? undefined : color)}
collection={menuPickerColorCollection}
/>
<MenuPicker
prompt="Border Color"
selected={selectedBorderColor || colorDefault}
onChange={(color) => setSelectedBorderColor(color === colorDefault ? undefined : color)}
collection={menuPickerColorCollection}
/>
<MenuPicker
prompt="Border Width"
selected={selectedBorderWidth || borderWidthDefault}
onChange={(color) => setSelectedBorderWidth(color === colorDefault ? undefined : color)}
collection={menuPickerBorderWidthCollection}
/>
<MenuPicker
prompt="Border Radius"
selected={selectedBorderRadius || borderRadiusDefault}
onChange={(radius) => setSelectedBorderRadius(radius === borderRadiusDefault ? undefined : radius)}
collection={menuPickerBorderRadiusCollection}
/>
</View>
<Separator vertical />
<View style={{ flexDirection: 'column', paddingHorizontal: 5 }}>
<Button
content={(openCalloutOnHoverAnchor ? 'Hover Color Anchor ' : 'Press Here ') + 'for Callout'}
onClick={toggleShowStandardCallout}
disabled={openCalloutOnHoverAnchor}
/>
<Text>
<Text>Visibility: </Text>
{isStandardCalloutVisible ? <Text style={{ color: 'green' }}>Visible</Text> : <Text style={{ color: 'red' }}>Not Visible</Text>}
</Text>
</View>
<Separator vertical />
<View style={{ flexDirection: 'column', paddingHorizontal: 5, maxWidth: 250 }}>
<Pressable
onHoverIn={() => updateCalloutTargetsHoverState(true, redTargetRef)}
onHoverOut={() => updateCalloutTargetsHoverState(false, redTargetRef)}
>
<View ref={redTargetRef} style={{ height: 40, width: 40, backgroundColor: 'red', padding: 5 }}>
{anchorRefsInfo.isCurrentAnchor[0] && <Text>{anchorRefsInfo.hoverCount}</Text>}
</View>
</Pressable>
<Pressable
onHoverIn={() => updateCalloutTargetsHoverState(true, greenTargetRef)}
onHoverOut={() => updateCalloutTargetsHoverState(false, greenTargetRef)}
>
<View ref={greenTargetRef} style={{ height: 40, width: 40, backgroundColor: 'green', padding: 5 }}>
{anchorRefsInfo.isCurrentAnchor[1] && <Text>{anchorRefsInfo.hoverCount}</Text>}
</View>
</Pressable>
<Pressable
onHoverIn={() => updateCalloutTargetsHoverState(true, blueTargetRef)}
onHoverOut={() => updateCalloutTargetsHoverState(false, blueTargetRef)}
>
<View ref={blueTargetRef} style={{ height: 40, width: 40, backgroundColor: 'blue', padding: 5 }}>
{anchorRefsInfo.isCurrentAnchor[2] && <Text style={{ color: 'white' }}>{anchorRefsInfo.hoverCount}</Text>}
</View>
</Pressable>
<Text componentRef={textRef}>
{'Complex'}
<Text>
{' text tree'}
<Text style={{ fontSize: 16 }} componentRef={textRefInner1}>
{' [twiceNested]'}
</Text>
{' with multiple nested text runs'}
</Text>
<Text style={{ fontSize: 20 }} componentRef={textRefInner2}>
{' [onceNested]'}
</Text>
{' and subtext runs'}
</Text>
</View>
</View>
<Separator />
<View style={{ paddingVertical: 5 }}>
<Button componentRef={decoyBtn1Ref} content="Custom reFocus w/ focus in Callout" />
<Button componentRef={decoyBtn2Ref} content="Custom reFocus w/o focus in Callout" />
</View>
{showStandardCallout && (
<Callout
{...{
doNotTakePointerCapture: openCalloutOnHoverAnchor ?? undefined,
...calloutTargetOrAnchor,
onDismiss: onDismissStandardCallout,
onShow: onShowStandardCallout,
...(customRestoreFocus && { onRestoreFocus: onRestoreFocusStandardCallout }),
accessibilityLabel: 'Standard Callout',
setInitialFocus: shouldSetInitialFocus,
isBeakVisible: isBeakVisible,
...(selectedBorderColor && { borderColor: selectedBorderColor }),
...(selectedBackgroundColor && { backgroundColor: selectedBackgroundColor }),
...(selectedBorderWidth && { borderWidth: parseInt(selectedBorderWidth) }),
...(selectedBorderRadius && { borderRadius: parseInt(selectedBorderRadius) }),
...(calloutDismissBehaviors && { dismissBehaviors: calloutDismissBehaviors }),
}}
>
<Pressable onHoverIn={() => calloutHoveredCallback(true)} onHoverOut={() => calloutHoveredCallback(false)}>
{showScrollViewCallout ? (
<View style={fluentTesterStyles.scrollViewContainer}>
<ScrollView contentContainerStyle={fluentTesterStyles.scrollViewStyle} showsVerticalScrollIndicator={true}>
<StealthButton content="click to change anchor" onClick={toggleCalloutRef} />
<StealthButton content="click to switch between anchor and rect" onClick={switchTargetRefOrRect} />
<StealthButton content="Click to add a button" style={fluentTesterStyles.testListItem} onClick={addButton} />
<StealthButton content="Click to remove a button" style={fluentTesterStyles.testListItem} onClick={removeButton} />
{scrollviewContents.map((value) => {
return <StealthButton key={value} content="Button" style={fluentTesterStyles.testListItem} />;
})}
</ScrollView>
</View>
) : (
//else
<View style={{ padding: 20, backgroundColor: calloutHovered ? 'lightgreen' : 'pink' }}>
<Button content="click to change anchor" onClick={toggleCalloutRef} />
<Button content="click to switch between anchor and rect" onClick={switchTargetRefOrRect} />
</View>
)}
</Pressable>
</Callout>
)}
</View>
);
};
const CustomCallout: React.FunctionComponent = () => {
const [showCustomizedCallout, setShowCustomizedCallout] = React.useState(false);
const [isCustomizedCalloutVisible, setIsCustomizedCalloutVisible] = React.useState(false);
const toggleShowCustomizedCallout = React.useCallback(() => {
setShowCustomizedCallout(!showCustomizedCallout);
// Unmounting a callout does not invoke onDismiss; onDismiss is only invoked
// for dismissals generated by the native app. When toggling to 'show',
// the isVisible state will be corrected to 'true' by the onShow callback.
setIsCustomizedCalloutVisible(false);
}, [showCustomizedCallout, setIsCustomizedCalloutVisible, setShowCustomizedCallout]);
const onShowCustomizedCallout = React.useCallback(() => {
setIsCustomizedCalloutVisible(true);
}, [setIsCustomizedCalloutVisible]);
const onDismissCustomizedCallout = React.useCallback(() => {
setIsCustomizedCalloutVisible(false);
// setting the internal state to false will instigate unmounting the
// zombie Callout control.
setShowCustomizedCallout(false);
}, [setIsCustomizedCalloutVisible, setShowCustomizedCallout]);
const myRect: KeyboardMetrics = { screenX: 10, screenY: 10, width: 100, height: 100 };
return (
<View>
<View style={{ flexDirection: 'column', paddingVertical: 5 }}>
<Button content="Press for Callout" onClick={toggleShowCustomizedCallout} />
<Text selectable={true}>
<Text>Visibility: </Text>
{isCustomizedCalloutVisible ? <Text style={{ color: 'green' }}>Visible</Text> : <Text style={{ color: 'red' }}>Not Visible</Text>}
</Text>
</View>
{showCustomizedCallout && (
<Callout
anchorRect={myRect}
onDismiss={onDismissCustomizedCallout}
onShow={onShowCustomizedCallout}
accessibilityLabel="Customized Callout"
accessibilityRole="alert"
accessibilityOnShowAnnouncement="Be informed that a customized callout has been opened."
>
<View style={{ padding: 20, borderWidth: 2, borderColor: 'black' }}>
<Text>just some text so it does not take focus and is not empty.</Text>
</View>
</Callout>
)}
</View>
);
};
const calloutSections: TestSection[] = [
{
name: 'Standard Usage',
testID: CALLOUT_TESTPAGE,
component: StandardCallout,
},
{
name: 'Customized Usage',
component: CustomCallout,
},
];
const e2eSections: TestSection[] = [
{
name: 'E2E Testing Callout',
component: E2ECalloutTest,
},
];
export const CalloutTest: React.FunctionComponent = () => {
const status: PlatformStatus = {
win32Status: 'Beta',
uwpStatus: 'Backlog',
iosStatus: 'Backlog',
macosStatus: 'Beta',
androidStatus: 'Backlog',
};
const description = 'A callout is an anchored tip that can be used to teach people or guide them through the app without blocking them.';
return <Test name="Callout Test" description={description} sections={calloutSections} status={status} e2eSections={e2eSections}></Test>;
};