@plexui/react-native 0.1.0 → 0.2.0
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/README.md +34 -0
- package/dist/es/components/ActionSheet/ActionSheet.js +53 -19
- package/dist/es/components/Alert/Alert.js +163 -0
- package/dist/es/components/Avatar/Avatar.js +20 -8
- package/dist/es/components/BottomActions/BottomActions.js +34 -0
- package/dist/es/components/BottomSheet/BottomSheet.js +85 -17
- package/dist/es/components/Button/Button.js +108 -28
- package/dist/es/components/Card/Card.js +16 -8
- package/dist/es/components/Checkbox/Checkbox.js +67 -99
- package/dist/es/components/Chip/Chip.js +16 -7
- package/dist/es/components/Dialog/Dialog.js +38 -24
- package/dist/es/components/Field/Field.js +91 -0
- package/dist/es/components/FloatingLabelInput/FloatingLabelInput.js +59 -0
- package/dist/es/components/Icon/createIcon.js +13 -2
- package/dist/es/components/Indicator/CircularProgress.js +80 -0
- package/dist/es/components/Indicator/LoadingDots.js +46 -0
- package/dist/es/components/Indicator/LoadingIndicator.js +101 -0
- package/dist/es/components/Indicator/index.js +3 -0
- package/dist/es/components/Input/Input.js +69 -60
- package/dist/es/components/InputGroup/InputGroup.js +130 -0
- package/dist/es/components/List/List.js +175 -59
- package/dist/es/components/Menu/Menu.js +69 -40
- package/dist/es/components/NavigationButton/NavigationButton.js +20 -4
- package/dist/es/components/OTPInput/OTPInput.js +9 -3
- package/dist/es/components/PageControl/PageControl.js +13 -7
- package/dist/es/components/Popover/Popover.js +128 -0
- package/dist/es/components/Progress/Progress.js +142 -0
- package/dist/es/components/ProgressSteps/ProgressSteps.js +207 -0
- package/dist/es/components/RollingNumber/RollingNumber.js +125 -0
- package/dist/es/components/Select/Select.js +97 -34
- package/dist/es/components/Separator/Separator.js +31 -0
- package/dist/es/components/ShimmerText/ShimmerText.js +98 -0
- package/dist/es/components/Stepper/Stepper.js +219 -0
- package/dist/es/components/Switch/Switch.js +26 -12
- package/dist/es/components/TagInput/TagInput.js +121 -0
- package/dist/es/components/Text/Text.js +7 -4
- package/dist/es/components/TextLink/TextLink.js +42 -0
- package/dist/es/components/Textarea/Textarea.js +12 -0
- package/dist/es/components/Tip/Tip.js +193 -0
- package/dist/es/components/Toolbar/Toolbar.js +6 -2
- package/dist/es/components/Tooltip/Tooltip.js +272 -0
- package/dist/es/index.js +20 -2
- package/dist/es/internal/FloatingPanel.js +111 -0
- package/dist/es/internal/fieldStack.js +73 -0
- package/dist/es/internal/floating.js +97 -0
- package/dist/es/internal/ripple.js +119 -0
- package/dist/es/internal/row.js +2 -1
- package/dist/es/internal/transition.js +113 -0
- package/dist/es/theme/ThemeProvider.js +41 -9
- package/dist/es/theme/material.js +37 -7
- package/dist/types/components/ActionSheet/ActionSheet.d.ts +15 -7
- package/dist/types/components/Alert/Alert.d.ts +51 -0
- package/dist/types/components/Avatar/Avatar.d.ts +4 -2
- package/dist/types/components/BottomActions/BottomActions.d.ts +18 -0
- package/dist/types/components/BottomSheet/BottomSheet.d.ts +4 -2
- package/dist/types/components/Button/Button.d.ts +20 -5
- package/dist/types/components/Card/Card.d.ts +14 -1
- package/dist/types/components/Checkbox/Checkbox.d.ts +4 -3
- package/dist/types/components/Chip/Chip.d.ts +12 -1
- package/dist/types/components/Dialog/Dialog.d.ts +9 -5
- package/dist/types/components/Field/Field.d.ts +64 -0
- package/dist/types/components/FloatingLabelInput/FloatingLabelInput.d.ts +42 -0
- package/dist/types/components/Indicator/CircularProgress.d.ts +34 -0
- package/dist/types/components/Indicator/LoadingDots.d.ts +17 -0
- package/dist/types/components/Indicator/LoadingIndicator.d.ts +46 -0
- package/dist/types/components/Indicator/index.d.ts +3 -0
- package/dist/types/components/Input/Input.d.ts +13 -3
- package/dist/types/components/InputGroup/InputGroup.d.ts +71 -0
- package/dist/types/components/List/List.d.ts +57 -14
- package/dist/types/components/Menu/Menu.d.ts +4 -0
- package/dist/types/components/NavigationButton/NavigationButton.d.ts +11 -3
- package/dist/types/components/OTPInput/OTPInput.d.ts +10 -1
- package/dist/types/components/PageControl/PageControl.d.ts +11 -1
- package/dist/types/components/Popover/Popover.d.ts +86 -0
- package/dist/types/components/Progress/Progress.d.ts +56 -0
- package/dist/types/components/ProgressSteps/ProgressSteps.d.ts +61 -0
- package/dist/types/components/RollingNumber/RollingNumber.d.ts +36 -0
- package/dist/types/components/Select/Select.d.ts +36 -8
- package/dist/types/components/Separator/Separator.d.ts +19 -0
- package/dist/types/components/ShimmerText/ShimmerText.d.ts +28 -0
- package/dist/types/components/Stepper/Stepper.d.ts +61 -0
- package/dist/types/components/Switch/Switch.d.ts +5 -5
- package/dist/types/components/TagInput/TagInput.d.ts +44 -0
- package/dist/types/components/TextLink/TextLink.d.ts +30 -0
- package/dist/types/components/Textarea/Textarea.d.ts +19 -0
- package/dist/types/components/Tip/Tip.d.ts +71 -0
- package/dist/types/components/Toolbar/Toolbar.d.ts +9 -1
- package/dist/types/components/Tooltip/Tooltip.d.ts +84 -0
- package/dist/types/index.d.ts +20 -2
- package/dist/types/internal/FloatingPanel.d.ts +70 -0
- package/dist/types/internal/fieldStack.d.ts +48 -0
- package/dist/types/internal/floating.d.ts +71 -0
- package/dist/types/internal/ripple.d.ts +17 -0
- package/dist/types/internal/row.d.ts +15 -3
- package/dist/types/internal/transition.d.ts +37 -0
- package/dist/types/theme/ThemeProvider.d.ts +28 -4
- package/dist/types/theme/material.d.ts +20 -6
- package/dist/types/theme/types.d.ts +8 -0
- package/package.json +2 -2
- package/src/components/ActionSheet/ActionSheet.tsx +107 -38
- package/src/components/Alert/Alert.tsx +279 -0
- package/src/components/Avatar/Avatar.tsx +21 -9
- package/src/components/BottomActions/BottomActions.tsx +54 -0
- package/src/components/BottomSheet/BottomSheet.tsx +111 -25
- package/src/components/Button/Button.tsx +170 -35
- package/src/components/Card/Card.tsx +33 -9
- package/src/components/Checkbox/Checkbox.tsx +89 -124
- package/src/components/Chip/Chip.tsx +49 -6
- package/src/components/Dialog/Dialog.tsx +68 -37
- package/src/components/Field/Field.tsx +227 -0
- package/src/components/FloatingLabelInput/FloatingLabelInput.tsx +144 -0
- package/src/components/Icon/createIcon.tsx +16 -6
- package/src/components/Indicator/CircularProgress.tsx +141 -0
- package/src/components/Indicator/LoadingDots.tsx +79 -0
- package/src/components/Indicator/LoadingIndicator.tsx +166 -0
- package/src/components/Indicator/index.ts +3 -0
- package/src/components/Input/Input.tsx +126 -97
- package/src/components/InputGroup/InputGroup.tsx +261 -0
- package/src/components/List/List.tsx +345 -88
- package/src/components/Menu/Menu.tsx +83 -41
- package/src/components/NavigationButton/NavigationButton.tsx +35 -5
- package/src/components/OTPInput/OTPInput.tsx +21 -2
- package/src/components/PageControl/PageControl.tsx +36 -8
- package/src/components/Popover/Popover.tsx +261 -0
- package/src/components/Progress/Progress.tsx +255 -0
- package/src/components/ProgressSteps/ProgressSteps.tsx +415 -0
- package/src/components/RollingNumber/RollingNumber.tsx +189 -0
- package/src/components/Select/Select.tsx +175 -71
- package/src/components/Separator/Separator.tsx +51 -0
- package/src/components/ShimmerText/ShimmerText.tsx +144 -0
- package/src/components/Stepper/Stepper.tsx +386 -0
- package/src/components/Switch/Switch.tsx +26 -12
- package/src/components/TagInput/TagInput.tsx +227 -0
- package/src/components/Text/Text.tsx +8 -4
- package/src/components/TextLink/TextLink.tsx +77 -0
- package/src/components/Textarea/Textarea.tsx +23 -0
- package/src/components/Tip/Tip.tsx +410 -0
- package/src/components/Toolbar/Toolbar.tsx +27 -2
- package/src/components/Tooltip/Tooltip.tsx +478 -0
- package/src/index.ts +36 -2
- package/src/internal/FloatingPanel.tsx +199 -0
- package/src/internal/fieldStack.tsx +179 -0
- package/src/internal/floating.ts +131 -0
- package/src/internal/ripple.tsx +132 -0
- package/src/internal/row.ts +16 -3
- package/src/internal/transition.ts +124 -0
- package/src/theme/ThemeProvider.tsx +65 -15
- package/src/theme/material.ts +39 -7
- package/src/theme/types.ts +8 -0
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { cloneElement, createContext, isValidElement, useContext, useEffect, useState } from "react";
|
|
3
|
+
import { NativeModules, Platform, Text as RNText, StyleSheet, View } from "react-native";
|
|
4
|
+
import CheckMd from "../../icons/plex/CheckMd";
|
|
5
|
+
import Copy from "../../icons/plex/Copy";
|
|
6
|
+
import { useDescription } from "../../internal/a11y";
|
|
7
|
+
import { FloatingPanel } from "../../internal/FloatingPanel";
|
|
8
|
+
import { anchorProps } from "../../internal/floating";
|
|
9
|
+
import { elevationShadow } from "../../theme/material";
|
|
10
|
+
import { useTheme } from "../../theme/ThemeProvider";
|
|
11
|
+
import { IconDefaultsProvider } from "../Icon/createIcon";
|
|
12
|
+
import { useMenuAnchor } from "../Menu/Menu";
|
|
13
|
+
import { Text } from "../Text/Text";
|
|
14
|
+
const TooltipContext = createContext(null);
|
|
15
|
+
/** How long a tooltip opened by a long press stays: Material's 1500ms. */
|
|
16
|
+
const SHOW_FOR = 1500;
|
|
17
|
+
const GUTTERS = {
|
|
18
|
+
sm: { vertical: 8, horizontal: 12 },
|
|
19
|
+
md: { vertical: 12, horizontal: 16 },
|
|
20
|
+
lg: { vertical: 14, horizontal: 18 },
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* Brief text that explains a control, as @plexui/ui's Tooltip. A phone has no hover: a long press on the trigger
|
|
24
|
+
* shows it for 1.5 seconds, as Android shows a tooltip, and a tap anywhere hides it; `forceOpen` keeps it shown. A
|
|
25
|
+
* screen reader reads the content as the trigger's description. On iOS (which has no tooltips) and the web it is the
|
|
26
|
+
* web's tooltip (the elevated surface with its shadow, `--radius-md`, 14/20 text; `compact` inverted, 2 by 8); on
|
|
27
|
+
* Android Material 3's tooltips: `compact` the plain tooltip (inverse surface, Body S, 4dp corners), otherwise the
|
|
28
|
+
* rich tooltip's surface (12dp corners, Body M, level 2 shadow). A disabled trigger shows its tooltip too.
|
|
29
|
+
*/
|
|
30
|
+
export function Tooltip({ children, content, maxWidth = 300, forceOpen, compact = false, side = "top", align = "center", alignOffset = 0, sideOffset = 5, gutterSize = "md", contentStyle, testID, }) {
|
|
31
|
+
const enabled = content !== null;
|
|
32
|
+
const [visible, setVisible] = useState(false);
|
|
33
|
+
const open = enabled && (forceOpen ?? visible);
|
|
34
|
+
const { anchor, place, setTrigger } = useMenuAnchor(open);
|
|
35
|
+
const show = () => {
|
|
36
|
+
if (!enabled || forceOpen !== undefined)
|
|
37
|
+
return;
|
|
38
|
+
place(() => setVisible(true));
|
|
39
|
+
};
|
|
40
|
+
// A long press shows it for a while; a tap anywhere hides it sooner
|
|
41
|
+
useEffect(() => {
|
|
42
|
+
if (!visible || forceOpen !== undefined)
|
|
43
|
+
return;
|
|
44
|
+
const timer = setTimeout(() => setVisible(false), SHOW_FOR);
|
|
45
|
+
return () => clearTimeout(timer);
|
|
46
|
+
}, [visible, forceOpen]);
|
|
47
|
+
const description = useDescription(enabled ? content : null);
|
|
48
|
+
const [touch] = useState(() => new LongTouch());
|
|
49
|
+
useEffect(() => () => touch.cancel(), [touch]);
|
|
50
|
+
const first = Array.isArray(children) ? children[0] : children;
|
|
51
|
+
// Plain text is a decorated trigger
|
|
52
|
+
const child = typeof first === "string" || typeof first === "number" ? _jsx(TriggerDecorator, { children: first }) : first;
|
|
53
|
+
const trigger = isValidElement(child)
|
|
54
|
+
? cloneElement(child, triggerProps(child, show, touch, description.props, anchorProps(child.props, setTrigger, open ? () => place() : undefined)))
|
|
55
|
+
: child;
|
|
56
|
+
return (_jsxs(TooltipContext.Provider, { value: { open, show }, children: [trigger, enabled && Platform.OS === "web" && description.id ? (
|
|
57
|
+
// The web's description: the trigger points at it with aria-describedby
|
|
58
|
+
_jsx(RNText, { nativeID: description.id, style: styles.hidden, children: textContent(content) })) : null, enabled ? (_jsx(TooltipPanel, { open: open, onDismiss: () => setVisible(false), anchor: anchor, side: side, align: align, alignOffset: alignOffset, sideOffset: sideOffset, compact: compact, maxWidth: maxWidth, gutterSize: gutterSize, style: contentStyle, testID: testID, children: content })) : null] }));
|
|
59
|
+
}
|
|
60
|
+
Tooltip.displayName = "Tooltip";
|
|
61
|
+
/** A press held for 500ms, as React Native's long press: a disabled control's own touches, which it does not take. */
|
|
62
|
+
class LongTouch {
|
|
63
|
+
timer;
|
|
64
|
+
start(run) {
|
|
65
|
+
clearTimeout(this.timer);
|
|
66
|
+
this.timer = setTimeout(run, LONG_PRESS);
|
|
67
|
+
}
|
|
68
|
+
cancel() {
|
|
69
|
+
clearTimeout(this.timer);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
const LONG_PRESS = 500;
|
|
73
|
+
/**
|
|
74
|
+
* The trigger's props: the tooltip as its description, the long press that shows it after its own, and its node and
|
|
75
|
+
* layout as the tooltip's anchor. A disabled control takes no press, so its touches show the tooltip.
|
|
76
|
+
*/
|
|
77
|
+
function triggerProps(child, show, touch, described, anchor) {
|
|
78
|
+
const own = child.props;
|
|
79
|
+
const disabled = own.disabled === true;
|
|
80
|
+
return {
|
|
81
|
+
...described,
|
|
82
|
+
...anchor,
|
|
83
|
+
onLongPress: (event) => {
|
|
84
|
+
own.onLongPress?.(event);
|
|
85
|
+
show();
|
|
86
|
+
},
|
|
87
|
+
...(disabled
|
|
88
|
+
? {
|
|
89
|
+
onTouchStart: (event) => {
|
|
90
|
+
own.onTouchStart?.(event);
|
|
91
|
+
touch.start(show);
|
|
92
|
+
},
|
|
93
|
+
onTouchEnd: (event) => {
|
|
94
|
+
own.onTouchEnd?.(event);
|
|
95
|
+
touch.cancel();
|
|
96
|
+
},
|
|
97
|
+
onTouchCancel: (event) => {
|
|
98
|
+
own.onTouchCancel?.(event);
|
|
99
|
+
touch.cancel();
|
|
100
|
+
},
|
|
101
|
+
onTouchMove: (event) => {
|
|
102
|
+
own.onTouchMove?.(event);
|
|
103
|
+
touch.cancel();
|
|
104
|
+
},
|
|
105
|
+
}
|
|
106
|
+
: null),
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
const textContent = (node) => node == null || typeof node === "boolean" ? "" : typeof node === "string" || typeof node === "number" ? String(node) : Array.isArray(node) ? node.map(textContent).join("") : isValidElement(node) ? textContent(node.props.children) : "";
|
|
110
|
+
/** The colors and shape of a tooltip on each platform. */
|
|
111
|
+
function tooltipLook(theme, compact, gutterSize) {
|
|
112
|
+
const m = theme.material;
|
|
113
|
+
if (theme.isAndroid) {
|
|
114
|
+
return compact
|
|
115
|
+
? // Material 3's plain tooltip
|
|
116
|
+
{ background: m.inverseSurface, text: m.inverseOnSurface, radius: 4, padding: { vertical: 4, horizontal: 8 }, minHeight: 24, shadow: undefined, maxWidth: 200, variant: "caption1", weight: "regular" }
|
|
117
|
+
: // Material 3's rich tooltip: its supporting text on its container
|
|
118
|
+
{ background: m.surfaceContainer, text: m.onSurfaceVariant, radius: 12, padding: { vertical: 12, horizontal: 16 }, minHeight: 0, shadow: elevationShadow.level2, maxWidth: 320, variant: "subheadline", weight: undefined };
|
|
119
|
+
}
|
|
120
|
+
const shadow = [theme.token("--shadow"), theme.token("--shadow-hairline")].filter((part) => typeof part === "string").join(", ");
|
|
121
|
+
return compact
|
|
122
|
+
? { background: theme.color("--gray-700"), text: theme.color("--gray-0"), radius: theme.length("--radius-md", 8), padding: { vertical: 2, horizontal: 8 }, minHeight: 0, shadow, maxWidth: undefined, variant: undefined, weight: undefined }
|
|
123
|
+
: { background: theme.color("--color-surface-elevated", theme.colors.surfaceElevated), text: theme.colors.text, radius: theme.length("--radius-md", 8), padding: GUTTERS[gutterSize], minHeight: 0, shadow, maxWidth: undefined, variant: undefined, weight: undefined };
|
|
124
|
+
}
|
|
125
|
+
function TooltipPanel({ open, onDismiss, anchor, side, align, alignOffset, sideOffset, compact, maxWidth, gutterSize, style, testID, children }) {
|
|
126
|
+
const theme = useTheme();
|
|
127
|
+
const look = tooltipLook(theme, compact, gutterSize);
|
|
128
|
+
// Material's tooltips stand 4dp from their anchor
|
|
129
|
+
const offset = theme.isAndroid ? Math.min(sideOffset, 4) : sideOffset;
|
|
130
|
+
const width = maxWidth === "none" ? {} : { maxWidth: look.maxWidth != null ? Math.min(maxWidth, look.maxWidth) : maxWidth };
|
|
131
|
+
return (_jsx(FloatingPanel, { open: open, onDismiss: onDismiss, anchor: anchor, side: side, align: align, alignOffset: alignOffset, sideOffset: offset, collisionPadding: 15, motion: "tooltip", passThrough: true, autoFocus: false, role: "tooltip", testID: testID, style: width, children: () => (_jsx(View, { style: [
|
|
132
|
+
styles.panel,
|
|
133
|
+
{
|
|
134
|
+
backgroundColor: look.background,
|
|
135
|
+
borderRadius: look.radius,
|
|
136
|
+
paddingVertical: look.padding.vertical,
|
|
137
|
+
paddingHorizontal: look.padding.horizontal,
|
|
138
|
+
minHeight: look.minHeight,
|
|
139
|
+
boxShadow: look.shadow,
|
|
140
|
+
},
|
|
141
|
+
style,
|
|
142
|
+
], children: _jsx(IconDefaultsProvider, { size: theme.isAndroid ? 16 : 18, color: look.text, children: typeof children === "string" || typeof children === "number" ? (_jsx(Text, { variant: look.variant, size: look.variant ? undefined : "sm", weight: look.weight, color: look.text, numberOfLines: maxWidth === "none" ? 1 : undefined, children: children })) : (children) }) })) }));
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Makes text a tooltip's trigger, as @plexui/ui's Tooltip.TriggerDecorator: a tap or a long press shows the tooltip,
|
|
146
|
+
* and the text gets the dotted underline of an open tooltip (the tertiary text color; Android draws it solid).
|
|
147
|
+
*/
|
|
148
|
+
export function TriggerDecorator({ children, focusable = true, testID, variant, size, weight, color, style, ...described }) {
|
|
149
|
+
const theme = useTheme();
|
|
150
|
+
const tooltip = useContext(TooltipContext);
|
|
151
|
+
const open = tooltip?.open ?? false;
|
|
152
|
+
return (_jsx(Text, { ...described,
|
|
153
|
+
// A text of its own: it takes the type the web's decorator inherits from the text around it
|
|
154
|
+
variant: variant, size: size, weight: weight, color: color, accessibilityRole: "text", onPress: tooltip?.show, onLongPress: tooltip?.show, suppressHighlighting: true, ...(Platform.OS === "web" && focusable ? { tabIndex: 0 } : null), testID: testID,
|
|
155
|
+
// The web's underline is there in a transparent color until the tooltip opens; Android draws no colored or
|
|
156
|
+
// dotted line, so the line itself comes and goes
|
|
157
|
+
style: [style, open ? [styles.decorator, { textDecorationColor: theme.color("--color-text-tertiary") }] : null], children: children }));
|
|
158
|
+
}
|
|
159
|
+
TriggerDecorator.displayName = "Tooltip.TriggerDecorator";
|
|
160
|
+
Tooltip.TriggerDecorator = TriggerDecorator;
|
|
161
|
+
/**
|
|
162
|
+
* The pending feedback of a CopyTooltip: its timer, and the copy it belongs to. A newer copy, a long press or a close
|
|
163
|
+
* drops an older one's.
|
|
164
|
+
*/
|
|
165
|
+
class Feedback {
|
|
166
|
+
mounted = true;
|
|
167
|
+
generation = 0;
|
|
168
|
+
timer;
|
|
169
|
+
/** Starts a new attempt and cancels the pending one. */
|
|
170
|
+
next() {
|
|
171
|
+
this.generation += 1;
|
|
172
|
+
clearTimeout(this.timer);
|
|
173
|
+
return this.generation;
|
|
174
|
+
}
|
|
175
|
+
current(generation) {
|
|
176
|
+
return generation === this.generation;
|
|
177
|
+
}
|
|
178
|
+
after(ms, run) {
|
|
179
|
+
clearTimeout(this.timer);
|
|
180
|
+
this.timer = setTimeout(run, ms);
|
|
181
|
+
}
|
|
182
|
+
mount() {
|
|
183
|
+
this.mounted = true;
|
|
184
|
+
return () => {
|
|
185
|
+
this.mounted = false;
|
|
186
|
+
clearTimeout(this.timer);
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
/** Copied! stays for a second, as on the web. */
|
|
191
|
+
const COPIED_FOR = 1000;
|
|
192
|
+
/**
|
|
193
|
+
* A trigger that copies a value, as @plexui/ui's CopyTooltip on a phone: a tap copies it and says Copied! with a check
|
|
194
|
+
* in a compact tooltip for a second; a long press shows Copy.
|
|
195
|
+
*/
|
|
196
|
+
export function CopyTooltip({ children, copyValue, onCopy, copy = writeClipboard, side = "top", align = "center", alignOffset = 0, sideOffset = 5, testID }) {
|
|
197
|
+
const [open, setOpen] = useState(false);
|
|
198
|
+
const [copied, setCopied] = useState(false);
|
|
199
|
+
const { anchor, place, setTrigger } = useMenuAnchor(open);
|
|
200
|
+
const [feedback] = useState(() => new Feedback());
|
|
201
|
+
useEffect(() => feedback.mount(), [feedback]);
|
|
202
|
+
const hide = () => {
|
|
203
|
+
feedback.next();
|
|
204
|
+
setOpen(false);
|
|
205
|
+
};
|
|
206
|
+
const run = async () => {
|
|
207
|
+
const own = feedback.next();
|
|
208
|
+
setCopied(false);
|
|
209
|
+
let succeeded = false;
|
|
210
|
+
try {
|
|
211
|
+
succeeded = (await copy(copyValue)) !== false;
|
|
212
|
+
}
|
|
213
|
+
catch {
|
|
214
|
+
succeeded = false;
|
|
215
|
+
}
|
|
216
|
+
if (!feedback.mounted)
|
|
217
|
+
return;
|
|
218
|
+
onCopy?.(succeeded);
|
|
219
|
+
if (!feedback.current(own) || !succeeded)
|
|
220
|
+
return;
|
|
221
|
+
setCopied(true);
|
|
222
|
+
place(() => setOpen(true));
|
|
223
|
+
feedback.after(COPIED_FOR, () => setOpen(false));
|
|
224
|
+
};
|
|
225
|
+
const ask = () => {
|
|
226
|
+
feedback.next();
|
|
227
|
+
setCopied(false);
|
|
228
|
+
place(() => setOpen(true));
|
|
229
|
+
feedback.after(SHOW_FOR, () => setOpen(false));
|
|
230
|
+
};
|
|
231
|
+
const first = Array.isArray(children) ? children[0] : children;
|
|
232
|
+
const child = typeof first === "string" || typeof first === "number" ? _jsx(TriggerDecorator, { children: first }) : first;
|
|
233
|
+
const own = isValidElement(child) ? child.props : null;
|
|
234
|
+
const trigger = isValidElement(child) && own
|
|
235
|
+
? cloneElement(child, {
|
|
236
|
+
...anchorProps(own, setTrigger, open ? () => place() : undefined),
|
|
237
|
+
onPress: (event) => {
|
|
238
|
+
own.onPress?.(event);
|
|
239
|
+
void run();
|
|
240
|
+
},
|
|
241
|
+
onLongPress: ask,
|
|
242
|
+
})
|
|
243
|
+
: child;
|
|
244
|
+
return (_jsxs(TooltipContext.Provider, { value: { open, show: () => void run() }, children: [trigger, _jsx(TooltipPanel, { open: open, onDismiss: hide, anchor: anchor, side: side, align: align, alignOffset: alignOffset, sideOffset: sideOffset, compact: true, maxWidth: "none", gutterSize: "md", testID: testID, children: _jsx(CopyLabel, { copied: copied }) })] }));
|
|
245
|
+
}
|
|
246
|
+
/** Copy with its icon, or Copied! with a check: the check's own room makes the gap smaller, as on the web. */
|
|
247
|
+
function CopyLabel({ copied }) {
|
|
248
|
+
const theme = useTheme();
|
|
249
|
+
const look = tooltipLook(theme, true, "md");
|
|
250
|
+
return (_jsxs(View, { style: [styles.copyLabel, { gap: copied ? 2 : 4 }], children: [copied ? _jsx(CheckMd, {}) : _jsx(Copy, {}), _jsx(Text, { variant: look.variant, size: look.variant ? undefined : "sm", weight: look.weight, color: look.text, children: copied ? "Copied!" : "Copy" })] }));
|
|
251
|
+
}
|
|
252
|
+
/** The web's Clipboard API; React Native's own clipboard module on a phone (its `setString`). */
|
|
253
|
+
async function writeClipboard(value) {
|
|
254
|
+
if (Platform.OS === "web") {
|
|
255
|
+
const clipboard = globalThis.navigator?.clipboard;
|
|
256
|
+
if (!clipboard?.writeText)
|
|
257
|
+
return false;
|
|
258
|
+
await clipboard.writeText(value);
|
|
259
|
+
return true;
|
|
260
|
+
}
|
|
261
|
+
const module = NativeModules.Clipboard;
|
|
262
|
+
if (!module?.setString)
|
|
263
|
+
return false;
|
|
264
|
+
module.setString(value);
|
|
265
|
+
return true;
|
|
266
|
+
}
|
|
267
|
+
const styles = StyleSheet.create({
|
|
268
|
+
panel: { justifyContent: "center", borderCurve: "continuous" },
|
|
269
|
+
hidden: { display: "none" },
|
|
270
|
+
decorator: { textDecorationLine: "underline", textDecorationStyle: "dotted" },
|
|
271
|
+
copyLabel: { flexDirection: "row", alignItems: "center" },
|
|
272
|
+
});
|
package/dist/es/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// @plexui/react-native: the components of Plex UI for React Native, drawn as iOS 26 or Material 3 from one set of
|
|
2
2
|
// tokens (@plexui/styles).
|
|
3
|
-
export { ThemeProvider, createTheme, defaultPlatform, interFonts, useTheme } from "./theme/ThemeProvider";
|
|
3
|
+
export { ThemeProvider, createTheme, defaultPlatform, interFonts, robotoFonts, useTheme } from "./theme/ThemeProvider";
|
|
4
4
|
export { materialColors, stateLayer, withAlpha } from "./theme/material";
|
|
5
5
|
export { materialType, typeStyle } from "./theme/typography";
|
|
6
6
|
export { Text, Heading, textColor, textStyle, TextColorProvider } from "./components/Text/Text";
|
|
@@ -17,6 +17,11 @@ export { Switch } from "./components/Switch/Switch";
|
|
|
17
17
|
export { Checkbox } from "./components/Checkbox/Checkbox";
|
|
18
18
|
export { RadioGroup } from "./components/RadioGroup/RadioGroup";
|
|
19
19
|
export { Input, InputAdornmentButton, TextField } from "./components/Input/Input";
|
|
20
|
+
export { Field, FieldError } from "./components/Field/Field";
|
|
21
|
+
export { Textarea } from "./components/Textarea/Textarea";
|
|
22
|
+
export { FloatingLabelInput } from "./components/FloatingLabelInput/FloatingLabelInput";
|
|
23
|
+
export { InputGroup, } from "./components/InputGroup/InputGroup";
|
|
24
|
+
export { TagInput } from "./components/TagInput/TagInput";
|
|
20
25
|
export { OTPInput } from "./components/OTPInput/OTPInput";
|
|
21
26
|
export { Select } from "./components/Select/Select";
|
|
22
27
|
export { Menu } from "./components/Menu/Menu";
|
|
@@ -26,10 +31,23 @@ export { Dialog } from "./components/Dialog/Dialog";
|
|
|
26
31
|
export { Toaster, toast } from "./components/Toast/Toast";
|
|
27
32
|
export { Avatar, AvatarGroup } from "./components/Avatar/Avatar";
|
|
28
33
|
export { Badge } from "./components/Badge/Badge";
|
|
29
|
-
export {
|
|
34
|
+
export { BottomActions } from "./components/BottomActions/BottomActions";
|
|
35
|
+
export { Card, cardColors } from "./components/Card/Card";
|
|
30
36
|
export { List, ListItem } from "./components/List/List";
|
|
31
37
|
export { Chip } from "./components/Chip/Chip";
|
|
32
38
|
export { Skeleton } from "./components/Skeleton/Skeleton";
|
|
39
|
+
export { Progress, progressSegments } from "./components/Progress/Progress";
|
|
40
|
+
export { ProgressSteps } from "./components/ProgressSteps/ProgressSteps";
|
|
41
|
+
export { Stepper, stepValue } from "./components/Stepper/Stepper";
|
|
42
|
+
export { CircularProgress, getSimulatedProgress, LoadingDots, LoadingIndicator } from "./components/Indicator";
|
|
43
|
+
export { ShimmerableText, ShimmerText } from "./components/ShimmerText/ShimmerText";
|
|
44
|
+
export { RollingNumber, rollingNumberParts } from "./components/RollingNumber/RollingNumber";
|
|
33
45
|
export { EmptyMessage, EmptyState } from "./components/EmptyMessage/EmptyMessage";
|
|
34
46
|
export { Slider } from "./components/Slider/Slider";
|
|
35
47
|
export { PageControl, pageControlDots } from "./components/PageControl/PageControl";
|
|
48
|
+
export { Alert } from "./components/Alert/Alert";
|
|
49
|
+
export { Tip } from "./components/Tip/Tip";
|
|
50
|
+
export { CopyTooltip, Tooltip } from "./components/Tooltip/Tooltip";
|
|
51
|
+
export { Popover, usePopoverController, } from "./components/Popover/Popover";
|
|
52
|
+
export { Separator } from "./components/Separator/Separator";
|
|
53
|
+
export { TextLink } from "./components/TextLink/TextLink";
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { useEffect, useState } from "react";
|
|
3
|
+
import { Animated, Easing, I18nManager, Platform, useWindowDimensions } from "react-native";
|
|
4
|
+
import { useSafeAreaInsets } from "react-native-safe-area-context";
|
|
5
|
+
import { useTheme } from "../theme/ThemeProvider";
|
|
6
|
+
import { floatingOrigin, placeFloating } from "./floating";
|
|
7
|
+
import { curves } from "./motion";
|
|
8
|
+
import { ACCELERATE_DECELERATE, canTransformGlass, glassOpacity, Overlay } from "./Overlay";
|
|
9
|
+
/** The collision padding of each side, as Radix reads it: a number for all four, an object's missing sides 0. */
|
|
10
|
+
export function paddingOf(padding = 0) {
|
|
11
|
+
if (typeof padding === "number")
|
|
12
|
+
return { top: padding, right: padding, bottom: padding, left: padding };
|
|
13
|
+
return { top: padding.top ?? 0, right: padding.right ?? 0, bottom: padding.bottom ?? 0, left: padding.left ?? 0 };
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* A panel at its trigger, over the app in the overlay layer (a transparent Modal: a tap outside, Android's back and
|
|
17
|
+
* the web's Escape close it). It is measured off the screen first, then placed on its side of the trigger (turned
|
|
18
|
+
* when it does not fit, moved along to stay inside the window and the safe area), as Radix Popper places the web's
|
|
19
|
+
* popovers. It grows from the trigger's side: on iOS from 0.95 as it fades in on the overlay's time (`--motion-enter`
|
|
20
|
+
* on `--cubic-enter`), as the web's phone popover; on Android as a popup in the Plex Android look, a fade over 150ms
|
|
21
|
+
* (accelerate-decelerate) as it grows from 0.95 over 200 on an overshoot of tension 2. With reduced motion it only
|
|
22
|
+
* fades.
|
|
23
|
+
*/
|
|
24
|
+
export function FloatingPanel({ open, onDismiss, anchor, side = "bottom", align = "center", sideOffset = 0, alignOffset = 0, collisionPadding = 0, avoidCollisions = true, arrow, glass = false, motion = "popup", passThrough = false, autoFocus = true, role, "aria-label": ariaLabel, "aria-labelledby": ariaLabelledBy, testID, style, translateX, children, }) {
|
|
25
|
+
const theme = useTheme();
|
|
26
|
+
const window = useWindowDimensions();
|
|
27
|
+
const insets = useSafeAreaInsets();
|
|
28
|
+
const reduced = theme.reducedMotion;
|
|
29
|
+
const android = theme.isAndroid;
|
|
30
|
+
const tooltip = motion === "tooltip";
|
|
31
|
+
// Unknown until the panel's first layout, off the screen: a guess would show it jump or turn
|
|
32
|
+
const [size, setSize] = useState(null);
|
|
33
|
+
const [wasOpen, setWasOpen] = useState(open);
|
|
34
|
+
// Android's popup runs on its own clock from the moment it is placed
|
|
35
|
+
const [clock] = useState(() => new Animated.Value(0));
|
|
36
|
+
if (wasOpen !== open) {
|
|
37
|
+
setWasOpen(open);
|
|
38
|
+
if (open) {
|
|
39
|
+
setSize(null);
|
|
40
|
+
clock.setValue(0);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
const placed = size != null;
|
|
44
|
+
useEffect(() => {
|
|
45
|
+
if (!android || tooltip || !open || !placed || reduced)
|
|
46
|
+
return;
|
|
47
|
+
clock.setValue(0);
|
|
48
|
+
const animation = Animated.timing(clock, { toValue: 1, duration: POP.scale, easing: Easing.linear, useNativeDriver: false });
|
|
49
|
+
animation.start();
|
|
50
|
+
return () => animation.stop();
|
|
51
|
+
// Once for each opening, as the panel is placed
|
|
52
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
53
|
+
}, [android, tooltip, open, placed]);
|
|
54
|
+
const pad = paddingOf(collisionPadding);
|
|
55
|
+
const padding = {
|
|
56
|
+
top: Math.max(pad.top, insets.top),
|
|
57
|
+
bottom: Math.max(pad.bottom, insets.bottom),
|
|
58
|
+
left: Math.max(pad.left, insets.left),
|
|
59
|
+
right: Math.max(pad.right, insets.right),
|
|
60
|
+
};
|
|
61
|
+
const box = anchor ?? { x: window.width / 2, y: window.height / 2, width: 0, height: 0 };
|
|
62
|
+
const place = size ? placeFloating({ anchor: box, size, window, side, align, sideOffset, alignOffset, padding, flip: avoidCollisions, arrow }) : null;
|
|
63
|
+
const top = place ? place.top : -window.height - 1000;
|
|
64
|
+
const left = place?.left ?? 0;
|
|
65
|
+
// The measures are from the screen's left; a phone in a right-to-left layout reads `left` as the start edge
|
|
66
|
+
const swapped = Platform.OS !== "web" && I18nManager.isRTL && I18nManager.doLeftAndRightSwapInRTL;
|
|
67
|
+
const horizontal = swapped ? { right: left } : { left };
|
|
68
|
+
const placement = { side: place?.side ?? side, arrow: place?.arrow ?? 0, size };
|
|
69
|
+
const origin = size ? floatingOrigin(placement.side, placement.arrow, size) : undefined;
|
|
70
|
+
const popFade = clock.interpolate({ inputRange: [0, POP.fade / POP.scale], outputRange: [0, 1], easing: ACCELERATE_DECELERATE, extrapolate: "clamp" });
|
|
71
|
+
const popScale = clock.interpolate({ inputRange: [0, 1], outputRange: [0.95, 1], easing: POP.easing });
|
|
72
|
+
return (_jsx(Overlay, { open: open, onDismiss: onDismiss, motion: "menu", enter: tooltip ? (android ? TOOLTIP.androidEnter : theme.duration("--transition-duration-basic", TOOLTIP.enter)) : undefined, exit: tooltip ? (android ? TOOLTIP.androidExit : 0) : undefined, testID: testID, children: (progress, opening, focus) => {
|
|
73
|
+
// Android: the pop on its clock as it opens, a fade as it leaves; iOS: the overlay's progress, glass at once
|
|
74
|
+
const opacity = reduced || !opening || tooltip ? progress : android ? popFade : glass ? glassOpacity(progress, opening) : progress;
|
|
75
|
+
const scaleBy = reduced
|
|
76
|
+
? null
|
|
77
|
+
: tooltip
|
|
78
|
+
? android
|
|
79
|
+
? progress.interpolate({ inputRange: [0, 1], outputRange: [0.8, 1] })
|
|
80
|
+
: null
|
|
81
|
+
: android
|
|
82
|
+
? opening
|
|
83
|
+
? popScale
|
|
84
|
+
: null
|
|
85
|
+
: glass && !canTransformGlass
|
|
86
|
+
? null
|
|
87
|
+
: progress.interpolate({ inputRange: [0, 1], outputRange: [0.95, 1] });
|
|
88
|
+
const transform = [
|
|
89
|
+
...(translateX ? [{ translateX: translateX }] : []),
|
|
90
|
+
...(scaleBy ? [{ scale: scaleBy }] : []),
|
|
91
|
+
];
|
|
92
|
+
return (_jsx(Animated.View, { ...(autoFocus ? focus : null), pointerEvents: passThrough ? "none" : "auto", role: role, "aria-label": ariaLabel, "aria-labelledby": ariaLabelledBy, onLayout: (event) => {
|
|
93
|
+
const { width, height } = event.nativeEvent.layout;
|
|
94
|
+
if (!size || size.width !== width || size.height !== height)
|
|
95
|
+
setSize({ width, height });
|
|
96
|
+
}, style: [
|
|
97
|
+
{ position: "absolute", top },
|
|
98
|
+
horizontal,
|
|
99
|
+
style,
|
|
100
|
+
{ opacity: place ? opacity : 0, transformOrigin: origin, transform: transform.length ? transform : undefined },
|
|
101
|
+
autoFocus ? focus.style : null,
|
|
102
|
+
], children: children(placement) }));
|
|
103
|
+
} }));
|
|
104
|
+
}
|
|
105
|
+
/** A tooltip's times: the web's `--transition-duration-basic` fade; Material's tooltip fade in and out (150, 75). */
|
|
106
|
+
const TOOLTIP = { enter: 120, androidEnter: 150, androidExit: 75 };
|
|
107
|
+
/**
|
|
108
|
+
* Android's popup entrance in the Plex Android look: a fade over 150ms (accelerate-decelerate) and a scale from 0.95
|
|
109
|
+
* over 200 on an overshoot of tension 2 (OvershootInterpolator(2)), as its message popups pop in.
|
|
110
|
+
*/
|
|
111
|
+
const POP = { fade: 150, scale: 200, easing: curves.overshoot(2) };
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { createContext, useEffect, useRef, useState } from "react";
|
|
3
|
+
import { Animated, StyleSheet, View } from "react-native";
|
|
4
|
+
import { dangerText } from "../theme/material";
|
|
5
|
+
import { useTheme } from "../theme/ThemeProvider";
|
|
6
|
+
import { Text } from "../components/Text/Text";
|
|
7
|
+
import { stiffSpring } from "./motion";
|
|
8
|
+
import { useDescription } from "./a11y";
|
|
9
|
+
/**
|
|
10
|
+
* Android: the label a Field hands the outlined field it wraps (an Input, a Textarea), which floats it in its frame as
|
|
11
|
+
* TextField's does, with the asterisk of a required one and the shake of an error. Null elsewhere.
|
|
12
|
+
*/
|
|
13
|
+
export const FloatLabelContext = createContext(null);
|
|
14
|
+
/**
|
|
15
|
+
* A label, a control and the help or the error under it: the stacked field of TextField and Field. iOS sets the label
|
|
16
|
+
* over the control (15/20 medium; no phone token for a stacked field's label), 8 above it, and the description or the
|
|
17
|
+
* error 4pt under the control (15/20, `--mobile-field-error-*`). Android floats the label in a field that draws it
|
|
18
|
+
* (the outlined field), or sets it over any other control (14/18 medium in the secondary gray, 14 in, as the help),
|
|
19
|
+
* and the help under it (14/18, 14 in, 6 under), the field shaken when an error appears.
|
|
20
|
+
*/
|
|
21
|
+
export function FieldStack({ label, hideLabel = false, required = false, description, errorMessage, invalid = false, labelAction, disabled = false, floating, describedBy: ownDescribedBy, hint, style, testID, children, }) {
|
|
22
|
+
const theme = useTheme();
|
|
23
|
+
const error = errorMessage != null && errorMessage !== false && errorMessage !== "";
|
|
24
|
+
const bad = invalid || error;
|
|
25
|
+
const help = error ? errorMessage : description;
|
|
26
|
+
// The help or the error describes the field: read after its label (a hint on a phone, `aria-describedby` on the
|
|
27
|
+
// web). A caller's own hint wins; its described-by ids are kept with this one.
|
|
28
|
+
const description_ = useDescription(help);
|
|
29
|
+
const describedBy = [ownDescribedBy, description_.props["aria-describedby"]].filter(Boolean).join(" ") || undefined;
|
|
30
|
+
const described = {
|
|
31
|
+
...description_.props,
|
|
32
|
+
...(describedBy ? { "aria-describedby": describedBy } : null),
|
|
33
|
+
...(hint != null ? { accessibilityHint: hint } : null),
|
|
34
|
+
};
|
|
35
|
+
// Android shakes the field when an error message appears: flung left at 350dp/s on a spring of stiffness 600 at half
|
|
36
|
+
// its critical damping, it swings back and settles where it stood
|
|
37
|
+
const [shake] = useState(() => new Animated.Value(0));
|
|
38
|
+
const hadError = useRef(error);
|
|
39
|
+
useEffect(() => {
|
|
40
|
+
const appeared = error && !hadError.current;
|
|
41
|
+
hadError.current = error;
|
|
42
|
+
if (!appeared || !theme.isAndroid || theme.reducedMotion)
|
|
43
|
+
return;
|
|
44
|
+
shake.setValue(0);
|
|
45
|
+
Animated.spring(shake, { toValue: 0, velocity: -350, ...stiffSpring(600, 0.5), useNativeDriver: false }).start();
|
|
46
|
+
}, [error, shake, theme.isAndroid, theme.reducedMotion]);
|
|
47
|
+
const control = (shift) => children({ described, invalid: bad, shake: shift ? shake : undefined });
|
|
48
|
+
const asterisk = (color) => (required ? _jsx(Text, { color: color, children: " *" }) : null);
|
|
49
|
+
if (theme.isAndroid) {
|
|
50
|
+
const labelLine = floating || hideLabel ? null : (
|
|
51
|
+
// Over a control that does not float a label: the help's type and inset, Medium
|
|
52
|
+
_jsxs(View, { style: styles.labelRow, children: [_jsxs(Text, { weight: "medium", color: theme.colors.textSecondary, style: [styles.androidLabel, { opacity: disabled ? 0.5 : 1 }], children: [label, asterisk(dangerText(theme))] }), labelAction] }));
|
|
53
|
+
return (_jsxs(View, { style: [styles.field, style], testID: testID, children: [labelLine, floating ? control(true) : _jsx(Animated.View, { style: { transform: [{ translateX: shake }] }, children: control(false) }), help != null || (floating && labelAction != null) ? (_jsxs(View, { style: styles.helpRow, children: [help != null ? (
|
|
54
|
+
// Android's help under a field: 14 in the gray of secondary text, or in the error color, at the text's 14
|
|
55
|
+
// (dimmed with a disabled field)
|
|
56
|
+
_jsx(Text, { nativeID: description_.id, color: bad ? dangerText(theme) : theme.colors.textSecondary, accessibilityLiveRegion: error ? "polite" : undefined, style: [styles.androidHelp, { opacity: disabled ? 0.5 : 1 }], children: help })) : (_jsx(View, { style: styles.grow })), floating ? labelAction : null] })) : null] }));
|
|
57
|
+
}
|
|
58
|
+
const labelText = (_jsxs(Text, { weight: "medium", style: [styles.iosLabel, labelAction != null ? styles.grow : null], color: disabled ? "disabled" : "primary", children: [label, asterisk(theme.color("--color-text-danger"))] }));
|
|
59
|
+
return (_jsxs(View, { style: [styles.field, { gap: 8 }, style], testID: testID, children: [hideLabel ? null : labelAction != null ? (_jsxs(View, { style: styles.labelRow, children: [labelText, labelAction] })) : (labelText), control(false), help != null ? (_jsx(Text, { nativeID: description_.id, color: bad ? theme.color("--color-text-danger") : "secondary", accessibilityLiveRegion: error ? "polite" : undefined, style: {
|
|
60
|
+
fontSize: theme.length("--mobile-field-error-font-size", 15),
|
|
61
|
+
lineHeight: theme.length("--mobile-field-error-line-height", 20),
|
|
62
|
+
marginTop: theme.length("--mobile-field-error-offset", 4) - 8,
|
|
63
|
+
}, children: help })) : null] }));
|
|
64
|
+
}
|
|
65
|
+
const styles = StyleSheet.create({
|
|
66
|
+
field: { alignSelf: "stretch" },
|
|
67
|
+
grow: { flex: 1, minWidth: 0 },
|
|
68
|
+
labelRow: { flexDirection: "row", alignItems: "center", gap: 8 },
|
|
69
|
+
iosLabel: { fontSize: 15, lineHeight: 20 },
|
|
70
|
+
androidLabel: { flex: 1, minWidth: 0, fontSize: 14, lineHeight: 18, paddingHorizontal: 14, paddingBottom: 6 },
|
|
71
|
+
helpRow: { flexDirection: "row", alignItems: "flex-start" },
|
|
72
|
+
androidHelp: { flex: 1, minWidth: 0, fontSize: 14, lineHeight: 18, paddingHorizontal: 14, paddingTop: 6 },
|
|
73
|
+
});
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { I18nManager, Platform } from "react-native";
|
|
2
|
+
const opposite = { top: "bottom", bottom: "top", left: "right", right: "left" };
|
|
3
|
+
/**
|
|
4
|
+
* Places a floating panel at its trigger as Radix Popper does on the web (floating-ui's `offset`, `flip`, `shift` and
|
|
5
|
+
* `arrow`): on its side, aligned to the trigger's start, center or end, turned to the other side when it does not fit
|
|
6
|
+
* there and the other side has more room, then moved along the edge to stay inside the window's padding; an arrow
|
|
7
|
+
* keeps pointing at the trigger's middle, clear of the panel's corners.
|
|
8
|
+
*/
|
|
9
|
+
export function placeFloating({ anchor, size, window, side, align, sideOffset, alignOffset = 0, padding, flip = true, arrow, rtl }) {
|
|
10
|
+
const room = (s) => s === "bottom"
|
|
11
|
+
? window.height - padding.bottom - (anchor.y + anchor.height + sideOffset)
|
|
12
|
+
: s === "top"
|
|
13
|
+
? anchor.y - sideOffset - padding.top
|
|
14
|
+
: s === "right"
|
|
15
|
+
? window.width - padding.right - (anchor.x + anchor.width + sideOffset)
|
|
16
|
+
: anchor.x - sideOffset - padding.left;
|
|
17
|
+
const needed = (s) => (s === "top" || s === "bottom" ? size.height : size.width);
|
|
18
|
+
let placed = side;
|
|
19
|
+
if (flip && room(side) < needed(side) && room(opposite[side]) > room(side))
|
|
20
|
+
placed = opposite[side];
|
|
21
|
+
const vertical = placed === "top" || placed === "bottom";
|
|
22
|
+
// The main axis
|
|
23
|
+
let left = 0;
|
|
24
|
+
let top = 0;
|
|
25
|
+
if (placed === "bottom")
|
|
26
|
+
top = anchor.y + anchor.height + sideOffset;
|
|
27
|
+
else if (placed === "top")
|
|
28
|
+
top = anchor.y - sideOffset - size.height;
|
|
29
|
+
else if (placed === "right")
|
|
30
|
+
left = anchor.x + anchor.width + sideOffset;
|
|
31
|
+
else
|
|
32
|
+
left = anchor.x - sideOffset - size.width;
|
|
33
|
+
// The cross axis: start and end are the reading edges along a horizontal edge
|
|
34
|
+
const length = vertical ? size.width : size.height;
|
|
35
|
+
const start = vertical ? anchor.x : anchor.y;
|
|
36
|
+
const extent = vertical ? anchor.width : anchor.height;
|
|
37
|
+
const flipped = vertical && (rtl ?? isRTLLayout());
|
|
38
|
+
const edge = align === "center" ? "center" : (align === "start") !== flipped ? "low" : "high";
|
|
39
|
+
let cross = edge === "center" ? start + extent / 2 - length / 2 : edge === "low" ? start + alignOffset : start + extent - length - alignOffset;
|
|
40
|
+
const low = vertical ? padding.left : padding.top;
|
|
41
|
+
const high = (vertical ? window.width - padding.right : window.height - padding.bottom) - length;
|
|
42
|
+
cross = Math.max(low, Math.min(cross, Math.max(low, high)));
|
|
43
|
+
if (vertical)
|
|
44
|
+
left = cross;
|
|
45
|
+
else
|
|
46
|
+
top = cross;
|
|
47
|
+
// The arrow points at the trigger's middle, clear of the corners
|
|
48
|
+
const middle = start + extent / 2 - cross;
|
|
49
|
+
const half = (arrow?.width ?? 0) / 2;
|
|
50
|
+
const inset = (arrow?.padding ?? 0) + half;
|
|
51
|
+
const arrowAt = Math.max(inset, Math.min(middle, length - inset));
|
|
52
|
+
return { left, top, side: placed, arrow: arrowAt };
|
|
53
|
+
}
|
|
54
|
+
/** The layout's direction: a phone set to a right-to-left language (react-native-web reads the document's). */
|
|
55
|
+
function isRTLLayout() {
|
|
56
|
+
if (Platform.OS === "web") {
|
|
57
|
+
const doc = globalThis.document;
|
|
58
|
+
return doc?.documentElement?.dir === "rtl";
|
|
59
|
+
}
|
|
60
|
+
return I18nManager.isRTL;
|
|
61
|
+
}
|
|
62
|
+
/** The origin a panel grows from: the middle of its edge at the trigger, as Radix's transform origin. */
|
|
63
|
+
export function floatingOrigin(side, arrow, size) {
|
|
64
|
+
switch (side) {
|
|
65
|
+
case "bottom":
|
|
66
|
+
return `${arrow}px 0px`;
|
|
67
|
+
case "top":
|
|
68
|
+
return `${arrow}px ${size.height}px`;
|
|
69
|
+
case "right":
|
|
70
|
+
return `0px ${arrow}px`;
|
|
71
|
+
case "left":
|
|
72
|
+
return `${size.width}px ${arrow}px`;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Props for a trigger element that a floating panel stands at: its node as the panel's anchor (the element itself,
|
|
77
|
+
* so the trigger keeps its own place in the layout: a Button, a Pressable, a View or a Text hands its native view to
|
|
78
|
+
* `ref`), after its own ref, and its layout, after its own `onLayout`, to place the panel again as it moves.
|
|
79
|
+
*/
|
|
80
|
+
export function anchorProps(props, setNode, onMove) {
|
|
81
|
+
return {
|
|
82
|
+
ref: (node) => {
|
|
83
|
+
setNode(node);
|
|
84
|
+
assignRef(props.ref, node);
|
|
85
|
+
},
|
|
86
|
+
onLayout: (event) => {
|
|
87
|
+
props.onLayout?.(event);
|
|
88
|
+
onMove?.();
|
|
89
|
+
},
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
function assignRef(ref, value) {
|
|
93
|
+
if (typeof ref === "function")
|
|
94
|
+
ref(value);
|
|
95
|
+
else if (ref != null && typeof ref === "object" && "current" in ref)
|
|
96
|
+
ref.current = value;
|
|
97
|
+
}
|