@plexui/react-native 0.1.1 → 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.
Files changed (149) hide show
  1. package/README.md +34 -0
  2. package/dist/es/components/ActionSheet/ActionSheet.js +53 -19
  3. package/dist/es/components/Alert/Alert.js +163 -0
  4. package/dist/es/components/Avatar/Avatar.js +20 -8
  5. package/dist/es/components/BottomActions/BottomActions.js +34 -0
  6. package/dist/es/components/BottomSheet/BottomSheet.js +85 -17
  7. package/dist/es/components/Button/Button.js +108 -28
  8. package/dist/es/components/Card/Card.js +16 -8
  9. package/dist/es/components/Checkbox/Checkbox.js +67 -99
  10. package/dist/es/components/Chip/Chip.js +16 -7
  11. package/dist/es/components/Dialog/Dialog.js +38 -24
  12. package/dist/es/components/Field/Field.js +91 -0
  13. package/dist/es/components/FloatingLabelInput/FloatingLabelInput.js +59 -0
  14. package/dist/es/components/Icon/createIcon.js +13 -2
  15. package/dist/es/components/Indicator/CircularProgress.js +80 -0
  16. package/dist/es/components/Indicator/LoadingDots.js +46 -0
  17. package/dist/es/components/Indicator/LoadingIndicator.js +101 -0
  18. package/dist/es/components/Indicator/index.js +3 -0
  19. package/dist/es/components/Input/Input.js +69 -60
  20. package/dist/es/components/InputGroup/InputGroup.js +130 -0
  21. package/dist/es/components/List/List.js +175 -59
  22. package/dist/es/components/Menu/Menu.js +69 -40
  23. package/dist/es/components/NavigationButton/NavigationButton.js +20 -4
  24. package/dist/es/components/OTPInput/OTPInput.js +9 -3
  25. package/dist/es/components/PageControl/PageControl.js +13 -7
  26. package/dist/es/components/Popover/Popover.js +128 -0
  27. package/dist/es/components/Progress/Progress.js +142 -0
  28. package/dist/es/components/ProgressSteps/ProgressSteps.js +207 -0
  29. package/dist/es/components/RollingNumber/RollingNumber.js +125 -0
  30. package/dist/es/components/Select/Select.js +97 -34
  31. package/dist/es/components/Separator/Separator.js +31 -0
  32. package/dist/es/components/ShimmerText/ShimmerText.js +98 -0
  33. package/dist/es/components/Stepper/Stepper.js +219 -0
  34. package/dist/es/components/Switch/Switch.js +26 -12
  35. package/dist/es/components/TagInput/TagInput.js +121 -0
  36. package/dist/es/components/Text/Text.js +7 -4
  37. package/dist/es/components/TextLink/TextLink.js +42 -0
  38. package/dist/es/components/Textarea/Textarea.js +12 -0
  39. package/dist/es/components/Tip/Tip.js +193 -0
  40. package/dist/es/components/Toolbar/Toolbar.js +6 -2
  41. package/dist/es/components/Tooltip/Tooltip.js +272 -0
  42. package/dist/es/index.js +20 -2
  43. package/dist/es/internal/FloatingPanel.js +111 -0
  44. package/dist/es/internal/fieldStack.js +73 -0
  45. package/dist/es/internal/floating.js +97 -0
  46. package/dist/es/internal/ripple.js +119 -0
  47. package/dist/es/internal/row.js +2 -1
  48. package/dist/es/internal/transition.js +113 -0
  49. package/dist/es/theme/ThemeProvider.js +41 -9
  50. package/dist/es/theme/material.js +37 -7
  51. package/dist/types/components/ActionSheet/ActionSheet.d.ts +15 -7
  52. package/dist/types/components/Alert/Alert.d.ts +51 -0
  53. package/dist/types/components/Avatar/Avatar.d.ts +4 -2
  54. package/dist/types/components/BottomActions/BottomActions.d.ts +18 -0
  55. package/dist/types/components/BottomSheet/BottomSheet.d.ts +4 -2
  56. package/dist/types/components/Button/Button.d.ts +20 -5
  57. package/dist/types/components/Card/Card.d.ts +14 -1
  58. package/dist/types/components/Checkbox/Checkbox.d.ts +4 -3
  59. package/dist/types/components/Chip/Chip.d.ts +12 -1
  60. package/dist/types/components/Dialog/Dialog.d.ts +9 -5
  61. package/dist/types/components/Field/Field.d.ts +64 -0
  62. package/dist/types/components/FloatingLabelInput/FloatingLabelInput.d.ts +42 -0
  63. package/dist/types/components/Indicator/CircularProgress.d.ts +34 -0
  64. package/dist/types/components/Indicator/LoadingDots.d.ts +17 -0
  65. package/dist/types/components/Indicator/LoadingIndicator.d.ts +46 -0
  66. package/dist/types/components/Indicator/index.d.ts +3 -0
  67. package/dist/types/components/Input/Input.d.ts +13 -3
  68. package/dist/types/components/InputGroup/InputGroup.d.ts +71 -0
  69. package/dist/types/components/List/List.d.ts +57 -14
  70. package/dist/types/components/Menu/Menu.d.ts +4 -0
  71. package/dist/types/components/NavigationButton/NavigationButton.d.ts +11 -3
  72. package/dist/types/components/OTPInput/OTPInput.d.ts +10 -1
  73. package/dist/types/components/PageControl/PageControl.d.ts +11 -1
  74. package/dist/types/components/Popover/Popover.d.ts +86 -0
  75. package/dist/types/components/Progress/Progress.d.ts +56 -0
  76. package/dist/types/components/ProgressSteps/ProgressSteps.d.ts +61 -0
  77. package/dist/types/components/RollingNumber/RollingNumber.d.ts +36 -0
  78. package/dist/types/components/Select/Select.d.ts +36 -8
  79. package/dist/types/components/Separator/Separator.d.ts +19 -0
  80. package/dist/types/components/ShimmerText/ShimmerText.d.ts +28 -0
  81. package/dist/types/components/Stepper/Stepper.d.ts +61 -0
  82. package/dist/types/components/Switch/Switch.d.ts +5 -5
  83. package/dist/types/components/TagInput/TagInput.d.ts +44 -0
  84. package/dist/types/components/TextLink/TextLink.d.ts +30 -0
  85. package/dist/types/components/Textarea/Textarea.d.ts +19 -0
  86. package/dist/types/components/Tip/Tip.d.ts +71 -0
  87. package/dist/types/components/Toolbar/Toolbar.d.ts +9 -1
  88. package/dist/types/components/Tooltip/Tooltip.d.ts +84 -0
  89. package/dist/types/index.d.ts +20 -2
  90. package/dist/types/internal/FloatingPanel.d.ts +70 -0
  91. package/dist/types/internal/fieldStack.d.ts +48 -0
  92. package/dist/types/internal/floating.d.ts +71 -0
  93. package/dist/types/internal/ripple.d.ts +17 -0
  94. package/dist/types/internal/row.d.ts +15 -3
  95. package/dist/types/internal/transition.d.ts +37 -0
  96. package/dist/types/theme/ThemeProvider.d.ts +28 -4
  97. package/dist/types/theme/material.d.ts +20 -6
  98. package/dist/types/theme/types.d.ts +8 -0
  99. package/package.json +2 -2
  100. package/src/components/ActionSheet/ActionSheet.tsx +107 -38
  101. package/src/components/Alert/Alert.tsx +279 -0
  102. package/src/components/Avatar/Avatar.tsx +21 -9
  103. package/src/components/BottomActions/BottomActions.tsx +54 -0
  104. package/src/components/BottomSheet/BottomSheet.tsx +111 -25
  105. package/src/components/Button/Button.tsx +170 -35
  106. package/src/components/Card/Card.tsx +33 -9
  107. package/src/components/Checkbox/Checkbox.tsx +89 -124
  108. package/src/components/Chip/Chip.tsx +49 -6
  109. package/src/components/Dialog/Dialog.tsx +68 -37
  110. package/src/components/Field/Field.tsx +227 -0
  111. package/src/components/FloatingLabelInput/FloatingLabelInput.tsx +144 -0
  112. package/src/components/Icon/createIcon.tsx +16 -6
  113. package/src/components/Indicator/CircularProgress.tsx +141 -0
  114. package/src/components/Indicator/LoadingDots.tsx +79 -0
  115. package/src/components/Indicator/LoadingIndicator.tsx +166 -0
  116. package/src/components/Indicator/index.ts +3 -0
  117. package/src/components/Input/Input.tsx +126 -97
  118. package/src/components/InputGroup/InputGroup.tsx +261 -0
  119. package/src/components/List/List.tsx +345 -88
  120. package/src/components/Menu/Menu.tsx +83 -41
  121. package/src/components/NavigationButton/NavigationButton.tsx +35 -5
  122. package/src/components/OTPInput/OTPInput.tsx +21 -2
  123. package/src/components/PageControl/PageControl.tsx +36 -8
  124. package/src/components/Popover/Popover.tsx +261 -0
  125. package/src/components/Progress/Progress.tsx +255 -0
  126. package/src/components/ProgressSteps/ProgressSteps.tsx +415 -0
  127. package/src/components/RollingNumber/RollingNumber.tsx +189 -0
  128. package/src/components/Select/Select.tsx +175 -71
  129. package/src/components/Separator/Separator.tsx +51 -0
  130. package/src/components/ShimmerText/ShimmerText.tsx +144 -0
  131. package/src/components/Stepper/Stepper.tsx +386 -0
  132. package/src/components/Switch/Switch.tsx +26 -12
  133. package/src/components/TagInput/TagInput.tsx +227 -0
  134. package/src/components/Text/Text.tsx +8 -4
  135. package/src/components/TextLink/TextLink.tsx +77 -0
  136. package/src/components/Textarea/Textarea.tsx +23 -0
  137. package/src/components/Tip/Tip.tsx +410 -0
  138. package/src/components/Toolbar/Toolbar.tsx +27 -2
  139. package/src/components/Tooltip/Tooltip.tsx +478 -0
  140. package/src/index.ts +36 -2
  141. package/src/internal/FloatingPanel.tsx +199 -0
  142. package/src/internal/fieldStack.tsx +179 -0
  143. package/src/internal/floating.ts +131 -0
  144. package/src/internal/ripple.tsx +132 -0
  145. package/src/internal/row.ts +16 -3
  146. package/src/internal/transition.ts +124 -0
  147. package/src/theme/ThemeProvider.tsx +65 -15
  148. package/src/theme/material.ts +39 -7
  149. package/src/theme/types.ts +8 -0
@@ -0,0 +1,199 @@
1
+ import { useEffect, useState, type ReactNode } from "react"
2
+ import { Animated, Easing, I18nManager, Platform, useWindowDimensions, type Role, type StyleProp, type ViewStyle } from "react-native"
3
+ import { useSafeAreaInsets } from "react-native-safe-area-context"
4
+ import type { MenuAnchor } from "../components/Menu/Menu"
5
+ import { useTheme } from "../theme/ThemeProvider"
6
+ import { floatingOrigin, placeFloating, type FloatingAlign, type FloatingSide } from "./floating"
7
+ import { curves } from "./motion"
8
+ import { ACCELERATE_DECELERATE, canTransformGlass, glassOpacity, Overlay } from "./Overlay"
9
+
10
+ export type CollisionPadding = number | Partial<Record<"top" | "right" | "bottom" | "left", number>>
11
+
12
+ export type FloatingPlacement = { side: FloatingSide; arrow: number; size: { width: number; height: number } | null }
13
+
14
+ export type FloatingPanelProps = {
15
+ open: boolean
16
+ onDismiss: () => void
17
+ /** The trigger's box in the window (`measure`); the middle of the screen until it is measured. */
18
+ anchor: MenuAnchor | null
19
+ side?: FloatingSide
20
+ align?: FloatingAlign
21
+ sideOffset?: number
22
+ alignOffset?: number
23
+ /** Kept free at the window's edges, the safe area at least; an object leaves 0 at a side it does not name. */
24
+ collisionPadding?: CollisionPadding
25
+ /** Turns to the other side when it does not fit. @default true */
26
+ avoidCollisions?: boolean
27
+ /** An arrow's length and the room it keeps from the panel's corners. */
28
+ arrow?: { width: number; padding: number }
29
+ /** The panel's glass appears at once and fades only as it leaves (`glassOpacity`). */
30
+ glass?: boolean
31
+ /**
32
+ * `popup`: it grows from 0.95 (Android's pop on its own clock); `tooltip`: a tooltip's entrance, a fade on iOS (the
33
+ * web's 120ms on `--cubic-enter`, gone at once) and Material's on Android (a fade as it grows from 0.8 over 150ms
34
+ * on LinearOutSlowIn, a fade out over 75). @default "popup"
35
+ */
36
+ motion?: "popup" | "tooltip"
37
+ /** Touches go through the panel to the overlay under it, which closes it (a tooltip's text). */
38
+ passThrough?: boolean
39
+ /** The panel takes the first focus on the web. @default true */
40
+ autoFocus?: boolean
41
+ role?: Role
42
+ "aria-label"?: string
43
+ "aria-labelledby"?: string
44
+ testID?: string
45
+ /** The panel's size limits (its width and min and max width). */
46
+ style?: StyleProp<ViewStyle>
47
+ /** Moves the panel along its own axis, as a shake. */
48
+ translateX?: Animated.Value | Animated.AnimatedInterpolation<number>
49
+ children: (placement: FloatingPlacement) => ReactNode
50
+ }
51
+
52
+ /** The collision padding of each side, as Radix reads it: a number for all four, an object's missing sides 0. */
53
+ export function paddingOf(padding: CollisionPadding = 0) {
54
+ if (typeof padding === "number") return { top: padding, right: padding, bottom: padding, left: padding }
55
+ return { top: padding.top ?? 0, right: padding.right ?? 0, bottom: padding.bottom ?? 0, left: padding.left ?? 0 }
56
+ }
57
+
58
+ /**
59
+ * A panel at its trigger, over the app in the overlay layer (a transparent Modal: a tap outside, Android's back and
60
+ * the web's Escape close it). It is measured off the screen first, then placed on its side of the trigger (turned
61
+ * when it does not fit, moved along to stay inside the window and the safe area), as Radix Popper places the web's
62
+ * popovers. It grows from the trigger's side: on iOS from 0.95 as it fades in on the overlay's time (`--motion-enter`
63
+ * on `--cubic-enter`), as the web's phone popover; on Android as a popup in the Plex Android look, a fade over 150ms
64
+ * (accelerate-decelerate) as it grows from 0.95 over 200 on an overshoot of tension 2. With reduced motion it only
65
+ * fades.
66
+ */
67
+ export function FloatingPanel({
68
+ open,
69
+ onDismiss,
70
+ anchor,
71
+ side = "bottom",
72
+ align = "center",
73
+ sideOffset = 0,
74
+ alignOffset = 0,
75
+ collisionPadding = 0,
76
+ avoidCollisions = true,
77
+ arrow,
78
+ glass = false,
79
+ motion = "popup",
80
+ passThrough = false,
81
+ autoFocus = true,
82
+ role,
83
+ "aria-label": ariaLabel,
84
+ "aria-labelledby": ariaLabelledBy,
85
+ testID,
86
+ style,
87
+ translateX,
88
+ children,
89
+ }: FloatingPanelProps) {
90
+ const theme = useTheme()
91
+ const window = useWindowDimensions()
92
+ const insets = useSafeAreaInsets()
93
+ const reduced = theme.reducedMotion
94
+ const android = theme.isAndroid
95
+ const tooltip = motion === "tooltip"
96
+ // Unknown until the panel's first layout, off the screen: a guess would show it jump or turn
97
+ const [size, setSize] = useState<{ width: number; height: number } | null>(null)
98
+ const [wasOpen, setWasOpen] = useState(open)
99
+ // Android's popup runs on its own clock from the moment it is placed
100
+ const [clock] = useState(() => new Animated.Value(0))
101
+ if (wasOpen !== open) {
102
+ setWasOpen(open)
103
+ if (open) {
104
+ setSize(null)
105
+ clock.setValue(0)
106
+ }
107
+ }
108
+ const placed = size != null
109
+ useEffect(() => {
110
+ if (!android || tooltip || !open || !placed || reduced) return
111
+ clock.setValue(0)
112
+ const animation = Animated.timing(clock, { toValue: 1, duration: POP.scale, easing: Easing.linear, useNativeDriver: false })
113
+ animation.start()
114
+ return () => animation.stop()
115
+ // Once for each opening, as the panel is placed
116
+ // eslint-disable-next-line react-hooks/exhaustive-deps
117
+ }, [android, tooltip, open, placed])
118
+ const pad = paddingOf(collisionPadding)
119
+ const padding = {
120
+ top: Math.max(pad.top, insets.top),
121
+ bottom: Math.max(pad.bottom, insets.bottom),
122
+ left: Math.max(pad.left, insets.left),
123
+ right: Math.max(pad.right, insets.right),
124
+ }
125
+ const box = anchor ?? { x: window.width / 2, y: window.height / 2, width: 0, height: 0 }
126
+ const place = size ? placeFloating({ anchor: box, size, window, side, align, sideOffset, alignOffset, padding, flip: avoidCollisions, arrow }) : null
127
+ const top = place ? place.top : -window.height - 1000
128
+ const left = place?.left ?? 0
129
+ // The measures are from the screen's left; a phone in a right-to-left layout reads `left` as the start edge
130
+ const swapped = Platform.OS !== "web" && I18nManager.isRTL && I18nManager.doLeftAndRightSwapInRTL
131
+ const horizontal = swapped ? { right: left } : { left }
132
+ const placement: FloatingPlacement = { side: place?.side ?? side, arrow: place?.arrow ?? 0, size }
133
+ const origin = size ? floatingOrigin(placement.side, placement.arrow, size) : undefined
134
+ const popFade = clock.interpolate({ inputRange: [0, POP.fade / POP.scale], outputRange: [0, 1], easing: ACCELERATE_DECELERATE, extrapolate: "clamp" })
135
+ const popScale = clock.interpolate({ inputRange: [0, 1], outputRange: [0.95, 1], easing: POP.easing })
136
+ return (
137
+ <Overlay
138
+ open={open}
139
+ onDismiss={onDismiss}
140
+ motion="menu"
141
+ enter={tooltip ? (android ? TOOLTIP.androidEnter : theme.duration("--transition-duration-basic", TOOLTIP.enter)) : undefined}
142
+ exit={tooltip ? (android ? TOOLTIP.androidExit : 0) : undefined}
143
+ testID={testID}
144
+ >
145
+ {(progress, opening, focus) => {
146
+ // Android: the pop on its clock as it opens, a fade as it leaves; iOS: the overlay's progress, glass at once
147
+ const opacity = reduced || !opening || tooltip ? progress : android ? popFade : glass ? glassOpacity(progress, opening) : progress
148
+ const scaleBy = reduced
149
+ ? null
150
+ : tooltip
151
+ ? android
152
+ ? progress.interpolate({ inputRange: [0, 1], outputRange: [0.8, 1] })
153
+ : null
154
+ : android
155
+ ? opening
156
+ ? popScale
157
+ : null
158
+ : glass && !canTransformGlass
159
+ ? null
160
+ : progress.interpolate({ inputRange: [0, 1], outputRange: [0.95, 1] })
161
+ const transform: ViewStyle["transform"] = [
162
+ ...(translateX ? [{ translateX: translateX as unknown as number }] : []),
163
+ ...(scaleBy ? [{ scale: scaleBy as unknown as number }] : []),
164
+ ]
165
+ return (
166
+ <Animated.View
167
+ {...(autoFocus ? focus : null)}
168
+ pointerEvents={passThrough ? "none" : "auto"}
169
+ role={role}
170
+ aria-label={ariaLabel}
171
+ aria-labelledby={ariaLabelledBy}
172
+ onLayout={(event) => {
173
+ const { width, height } = event.nativeEvent.layout
174
+ if (!size || size.width !== width || size.height !== height) setSize({ width, height })
175
+ }}
176
+ style={[
177
+ { position: "absolute", top },
178
+ horizontal,
179
+ style,
180
+ { opacity: place ? opacity : 0, transformOrigin: origin, transform: transform.length ? transform : undefined },
181
+ autoFocus ? focus.style : null,
182
+ ]}
183
+ >
184
+ {children(placement)}
185
+ </Animated.View>
186
+ )
187
+ }}
188
+ </Overlay>
189
+ )
190
+ }
191
+
192
+ /** A tooltip's times: the web's `--transition-duration-basic` fade; Material's tooltip fade in and out (150, 75). */
193
+ const TOOLTIP = { enter: 120, androidEnter: 150, androidExit: 75 } as const
194
+
195
+ /**
196
+ * Android's popup entrance in the Plex Android look: a fade over 150ms (accelerate-decelerate) and a scale from 0.95
197
+ * over 200 on an overshoot of tension 2 (OvershootInterpolator(2)), as its message popups pop in.
198
+ */
199
+ const POP = { fade: 150, scale: 200, easing: curves.overshoot(2) } as const
@@ -0,0 +1,179 @@
1
+ import { createContext, useEffect, useRef, useState, type ReactNode } from "react"
2
+ import { Animated, StyleSheet, View, type StyleProp, type ViewStyle } from "react-native"
3
+ import { dangerText } from "../theme/material"
4
+ import { useTheme } from "../theme/ThemeProvider"
5
+ import { Text } from "../components/Text/Text"
6
+ import { stiffSpring } from "./motion"
7
+ import { useDescription } from "./a11y"
8
+
9
+ /** React Native Web's description link, which React Native's own types leave out. */
10
+ type DescribedBy = { "aria-describedby"?: string }
11
+
12
+ /**
13
+ * Android: the label a Field hands the outlined field it wraps (an Input, a Textarea), which floats it in its frame as
14
+ * TextField's does, with the asterisk of a required one and the shake of an error. Null elsewhere.
15
+ */
16
+ export const FloatLabelContext = createContext<{ label: ReactNode; required: boolean; shift?: Animated.Value } | null>(null)
17
+
18
+ /** What a field's stack hands its control: the description's link, the invalid state and Android's shake. */
19
+ export type FieldStackControl = {
20
+ /** `aria-describedby` on the web, `accessibilityHint` on a phone, with a caller's own kept. */
21
+ described: Record<string, unknown>
22
+ invalid: boolean
23
+ /** Android: the field moves sideways by it when an error appears (InputField's `shift`). */
24
+ shake?: Animated.Value
25
+ }
26
+
27
+ export type FieldStackProps = {
28
+ label: ReactNode
29
+ /** The label is not drawn; the control still carries it as its name. */
30
+ hideLabel?: boolean
31
+ required?: boolean
32
+ description?: ReactNode
33
+ errorMessage?: ReactNode
34
+ /** Marks the control invalid without a message (the help turns the danger color). */
35
+ invalid?: boolean
36
+ /** Content at the end of the label's line, such as a link that acts on the field. */
37
+ labelAction?: ReactNode
38
+ disabled?: boolean
39
+ /** Android: the control draws the label itself, floating in the field (an Input, a Textarea). */
40
+ floating: boolean
41
+ /** The caller's own `aria-describedby` and `accessibilityHint`, kept with the stack's. */
42
+ describedBy?: string
43
+ hint?: string
44
+ style?: StyleProp<ViewStyle>
45
+ testID?: string
46
+ children: (control: FieldStackControl) => ReactNode
47
+ }
48
+
49
+ /**
50
+ * A label, a control and the help or the error under it: the stacked field of TextField and Field. iOS sets the label
51
+ * over the control (15/20 medium; no phone token for a stacked field's label), 8 above it, and the description or the
52
+ * error 4pt under the control (15/20, `--mobile-field-error-*`). Android floats the label in a field that draws it
53
+ * (the outlined field), or sets it over any other control (14/18 medium in the secondary gray, 14 in, as the help),
54
+ * and the help under it (14/18, 14 in, 6 under), the field shaken when an error appears.
55
+ */
56
+ export function FieldStack({
57
+ label,
58
+ hideLabel = false,
59
+ required = false,
60
+ description,
61
+ errorMessage,
62
+ invalid = false,
63
+ labelAction,
64
+ disabled = false,
65
+ floating,
66
+ describedBy: ownDescribedBy,
67
+ hint,
68
+ style,
69
+ testID,
70
+ children,
71
+ }: FieldStackProps) {
72
+ const theme = useTheme()
73
+ const error = errorMessage != null && errorMessage !== false && errorMessage !== ""
74
+ const bad = invalid || error
75
+ const help = error ? errorMessage : description
76
+ // The help or the error describes the field: read after its label (a hint on a phone, `aria-describedby` on the
77
+ // web). A caller's own hint wins; its described-by ids are kept with this one.
78
+ const description_ = useDescription(help)
79
+ const describedBy = [ownDescribedBy, (description_.props as DescribedBy)["aria-describedby"]].filter(Boolean).join(" ") || undefined
80
+ const described = {
81
+ ...description_.props,
82
+ ...(describedBy ? { "aria-describedby": describedBy } : null),
83
+ ...(hint != null ? { accessibilityHint: hint } : null),
84
+ }
85
+ // Android shakes the field when an error message appears: flung left at 350dp/s on a spring of stiffness 600 at half
86
+ // its critical damping, it swings back and settles where it stood
87
+ const [shake] = useState(() => new Animated.Value(0))
88
+ const hadError = useRef(error)
89
+ useEffect(() => {
90
+ const appeared = error && !hadError.current
91
+ hadError.current = error
92
+ if (!appeared || !theme.isAndroid || theme.reducedMotion) return
93
+ shake.setValue(0)
94
+ Animated.spring(shake, { toValue: 0, velocity: -350, ...stiffSpring(600, 0.5), useNativeDriver: false }).start()
95
+ }, [error, shake, theme.isAndroid, theme.reducedMotion])
96
+ const control = (shift: boolean) => children({ described, invalid: bad, shake: shift ? shake : undefined })
97
+ const asterisk = (color: string) => (required ? <Text color={color}> *</Text> : null)
98
+ if (theme.isAndroid) {
99
+ const labelLine =
100
+ floating || hideLabel ? null : (
101
+ // Over a control that does not float a label: the help's type and inset, Medium
102
+ <View style={styles.labelRow}>
103
+ <Text weight="medium" color={theme.colors.textSecondary} style={[styles.androidLabel, { opacity: disabled ? 0.5 : 1 }]}>
104
+ {label}
105
+ {asterisk(dangerText(theme))}
106
+ </Text>
107
+ {labelAction}
108
+ </View>
109
+ )
110
+ return (
111
+ <View style={[styles.field, style]} testID={testID}>
112
+ {labelLine}
113
+ {floating ? control(true) : <Animated.View style={{ transform: [{ translateX: shake }] }}>{control(false)}</Animated.View>}
114
+ {help != null || (floating && labelAction != null) ? (
115
+ <View style={styles.helpRow}>
116
+ {help != null ? (
117
+ // Android's help under a field: 14 in the gray of secondary text, or in the error color, at the text's 14
118
+ // (dimmed with a disabled field)
119
+ <Text
120
+ nativeID={description_.id}
121
+ color={bad ? dangerText(theme) : theme.colors.textSecondary}
122
+ accessibilityLiveRegion={error ? "polite" : undefined}
123
+ style={[styles.androidHelp, { opacity: disabled ? 0.5 : 1 }]}
124
+ >
125
+ {help}
126
+ </Text>
127
+ ) : (
128
+ <View style={styles.grow} />
129
+ )}
130
+ {floating ? labelAction : null}
131
+ </View>
132
+ ) : null}
133
+ </View>
134
+ )
135
+ }
136
+ const labelText = (
137
+ <Text weight="medium" style={[styles.iosLabel, labelAction != null ? styles.grow : null]} color={disabled ? "disabled" : "primary"}>
138
+ {label}
139
+ {asterisk(theme.color("--color-text-danger"))}
140
+ </Text>
141
+ )
142
+ return (
143
+ <View style={[styles.field, { gap: 8 }, style]} testID={testID}>
144
+ {hideLabel ? null : labelAction != null ? (
145
+ <View style={styles.labelRow}>
146
+ {labelText}
147
+ {labelAction}
148
+ </View>
149
+ ) : (
150
+ labelText
151
+ )}
152
+ {control(false)}
153
+ {help != null ? (
154
+ <Text
155
+ nativeID={description_.id}
156
+ color={bad ? theme.color("--color-text-danger") : "secondary"}
157
+ accessibilityLiveRegion={error ? "polite" : undefined}
158
+ style={{
159
+ fontSize: theme.length("--mobile-field-error-font-size", 15),
160
+ lineHeight: theme.length("--mobile-field-error-line-height", 20),
161
+ marginTop: theme.length("--mobile-field-error-offset", 4) - 8,
162
+ }}
163
+ >
164
+ {help}
165
+ </Text>
166
+ ) : null}
167
+ </View>
168
+ )
169
+ }
170
+
171
+ const styles = StyleSheet.create({
172
+ field: { alignSelf: "stretch" },
173
+ grow: { flex: 1, minWidth: 0 },
174
+ labelRow: { flexDirection: "row", alignItems: "center", gap: 8 },
175
+ iosLabel: { fontSize: 15, lineHeight: 20 },
176
+ androidLabel: { flex: 1, minWidth: 0, fontSize: 14, lineHeight: 18, paddingHorizontal: 14, paddingBottom: 6 },
177
+ helpRow: { flexDirection: "row", alignItems: "flex-start" },
178
+ androidHelp: { flex: 1, minWidth: 0, fontSize: 14, lineHeight: 18, paddingHorizontal: 14, paddingTop: 6 },
179
+ })
@@ -0,0 +1,131 @@
1
+ import { I18nManager, Platform } from "react-native"
2
+ import type { MenuAnchor } from "../components/Menu/Menu"
3
+
4
+ export type FloatingSide = "top" | "right" | "bottom" | "left"
5
+ export type FloatingAlign = "start" | "center" | "end"
6
+
7
+ export type FloatingInput = {
8
+ /** The trigger's box in the window (`measure`). */
9
+ anchor: MenuAnchor
10
+ /** The panel's size, once measured. */
11
+ size: { width: number; height: number }
12
+ /** The window's size. */
13
+ window: { width: number; height: number }
14
+ side: FloatingSide
15
+ align: FloatingAlign
16
+ /** From the trigger's edge to the panel's (to an arrow's point when it has one). */
17
+ sideOffset: number
18
+ /** Along the trigger's edge, from the aligned edge. */
19
+ alignOffset?: number
20
+ /** Kept free at each edge of the window (the safe area included by the caller). */
21
+ padding: { top: number; right: number; bottom: number; left: number }
22
+ /** Turns to the opposite side when the panel does not fit on its own (Radix's `avoidCollisions`). @default true */
23
+ flip?: boolean
24
+ /** An arrow's length along the panel's edge and the room it keeps from the corners (Radix's `arrowPadding`). */
25
+ arrow?: { width: number; padding: number }
26
+ /** A right-to-left layout: `start` is the right edge. */
27
+ rtl?: boolean
28
+ }
29
+
30
+ export type FloatingPlace = {
31
+ left: number
32
+ top: number
33
+ /** The side it ended on, after a turn. */
34
+ side: FloatingSide
35
+ /** Where the arrow's middle stands along the panel's edge, from its left or top. */
36
+ arrow: number
37
+ }
38
+
39
+ const opposite: Record<FloatingSide, FloatingSide> = { top: "bottom", bottom: "top", left: "right", right: "left" }
40
+
41
+ /**
42
+ * Places a floating panel at its trigger as Radix Popper does on the web (floating-ui's `offset`, `flip`, `shift` and
43
+ * `arrow`): on its side, aligned to the trigger's start, center or end, turned to the other side when it does not fit
44
+ * there and the other side has more room, then moved along the edge to stay inside the window's padding; an arrow
45
+ * keeps pointing at the trigger's middle, clear of the panel's corners.
46
+ */
47
+ export function placeFloating({ anchor, size, window, side, align, sideOffset, alignOffset = 0, padding, flip = true, arrow, rtl }: FloatingInput): FloatingPlace {
48
+ const room = (s: FloatingSide) =>
49
+ s === "bottom"
50
+ ? window.height - padding.bottom - (anchor.y + anchor.height + sideOffset)
51
+ : s === "top"
52
+ ? anchor.y - sideOffset - padding.top
53
+ : s === "right"
54
+ ? window.width - padding.right - (anchor.x + anchor.width + sideOffset)
55
+ : anchor.x - sideOffset - padding.left
56
+ const needed = (s: FloatingSide) => (s === "top" || s === "bottom" ? size.height : size.width)
57
+ let placed = side
58
+ if (flip && room(side) < needed(side) && room(opposite[side]) > room(side)) placed = opposite[side]
59
+ const vertical = placed === "top" || placed === "bottom"
60
+ // The main axis
61
+ let left = 0
62
+ let top = 0
63
+ if (placed === "bottom") top = anchor.y + anchor.height + sideOffset
64
+ else if (placed === "top") top = anchor.y - sideOffset - size.height
65
+ else if (placed === "right") left = anchor.x + anchor.width + sideOffset
66
+ else left = anchor.x - sideOffset - size.width
67
+ // The cross axis: start and end are the reading edges along a horizontal edge
68
+ const length = vertical ? size.width : size.height
69
+ const start = vertical ? anchor.x : anchor.y
70
+ const extent = vertical ? anchor.width : anchor.height
71
+ const flipped = vertical && (rtl ?? isRTLLayout())
72
+ const edge = align === "center" ? "center" : (align === "start") !== flipped ? "low" : "high"
73
+ let cross = edge === "center" ? start + extent / 2 - length / 2 : edge === "low" ? start + alignOffset : start + extent - length - alignOffset
74
+ const low = vertical ? padding.left : padding.top
75
+ const high = (vertical ? window.width - padding.right : window.height - padding.bottom) - length
76
+ cross = Math.max(low, Math.min(cross, Math.max(low, high)))
77
+ if (vertical) left = cross
78
+ else top = cross
79
+ // The arrow points at the trigger's middle, clear of the corners
80
+ const middle = start + extent / 2 - cross
81
+ const half = (arrow?.width ?? 0) / 2
82
+ const inset = (arrow?.padding ?? 0) + half
83
+ const arrowAt = Math.max(inset, Math.min(middle, length - inset))
84
+ return { left, top, side: placed, arrow: arrowAt }
85
+ }
86
+
87
+ /** The layout's direction: a phone set to a right-to-left language (react-native-web reads the document's). */
88
+ function isRTLLayout() {
89
+ if (Platform.OS === "web") {
90
+ const doc = (globalThis as { document?: { documentElement?: { dir?: string } } }).document
91
+ return doc?.documentElement?.dir === "rtl"
92
+ }
93
+ return I18nManager.isRTL
94
+ }
95
+
96
+ /** The origin a panel grows from: the middle of its edge at the trigger, as Radix's transform origin. */
97
+ export function floatingOrigin(side: FloatingSide, arrow: number, size: { width: number; height: number }) {
98
+ switch (side) {
99
+ case "bottom":
100
+ return `${arrow}px 0px`
101
+ case "top":
102
+ return `${arrow}px ${size.height}px`
103
+ case "right":
104
+ return `0px ${arrow}px`
105
+ case "left":
106
+ return `${size.width}px ${arrow}px`
107
+ }
108
+ }
109
+
110
+ /**
111
+ * Props for a trigger element that a floating panel stands at: its node as the panel's anchor (the element itself,
112
+ * so the trigger keeps its own place in the layout: a Button, a Pressable, a View or a Text hands its native view to
113
+ * `ref`), after its own ref, and its layout, after its own `onLayout`, to place the panel again as it moves.
114
+ */
115
+ export function anchorProps(props: { ref?: unknown; onLayout?: (event: never) => void }, setNode: (node: never) => void, onMove?: () => void) {
116
+ return {
117
+ ref: (node: unknown) => {
118
+ setNode(node as never)
119
+ assignRef(props.ref, node)
120
+ },
121
+ onLayout: (event: never) => {
122
+ props.onLayout?.(event)
123
+ onMove?.()
124
+ },
125
+ }
126
+ }
127
+
128
+ function assignRef(ref: unknown, value: unknown) {
129
+ if (typeof ref === "function") ref(value)
130
+ else if (ref != null && typeof ref === "object" && "current" in ref) (ref as { current: unknown }).current = value
131
+ }
@@ -0,0 +1,132 @@
1
+ import { useRef, useState, type ComponentRef, type ReactNode } from "react"
2
+ import { Animated, Easing, Platform, StyleSheet, View, type GestureResponderEvent, type StyleProp, type ViewStyle } from "react-native"
3
+
4
+ /** Android's RippleForeground: the wave grows and slides to the middle over 225ms on Material's standard curve. */
5
+ const ENTER = 225
6
+ /** It fades in over 75ms, shows at least 225ms in all, and fades out over 150ms once let go. */
7
+ const FADE_IN = 75
8
+ const HOLD = FADE_IN + 150
9
+ const FADE_OUT = 150
10
+ const STANDARD = Easing.bezier(0.4, 0, 0.2, 1)
11
+
12
+ type Wave = { key: number; x: number; y: number; width: number; height: number }
13
+
14
+ export type Ripple = {
15
+ /** Starts a wave at the event's touch point (the middle when it has none, as a key's press). */
16
+ start: (event?: GestureResponderEvent) => void
17
+ /** Lets the wave go: it fades once it has shown its 225ms. */
18
+ end: () => void
19
+ /** The wave's layer: first inside the view that takes the press, which clips it to its corners. */
20
+ layer: ReactNode
21
+ }
22
+
23
+ /**
24
+ * Android's ripple where the platform draws none: the web's Android look, and a List row held through the control at
25
+ * its end. A wave starts at the touch point, 60% of the view's larger side across, and grows to cover the view as its
26
+ * centre slides to the view's middle, as Android's bounded ripple does; no scale and no flat layer. `clip` places and
27
+ * sizes the layer (the 40dp circle of a bar's borderless icon); by default it fills the view.
28
+ */
29
+ export function useRipple(color: string, enabled: boolean, reduced: boolean, clip?: StyleProp<ViewStyle>): Ripple {
30
+ const node = useRef<ComponentRef<typeof View> | null>(null)
31
+ const [wave, setWave] = useState<Wave | null>(null)
32
+ const [grow] = useState(() => new Animated.Value(0))
33
+ const [shown] = useState(() => new Animated.Value(0))
34
+ const [gone] = useState(() => new Animated.Value(0))
35
+ const count = useRef(0)
36
+ // The wave on screen, and whether the finger is still down
37
+ const live = useRef<number | null>(null)
38
+ const down = useRef(false)
39
+ const started = useRef(0)
40
+ const end = () => {
41
+ down.current = false
42
+ const key = live.current
43
+ if (key == null) return
44
+ live.current = null
45
+ const clear = () => setWave((current) => (current?.key === key ? null : current))
46
+ if (reduced) return clear()
47
+ const delay = Math.max(0, HOLD - (Date.now() - started.current))
48
+ Animated.timing(gone, { toValue: 1, duration: FADE_OUT, delay, easing: Easing.linear, useNativeDriver: false }).start(({ finished }) => {
49
+ if (finished) clear()
50
+ })
51
+ }
52
+ const start = (event?: GestureResponderEvent) => {
53
+ if (!enabled) return
54
+ const touch = event?.nativeEvent as { pageX?: number; pageY?: number } | undefined
55
+ const pageX = typeof touch?.pageX === "number" && Number.isFinite(touch.pageX) ? touch.pageX : null
56
+ const pageY = typeof touch?.pageY === "number" && Number.isFinite(touch.pageY) ? touch.pageY : null
57
+ const key = ++count.current
58
+ down.current = true
59
+ const begin = (left: number, top: number, width: number, height: number) => {
60
+ if (key !== count.current || !width || !height) return
61
+ const x = pageX == null ? width / 2 : pageX - left
62
+ const y = pageY == null ? height / 2 : pageY - top
63
+ live.current = key
64
+ started.current = Date.now()
65
+ setWave({ key, x, y, width, height })
66
+ grow.stopAnimation()
67
+ shown.stopAnimation()
68
+ gone.stopAnimation()
69
+ gone.setValue(0)
70
+ if (reduced) {
71
+ grow.setValue(1)
72
+ shown.setValue(1)
73
+ } else {
74
+ grow.setValue(0)
75
+ shown.setValue(0)
76
+ Animated.parallel([
77
+ Animated.timing(grow, { toValue: 1, duration: ENTER, easing: STANDARD, useNativeDriver: false }),
78
+ Animated.timing(shown, { toValue: 1, duration: FADE_IN, easing: Easing.linear, useNativeDriver: false }),
79
+ ]).start()
80
+ }
81
+ // Let go before the view was measured
82
+ if (!down.current) end()
83
+ }
84
+ if (Platform.OS === "web") {
85
+ // The page's measure of the element, in the same frame as the event's page point
86
+ const element = node.current as unknown as { getBoundingClientRect?: () => { left: number; top: number; width: number; height: number } } | null
87
+ const rect = element?.getBoundingClientRect?.()
88
+ if (!rect) return
89
+ const scroll = globalThis as unknown as { scrollX?: number; scrollY?: number }
90
+ begin(rect.left + (scroll.scrollX ?? 0), rect.top + (scroll.scrollY ?? 0), rect.width, rect.height)
91
+ return
92
+ }
93
+ node.current?.measure((_x, _y, width, height, left, top) => begin(left, top, width, height))
94
+ }
95
+ let drawn: ReactNode = null
96
+ if (wave) {
97
+ const { x, y, width, height } = wave
98
+ // The radius that covers the view from its middle, and the wave's first: 30% of the larger side
99
+ const radius = Math.hypot(width, height) / 2
100
+ const first = Math.min(1, (Math.max(width, height) * 0.3) / radius)
101
+ drawn = (
102
+ <Animated.View
103
+ style={{
104
+ position: "absolute",
105
+ left: width / 2 - radius,
106
+ top: height / 2 - radius,
107
+ width: radius * 2,
108
+ height: radius * 2,
109
+ borderRadius: radius,
110
+ backgroundColor: color,
111
+ opacity: Animated.multiply(shown, Animated.subtract(1, gone)),
112
+ transform: [
113
+ { translateX: grow.interpolate({ inputRange: [0, 1], outputRange: [x - width / 2, 0] }) },
114
+ { translateY: grow.interpolate({ inputRange: [0, 1], outputRange: [y - height / 2, 0] }) },
115
+ { scale: grow.interpolate({ inputRange: [0, 1], outputRange: [first, 1] }) },
116
+ ],
117
+ }}
118
+ />
119
+ )
120
+ }
121
+ const layer = enabled ? (
122
+ <View ref={node} pointerEvents="none" collapsable={false} style={[clip ? styles.placed : StyleSheet.absoluteFill, styles.clip, clip]}>
123
+ {drawn}
124
+ </View>
125
+ ) : null
126
+ return { start, end, layer }
127
+ }
128
+
129
+ const styles = StyleSheet.create({
130
+ clip: { overflow: "hidden" },
131
+ placed: { position: "absolute" },
132
+ })