-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathcontext-menu.tsx
More file actions
650 lines (575 loc) · 16 KB
/
context-menu.tsx
File metadata and controls
650 lines (575 loc) · 16 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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
import { augmentRef } from '@rn-primitives/utils';
import {
useControllableState,
useRelativePosition,
type LayoutPosition,
} from '@rn-primitives/hooks';
import { Portal as RNPPortal } from '@rn-primitives/portal';
import * as Slot from '@rn-primitives/slot';
import * as React from 'react';
import {
BackHandler,
Pressable,
Text,
View,
type AccessibilityActionEvent,
type GestureResponderEvent,
type LayoutChangeEvent,
type LayoutRectangle,
} from 'react-native';
import type {
CheckboxItemProps,
CheckboxItemRef,
ContentProps,
ContentRef,
GroupProps,
GroupRef,
ItemIndicatorProps,
ItemIndicatorRef,
ItemProps,
ItemRef,
LabelProps,
LabelRef,
OverlayProps,
OverlayRef,
PortalProps,
RadioGroupProps,
RadioGroupRef,
RadioItemProps,
RadioItemRef,
RootProps,
RootRef,
SeparatorProps,
SeparatorRef,
SubContentProps,
SubContentRef,
SubProps,
SubRef,
SubTriggerProps,
SubTriggerRef,
TriggerProps,
TriggerRef,
} from './types';
interface IRootContext extends RootProps {
open: boolean;
onOpenChange: (open: boolean) => void;
pressPosition: LayoutPosition | null;
setPressPosition: (pressPosition: LayoutPosition | null) => void;
contentLayout: LayoutRectangle | null;
setContentLayout: (contentLayout: LayoutRectangle | null) => void;
nativeID: string;
}
const RootContext = React.createContext<IRootContext | null>(null);
const Root = React.forwardRef<RootRef, RootProps>(
({ asChild, relativeTo = 'longPress', onOpenChange: onOpenChangeProp, ...viewProps }, ref) => {
const nativeID = React.useId();
const [pressPosition, setPressPosition] = React.useState<LayoutPosition | null>(null);
const [contentLayout, setContentLayout] = React.useState<LayoutRectangle | null>(null);
const [open, setOpen] = React.useState(false);
function onOpenChange(value: boolean) {
setOpen(value);
onOpenChangeProp?.(value);
}
const Component = asChild ? Slot.View : View;
return (
<RootContext.Provider
value={{
open,
onOpenChange,
relativeTo,
contentLayout,
nativeID,
pressPosition,
setContentLayout,
setPressPosition,
}}
>
<Component ref={ref} {...viewProps} />
</RootContext.Provider>
);
}
);
Root.displayName = 'RootNativeContextMenu';
function useRootContext() {
const context = React.useContext(RootContext);
if (!context) {
throw new Error(
'ContextMenu compound components cannot be rendered outside the ContextMenu component'
);
}
return context;
}
const accessibilityActions = [{ name: 'longpress' }];
const Trigger = React.forwardRef<TriggerRef, TriggerProps>(
(
{
asChild,
onLongPress: onLongPressProp,
disabled = false,
onAccessibilityAction: onAccessibilityActionProp,
...props
},
ref
) => {
const { open, onOpenChange, relativeTo, setPressPosition } = useRootContext();
function onLongPress(ev: GestureResponderEvent) {
if (disabled) return;
if (relativeTo === 'longPress') {
setPressPosition({
width: 0,
pageX: ev.nativeEvent.pageX,
pageY: ev.nativeEvent.pageY,
height: 0,
});
}
if (relativeTo === 'trigger') {
if (typeof ref === 'function') return;
ref?.current?.measure((_x, _y, width, height, pageX, pageY) => {
setPressPosition({ width, pageX, pageY: pageY, height });
});
}
onOpenChange(!open);
onLongPressProp?.(ev);
}
function onAccessibilityAction(event: AccessibilityActionEvent) {
if (disabled) return;
if (event.nativeEvent.actionName === 'longpress') {
setPressPosition({
width: 0,
pageX: 0,
pageY: 0,
height: 0,
});
const newValue = !open;
onOpenChange(newValue);
}
onAccessibilityActionProp?.(event);
}
const methods = {
open: () => {
onOpenChange(true);
(ref as React.RefObject<TriggerRef>)?.current?.measure(
(_x, _y, width, height, pageX, pageY) => {
setPressPosition({ width, pageX, pageY: pageY, height });
}
);
},
close: () => {
setPressPosition(null);
onOpenChange(false);
},
};
const Component = asChild ? Slot.Pressable : Pressable;
return (
<Component
ref={(self) => augmentRef(ref, self, methods)}
aria-disabled={disabled ?? undefined}
role='button'
onLongPress={onLongPress}
disabled={disabled ?? undefined}
aria-expanded={open}
accessibilityActions={accessibilityActions}
onAccessibilityAction={onAccessibilityAction}
{...props}
/>
);
}
);
Trigger.displayName = 'TriggerNativeContextMenu';
/**
* @warning when using a custom `<PortalHost />`, you will have to adjust the Content's sideOffset to account for nav elements like headers.
*/
function Portal({ forceMount, hostName, children }: PortalProps) {
const value = useRootContext();
if (!value.pressPosition) {
return null;
}
if (!forceMount) {
if (!value.open) {
return null;
}
}
return (
<RNPPortal hostName={hostName} name={`${value.nativeID}_portal`}>
<RootContext.Provider value={value}>{children}</RootContext.Provider>
</RNPPortal>
);
}
const Overlay = React.forwardRef<OverlayRef, OverlayProps>(
({ asChild, forceMount, onPress: OnPressProp, closeOnPress = true, ...props }, ref) => {
const { open, onOpenChange, setContentLayout, setPressPosition } = useRootContext();
function onPress(ev: GestureResponderEvent) {
if (closeOnPress) {
setPressPosition(null);
setContentLayout(null);
onOpenChange(false);
}
OnPressProp?.(ev);
}
if (!forceMount) {
if (!open) {
return null;
}
}
const Component = asChild ? Slot.Pressable : Pressable;
return <Component ref={ref} onPress={onPress} {...props} />;
}
);
Overlay.displayName = 'OverlayNativeContextMenu';
const Content = React.forwardRef<ContentRef, ContentProps>(
(
{
asChild = false,
forceMount,
align = 'start',
side = 'bottom',
sideOffset = 0,
alignOffset = 0,
avoidCollisions = true,
onLayout: onLayoutProp,
insets,
style,
disablePositioningStyle,
...props
},
ref
) => {
const {
open,
onOpenChange,
contentLayout,
nativeID,
pressPosition,
setContentLayout,
setPressPosition,
} = useRootContext();
React.useEffect(() => {
const backHandler = BackHandler.addEventListener('hardwareBackPress', () => {
setPressPosition(null);
setContentLayout(null);
onOpenChange(false);
return true;
});
return () => {
setContentLayout(null);
backHandler.remove();
};
}, []);
const positionStyle = useRelativePosition({
align,
avoidCollisions,
triggerPosition: pressPosition,
contentLayout,
alignOffset,
insets,
sideOffset,
side,
disablePositioningStyle,
});
function onLayout(event: LayoutChangeEvent) {
setContentLayout(event.nativeEvent.layout);
onLayoutProp?.(event);
}
if (!forceMount) {
if (!open) {
return null;
}
}
const Component = asChild ? Slot.View : View;
return (
<Component
ref={ref}
role='menu'
nativeID={nativeID}
aria-modal={true}
style={[positionStyle, style]}
onLayout={onLayout}
onStartShouldSetResponder={onStartShouldSetResponder}
{...props}
/>
);
}
);
Content.displayName = 'ContentNativeContextMenu';
const Item = React.forwardRef<ItemRef, ItemProps>(
(
{ asChild, textValue, onPress: onPressProp, disabled = false, closeOnPress = true, ...props },
ref
) => {
const { onOpenChange, setContentLayout, setPressPosition } = useRootContext();
function onPress(ev: GestureResponderEvent) {
if (closeOnPress) {
setPressPosition(null);
setContentLayout(null);
onOpenChange(false);
}
onPressProp?.(ev);
}
const Component = asChild ? Slot.Pressable : Pressable;
return (
<Component
ref={ref}
role='menuitem'
onPress={onPress}
disabled={disabled}
aria-valuetext={textValue}
aria-disabled={!!disabled}
accessibilityState={{ disabled: !!disabled }}
{...props}
/>
);
}
);
Item.displayName = 'ItemNativeContextMenu';
const Group = React.forwardRef<GroupRef, GroupProps>(({ asChild, ...props }, ref) => {
const Component = asChild ? Slot.View : View;
return <Component ref={ref} role='group' {...props} />;
});
Group.displayName = 'GroupNativeContextMenu';
const Label = React.forwardRef<LabelRef, LabelProps>(({ asChild, ...props }, ref) => {
const Component = asChild ? Slot.Text : Text;
return <Component ref={ref} {...props} />;
});
Label.displayName = 'LabelNativeContextMenu';
type FormItemContext =
| { checked: boolean }
| {
value: string | undefined;
onValueChange: (value: string) => void;
};
const FormItemContext = React.createContext<FormItemContext | null>(null);
const CheckboxItem = React.forwardRef<CheckboxItemRef, CheckboxItemProps>(
(
{
asChild,
checked,
onCheckedChange,
textValue,
onPress: onPressProp,
closeOnPress = true,
disabled = false,
...props
},
ref
) => {
const { onOpenChange, setContentLayout, setPressPosition, nativeID } = useRootContext();
function onPress(ev: GestureResponderEvent) {
onCheckedChange(!checked);
if (closeOnPress) {
setPressPosition(null);
setContentLayout(null);
onOpenChange(false);
}
onPressProp?.(ev);
}
const Component = asChild ? Slot.Pressable : Pressable;
return (
<FormItemContext.Provider value={{ checked }}>
<Component
ref={ref}
role='checkbox'
aria-checked={checked}
onPress={onPress}
disabled={disabled}
aria-disabled={!!disabled}
aria-valuetext={textValue}
accessibilityState={{ disabled: !!disabled }}
{...props}
/>
</FormItemContext.Provider>
);
}
);
CheckboxItem.displayName = 'CheckboxItemNativeContextMenu';
function useFormItemContext() {
const context = React.useContext(FormItemContext);
if (!context) {
throw new Error(
'CheckboxItem or RadioItem compound components cannot be rendered outside of a CheckboxItem or RadioItem component'
);
}
return context;
}
const RadioGroup = React.forwardRef<RadioGroupRef, RadioGroupProps>(
({ asChild, value, onValueChange, ...props }, ref) => {
const Component = asChild ? Slot.View : View;
return (
<FormItemContext.Provider value={{ value, onValueChange }}>
<Component ref={ref} role='radiogroup' {...props} />
</FormItemContext.Provider>
);
}
);
RadioGroup.displayName = 'RadioGroupNativeContextMenu';
type BothFormItemContext = Exclude<FormItemContext, { checked: boolean }> & {
checked: boolean;
};
const RadioItemContext = React.createContext({} as { itemValue: string });
const RadioItem = React.forwardRef<RadioItemRef, RadioItemProps>(
(
{
asChild,
value: itemValue,
textValue,
onPress: onPressProp,
disabled = false,
closeOnPress = true,
...props
},
ref
) => {
const { onOpenChange, setContentLayout, setPressPosition } = useRootContext();
const { value, onValueChange } = useFormItemContext() as BothFormItemContext;
function onPress(ev: GestureResponderEvent) {
onValueChange(itemValue);
if (closeOnPress) {
setPressPosition(null);
setContentLayout(null);
onOpenChange(false);
}
onPressProp?.(ev);
}
const Component = asChild ? Slot.Pressable : Pressable;
return (
<RadioItemContext.Provider value={{ itemValue }}>
<Component
ref={ref}
onPress={onPress}
role='radio'
aria-checked={value === itemValue}
disabled={disabled ?? false}
accessibilityState={{
disabled: disabled ?? false,
checked: value === itemValue,
}}
aria-valuetext={textValue}
{...props}
/>
</RadioItemContext.Provider>
);
}
);
RadioItem.displayName = 'RadioItemNativeContextMenu';
function useItemIndicatorContext() {
return React.useContext(RadioItemContext);
}
const ItemIndicator = React.forwardRef<ItemIndicatorRef, ItemIndicatorProps>(
({ asChild, forceMount, ...props }, ref) => {
const { itemValue } = useItemIndicatorContext();
const { checked, value } = useFormItemContext() as BothFormItemContext;
if (!forceMount) {
if (itemValue == null && !checked) {
return null;
}
if (value !== itemValue) {
return null;
}
}
const Component = asChild ? Slot.View : View;
return <Component ref={ref} role='presentation' {...props} />;
}
);
ItemIndicator.displayName = 'ItemIndicatorNativeContextMenu';
const Separator = React.forwardRef<SeparatorRef, SeparatorProps>(
({ asChild, decorative, ...props }, ref) => {
const Component = asChild ? Slot.View : View;
return <Component role={decorative ? 'presentation' : 'separator'} ref={ref} {...props} />;
}
);
Separator.displayName = 'SeparatorNativeContextMenu';
const SubContext = React.createContext<{
nativeID: string;
open: boolean;
onOpenChange: (value: boolean) => void;
} | null>(null);
const Sub = React.forwardRef<SubRef, SubProps>(
({ asChild, defaultOpen, open: openProp, onOpenChange: onOpenChangeProp, ...props }, ref) => {
const nativeID = React.useId();
const [open = false, onOpenChange] = useControllableState({
prop: openProp,
defaultProp: defaultOpen,
onChange: onOpenChangeProp,
});
const Component = asChild ? Slot.View : View;
return (
<SubContext.Provider
value={{
nativeID,
open,
onOpenChange,
}}
>
<Component ref={ref} {...props} />
</SubContext.Provider>
);
}
);
Sub.displayName = 'SubNativeContextMenu';
function useSubContext() {
const context = React.useContext(SubContext);
if (!context) {
throw new Error('Sub compound components cannot be rendered outside of a Sub component');
}
return context;
}
const SubTrigger = React.forwardRef<SubTriggerRef, SubTriggerProps>(
({ asChild, textValue, onPress: onPressProp, disabled = false, ...props }, ref) => {
const { nativeID, open, onOpenChange } = useSubContext();
function onPress(ev: GestureResponderEvent) {
onOpenChange(!open);
onPressProp?.(ev);
}
const Component = asChild ? Slot.Pressable : Pressable;
return (
<Component
ref={ref}
aria-valuetext={textValue}
role='menuitem'
aria-expanded={open}
accessibilityState={{ expanded: open, disabled: !!disabled }}
nativeID={nativeID}
onPress={onPress}
disabled={disabled}
aria-disabled={!!disabled}
{...props}
/>
);
}
);
SubTrigger.displayName = 'SubTriggerNativeContextMenu';
const SubContent = React.forwardRef<SubContentRef, SubContentProps>(
({ asChild = false, forceMount, ...props }, ref) => {
const { open, nativeID } = useSubContext();
if (!forceMount) {
if (!open) {
return null;
}
}
const Component = asChild ? Slot.Pressable : Pressable;
return <Component ref={ref} role='group' aria-labelledby={nativeID} {...props} />;
}
);
Content.displayName = 'ContentNativeContextMenu';
export {
CheckboxItem,
Content,
Group,
Item,
ItemIndicator,
Label,
Overlay,
Portal,
RadioGroup,
RadioItem,
Root,
Separator,
Sub,
SubContent,
SubTrigger,
Trigger,
useRootContext,
useSubContext,
};
function onStartShouldSetResponder() {
return true;
}