@plexui/react-native 0.2.3 → 0.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/es/components/ContextMenu/ContextMenu.js +128 -0
- package/dist/es/components/List/List.js +3 -3
- package/dist/es/components/Menu/Menu.js +50 -9
- package/dist/es/index.js +1 -0
- package/dist/types/components/ContextMenu/ContextMenu.d.ts +57 -0
- package/dist/types/components/List/List.d.ts +3 -1
- package/dist/types/components/Menu/Menu.d.ts +15 -6
- package/dist/types/index.d.ts +1 -0
- package/package.json +1 -1
- package/src/components/ContextMenu/ContextMenu.tsx +164 -0
- package/src/components/List/List.tsx +8 -1
- package/src/components/Menu/Menu.tsx +67 -7
- package/src/index.ts +1 -0
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { cloneElement, createContext, isValidElement, useCallback, useContext, useEffect, useRef, useState } from "react";
|
|
3
|
+
import { Dimensions, Platform, View } from "react-native";
|
|
4
|
+
import { useTheme } from "../../theme/ThemeProvider";
|
|
5
|
+
import { Menu, MenuSurface, hasCheckedItems, measure } from "../Menu/Menu";
|
|
6
|
+
/** A touch and hold opens the menu after half a second, as the web's ContextMenu (CONTEXT_MENU_LONG_PRESS). */
|
|
7
|
+
export const CONTEXT_MENU_LONG_PRESS = 500;
|
|
8
|
+
const ContextMenuContext = createContext(null);
|
|
9
|
+
function useContextMenu(part) {
|
|
10
|
+
const context = useContext(ContextMenuContext);
|
|
11
|
+
if (!context)
|
|
12
|
+
throw new Error(`${part} must be inside a ContextMenu`);
|
|
13
|
+
return context;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* A menu of actions for an element, opened by a touch and hold (and a right click on the web): on iOS 27 the screen
|
|
17
|
+
* dims and blurs, the element stays clear over it as a lifted preview, and the glass menu stands 16 from it by the
|
|
18
|
+
* third of the screen it is in, over it when it stands in the lower half; on Android Material's popup under the
|
|
19
|
+
* element, the screen dimmed around it. Parts as @plexui/ui: `ContextMenu.Trigger`, `ContextMenu.Content`, and the
|
|
20
|
+
* items of Menu (`ContextMenu.Item`, `ContextMenu.Separator`, `ContextMenu.Label`).
|
|
21
|
+
*/
|
|
22
|
+
function ContextMenuRoot({ children, open: openProp, onOpenChange }) {
|
|
23
|
+
const [own, setOwn] = useState(false);
|
|
24
|
+
const open = openProp ?? own;
|
|
25
|
+
const [anchor, setAnchor] = useState(null);
|
|
26
|
+
const target = useRef({ node: null, preview: null });
|
|
27
|
+
const [preview, setPreview] = useState(null);
|
|
28
|
+
const apply = useCallback((next) => {
|
|
29
|
+
if (openProp === undefined)
|
|
30
|
+
setOwn(next);
|
|
31
|
+
onOpenChange?.(next);
|
|
32
|
+
}, [openProp, onOpenChange]);
|
|
33
|
+
const setOpen = apply;
|
|
34
|
+
const applyRef = useRef(apply);
|
|
35
|
+
applyRef.current = apply;
|
|
36
|
+
// The element is measured as the menu opens, by a touch and hold or by `open`: the menu and its preview stand where
|
|
37
|
+
// it is. Until then nothing is drawn
|
|
38
|
+
useEffect(() => {
|
|
39
|
+
if (!open) {
|
|
40
|
+
setAnchor(null);
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
let live = true;
|
|
44
|
+
measure(target.current.node, (box) => {
|
|
45
|
+
if (!live)
|
|
46
|
+
return;
|
|
47
|
+
setAnchor(box);
|
|
48
|
+
setPreview(target.current.preview);
|
|
49
|
+
});
|
|
50
|
+
// A turn of the screen moves the element: the menu closes, as iOS dismisses a context menu on rotation
|
|
51
|
+
const sub = Dimensions.addEventListener("change", () => applyRef.current(false));
|
|
52
|
+
return () => {
|
|
53
|
+
live = false;
|
|
54
|
+
sub.remove();
|
|
55
|
+
};
|
|
56
|
+
}, [open]);
|
|
57
|
+
const setTarget = useCallback((node, next) => {
|
|
58
|
+
target.current = { node, preview: next };
|
|
59
|
+
}, []);
|
|
60
|
+
return _jsx(ContextMenuContext.Provider, { value: { open, setOpen, anchor, preview, setTarget }, children: children });
|
|
61
|
+
}
|
|
62
|
+
ContextMenuRoot.displayName = "ContextMenu";
|
|
63
|
+
/**
|
|
64
|
+
* The element the menu is for: a ListItem, a Pressable, a Button, anything with `onLongPress`. Its own press still
|
|
65
|
+
* runs; a touch and hold opens the menu, and so does a right click on the web. The lifted preview on iOS is the
|
|
66
|
+
* element drawn again from its props, so state the element keeps inside itself is not in it.
|
|
67
|
+
*/
|
|
68
|
+
function ContextMenuTrigger({ children, style }) {
|
|
69
|
+
const theme = useTheme();
|
|
70
|
+
const { setOpen, setTarget, open, anchor } = useContextMenu("ContextMenu.Trigger");
|
|
71
|
+
const child = isValidElement(children) ? children : null;
|
|
72
|
+
const own = child?.props;
|
|
73
|
+
// The element's own long press gets its event, then the menu opens
|
|
74
|
+
const hold = (...args) => {
|
|
75
|
+
own?.onLongPress?.(...args);
|
|
76
|
+
setOpen(true);
|
|
77
|
+
};
|
|
78
|
+
// VoiceOver and TalkBack open it through the element's long-press action (ListItem names it itself)
|
|
79
|
+
const actions = own?.accessibilityActions ?? [];
|
|
80
|
+
const a11y = actions.some((action) => action.name === "longpress")
|
|
81
|
+
? null
|
|
82
|
+
: {
|
|
83
|
+
accessibilityActions: [...actions, { name: "longpress" }],
|
|
84
|
+
onAccessibilityAction: (event) => {
|
|
85
|
+
if (event.nativeEvent.actionName === "longpress")
|
|
86
|
+
hold();
|
|
87
|
+
else
|
|
88
|
+
own?.onAccessibilityAction?.(event);
|
|
89
|
+
},
|
|
90
|
+
};
|
|
91
|
+
// The preview is the element drawn again over the dimming, taking no presses
|
|
92
|
+
const preview = child ? cloneElement(child, { onLongPress: undefined, onPress: undefined }) : null;
|
|
93
|
+
const web = Platform.OS === "web";
|
|
94
|
+
return (_jsx(View, { ref: (next) => setTarget(next, preview), collapsable: false,
|
|
95
|
+
// On iOS the element hides under its lifted preview while the menu is open, so it is not drawn twice; Android
|
|
96
|
+
// keeps it lit in its place
|
|
97
|
+
style: [{ opacity: open && anchor && !theme.isAndroid ? 0 : 1 }, style], ...(web
|
|
98
|
+
? {
|
|
99
|
+
onContextMenu: (event) => {
|
|
100
|
+
event.preventDefault();
|
|
101
|
+
setOpen(true);
|
|
102
|
+
},
|
|
103
|
+
}
|
|
104
|
+
: null), children: child ? cloneElement(child, { onLongPress: hold, ...a11y }) : children }));
|
|
105
|
+
}
|
|
106
|
+
ContextMenuTrigger.displayName = "ContextMenu.Trigger";
|
|
107
|
+
/** The menu: Menu's items, 250 wide on iOS. */
|
|
108
|
+
function ContextMenuContent(props) {
|
|
109
|
+
const theme = useTheme();
|
|
110
|
+
const { open, setOpen, anchor, preview } = useContextMenu("ContextMenu.Content");
|
|
111
|
+
// The preview keeps a row's corners to half its height, else the kit's 30 (--context-menu-preview-radius)
|
|
112
|
+
// Items that say `checked` make a set of options, with the checkmark's column, as in Menu
|
|
113
|
+
const check = hasCheckedItems(props.children);
|
|
114
|
+
const radius = Math.min(theme.length("--context-menu-preview-radius", 30), (anchor?.height ?? 60) / 2);
|
|
115
|
+
if (theme.isAndroid) {
|
|
116
|
+
// Android: Material's popup under the element at its end, the screen dimmed around it
|
|
117
|
+
return _jsx(MenuSurface, { ...props, check: check, open: open && anchor != null, anchor: anchor, row: anchor ? { box: anchor, radius: { top: 0, bottom: 0 } } : null, cover: false, onClose: () => setOpen(false) });
|
|
118
|
+
}
|
|
119
|
+
return _jsx(MenuSurface, { ...props, check: check, open: open && anchor != null, anchor: anchor, context: { preview, radius }, onClose: () => setOpen(false) });
|
|
120
|
+
}
|
|
121
|
+
ContextMenuContent.displayName = "ContextMenu.Content";
|
|
122
|
+
export const ContextMenu = Object.assign(ContextMenuRoot, {
|
|
123
|
+
Trigger: ContextMenuTrigger,
|
|
124
|
+
Content: ContextMenuContent,
|
|
125
|
+
Item: Menu.Item,
|
|
126
|
+
Separator: Menu.Separator,
|
|
127
|
+
Label: Menu.Label,
|
|
128
|
+
});
|
|
@@ -115,7 +115,7 @@ export function List({ children, header, description, footer, error, surface = "
|
|
|
115
115
|
* in (a multi-select list); a 1px divider from the text's start to the card's end, none under a row with a tile (a
|
|
116
116
|
* tiled row draws none).
|
|
117
117
|
*/
|
|
118
|
-
export function ListItem({ label, description, leading, leadingColor, value, valuePlacement = "end", trailing, error, checked: checkedProp, defaultChecked, onCheckedChange, disclosure, onPress, disabled = false, destructive = false, accessibilityLabel, testID, }) {
|
|
118
|
+
export function ListItem({ label, description, leading, leadingColor, value, valuePlacement = "end", trailing, error, checked: checkedProp, defaultChecked, onCheckedChange, disclosure, onPress, onLongPress, disabled = false, destructive = false, accessibilityLabel, testID, }) {
|
|
119
119
|
const theme = useTheme();
|
|
120
120
|
const { last, top, bottom } = useContext(PlaceContext);
|
|
121
121
|
// A menu opened from the row finds the row (Android lights it over the dim of the screen)
|
|
@@ -185,13 +185,13 @@ export function ListItem({ label, description, leading, leadingColor, value, val
|
|
|
185
185
|
const startColor = atStart || checkLeads ? undefined : leadingColor;
|
|
186
186
|
return (_jsx(RowFrameContext.Provider, { value: frame, children: theme.isAndroid ? (_jsx(AndroidContent, { handPress: handPress, theme: theme, label: label, description: description, descriptionId: described.id, leading: start, leadingColor: startColor, leadingMark: atStart != null || checkLeads, value: value, valuePlacement: valuePlacement, trailing: end, edgeControl: found != null || switchRow, error: error, disabled: disabled, destructive: destructive, last: last })) : (_jsx(IOSContent, { theme: theme, label: label, description: description, descriptionId: described.id, leading: start, leadingColor: startColor, value: value, valuePlacement: valuePlacement, trailing: end, edgeControl: found != null || switchRow, error: error, chevron: chevron, disabled: disabled, destructive: destructive, last: last })) }));
|
|
187
187
|
};
|
|
188
|
-
if (press) {
|
|
188
|
+
if (press || onLongPress) {
|
|
189
189
|
// Android's rows press in the list selector: the native ripple on a device, the drawn one on the web
|
|
190
190
|
const highlight = controlHighlight(theme.scheme);
|
|
191
191
|
const drawn = theme.isAndroid && !nativeRipple;
|
|
192
192
|
return (_jsx(Pressable, { ref: frameNode, collapsable: false, ...described.props, ...(switchRow ? spaceToggles(toggle) : null),
|
|
193
193
|
// A row that toggles its control is not a second element for a screen reader or a Tab stop: the control is
|
|
194
|
-
accessible: endOwnsFocus ? false : undefined, focusable: endOwnsFocus ? false : undefined, tabIndex: endOwnsFocus ? -1 : undefined, accessibilityRole: endOwnsFocus ? undefined : switchRow ? "switch" : "button", accessibilityLabel: endOwnsFocus ? undefined : name, accessibilityState: endOwnsFocus ? undefined : switchRow ? { checked, disabled } : { disabled }, "aria-checked": switchRow ? checked : undefined, disabled: disabled, onPress: press, onPressIn: drawn ? ripple.start : undefined, onPressOut: drawn ? ripple.end : undefined, android_ripple: nativeRipple ? { color: highlight, foreground: true } : undefined, testID: testID, style: ({ pressed }) => row(pressed || held), children: ({ pressed }) => (_jsxs(RowHoldContext.Provider, { value: disabled ? null : theme.isAndroid ? hold : setHeld, children: [ripple.layer, content(pressed || held)] })) }));
|
|
194
|
+
accessible: endOwnsFocus ? false : undefined, focusable: endOwnsFocus ? false : undefined, tabIndex: endOwnsFocus ? -1 : undefined, accessibilityRole: endOwnsFocus ? undefined : switchRow ? "switch" : "button", accessibilityLabel: endOwnsFocus ? undefined : name, accessibilityState: endOwnsFocus ? undefined : switchRow ? { checked, disabled } : { disabled }, "aria-checked": switchRow ? checked : undefined, disabled: disabled, onPress: press, onLongPress: onLongPress, delayLongPress: onLongPress ? 500 : undefined, accessibilityActions: onLongPress ? [{ name: "longpress" }] : undefined, onAccessibilityAction: onLongPress ? (event) => event.nativeEvent.actionName === "longpress" && onLongPress() : undefined, onPressIn: drawn ? ripple.start : undefined, onPressOut: drawn ? ripple.end : undefined, android_ripple: nativeRipple ? { color: highlight, foreground: true } : undefined, testID: testID, style: ({ pressed }) => row(pressed || held), children: ({ pressed }) => (_jsxs(RowHoldContext.Provider, { value: disabled ? null : theme.isAndroid ? hold : setHeld, children: [ripple.layer, content(pressed || held)] })) }));
|
|
195
195
|
}
|
|
196
196
|
// A row that only shows (a detail, a value) is one element named by `accessibilityLabel` when it has one; with a
|
|
197
197
|
// control at its end it stays a group, so the control is still reached
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { jsx as _jsx,
|
|
1
|
+
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
2
|
import { Children, cloneElement, createContext, Fragment, isValidElement, useCallback, useContext, useEffect, useLayoutEffect, useMemo, useRef, useState } from "react";
|
|
3
3
|
import { Animated, Easing, I18nManager, PanResponder, Platform, Pressable, ScrollView, StyleSheet, View, useWindowDimensions } from "react-native";
|
|
4
4
|
import { useSafeAreaInsets } from "react-native-safe-area-context";
|
|
@@ -156,7 +156,7 @@ function MenuContent(props) {
|
|
|
156
156
|
// A menu whose items say `checked` (true or false) is a set of options: it keeps the checkmark's column, as Select's
|
|
157
157
|
return _jsx(MenuSurface, { ...props, check: hasCheckedItems(props.children), open: menu.open, anchor: menu.anchor, row: menu.row, header: menu.header, onClose: () => menu.setOpen(false) });
|
|
158
158
|
}
|
|
159
|
-
function hasCheckedItems(children) {
|
|
159
|
+
export function hasCheckedItems(children) {
|
|
160
160
|
return Children.toArray(children).some((child) => isValidElement(child) ? child.props.checked !== undefined || (child.type === Fragment && hasCheckedItems(child.props.children)) : false);
|
|
161
161
|
}
|
|
162
162
|
MenuContent.displayName = "Menu.Content";
|
|
@@ -166,7 +166,7 @@ export function MenuSurface(props) {
|
|
|
166
166
|
return theme.isAndroid ? _jsx(AndroidMenuSurface, { ...props }) : _jsx(IOSMenuSurface, { ...props });
|
|
167
167
|
}
|
|
168
168
|
/** iOS 26's pop-up menu: glass, a submenu over the menu at its row while the menu steps back. */
|
|
169
|
-
function IOSMenuSurface({ open, anchor, onClose, align = "start", width, check = false, multiple = false, cover, children, "aria-label": ariaLabel, testID }) {
|
|
169
|
+
function IOSMenuSurface({ open, anchor, onClose, align = "start", width, check = false, multiple = false, cover, context, children, "aria-label": ariaLabel, testID }) {
|
|
170
170
|
const theme = useTheme();
|
|
171
171
|
const insets = useSafeAreaInsets();
|
|
172
172
|
const window = useWindowDimensions();
|
|
@@ -207,9 +207,34 @@ function IOSMenuSurface({ open, anchor, onClose, align = "start", width, check =
|
|
|
207
207
|
const below = over ? box.y : box.y + box.height + gap;
|
|
208
208
|
const fitsBelow = below + height <= window.height - bottomEdge;
|
|
209
209
|
const above = over ? box.y + box.height - height : box.y - gap - height;
|
|
210
|
-
|
|
210
|
+
let top = size == null ? -window.height - 1000 : fitsBelow ? below : Math.max(topEdge, above);
|
|
211
|
+
let previewTop = box.y;
|
|
212
|
+
if (context) {
|
|
213
|
+
// iOS 27.2 places a context menu by where the element stands (ref-app menus-context): in the left third at its
|
|
214
|
+
// left edge, in the right third at its right edge, else at its centre; 16 under it, or over it when the element
|
|
215
|
+
// is in the lower half and the menu fits there (the web's ContextMenu, contextMenuPreview)
|
|
216
|
+
const centre = box.x + box.width / 2;
|
|
217
|
+
left = centre < window.width / 3 ? box.x : centre > (window.width * 2) / 3 ? box.x + box.width - measured : centre - measured / 2;
|
|
218
|
+
left = Math.max(margin, Math.min(left, window.width - measured - margin));
|
|
219
|
+
const lift = 16;
|
|
220
|
+
const overIt = box.y - lift - height;
|
|
221
|
+
const underIt = box.y + box.height + lift;
|
|
222
|
+
const lower = box.y + box.height / 2 > window.height / 2;
|
|
223
|
+
if (size != null) {
|
|
224
|
+
if (lower && overIt >= topEdge)
|
|
225
|
+
top = overIt;
|
|
226
|
+
else {
|
|
227
|
+
// Where the menu does not fit under the element it lifts the element up with it, as iOS moves the preview, so
|
|
228
|
+
// the menu never covers it
|
|
229
|
+
const overflow = underIt + height - (window.height - bottomEdge);
|
|
230
|
+
previewTop = overflow > 0 ? Math.max(topEdge, box.y - overflow) : box.y;
|
|
231
|
+
top = Math.max(topEdge, Math.min(previewTop + box.height + lift, window.height - bottomEdge - height));
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
}
|
|
211
235
|
// The measures are from the screen's left; a phone in a right-to-left layout reads `left` as the start edge
|
|
212
|
-
const
|
|
236
|
+
const swap = Platform.OS !== "web" && I18nManager.isRTL && I18nManager.doLeftAndRightSwapInRTL;
|
|
237
|
+
const horizontal = swap ? { right: left } : { left };
|
|
213
238
|
const maxHeight = window.height - topEdge - bottomEdge;
|
|
214
239
|
const timing = overlayTiming(theme, "menu");
|
|
215
240
|
const moveTo = (value, to, enter, done) => {
|
|
@@ -282,7 +307,22 @@ function IOSMenuSurface({ open, anchor, onClose, align = "start", width, check =
|
|
|
282
307
|
const subTop = sub == null || subHeight == null
|
|
283
308
|
? -window.height - 1000
|
|
284
309
|
: Math.max(topEdge, Math.min(sub.y - gutter, window.height - bottomEdge - subHeight));
|
|
285
|
-
return (_jsx(Overlay, { open: open, onDismiss: onClose, motion: "menu", testID: testID, children: (progress, opening) => (_jsxs(_Fragment, { children: [
|
|
310
|
+
return (_jsx(Overlay, { open: open, onDismiss: onClose, motion: "menu", testID: testID, children: (progress, opening) => (_jsxs(_Fragment, { children: [context ? (_jsxs(_Fragment, { children: [_jsx(Animated.View, { pointerEvents: "none", style: [
|
|
311
|
+
StyleSheet.absoluteFill,
|
|
312
|
+
{ backgroundColor: theme.color("--context-menu-dimming-background", theme.scheme === "dark" ? "rgba(0, 0, 0, 0.5)" : "rgba(0, 0, 0, 0.2)"), opacity: progress },
|
|
313
|
+
web ? { backdropFilter: "blur(30px)", WebkitBackdropFilter: "blur(30px)" } : null,
|
|
314
|
+
] }), _jsx(View, { pointerEvents: "none", style: [
|
|
315
|
+
styles.preview,
|
|
316
|
+
swap ? { right: box.x } : { left: box.x },
|
|
317
|
+
{
|
|
318
|
+
top: previewTop,
|
|
319
|
+
width: box.width,
|
|
320
|
+
height: box.height,
|
|
321
|
+
borderRadius: context.radius,
|
|
322
|
+
backgroundColor: theme.colors.groupedSurface,
|
|
323
|
+
boxShadow: String(theme.token("--context-menu-preview-shadow") ?? "0 0 32px rgba(0, 0, 0, 0.2)"),
|
|
324
|
+
},
|
|
325
|
+
], children: context.preview })] })) : null, _jsx(Animated.View, { ref: panel, ...(web ? { tabIndex: -1 } : null), accessibilityRole: "menu", "aria-label": ariaLabel, ...keysOf(panel), onLayout: (event) => setSize({ width: event.nativeEvent.layout.width, height: event.nativeEvent.layout.height }), style: [
|
|
286
326
|
styles.surface,
|
|
287
327
|
horizontal,
|
|
288
328
|
{
|
|
@@ -730,7 +770,7 @@ const focusVisible = (node) => {
|
|
|
730
770
|
function PopupCheck({ color }) {
|
|
731
771
|
return (_jsx(Svg, { width: 26, height: 26, viewBox: "0 0 26 26", children: _jsx(Path, { d: "M7.54 13.04 L11.5 17 L20.41 8.09", stroke: color, strokeWidth: 1.9, strokeLinecap: "round", strokeLinejoin: "round", fill: "none" }) }));
|
|
732
772
|
}
|
|
733
|
-
function MenuItem(props) {
|
|
773
|
+
export function MenuItem(props) {
|
|
734
774
|
return _jsx(MenuRow, { ...props });
|
|
735
775
|
}
|
|
736
776
|
MenuItem.displayName = "Menu.Item";
|
|
@@ -850,7 +890,7 @@ MenuSubTrigger.displayName = "Menu.SubTrigger";
|
|
|
850
890
|
/** The items of a submenu; they show in the menu's panel once its trigger is pressed. */
|
|
851
891
|
const MenuSubContent = () => null;
|
|
852
892
|
MenuSubContent.displayName = "Menu.SubContent";
|
|
853
|
-
function MenuSeparator() {
|
|
893
|
+
export function MenuSeparator() {
|
|
854
894
|
const theme = useTheme();
|
|
855
895
|
const surface = useContext(SurfaceContext);
|
|
856
896
|
// Android parts a popup's groups with an 8dp band: a light gray (#F5F5F5, #262626 at night) in a row's menu, the text
|
|
@@ -870,7 +910,7 @@ function MenuSeparator() {
|
|
|
870
910
|
}
|
|
871
911
|
MenuSeparator.displayName = "Menu.Separator";
|
|
872
912
|
/** A heading over a group of items. */
|
|
873
|
-
function MenuLabel({ children }) {
|
|
913
|
+
export function MenuLabel({ children }) {
|
|
874
914
|
const theme = useTheme();
|
|
875
915
|
const surface = useContext(SurfaceContext);
|
|
876
916
|
if (theme.isAndroid) {
|
|
@@ -905,6 +945,7 @@ export const Menu = Object.assign(MenuRoot, {
|
|
|
905
945
|
SubContent: MenuSubContent,
|
|
906
946
|
});
|
|
907
947
|
const styles = StyleSheet.create({
|
|
948
|
+
preview: { position: "absolute", overflow: "hidden" },
|
|
908
949
|
trigger: { alignSelf: "flex-start" },
|
|
909
950
|
surface: { position: "absolute", outlineWidth: 0, outlineStyle: "none" },
|
|
910
951
|
shrink: { flexShrink: 1 },
|
package/dist/es/index.js
CHANGED
|
@@ -25,6 +25,7 @@ export { TagInput } from "./components/TagInput/TagInput";
|
|
|
25
25
|
export { OTPInput } from "./components/OTPInput/OTPInput";
|
|
26
26
|
export { Select } from "./components/Select/Select";
|
|
27
27
|
export { Menu } from "./components/Menu/Menu";
|
|
28
|
+
export { ContextMenu } from "./components/ContextMenu/ContextMenu";
|
|
28
29
|
export { ActionSheet } from "./components/ActionSheet/ActionSheet";
|
|
29
30
|
export { BottomSheet } from "./components/BottomSheet/BottomSheet";
|
|
30
31
|
export { Dialog } from "./components/Dialog/Dialog";
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { type ReactElement, type ReactNode } from "react";
|
|
2
|
+
import { type AccessibilityActionEvent, type StyleProp, type ViewStyle } from "react-native";
|
|
3
|
+
import { type MenuContentProps } from "../Menu/Menu";
|
|
4
|
+
/** A touch and hold opens the menu after half a second, as the web's ContextMenu (CONTEXT_MENU_LONG_PRESS). */
|
|
5
|
+
export declare const CONTEXT_MENU_LONG_PRESS = 500;
|
|
6
|
+
export type ContextMenuProps = {
|
|
7
|
+
children: ReactNode;
|
|
8
|
+
/** Holds the menu open or closed. */
|
|
9
|
+
open?: boolean;
|
|
10
|
+
onOpenChange?: (open: boolean) => void;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* A menu of actions for an element, opened by a touch and hold (and a right click on the web): on iOS 27 the screen
|
|
14
|
+
* dims and blurs, the element stays clear over it as a lifted preview, and the glass menu stands 16 from it by the
|
|
15
|
+
* third of the screen it is in, over it when it stands in the lower half; on Android Material's popup under the
|
|
16
|
+
* element, the screen dimmed around it. Parts as @plexui/ui: `ContextMenu.Trigger`, `ContextMenu.Content`, and the
|
|
17
|
+
* items of Menu (`ContextMenu.Item`, `ContextMenu.Separator`, `ContextMenu.Label`).
|
|
18
|
+
*/
|
|
19
|
+
declare function ContextMenuRoot({ children, open: openProp, onOpenChange }: ContextMenuProps): import("react").JSX.Element;
|
|
20
|
+
declare namespace ContextMenuRoot {
|
|
21
|
+
var displayName: string;
|
|
22
|
+
}
|
|
23
|
+
type Holdable = {
|
|
24
|
+
onLongPress?: (...args: unknown[]) => void;
|
|
25
|
+
onPress?: unknown;
|
|
26
|
+
accessibilityActions?: ReadonlyArray<{
|
|
27
|
+
name: string;
|
|
28
|
+
label?: string;
|
|
29
|
+
}>;
|
|
30
|
+
onAccessibilityAction?: (event: AccessibilityActionEvent) => void;
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* The element the menu is for: a ListItem, a Pressable, a Button, anything with `onLongPress`. Its own press still
|
|
34
|
+
* runs; a touch and hold opens the menu, and so does a right click on the web. The lifted preview on iOS is the
|
|
35
|
+
* element drawn again from its props, so state the element keeps inside itself is not in it.
|
|
36
|
+
*/
|
|
37
|
+
declare function ContextMenuTrigger({ children, style }: {
|
|
38
|
+
children: ReactElement<Holdable>;
|
|
39
|
+
style?: StyleProp<ViewStyle>;
|
|
40
|
+
}): import("react").JSX.Element;
|
|
41
|
+
declare namespace ContextMenuTrigger {
|
|
42
|
+
var displayName: string;
|
|
43
|
+
}
|
|
44
|
+
export type ContextMenuContentProps = Omit<MenuContentProps, "align">;
|
|
45
|
+
/** The menu: Menu's items, 250 wide on iOS. */
|
|
46
|
+
declare function ContextMenuContent(props: ContextMenuContentProps): import("react").JSX.Element;
|
|
47
|
+
declare namespace ContextMenuContent {
|
|
48
|
+
var displayName: string;
|
|
49
|
+
}
|
|
50
|
+
export declare const ContextMenu: typeof ContextMenuRoot & {
|
|
51
|
+
Trigger: typeof ContextMenuTrigger;
|
|
52
|
+
Content: typeof ContextMenuContent;
|
|
53
|
+
Item: typeof import("../Menu/Menu").MenuItem;
|
|
54
|
+
Separator: typeof import("../Menu/Menu").MenuSeparator;
|
|
55
|
+
Label: typeof import("../Menu/Menu").MenuLabel;
|
|
56
|
+
};
|
|
57
|
+
export {};
|
|
@@ -101,6 +101,8 @@ export type ListItemProps = {
|
|
|
101
101
|
/** The chevron of a row that opens a page. @default true when the row has `onPress`, no `trailing` and is not `destructive` */
|
|
102
102
|
disclosure?: boolean;
|
|
103
103
|
onPress?: () => void;
|
|
104
|
+
/** A touch and hold on the row (half a second): a ContextMenu around the row sets it. */
|
|
105
|
+
onLongPress?: () => void;
|
|
104
106
|
disabled?: boolean;
|
|
105
107
|
/** A row in red, as a destructive action row. */
|
|
106
108
|
destructive?: boolean;
|
|
@@ -119,7 +121,7 @@ export type ListItemProps = {
|
|
|
119
121
|
* in (a multi-select list); a 1px divider from the text's start to the card's end, none under a row with a tile (a
|
|
120
122
|
* tiled row draws none).
|
|
121
123
|
*/
|
|
122
|
-
export declare function ListItem({ label, description, leading, leadingColor, value, valuePlacement, trailing, error, checked: checkedProp, defaultChecked, onCheckedChange, disclosure, onPress, disabled, destructive, accessibilityLabel, testID, }: ListItemProps): import("react").JSX.Element;
|
|
124
|
+
export declare function ListItem({ label, description, leading, leadingColor, value, valuePlacement, trailing, error, checked: checkedProp, defaultChecked, onCheckedChange, disclosure, onPress, onLongPress, disabled, destructive, accessibilityLabel, testID, }: ListItemProps): import("react").JSX.Element;
|
|
123
125
|
export type StackedRowProps = {
|
|
124
126
|
/** The label over the value; none for a row whose placeholder names the field (`hideLabel`). */
|
|
125
127
|
label?: ReactNode;
|
|
@@ -70,6 +70,7 @@ declare function MenuContent(props: MenuContentProps): import("react").JSX.Eleme
|
|
|
70
70
|
declare namespace MenuContent {
|
|
71
71
|
var displayName: string;
|
|
72
72
|
}
|
|
73
|
+
export declare function hasCheckedItems(children: ReactNode): boolean;
|
|
73
74
|
type SurfaceProps = MenuContentProps & {
|
|
74
75
|
open: boolean;
|
|
75
76
|
anchor: MenuAnchor | null;
|
|
@@ -83,6 +84,14 @@ type SurfaceProps = MenuContentProps & {
|
|
|
83
84
|
row?: MenuRow | null;
|
|
84
85
|
/** Opened from a bar's button: on Android the header menu, in the bar's glass. */
|
|
85
86
|
header?: boolean;
|
|
87
|
+
/**
|
|
88
|
+
* A context menu's touch and hold (ContextMenu): on iOS the screen dims and blurs but the element, drawn again
|
|
89
|
+
* over the dimming as its lifted `preview`, and the menu stands 16 from it, by the third of the screen it is in.
|
|
90
|
+
*/
|
|
91
|
+
context?: {
|
|
92
|
+
preview: ReactNode;
|
|
93
|
+
radius: number;
|
|
94
|
+
};
|
|
86
95
|
};
|
|
87
96
|
/** The floating panel of a menu, placed under its anchor (over it when the screen ends first). */
|
|
88
97
|
export declare function MenuSurface(props: SurfaceProps): import("react").JSX.Element;
|
|
@@ -104,8 +113,8 @@ export type MenuItemProps = {
|
|
|
104
113
|
onSelect?: () => void;
|
|
105
114
|
testID?: string;
|
|
106
115
|
};
|
|
107
|
-
declare function MenuItem(props: MenuItemProps): import("react").JSX.Element;
|
|
108
|
-
declare namespace MenuItem {
|
|
116
|
+
export declare function MenuItem(props: MenuItemProps): import("react").JSX.Element;
|
|
117
|
+
export declare namespace MenuItem {
|
|
109
118
|
var displayName: string;
|
|
110
119
|
}
|
|
111
120
|
export type MenuSubContentProps = {
|
|
@@ -134,15 +143,15 @@ declare function MenuSubTrigger({ children, icon, disabled, testID }: MenuSubTri
|
|
|
134
143
|
declare namespace MenuSubTrigger {
|
|
135
144
|
var displayName: string;
|
|
136
145
|
}
|
|
137
|
-
declare function MenuSeparator(): import("react").JSX.Element;
|
|
138
|
-
declare namespace MenuSeparator {
|
|
146
|
+
export declare function MenuSeparator(): import("react").JSX.Element;
|
|
147
|
+
export declare namespace MenuSeparator {
|
|
139
148
|
var displayName: string;
|
|
140
149
|
}
|
|
141
150
|
/** A heading over a group of items. */
|
|
142
|
-
declare function MenuLabel({ children }: {
|
|
151
|
+
export declare function MenuLabel({ children }: {
|
|
143
152
|
children: ReactNode;
|
|
144
153
|
}): import("react").JSX.Element;
|
|
145
|
-
declare namespace MenuLabel {
|
|
154
|
+
export declare namespace MenuLabel {
|
|
146
155
|
var displayName: string;
|
|
147
156
|
}
|
|
148
157
|
export declare const Menu: typeof MenuRoot & {
|
package/dist/types/index.d.ts
CHANGED
|
@@ -24,6 +24,7 @@ export { TagInput, type Tag, type TagInputProps } from "./components/TagInput/Ta
|
|
|
24
24
|
export { OTPInput, type OTPInputProps } from "./components/OTPInput/OTPInput";
|
|
25
25
|
export { Select, type SelectOption, type SelectProps } from "./components/Select/Select";
|
|
26
26
|
export { Menu, type MenuContentProps, type MenuItemProps, type MenuProps } from "./components/Menu/Menu";
|
|
27
|
+
export { ContextMenu, type ContextMenuContentProps, type ContextMenuProps } from "./components/ContextMenu/ContextMenu";
|
|
27
28
|
export { ActionSheet, type ActionSheetAction, type ActionSheetProps } from "./components/ActionSheet/ActionSheet";
|
|
28
29
|
export { BottomSheet, type BottomSheetFooterProps, type BottomSheetProps } from "./components/BottomSheet/BottomSheet";
|
|
29
30
|
export { Dialog, type DialogAction, type DialogProps } from "./components/Dialog/Dialog";
|
package/package.json
CHANGED
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import { cloneElement, createContext, isValidElement, useCallback, useContext, useEffect, useRef, useState, type ReactElement, type ReactNode } from "react"
|
|
2
|
+
import { Dimensions, Platform, View, type AccessibilityActionEvent, type StyleProp, type ViewStyle } from "react-native"
|
|
3
|
+
import { useTheme } from "../../theme/ThemeProvider"
|
|
4
|
+
import { Menu, MenuSurface, hasCheckedItems, measure, type MenuAnchor, type MenuContentProps, type Measurable } from "../Menu/Menu"
|
|
5
|
+
|
|
6
|
+
/** A touch and hold opens the menu after half a second, as the web's ContextMenu (CONTEXT_MENU_LONG_PRESS). */
|
|
7
|
+
export const CONTEXT_MENU_LONG_PRESS = 500
|
|
8
|
+
|
|
9
|
+
type ContextMenuState = {
|
|
10
|
+
open: boolean
|
|
11
|
+
setOpen: (next: boolean) => void
|
|
12
|
+
anchor: MenuAnchor | null
|
|
13
|
+
preview: ReactNode
|
|
14
|
+
setTarget: (node: Measurable | null, preview: ReactNode) => void
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const ContextMenuContext = createContext<ContextMenuState | null>(null)
|
|
18
|
+
|
|
19
|
+
function useContextMenu(part: string) {
|
|
20
|
+
const context = useContext(ContextMenuContext)
|
|
21
|
+
if (!context) throw new Error(`${part} must be inside a ContextMenu`)
|
|
22
|
+
return context
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export type ContextMenuProps = {
|
|
26
|
+
children: ReactNode
|
|
27
|
+
/** Holds the menu open or closed. */
|
|
28
|
+
open?: boolean
|
|
29
|
+
onOpenChange?: (open: boolean) => void
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* A menu of actions for an element, opened by a touch and hold (and a right click on the web): on iOS 27 the screen
|
|
34
|
+
* dims and blurs, the element stays clear over it as a lifted preview, and the glass menu stands 16 from it by the
|
|
35
|
+
* third of the screen it is in, over it when it stands in the lower half; on Android Material's popup under the
|
|
36
|
+
* element, the screen dimmed around it. Parts as @plexui/ui: `ContextMenu.Trigger`, `ContextMenu.Content`, and the
|
|
37
|
+
* items of Menu (`ContextMenu.Item`, `ContextMenu.Separator`, `ContextMenu.Label`).
|
|
38
|
+
*/
|
|
39
|
+
function ContextMenuRoot({ children, open: openProp, onOpenChange }: ContextMenuProps) {
|
|
40
|
+
const [own, setOwn] = useState(false)
|
|
41
|
+
const open = openProp ?? own
|
|
42
|
+
const [anchor, setAnchor] = useState<MenuAnchor | null>(null)
|
|
43
|
+
const target = useRef<{ node: Measurable | null; preview: ReactNode }>({ node: null, preview: null })
|
|
44
|
+
const [preview, setPreview] = useState<ReactNode>(null)
|
|
45
|
+
const apply = useCallback(
|
|
46
|
+
(next: boolean) => {
|
|
47
|
+
if (openProp === undefined) setOwn(next)
|
|
48
|
+
onOpenChange?.(next)
|
|
49
|
+
},
|
|
50
|
+
[openProp, onOpenChange],
|
|
51
|
+
)
|
|
52
|
+
const setOpen = apply
|
|
53
|
+
const applyRef = useRef(apply)
|
|
54
|
+
applyRef.current = apply
|
|
55
|
+
// The element is measured as the menu opens, by a touch and hold or by `open`: the menu and its preview stand where
|
|
56
|
+
// it is. Until then nothing is drawn
|
|
57
|
+
useEffect(() => {
|
|
58
|
+
if (!open) {
|
|
59
|
+
setAnchor(null)
|
|
60
|
+
return
|
|
61
|
+
}
|
|
62
|
+
let live = true
|
|
63
|
+
measure(target.current.node, (box) => {
|
|
64
|
+
if (!live) return
|
|
65
|
+
setAnchor(box)
|
|
66
|
+
setPreview(target.current.preview)
|
|
67
|
+
})
|
|
68
|
+
// A turn of the screen moves the element: the menu closes, as iOS dismisses a context menu on rotation
|
|
69
|
+
const sub = Dimensions.addEventListener("change", () => applyRef.current(false))
|
|
70
|
+
return () => {
|
|
71
|
+
live = false
|
|
72
|
+
sub.remove()
|
|
73
|
+
}
|
|
74
|
+
}, [open])
|
|
75
|
+
const setTarget = useCallback((node: Measurable | null, next: ReactNode) => {
|
|
76
|
+
target.current = { node, preview: next }
|
|
77
|
+
}, [])
|
|
78
|
+
return <ContextMenuContext.Provider value={{ open, setOpen, anchor, preview, setTarget }}>{children}</ContextMenuContext.Provider>
|
|
79
|
+
}
|
|
80
|
+
ContextMenuRoot.displayName = "ContextMenu"
|
|
81
|
+
|
|
82
|
+
type Holdable = {
|
|
83
|
+
onLongPress?: (...args: unknown[]) => void
|
|
84
|
+
onPress?: unknown
|
|
85
|
+
accessibilityActions?: ReadonlyArray<{ name: string; label?: string }>
|
|
86
|
+
onAccessibilityAction?: (event: AccessibilityActionEvent) => void
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* The element the menu is for: a ListItem, a Pressable, a Button, anything with `onLongPress`. Its own press still
|
|
91
|
+
* runs; a touch and hold opens the menu, and so does a right click on the web. The lifted preview on iOS is the
|
|
92
|
+
* element drawn again from its props, so state the element keeps inside itself is not in it.
|
|
93
|
+
*/
|
|
94
|
+
function ContextMenuTrigger({ children, style }: { children: ReactElement<Holdable>; style?: StyleProp<ViewStyle> }) {
|
|
95
|
+
const theme = useTheme()
|
|
96
|
+
const { setOpen, setTarget, open, anchor } = useContextMenu("ContextMenu.Trigger")
|
|
97
|
+
const child = isValidElement<Holdable>(children) ? children : null
|
|
98
|
+
const own = child?.props
|
|
99
|
+
// The element's own long press gets its event, then the menu opens
|
|
100
|
+
const hold = (...args: unknown[]) => {
|
|
101
|
+
own?.onLongPress?.(...args)
|
|
102
|
+
setOpen(true)
|
|
103
|
+
}
|
|
104
|
+
// VoiceOver and TalkBack open it through the element's long-press action (ListItem names it itself)
|
|
105
|
+
const actions = own?.accessibilityActions ?? []
|
|
106
|
+
const a11y = actions.some((action) => action.name === "longpress")
|
|
107
|
+
? null
|
|
108
|
+
: {
|
|
109
|
+
accessibilityActions: [...actions, { name: "longpress" }],
|
|
110
|
+
onAccessibilityAction: (event: AccessibilityActionEvent) => {
|
|
111
|
+
if (event.nativeEvent.actionName === "longpress") hold()
|
|
112
|
+
else own?.onAccessibilityAction?.(event)
|
|
113
|
+
},
|
|
114
|
+
}
|
|
115
|
+
// The preview is the element drawn again over the dimming, taking no presses
|
|
116
|
+
const preview = child ? cloneElement(child, { onLongPress: undefined, onPress: undefined }) : null
|
|
117
|
+
const web = Platform.OS === "web"
|
|
118
|
+
return (
|
|
119
|
+
<View
|
|
120
|
+
ref={(next: unknown) => setTarget(next as Measurable | null, preview)}
|
|
121
|
+
collapsable={false}
|
|
122
|
+
// On iOS the element hides under its lifted preview while the menu is open, so it is not drawn twice; Android
|
|
123
|
+
// keeps it lit in its place
|
|
124
|
+
style={[{ opacity: open && anchor && !theme.isAndroid ? 0 : 1 }, style]}
|
|
125
|
+
{...(web
|
|
126
|
+
? {
|
|
127
|
+
onContextMenu: (event: { preventDefault: () => void }) => {
|
|
128
|
+
event.preventDefault()
|
|
129
|
+
setOpen(true)
|
|
130
|
+
},
|
|
131
|
+
}
|
|
132
|
+
: null)}
|
|
133
|
+
>
|
|
134
|
+
{child ? cloneElement(child, { onLongPress: hold, ...a11y }) : children}
|
|
135
|
+
</View>
|
|
136
|
+
)
|
|
137
|
+
}
|
|
138
|
+
ContextMenuTrigger.displayName = "ContextMenu.Trigger"
|
|
139
|
+
|
|
140
|
+
export type ContextMenuContentProps = Omit<MenuContentProps, "align">
|
|
141
|
+
|
|
142
|
+
/** The menu: Menu's items, 250 wide on iOS. */
|
|
143
|
+
function ContextMenuContent(props: ContextMenuContentProps) {
|
|
144
|
+
const theme = useTheme()
|
|
145
|
+
const { open, setOpen, anchor, preview } = useContextMenu("ContextMenu.Content")
|
|
146
|
+
// The preview keeps a row's corners to half its height, else the kit's 30 (--context-menu-preview-radius)
|
|
147
|
+
// Items that say `checked` make a set of options, with the checkmark's column, as in Menu
|
|
148
|
+
const check = hasCheckedItems(props.children)
|
|
149
|
+
const radius = Math.min(theme.length("--context-menu-preview-radius", 30), (anchor?.height ?? 60) / 2)
|
|
150
|
+
if (theme.isAndroid) {
|
|
151
|
+
// Android: Material's popup under the element at its end, the screen dimmed around it
|
|
152
|
+
return <MenuSurface {...props} check={check} open={open && anchor != null} anchor={anchor} row={anchor ? { box: anchor, radius: { top: 0, bottom: 0 } } : null} cover={false} onClose={() => setOpen(false)} />
|
|
153
|
+
}
|
|
154
|
+
return <MenuSurface {...props} check={check} open={open && anchor != null} anchor={anchor} context={{ preview, radius }} onClose={() => setOpen(false)} />
|
|
155
|
+
}
|
|
156
|
+
ContextMenuContent.displayName = "ContextMenu.Content"
|
|
157
|
+
|
|
158
|
+
export const ContextMenu = Object.assign(ContextMenuRoot, {
|
|
159
|
+
Trigger: ContextMenuTrigger,
|
|
160
|
+
Content: ContextMenuContent,
|
|
161
|
+
Item: Menu.Item,
|
|
162
|
+
Separator: Menu.Separator,
|
|
163
|
+
Label: Menu.Label,
|
|
164
|
+
})
|
|
@@ -330,6 +330,8 @@ export type ListItemProps = {
|
|
|
330
330
|
/** The chevron of a row that opens a page. @default true when the row has `onPress`, no `trailing` and is not `destructive` */
|
|
331
331
|
disclosure?: boolean
|
|
332
332
|
onPress?: () => void
|
|
333
|
+
/** A touch and hold on the row (half a second): a ContextMenu around the row sets it. */
|
|
334
|
+
onLongPress?: () => void
|
|
333
335
|
disabled?: boolean
|
|
334
336
|
/** A row in red, as a destructive action row. */
|
|
335
337
|
destructive?: boolean
|
|
@@ -363,6 +365,7 @@ export function ListItem({
|
|
|
363
365
|
onCheckedChange,
|
|
364
366
|
disclosure,
|
|
365
367
|
onPress,
|
|
368
|
+
onLongPress,
|
|
366
369
|
disabled = false,
|
|
367
370
|
destructive = false,
|
|
368
371
|
accessibilityLabel,
|
|
@@ -452,7 +455,7 @@ export function ListItem({
|
|
|
452
455
|
</RowFrameContext.Provider>
|
|
453
456
|
)
|
|
454
457
|
}
|
|
455
|
-
if (press) {
|
|
458
|
+
if (press || onLongPress) {
|
|
456
459
|
// Android's rows press in the list selector: the native ripple on a device, the drawn one on the web
|
|
457
460
|
const highlight = controlHighlight(theme.scheme)
|
|
458
461
|
const drawn = theme.isAndroid && !nativeRipple
|
|
@@ -472,6 +475,10 @@ export function ListItem({
|
|
|
472
475
|
aria-checked={switchRow ? checked : undefined}
|
|
473
476
|
disabled={disabled}
|
|
474
477
|
onPress={press}
|
|
478
|
+
onLongPress={onLongPress}
|
|
479
|
+
delayLongPress={onLongPress ? 500 : undefined}
|
|
480
|
+
accessibilityActions={onLongPress ? [{ name: "longpress" }] : undefined}
|
|
481
|
+
onAccessibilityAction={onLongPress ? (event) => event.nativeEvent.actionName === "longpress" && onLongPress() : undefined}
|
|
475
482
|
onPressIn={drawn ? ripple.start : undefined}
|
|
476
483
|
onPressOut={drawn ? ripple.end : undefined}
|
|
477
484
|
android_ripple={nativeRipple ? { color: highlight, foreground: true } : undefined}
|
|
@@ -199,7 +199,7 @@ function MenuContent(props: MenuContentProps) {
|
|
|
199
199
|
return <MenuSurface {...props} check={hasCheckedItems(props.children)} open={menu.open} anchor={menu.anchor} row={menu.row} header={menu.header} onClose={() => menu.setOpen(false)} />
|
|
200
200
|
}
|
|
201
201
|
|
|
202
|
-
function hasCheckedItems(children: ReactNode): boolean {
|
|
202
|
+
export function hasCheckedItems(children: ReactNode): boolean {
|
|
203
203
|
return Children.toArray(children).some((child) =>
|
|
204
204
|
isValidElement<{ checked?: boolean; children?: ReactNode }>(child) ? child.props.checked !== undefined || (child.type === Fragment && hasCheckedItems(child.props.children)) : false,
|
|
205
205
|
)
|
|
@@ -219,6 +219,11 @@ type SurfaceProps = MenuContentProps & {
|
|
|
219
219
|
row?: MenuRow | null
|
|
220
220
|
/** Opened from a bar's button: on Android the header menu, in the bar's glass. */
|
|
221
221
|
header?: boolean
|
|
222
|
+
/**
|
|
223
|
+
* A context menu's touch and hold (ContextMenu): on iOS the screen dims and blurs but the element, drawn again
|
|
224
|
+
* over the dimming as its lifted `preview`, and the menu stands 16 from it, by the third of the screen it is in.
|
|
225
|
+
*/
|
|
226
|
+
context?: { preview: ReactNode; radius: number }
|
|
222
227
|
}
|
|
223
228
|
|
|
224
229
|
/** The floating panel of a menu, placed under its anchor (over it when the screen ends first). */
|
|
@@ -228,7 +233,7 @@ export function MenuSurface(props: SurfaceProps) {
|
|
|
228
233
|
}
|
|
229
234
|
|
|
230
235
|
/** iOS 26's pop-up menu: glass, a submenu over the menu at its row while the menu steps back. */
|
|
231
|
-
function IOSMenuSurface({ open, anchor, onClose, align = "start", width, check = false, multiple = false, cover, children, "aria-label": ariaLabel, testID }: SurfaceProps) {
|
|
236
|
+
function IOSMenuSurface({ open, anchor, onClose, align = "start", width, check = false, multiple = false, cover, context, children, "aria-label": ariaLabel, testID }: SurfaceProps) {
|
|
232
237
|
const theme = useTheme()
|
|
233
238
|
const insets = useSafeAreaInsets()
|
|
234
239
|
const window = useWindowDimensions()
|
|
@@ -269,9 +274,33 @@ function IOSMenuSurface({ open, anchor, onClose, align = "start", width, check =
|
|
|
269
274
|
const below = over ? box.y : box.y + box.height + gap
|
|
270
275
|
const fitsBelow = below + height <= window.height - bottomEdge
|
|
271
276
|
const above = over ? box.y + box.height - height : box.y - gap - height
|
|
272
|
-
|
|
277
|
+
let top = size == null ? -window.height - 1000 : fitsBelow ? below : Math.max(topEdge, above)
|
|
278
|
+
let previewTop = box.y
|
|
279
|
+
if (context) {
|
|
280
|
+
// iOS 27.2 places a context menu by where the element stands (ref-app menus-context): in the left third at its
|
|
281
|
+
// left edge, in the right third at its right edge, else at its centre; 16 under it, or over it when the element
|
|
282
|
+
// is in the lower half and the menu fits there (the web's ContextMenu, contextMenuPreview)
|
|
283
|
+
const centre = box.x + box.width / 2
|
|
284
|
+
left = centre < window.width / 3 ? box.x : centre > (window.width * 2) / 3 ? box.x + box.width - measured : centre - measured / 2
|
|
285
|
+
left = Math.max(margin, Math.min(left, window.width - measured - margin))
|
|
286
|
+
const lift = 16
|
|
287
|
+
const overIt = box.y - lift - height
|
|
288
|
+
const underIt = box.y + box.height + lift
|
|
289
|
+
const lower = box.y + box.height / 2 > window.height / 2
|
|
290
|
+
if (size != null) {
|
|
291
|
+
if (lower && overIt >= topEdge) top = overIt
|
|
292
|
+
else {
|
|
293
|
+
// Where the menu does not fit under the element it lifts the element up with it, as iOS moves the preview, so
|
|
294
|
+
// the menu never covers it
|
|
295
|
+
const overflow = underIt + height - (window.height - bottomEdge)
|
|
296
|
+
previewTop = overflow > 0 ? Math.max(topEdge, box.y - overflow) : box.y
|
|
297
|
+
top = Math.max(topEdge, Math.min(previewTop + box.height + lift, window.height - bottomEdge - height))
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
}
|
|
273
301
|
// The measures are from the screen's left; a phone in a right-to-left layout reads `left` as the start edge
|
|
274
|
-
const
|
|
302
|
+
const swap = Platform.OS !== "web" && I18nManager.isRTL && I18nManager.doLeftAndRightSwapInRTL
|
|
303
|
+
const horizontal = swap ? { right: left } : { left }
|
|
275
304
|
const maxHeight = window.height - topEdge - bottomEdge
|
|
276
305
|
const timing = overlayTiming(theme, "menu")
|
|
277
306
|
const moveTo = (value: Animated.Value, to: number, enter: boolean, done?: () => void) => {
|
|
@@ -347,6 +376,36 @@ function IOSMenuSurface({ open, anchor, onClose, align = "start", width, check =
|
|
|
347
376
|
<Overlay open={open} onDismiss={onClose} motion="menu" testID={testID}>
|
|
348
377
|
{(progress, opening) => (
|
|
349
378
|
<>
|
|
379
|
+
{context ? (
|
|
380
|
+
<>
|
|
381
|
+
{/* The page dims (black at 20%, 50% at night) over a 30px blur on the web; the element stays clear */}
|
|
382
|
+
<Animated.View
|
|
383
|
+
pointerEvents="none"
|
|
384
|
+
style={[
|
|
385
|
+
StyleSheet.absoluteFill,
|
|
386
|
+
{ backgroundColor: theme.color("--context-menu-dimming-background", theme.scheme === "dark" ? "rgba(0, 0, 0, 0.5)" : "rgba(0, 0, 0, 0.2)"), opacity: progress },
|
|
387
|
+
web ? ({ backdropFilter: "blur(30px)", WebkitBackdropFilter: "blur(30px)" } as ViewStyle) : null,
|
|
388
|
+
]}
|
|
389
|
+
/>
|
|
390
|
+
<View
|
|
391
|
+
pointerEvents="none"
|
|
392
|
+
style={[
|
|
393
|
+
styles.preview,
|
|
394
|
+
swap ? { right: box.x } : { left: box.x },
|
|
395
|
+
{
|
|
396
|
+
top: previewTop,
|
|
397
|
+
width: box.width,
|
|
398
|
+
height: box.height,
|
|
399
|
+
borderRadius: context.radius,
|
|
400
|
+
backgroundColor: theme.colors.groupedSurface,
|
|
401
|
+
boxShadow: String(theme.token("--context-menu-preview-shadow") ?? "0 0 32px rgba(0, 0, 0, 0.2)"),
|
|
402
|
+
},
|
|
403
|
+
]}
|
|
404
|
+
>
|
|
405
|
+
{context.preview}
|
|
406
|
+
</View>
|
|
407
|
+
</>
|
|
408
|
+
) : null}
|
|
350
409
|
<Animated.View
|
|
351
410
|
ref={panel as never}
|
|
352
411
|
// The panel takes the focus of a menu opened by a pointer (the web), with no ring of its own
|
|
@@ -996,7 +1055,7 @@ type RowExtras = {
|
|
|
996
1055
|
rowRef?: { current: unknown }
|
|
997
1056
|
}
|
|
998
1057
|
|
|
999
|
-
function MenuItem(props: MenuItemProps) {
|
|
1058
|
+
export function MenuItem(props: MenuItemProps) {
|
|
1000
1059
|
return <MenuRow {...props} />
|
|
1001
1060
|
}
|
|
1002
1061
|
MenuItem.displayName = "Menu.Item"
|
|
@@ -1216,7 +1275,7 @@ MenuSubTrigger.displayName = "Menu.SubTrigger"
|
|
|
1216
1275
|
const MenuSubContent: ((props: MenuSubContentProps) => null) & { displayName?: string } = () => null
|
|
1217
1276
|
MenuSubContent.displayName = "Menu.SubContent"
|
|
1218
1277
|
|
|
1219
|
-
function MenuSeparator() {
|
|
1278
|
+
export function MenuSeparator() {
|
|
1220
1279
|
const theme = useTheme()
|
|
1221
1280
|
const surface = useContext(SurfaceContext)
|
|
1222
1281
|
// Android parts a popup's groups with an 8dp band: a light gray (#F5F5F5, #262626 at night) in a row's menu, the text
|
|
@@ -1245,7 +1304,7 @@ function MenuSeparator() {
|
|
|
1245
1304
|
MenuSeparator.displayName = "Menu.Separator"
|
|
1246
1305
|
|
|
1247
1306
|
/** A heading over a group of items. */
|
|
1248
|
-
function MenuLabel({ children }: { children: ReactNode }) {
|
|
1307
|
+
export function MenuLabel({ children }: { children: ReactNode }) {
|
|
1249
1308
|
const theme = useTheme()
|
|
1250
1309
|
const surface = useContext(SurfaceContext)
|
|
1251
1310
|
if (theme.isAndroid) {
|
|
@@ -1295,6 +1354,7 @@ export const Menu = Object.assign(MenuRoot, {
|
|
|
1295
1354
|
})
|
|
1296
1355
|
|
|
1297
1356
|
const styles = StyleSheet.create({
|
|
1357
|
+
preview: { position: "absolute", overflow: "hidden" },
|
|
1298
1358
|
trigger: { alignSelf: "flex-start" },
|
|
1299
1359
|
surface: { position: "absolute", outlineWidth: 0, outlineStyle: "none" as "solid" },
|
|
1300
1360
|
shrink: { flexShrink: 1 },
|
package/src/index.ts
CHANGED
|
@@ -80,6 +80,7 @@ export { TagInput, type Tag, type TagInputProps } from "./components/TagInput/Ta
|
|
|
80
80
|
export { OTPInput, type OTPInputProps } from "./components/OTPInput/OTPInput"
|
|
81
81
|
export { Select, type SelectOption, type SelectProps } from "./components/Select/Select"
|
|
82
82
|
export { Menu, type MenuContentProps, type MenuItemProps, type MenuProps } from "./components/Menu/Menu"
|
|
83
|
+
export { ContextMenu, type ContextMenuContentProps, type ContextMenuProps } from "./components/ContextMenu/ContextMenu"
|
|
83
84
|
export { ActionSheet, type ActionSheetAction, type ActionSheetProps } from "./components/ActionSheet/ActionSheet"
|
|
84
85
|
export { BottomSheet, type BottomSheetFooterProps, type BottomSheetProps } from "./components/BottomSheet/BottomSheet"
|
|
85
86
|
export { Dialog, type DialogAction, type DialogProps } from "./components/Dialog/Dialog"
|