@plexui/react-native 0.1.1 → 0.2.1
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 +67 -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 +39 -19
- 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 +23 -4
- 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 +123 -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 +83 -20
- 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,141 @@
|
|
|
1
|
+
import { useEffect, useState } from "react"
|
|
2
|
+
import { View, type StyleProp, type ViewStyle } from "react-native"
|
|
3
|
+
import Svg, { Circle } from "react-native-svg"
|
|
4
|
+
import { curves } from "../../internal/motion"
|
|
5
|
+
import { cssEase, useTweenedNumber } from "../../internal/transition"
|
|
6
|
+
import { useTheme } from "../../theme/ThemeProvider"
|
|
7
|
+
import type { Theme } from "../../theme/types"
|
|
8
|
+
|
|
9
|
+
export type CircularProgressProps = {
|
|
10
|
+
/** Maximum duration to use for simulated upload progress in ms. @default 15000 */
|
|
11
|
+
maxDuration?: number
|
|
12
|
+
/** Sets the progress to full and resolves the simulated timer. @default false */
|
|
13
|
+
done?: boolean
|
|
14
|
+
/** A fixed progress, 0 to 100, instead of the simulated one. */
|
|
15
|
+
progress?: number
|
|
16
|
+
/** Size of the indicator, in pixels. @default 28 */
|
|
17
|
+
size?: number
|
|
18
|
+
/** Stroke width of the ring, in pixels of its 20px drawing, between 1 and 4. @default 2 */
|
|
19
|
+
strokeWidth?: number
|
|
20
|
+
/** Color of the active progress track (stroke): a color or a token, such as `"var(--gray-1000)"`. */
|
|
21
|
+
trackActiveColor?: string
|
|
22
|
+
/** Color of the base track (background stroke): a color or a token, such as `"var(--alpha-15)"`. */
|
|
23
|
+
trackColor?: string
|
|
24
|
+
/** The name read out, as a progress bar. */
|
|
25
|
+
"aria-label"?: string
|
|
26
|
+
accessibilityLabel?: string
|
|
27
|
+
testID?: string
|
|
28
|
+
style?: StyleProp<ViewStyle>
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Deterministically returns a progress value between 0 and 100 that decays as it approaches 100, based on a maximum
|
|
33
|
+
* duration and the elapsed time, as @plexui/ui's simulated progress.
|
|
34
|
+
*/
|
|
35
|
+
export function getSimulatedProgress(maxTime: number, elapsedTime: number) {
|
|
36
|
+
const P_max = 99.9 // Maximum percentage to approach (less than 100%)
|
|
37
|
+
const P1 = 35 // Percentage at which the rate slows down
|
|
38
|
+
const initialRate = (3 * 100) / maxTime // Initial rate (% per ms)
|
|
39
|
+
const t1 = P1 / initialRate // Time when progress reaches P1
|
|
40
|
+
let percentage
|
|
41
|
+
if (elapsedTime <= t1) {
|
|
42
|
+
// Phase 1: a linear increase at the start, to show consistency in progress
|
|
43
|
+
percentage = initialRate * elapsedTime
|
|
44
|
+
} else {
|
|
45
|
+
// Phase 2: the progress slows down, approaching P_max
|
|
46
|
+
const k = 4
|
|
47
|
+
const remainingTime = maxTime - t1
|
|
48
|
+
const timeSinceT1 = elapsedTime - t1
|
|
49
|
+
percentage = P1 + (P_max - P1) * (1 - Math.exp(-k * (timeSinceT1 / remainingTime)))
|
|
50
|
+
}
|
|
51
|
+
return Math.min(percentage, P_max)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** A progress that rises every 100ms from the mount, slowing as it nears 100, and is 100 once `completed`. */
|
|
55
|
+
function useSimulatedProgress(maxDuration: number, completed: boolean) {
|
|
56
|
+
const [startAt] = useState(() => Date.now())
|
|
57
|
+
const [progress, setProgress] = useState(() => getSimulatedProgress(maxDuration, Date.now() - startAt))
|
|
58
|
+
useEffect(() => {
|
|
59
|
+
if (completed) return
|
|
60
|
+
const id = setInterval(() => {
|
|
61
|
+
const elapsed = Date.now() - startAt
|
|
62
|
+
setProgress(elapsed >= maxDuration ? 100 : getSimulatedProgress(maxDuration, elapsed))
|
|
63
|
+
}, 100)
|
|
64
|
+
return () => clearInterval(id)
|
|
65
|
+
}, [completed, maxDuration, startAt])
|
|
66
|
+
return completed ? 100 : progress
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** A color prop: a token (`var(--…)`) resolved for the scheme, or a color as it is. */
|
|
70
|
+
const resolve = (theme: Theme, value: string | undefined, fallback: string) => {
|
|
71
|
+
if (!value) return fallback
|
|
72
|
+
if (!value.includes("var(") && !value.includes("light-dark(")) return value
|
|
73
|
+
const resolved = theme.value(value)
|
|
74
|
+
return typeof resolved === "string" ? resolved : fallback
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** The drawing: a ring of radius 8 in a 20px box, its progress a dash of 50 that starts at 12 o'clock. */
|
|
78
|
+
const DASH = 50
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* A progress ring, as @plexui/ui's CircularProgress: a light green track and a green arc for its share, 28px by
|
|
82
|
+
* default. Without `progress` it simulates one over `maxDuration`, and `done` fills it. Neither platform has a ring
|
|
83
|
+
* of its own for this, so both draw the Plex ring; a new value moves the arc in 300ms (ease) on iOS, as the web's
|
|
84
|
+
* transition, and in Android's determinate radial progress time (200ms, decelerating); at once with reduced motion.
|
|
85
|
+
*/
|
|
86
|
+
export function CircularProgress({
|
|
87
|
+
maxDuration = 15000,
|
|
88
|
+
done = false,
|
|
89
|
+
progress: progressProp,
|
|
90
|
+
size = 28,
|
|
91
|
+
strokeWidth,
|
|
92
|
+
trackActiveColor,
|
|
93
|
+
trackColor,
|
|
94
|
+
"aria-label": ariaLabel,
|
|
95
|
+
accessibilityLabel,
|
|
96
|
+
testID,
|
|
97
|
+
style,
|
|
98
|
+
}: CircularProgressProps) {
|
|
99
|
+
const theme = useTheme()
|
|
100
|
+
const simulated = useSimulatedProgress(maxDuration, progressProp !== undefined || done)
|
|
101
|
+
const progress = Math.max(0, Math.min(100, progressProp ?? simulated))
|
|
102
|
+
const shown = useTweenedNumber(progress, {
|
|
103
|
+
duration: theme.isAndroid ? 200 : 300,
|
|
104
|
+
easing: theme.isAndroid ? curves.decelerate : cssEase,
|
|
105
|
+
reducedMotion: theme.reducedMotion,
|
|
106
|
+
})
|
|
107
|
+
const stroke = strokeWidth ? Math.min(4, strokeWidth) : 2
|
|
108
|
+
// The web's `light-dark(#deede9, var(--green-a50))`: a light green, the green at 15% at night
|
|
109
|
+
const track = resolve(theme, trackColor, theme.scheme === "dark" ? theme.color("--green-a50") : "#DEEDE9")
|
|
110
|
+
const active = resolve(theme, trackActiveColor, theme.color("--green-500"))
|
|
111
|
+
const name = accessibilityLabel ?? ariaLabel
|
|
112
|
+
return (
|
|
113
|
+
<View
|
|
114
|
+
accessible={name != null}
|
|
115
|
+
accessibilityRole={name != null ? "progressbar" : undefined}
|
|
116
|
+
accessibilityLabel={name}
|
|
117
|
+
accessibilityValue={name != null ? { min: 0, max: 100, now: Math.round(progress) } : undefined}
|
|
118
|
+
importantForAccessibility={name != null ? undefined : "no-hide-descendants"}
|
|
119
|
+
accessibilityElementsHidden={name == null}
|
|
120
|
+
testID={testID}
|
|
121
|
+
style={[{ width: size, height: size }, style]}
|
|
122
|
+
>
|
|
123
|
+
<Svg width={size} height={size} viewBox="0 0 20 20">
|
|
124
|
+
<Circle cx={10} cy={10} r={8} fill="none" stroke={track} strokeWidth={stroke} />
|
|
125
|
+
<Circle
|
|
126
|
+
cx={10}
|
|
127
|
+
cy={10}
|
|
128
|
+
r={8}
|
|
129
|
+
fill="none"
|
|
130
|
+
stroke={active}
|
|
131
|
+
strokeWidth={stroke}
|
|
132
|
+
strokeLinecap="round"
|
|
133
|
+
strokeDasharray={[DASH, DASH]}
|
|
134
|
+
strokeDashoffset={DASH - DASH * (shown / 100)}
|
|
135
|
+
// The dash starts at 12 o'clock, as the web's ring turned back a quarter
|
|
136
|
+
transform="rotate(-90 10 10)"
|
|
137
|
+
/>
|
|
138
|
+
</Svg>
|
|
139
|
+
</View>
|
|
140
|
+
)
|
|
141
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { Animated, StyleSheet, View, type StyleProp, type ViewStyle } from "react-native"
|
|
2
|
+
import { curves } from "../../internal/motion"
|
|
3
|
+
import { cssEase, useLoopClock } from "../../internal/transition"
|
|
4
|
+
import { useTheme } from "../../theme/ThemeProvider"
|
|
5
|
+
import { textColor, type TextColor } from "../Text/Text"
|
|
6
|
+
|
|
7
|
+
export type LoadingDotsProps = {
|
|
8
|
+
/** The color of the dots, a text color or any color string; the web takes the text color. @default "primary" */
|
|
9
|
+
color?: TextColor | (string & {})
|
|
10
|
+
/** The name read out, as a busy progress indicator; without one the dots are decorative, as on the web. */
|
|
11
|
+
"aria-label"?: string
|
|
12
|
+
accessibilityLabel?: string
|
|
13
|
+
testID?: string
|
|
14
|
+
style?: StyleProp<ViewStyle>
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const DOT = 8
|
|
18
|
+
const GAP = 4
|
|
19
|
+
|
|
20
|
+
/** iOS, as the web's dots: each pulses .15 → .4 → .15 in 1s (ease), 0.2s after the one before it. */
|
|
21
|
+
const IOS = { period: 1000, delays: [0, 200, 400] }
|
|
22
|
+
/** Android, the Plex Android look's typing dots: each grows over 320ms and shrinks over 320ms (decelerating) every 800ms, 150ms after the one before it; 1.33 to 2.33 of radius. */
|
|
23
|
+
const ANDROID = { period: 800, delays: [0, 150, 300], rest: 1.33 / 2.33 }
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Three pulsing dots for typing or a short wait, as @plexui/ui's LoadingDots: 8px dots 4px apart, as tall as a line
|
|
27
|
+
* of text (24). iOS pulses their opacity as the web does; Android grows and shrinks them on the Plex Android look's
|
|
28
|
+
* typing timing, in the same tones. With reduced motion they stand still.
|
|
29
|
+
*/
|
|
30
|
+
export function LoadingDots({ color, "aria-label": ariaLabel, accessibilityLabel, testID, style }: LoadingDotsProps) {
|
|
31
|
+
const theme = useTheme()
|
|
32
|
+
const ink = textColor(theme, color ?? "primary")
|
|
33
|
+
const name = accessibilityLabel ?? ariaLabel
|
|
34
|
+
const timing = theme.isAndroid ? ANDROID : IOS
|
|
35
|
+
return (
|
|
36
|
+
<View
|
|
37
|
+
accessible={name != null}
|
|
38
|
+
accessibilityRole={name != null ? "progressbar" : undefined}
|
|
39
|
+
accessibilityLabel={name}
|
|
40
|
+
accessibilityState={name != null ? { busy: true } : undefined}
|
|
41
|
+
importantForAccessibility={name != null ? undefined : "no-hide-descendants"}
|
|
42
|
+
accessibilityElementsHidden={name == null}
|
|
43
|
+
testID={testID}
|
|
44
|
+
style={[styles.root, { height: theme.length("--font-text-md-line-height", 24) }, style]}
|
|
45
|
+
>
|
|
46
|
+
{timing.delays.map((delay, index) => (
|
|
47
|
+
<Dot key={index} delay={delay} color={ink} android={theme.isAndroid} still={theme.reducedMotion} />
|
|
48
|
+
))}
|
|
49
|
+
</View>
|
|
50
|
+
)
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function Dot({ delay, color, android, still }: { delay: number; color: string; android: boolean; still: boolean }) {
|
|
54
|
+
const { period } = android ? ANDROID : IOS
|
|
55
|
+
const clock = useLoopClock(period, !still, true, delay)
|
|
56
|
+
if (android) {
|
|
57
|
+
// Grows for 320ms, shrinks for 320ms, rests until 800ms; its tone follows its size
|
|
58
|
+
const grow = clock.interpolate({ inputRange: [0, 320, 640, period], outputRange: [0, 1, 0, 0], easing: curves.decelerate })
|
|
59
|
+
return (
|
|
60
|
+
<Animated.View
|
|
61
|
+
style={[
|
|
62
|
+
styles.dot,
|
|
63
|
+
{
|
|
64
|
+
backgroundColor: color,
|
|
65
|
+
opacity: grow.interpolate({ inputRange: [0, 1], outputRange: [0.15, 0.4] }),
|
|
66
|
+
transform: [{ scale: grow.interpolate({ inputRange: [0, 1], outputRange: [ANDROID.rest, 1] }) }],
|
|
67
|
+
},
|
|
68
|
+
]}
|
|
69
|
+
/>
|
|
70
|
+
)
|
|
71
|
+
}
|
|
72
|
+
const opacity = clock.interpolate({ inputRange: [0, 0.2 * period, period], outputRange: [0.15, 0.4, 0.15], easing: cssEase })
|
|
73
|
+
return <Animated.View style={[styles.dot, { backgroundColor: color, opacity }]} />
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const styles = StyleSheet.create({
|
|
77
|
+
root: { flexDirection: "row", alignItems: "center", justifyContent: "flex-start", gap: GAP },
|
|
78
|
+
dot: { width: DOT, height: DOT, borderRadius: DOT / 2 },
|
|
79
|
+
})
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import { useEffect, useState } from "react"
|
|
2
|
+
import { Animated, Easing, StyleSheet, View, type StyleProp, type ViewStyle } from "react-native"
|
|
3
|
+
import Svg, { Circle } from "react-native-svg"
|
|
4
|
+
import { nativeDriver } from "../../internal/transition"
|
|
5
|
+
import { useTheme } from "../../theme/ThemeProvider"
|
|
6
|
+
import type { Theme } from "../../theme/types"
|
|
7
|
+
import { textColor, type TextColor } from "../Text/Text"
|
|
8
|
+
|
|
9
|
+
export type LoadingIndicatorProps = {
|
|
10
|
+
/** Size of the indicator, in pixels. @default 20 */
|
|
11
|
+
size?: number
|
|
12
|
+
/**
|
|
13
|
+
* The spokes' width on iOS (by default the iOS 27 kit's proportion of the size: 2/14 up to 16, 4/30 to 27, 5/35
|
|
14
|
+
* from 28), the arc's stroke on Android (by default an eighth of the size).
|
|
15
|
+
*/
|
|
16
|
+
strokeWidth?: number
|
|
17
|
+
/** The color, a text color or any color string; the web takes the text color (`currentcolor`). @default "primary" (iOS), the accent (Android) */
|
|
18
|
+
color?: TextColor | (string & {})
|
|
19
|
+
/** The time of one turn in ms, as the web's `--indicator-rotate-duration`; Android's cycle keeps its proportion. @default 800 */
|
|
20
|
+
duration?: number
|
|
21
|
+
/** The name read out, as a busy progress indicator; without one the spinner is decorative, as on the web. */
|
|
22
|
+
"aria-label"?: string
|
|
23
|
+
accessibilityLabel?: string
|
|
24
|
+
testID?: string
|
|
25
|
+
style?: StyleProp<ViewStyle>
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** The opacities of the eight spokes, from the top clockwise: the leading spoke, then the faintest, brightening. */
|
|
29
|
+
const SPOKES = [1, 0.15, 0.27, 0.39, 0.51, 0.63, 0.75, 0.87]
|
|
30
|
+
|
|
31
|
+
/** The kit's spoke for a size: Small (14) up to 16, Regular (30) to 27, Large (35) from 28, as width and height. */
|
|
32
|
+
function spoke(size: number) {
|
|
33
|
+
if (size <= 16) return { width: (2 / 14) * size, height: (4 / 14) * size }
|
|
34
|
+
if (size >= 28) return { width: (5 / 35) * size, height: (13 / 35) * size }
|
|
35
|
+
return { width: (4 / 30) * size, height: (10 / 30) * size }
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** CSS `steps(8, end)`: the spinner turns a spoke at a time. */
|
|
39
|
+
const eightSteps = (t: number) => Math.min(1, Math.floor(t * 8) / 8)
|
|
40
|
+
|
|
41
|
+
const defaultColor = (theme: Theme, color: LoadingIndicatorProps["color"]) =>
|
|
42
|
+
color ? textColor(theme, color) : theme.isAndroid ? theme.material.primary : textColor(theme, "primary")
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* A spinner for a loading state, as @plexui/ui's LoadingIndicator. iOS: the iOS 27 spinner (UIActivityIndicatorView)
|
|
46
|
+
* of eight rounded spokes, as the touch stylesheet draws it: 20pt, the spokes brightening to the leading one, a turn in
|
|
47
|
+
* 800ms in eight steps. Android: Material's circular progress, the arc of CircularProgressDrawable (a 5.4s cycle of four
|
|
48
|
+
* 250° surges) in the accent. With reduced motion it stands still.
|
|
49
|
+
*/
|
|
50
|
+
export function LoadingIndicator({ size = 20, strokeWidth, color, duration = 800, "aria-label": ariaLabel, accessibilityLabel, testID, style }: LoadingIndicatorProps) {
|
|
51
|
+
const theme = useTheme()
|
|
52
|
+
const ink = defaultColor(theme, color)
|
|
53
|
+
const name = accessibilityLabel ?? ariaLabel
|
|
54
|
+
return (
|
|
55
|
+
<View
|
|
56
|
+
accessible={name != null}
|
|
57
|
+
accessibilityRole={name != null ? "progressbar" : undefined}
|
|
58
|
+
accessibilityLabel={name}
|
|
59
|
+
accessibilityState={name != null ? { busy: true } : undefined}
|
|
60
|
+
importantForAccessibility={name != null ? undefined : "no-hide-descendants"}
|
|
61
|
+
accessibilityElementsHidden={name == null}
|
|
62
|
+
testID={testID}
|
|
63
|
+
style={[{ width: size, height: size }, style]}
|
|
64
|
+
>
|
|
65
|
+
{theme.isAndroid ? (
|
|
66
|
+
<Arc size={size} stroke={strokeWidth ?? size / 8} color={ink} cycle={(SPIN.cycle * duration) / 800} still={theme.reducedMotion} />
|
|
67
|
+
) : (
|
|
68
|
+
<Spokes size={size} strokeWidth={strokeWidth} color={ink} duration={duration} still={theme.reducedMotion} />
|
|
69
|
+
)}
|
|
70
|
+
</View>
|
|
71
|
+
)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function Spokes({ size, strokeWidth, color, duration, still }: { size: number; strokeWidth?: number; color: string; duration: number; still: boolean }) {
|
|
75
|
+
const [turn] = useState(() => new Animated.Value(0))
|
|
76
|
+
useEffect(() => {
|
|
77
|
+
turn.setValue(0)
|
|
78
|
+
if (still || duration <= 0) return
|
|
79
|
+
const loop = Animated.loop(Animated.timing(turn, { toValue: 1, duration, easing: eightSteps, useNativeDriver: nativeDriver, isInteraction: false }))
|
|
80
|
+
loop.start()
|
|
81
|
+
return () => loop.stop()
|
|
82
|
+
}, [turn, duration, still])
|
|
83
|
+
const { width: kitWidth, height } = spoke(size)
|
|
84
|
+
const width = strokeWidth ?? kitWidth
|
|
85
|
+
return (
|
|
86
|
+
<Animated.View style={[StyleSheet.absoluteFill, { transform: [{ rotate: turn.interpolate({ inputRange: [0, 1], outputRange: ["0deg", "360deg"] }) }] }]}>
|
|
87
|
+
{SPOKES.map((opacity, index) => (
|
|
88
|
+
<View key={index} style={[StyleSheet.absoluteFill, { opacity, transform: [{ rotate: `${index * 45}deg` }] }]}>
|
|
89
|
+
<View style={{ position: "absolute", top: 0, left: (size - width) / 2, width, height, borderRadius: width / 2, backgroundColor: color }} />
|
|
90
|
+
</View>
|
|
91
|
+
))}
|
|
92
|
+
</Animated.View>
|
|
93
|
+
)
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** Material's standard curve, on which the arc surges. */
|
|
97
|
+
const standard = Easing.bezier(0.4, 0, 0.2, 1)
|
|
98
|
+
/**
|
|
99
|
+
* Android's spinner over one 5400ms cycle (CircularProgressDrawable): the arc's head turns 1520° and makes four surges
|
|
100
|
+
* of 250°, 667ms each, 1350ms apart; its tail turns 20° behind the head's steady turn and makes the same surges 667ms
|
|
101
|
+
* later.
|
|
102
|
+
*/
|
|
103
|
+
export const SPIN = { cycle: 5400, turn: 1520, lag: 20, surge: 250, surgeTime: 667, surgeEvery: 1350, surges: 4 }
|
|
104
|
+
|
|
105
|
+
/** The spinner at a share of its cycle: how far its tail has turned and how long its arc is, in degrees. */
|
|
106
|
+
export function spinnerAt(share: number) {
|
|
107
|
+
const t = share * SPIN.cycle
|
|
108
|
+
const surges = (delay: number) => {
|
|
109
|
+
let total = 0
|
|
110
|
+
for (let index = 0; index < SPIN.surges; index++) {
|
|
111
|
+
const from = delay + index * SPIN.surgeEvery
|
|
112
|
+
total += standard(Math.min(1, Math.max(0, (t - from) / SPIN.surgeTime))) * SPIN.surge
|
|
113
|
+
}
|
|
114
|
+
return total
|
|
115
|
+
}
|
|
116
|
+
const head = (t / SPIN.cycle) * SPIN.turn + surges(0)
|
|
117
|
+
// The tail stands still until the head is 20° on
|
|
118
|
+
const lagTime = (SPIN.lag * SPIN.cycle) / SPIN.turn
|
|
119
|
+
const tail = Math.max(0, (t - lagTime) / (SPIN.cycle - lagTime)) * (SPIN.turn - SPIN.lag) + surges(SPIN.surgeTime)
|
|
120
|
+
return { tail, arc: head - tail }
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** The still frame: the end of the first surge, a 270° arc. */
|
|
124
|
+
const STILL = SPIN.surgeTime / SPIN.cycle
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* The arc is redrawn from a clock on each frame: an animated SVG prop does not reach the element on the web. The
|
|
128
|
+
* stroke runs on a circle that fills the box, as Android lays its drawable out (8.75 of 20 with a 2.5 stroke).
|
|
129
|
+
*/
|
|
130
|
+
function Arc({ size, stroke, color, cycle, still }: { size: number; stroke: number; color: string; cycle: number; still: boolean }) {
|
|
131
|
+
const [time, setShare] = useState(STILL)
|
|
132
|
+
const share = still || cycle <= 0 ? STILL : time
|
|
133
|
+
useEffect(() => {
|
|
134
|
+
if (still || cycle <= 0) return
|
|
135
|
+
const time = new Animated.Value(0)
|
|
136
|
+
const id = time.addListener(({ value }) => setShare(value))
|
|
137
|
+
const loop = Animated.loop(Animated.timing(time, { toValue: 1, duration: cycle, easing: Easing.linear, useNativeDriver: false, isInteraction: false }))
|
|
138
|
+
loop.start()
|
|
139
|
+
return () => {
|
|
140
|
+
loop.stop()
|
|
141
|
+
time.removeListener(id)
|
|
142
|
+
}
|
|
143
|
+
}, [cycle, still])
|
|
144
|
+
const { tail, arc } = spinnerAt(share)
|
|
145
|
+
const radius = Math.max(0, (size - stroke) / 2)
|
|
146
|
+
const length = 2 * Math.PI * radius
|
|
147
|
+
return (
|
|
148
|
+
// The circle's stroke starts at 3 o'clock and runs clockwise, as Android's arcs
|
|
149
|
+
<View style={[StyleSheet.absoluteFill, { transform: [{ rotate: `${tail}deg` }] }]}>
|
|
150
|
+
<Svg width={size} height={size}>
|
|
151
|
+
<Circle
|
|
152
|
+
cx={size / 2}
|
|
153
|
+
cy={size / 2}
|
|
154
|
+
r={radius}
|
|
155
|
+
fill="none"
|
|
156
|
+
stroke={color}
|
|
157
|
+
strokeWidth={stroke}
|
|
158
|
+
strokeLinecap="round"
|
|
159
|
+
// One dash as long as the arc, the rest of the circle a gap
|
|
160
|
+
strokeDasharray={[length, length]}
|
|
161
|
+
strokeDashoffset={length * (1 - arc / 360)}
|
|
162
|
+
/>
|
|
163
|
+
</Svg>
|
|
164
|
+
</View>
|
|
165
|
+
)
|
|
166
|
+
}
|