@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,415 @@
|
|
|
1
|
+
import { cloneElement, createContext, isValidElement, useContext, useMemo, useState, type ReactElement, type ReactNode } from "react"
|
|
2
|
+
import { Animated, Platform, StyleSheet, View, type LayoutChangeEvent, type StyleProp, type ViewStyle } from "react-native"
|
|
3
|
+
import CheckMd from "../../icons/plex/CheckMd"
|
|
4
|
+
import { flattenChildren, textOf } from "../../internal/children"
|
|
5
|
+
import { cssEasing, useColorTransition } from "../../internal/transition"
|
|
6
|
+
import { useTheme } from "../../theme/ThemeProvider"
|
|
7
|
+
import type { Theme } from "../../theme/types"
|
|
8
|
+
import { IconDefaultsProvider } from "../Icon"
|
|
9
|
+
import { Text } from "../Text/Text"
|
|
10
|
+
|
|
11
|
+
type Orientation = "horizontal" | "vertical"
|
|
12
|
+
type StepState = "completed" | "active" | "inactive"
|
|
13
|
+
type ConnectorStyle = "solid" | "dashed"
|
|
14
|
+
type Variant = "default" | "minimal"
|
|
15
|
+
type Color = "default" | "success"
|
|
16
|
+
export type ProgressStepsSize = "sm" | "md" | "lg"
|
|
17
|
+
|
|
18
|
+
export type ProgressStepsProps = {
|
|
19
|
+
/** Current active step (1-indexed, starts from 1) */
|
|
20
|
+
current: number
|
|
21
|
+
/** Total number of steps (required for the minimal variant) */
|
|
22
|
+
total?: number
|
|
23
|
+
/** @default "default" */
|
|
24
|
+
variant?: Variant
|
|
25
|
+
/** The color of completed and active steps. @default "default" */
|
|
26
|
+
color?: Color
|
|
27
|
+
/** @default "horizontal" */
|
|
28
|
+
orientation?: Orientation
|
|
29
|
+
/** The size of the step indicators. @default "md" */
|
|
30
|
+
size?: ProgressStepsSize
|
|
31
|
+
/** @default "solid" */
|
|
32
|
+
connectorStyle?: ConnectorStyle
|
|
33
|
+
testID?: string
|
|
34
|
+
style?: StyleProp<ViewStyle>
|
|
35
|
+
children?: ReactNode
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export type StepProps = {
|
|
39
|
+
/** A custom icon instead of the step's number */
|
|
40
|
+
icon?: ReactNode
|
|
41
|
+
/** The step's content: a Title and/or a Description */
|
|
42
|
+
children?: ReactNode
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export type StepTitleProps = {
|
|
46
|
+
children: ReactNode
|
|
47
|
+
/**
|
|
48
|
+
* Hides the title from view and keeps it for screen readers, for steps that show their indicators alone: an icon
|
|
49
|
+
* or a check has no name. The step then takes no room under its indicator.
|
|
50
|
+
* @default false
|
|
51
|
+
*/
|
|
52
|
+
visuallyHidden?: boolean
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export type StepDescriptionProps = { children: ReactNode }
|
|
56
|
+
|
|
57
|
+
type Metrics = { indicator: number; rowGap: number; contentGap: number; font: number; icon: number; line: number }
|
|
58
|
+
|
|
59
|
+
type Colors = {
|
|
60
|
+
indicator: Record<StepState, string>
|
|
61
|
+
indicatorText: Record<StepState, string>
|
|
62
|
+
connector: string
|
|
63
|
+
connectorCompleted: string
|
|
64
|
+
title: string
|
|
65
|
+
titleInactive: string
|
|
66
|
+
description: string
|
|
67
|
+
descriptionInactive: string
|
|
68
|
+
minimalUpcoming: string
|
|
69
|
+
minimalCompleted: string
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
type Context = {
|
|
73
|
+
current: number
|
|
74
|
+
orientation: Orientation
|
|
75
|
+
connectorStyle: ConnectorStyle
|
|
76
|
+
total: number
|
|
77
|
+
metrics: Metrics
|
|
78
|
+
colors: Colors
|
|
79
|
+
connectorWidth: number
|
|
80
|
+
dash: number
|
|
81
|
+
gap: number
|
|
82
|
+
motion: { duration: number; easing: ReturnType<typeof cssEasing>; reducedMotion: boolean }
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const StepsContext = createContext<Context | null>(null)
|
|
86
|
+
const StepContext = createContext<{ state: StepState; first: ReactNode } | null>(null)
|
|
87
|
+
|
|
88
|
+
const useSteps = () => {
|
|
89
|
+
const context = useContext(StepsContext)
|
|
90
|
+
if (!context) throw new Error("ProgressSteps components must be wrapped in <ProgressSteps />")
|
|
91
|
+
return context
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const metricsOf = (theme: Theme, size: ProgressStepsSize): Metrics => ({
|
|
95
|
+
indicator: theme.length(`--progress-steps-indicator-size-${size}`, { sm: 24, md: 32, lg: 40 }[size]),
|
|
96
|
+
rowGap: theme.length(`--progress-steps-row-gap-${size}`, { sm: 16, md: 24, lg: 32 }[size]),
|
|
97
|
+
contentGap: theme.length(`--progress-steps-content-gap-${size}`, { sm: 10, md: 12, lg: 16 }[size]),
|
|
98
|
+
font: theme.length(`--progress-steps-font-size-${size}`, { sm: 12, md: 14, lg: 16 }[size]),
|
|
99
|
+
icon: theme.length(`--progress-steps-icon-size-${size}`, { sm: 14, md: 16, lg: 18 }[size]),
|
|
100
|
+
line: theme.length("--font-text-sm-line-height", 20),
|
|
101
|
+
})
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* The colors of the steps. iOS: the tokens (the ink for completed and active steps, `--gray-100` ahead). Android: the
|
|
105
|
+
* Plex Android look's accent for completed and active steps on `surfaceContainerHighest`, as Material draws a filled
|
|
106
|
+
* selection. `color="success"` takes the solid success colors on both.
|
|
107
|
+
*/
|
|
108
|
+
function colorsOf(theme: Theme, color: Color): Colors {
|
|
109
|
+
const m = theme.material
|
|
110
|
+
const success = color === "success"
|
|
111
|
+
const successFill = theme.color("--color-background-success-solid")
|
|
112
|
+
const successText = theme.color("--color-text-success-solid", "#FFFFFF")
|
|
113
|
+
if (theme.isAndroid) {
|
|
114
|
+
const fill = success ? successFill : m.primary
|
|
115
|
+
const onFill = success ? successText : m.onPrimary
|
|
116
|
+
return {
|
|
117
|
+
indicator: { completed: fill, active: fill, inactive: m.surfaceContainerHighest },
|
|
118
|
+
indicatorText: { completed: onFill, active: onFill, inactive: m.onSurfaceVariant },
|
|
119
|
+
connector: m.surfaceContainerHighest,
|
|
120
|
+
connectorCompleted: fill,
|
|
121
|
+
title: m.onSurface,
|
|
122
|
+
titleInactive: m.onSurfaceVariant,
|
|
123
|
+
description: m.onSurfaceVariant,
|
|
124
|
+
descriptionInactive: m.onSurfaceVariant,
|
|
125
|
+
minimalUpcoming: m.surfaceContainerHighest,
|
|
126
|
+
minimalCompleted: fill,
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
return {
|
|
130
|
+
indicator: {
|
|
131
|
+
completed: success ? successFill : theme.color("--progress-steps-indicator-background-completed"),
|
|
132
|
+
active: success ? successFill : theme.color("--progress-steps-indicator-background-active"),
|
|
133
|
+
inactive: theme.color("--progress-steps-indicator-background"),
|
|
134
|
+
},
|
|
135
|
+
indicatorText: {
|
|
136
|
+
completed: success ? successText : theme.color("--progress-steps-indicator-text-completed"),
|
|
137
|
+
active: success ? successText : theme.color("--progress-steps-indicator-text-active"),
|
|
138
|
+
inactive: theme.color("--progress-steps-indicator-text"),
|
|
139
|
+
},
|
|
140
|
+
connector: theme.color("--progress-steps-connector-color"),
|
|
141
|
+
connectorCompleted: success ? successFill : theme.color("--progress-steps-connector-color-completed"),
|
|
142
|
+
title: theme.color("--progress-steps-title-color", theme.colors.text),
|
|
143
|
+
titleInactive: theme.color("--progress-steps-title-color-inactive"),
|
|
144
|
+
description: theme.color("--progress-steps-description-color"),
|
|
145
|
+
descriptionInactive: theme.color("--progress-steps-description-color-inactive"),
|
|
146
|
+
minimalUpcoming: theme.color("--progress-steps-minimal-color-upcoming"),
|
|
147
|
+
minimalCompleted: success ? successFill : theme.color("--progress-steps-minimal-color-completed"),
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
const web = Platform.OS === "web"
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* The steps of a multi-step flow, such as a checkout or onboarding, and the current one, as @plexui/ui's
|
|
155
|
+
* ProgressSteps: a numbered indicator per step (a check once completed), the connectors between them and each step's
|
|
156
|
+
* title and description, in a row or a column; or, with `variant="minimal"`, a bar of short segments. Neither
|
|
157
|
+
* platform has a control of its own, so both draw the Plex steps: iOS in the tokens (indicators 24, 32, 40; the ink
|
|
158
|
+
* for completed and active steps), Android in the Plex Android look's accent. A change of step fades the colors in
|
|
159
|
+
* `--transition-duration-basic` (120ms, ease), at once with reduced motion.
|
|
160
|
+
*/
|
|
161
|
+
export function ProgressSteps({
|
|
162
|
+
current,
|
|
163
|
+
total,
|
|
164
|
+
variant = "default",
|
|
165
|
+
color = "default",
|
|
166
|
+
orientation = "horizontal",
|
|
167
|
+
size = "md",
|
|
168
|
+
connectorStyle = "solid",
|
|
169
|
+
testID,
|
|
170
|
+
style,
|
|
171
|
+
children,
|
|
172
|
+
}: ProgressStepsProps) {
|
|
173
|
+
const theme = useTheme()
|
|
174
|
+
const steps = flattenChildren(children).filter(isValidElement)
|
|
175
|
+
const minimal = variant === "minimal"
|
|
176
|
+
const count = minimal ? Math.max(0, total ?? 0) : steps.length
|
|
177
|
+
const motion = useMemo(
|
|
178
|
+
() => ({
|
|
179
|
+
duration: theme.duration("--transition-duration-basic", 120),
|
|
180
|
+
easing: cssEasing(theme.token("--transition-ease-basic")),
|
|
181
|
+
reducedMotion: theme.reducedMotion,
|
|
182
|
+
}),
|
|
183
|
+
[theme],
|
|
184
|
+
)
|
|
185
|
+
const colors = colorsOf(theme, color)
|
|
186
|
+
|
|
187
|
+
if (minimal) {
|
|
188
|
+
const key = size === "lg" ? "lg" : size === "sm" ? "sm" : "md"
|
|
189
|
+
const width = theme.length(`--progress-steps-minimal-width-${key}`, key === "lg" ? 32 : 24)
|
|
190
|
+
const height = theme.length(`--progress-steps-minimal-height-${key}`, key === "lg" ? 4 : 3)
|
|
191
|
+
const gap = theme.length(`--progress-steps-minimal-gap-${key}`, key === "lg" ? 6 : 4)
|
|
192
|
+
return (
|
|
193
|
+
<View
|
|
194
|
+
accessible
|
|
195
|
+
accessibilityRole="progressbar"
|
|
196
|
+
accessibilityLabel="Progress"
|
|
197
|
+
accessibilityValue={{ min: 1, max: count, now: current }}
|
|
198
|
+
{...(web ? { "aria-valuemin": 1, "aria-valuemax": count, "aria-valuenow": current } : null)}
|
|
199
|
+
testID={testID}
|
|
200
|
+
style={[styles.minimal, { gap }, style]}
|
|
201
|
+
>
|
|
202
|
+
{Array.from({ length: count }, (_, index) => (
|
|
203
|
+
<MinimalSegment
|
|
204
|
+
key={index}
|
|
205
|
+
color={index < current ? colors.minimalCompleted : colors.minimalUpcoming}
|
|
206
|
+
width={width}
|
|
207
|
+
height={height}
|
|
208
|
+
radius={theme.length("--progress-steps-minimal-radius", 9999)}
|
|
209
|
+
motion={motion}
|
|
210
|
+
/>
|
|
211
|
+
))}
|
|
212
|
+
</View>
|
|
213
|
+
)
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
const context: Context = {
|
|
217
|
+
current,
|
|
218
|
+
orientation,
|
|
219
|
+
connectorStyle,
|
|
220
|
+
total: count,
|
|
221
|
+
metrics: metricsOf(theme, size),
|
|
222
|
+
colors,
|
|
223
|
+
connectorWidth: theme.length("--progress-steps-connector-width", 2),
|
|
224
|
+
dash: theme.length("--progress-steps-connector-dash-length", 4),
|
|
225
|
+
gap: theme.length("--progress-steps-connector-dash-gap", 4),
|
|
226
|
+
motion,
|
|
227
|
+
}
|
|
228
|
+
const horizontal = orientation === "horizontal"
|
|
229
|
+
return (
|
|
230
|
+
<StepsContext.Provider value={context}>
|
|
231
|
+
<View
|
|
232
|
+
{...(web ? { role: "navigation", "aria-label": "Progress" } : null)}
|
|
233
|
+
testID={testID}
|
|
234
|
+
style={[styles.root, style]}
|
|
235
|
+
>
|
|
236
|
+
<View
|
|
237
|
+
accessibilityRole="list"
|
|
238
|
+
style={horizontal ? [styles.row, { gap: context.metrics.rowGap }] : styles.column}
|
|
239
|
+
>
|
|
240
|
+
{steps.map((step, index) => cloneElement(step as ReactElement<{ _index?: number }>, { _index: index }))}
|
|
241
|
+
</View>
|
|
242
|
+
</View>
|
|
243
|
+
</StepsContext.Provider>
|
|
244
|
+
)
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
function MinimalSegment({ color, width, height, radius, motion }: { color: string; width: number; height: number; radius: number; motion: Context["motion"] }) {
|
|
248
|
+
const backgroundColor = useColorTransition(color, motion)
|
|
249
|
+
return <Animated.View style={{ width, height, borderRadius: Math.min(radius, height / 2), backgroundColor }} />
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/** A step: its indicator, the connector to the next step, and its title and description. */
|
|
253
|
+
function Step({ icon, children, _index = 0 }: StepProps & { _index?: number }) {
|
|
254
|
+
const context = useSteps()
|
|
255
|
+
const { current, orientation, total, metrics, colors, motion } = context
|
|
256
|
+
const state: StepState = _index < current - 1 ? "completed" : _index === current - 1 ? "active" : "inactive"
|
|
257
|
+
const horizontal = orientation === "horizontal"
|
|
258
|
+
const [width, setWidth] = useState(0)
|
|
259
|
+
const onLayout = horizontal ? (event: LayoutChangeEvent) => setWidth(event.nativeEvent.layout.width) : undefined
|
|
260
|
+
const background = useColorTransition(colors.indicator[state], motion)
|
|
261
|
+
const ink = colors.indicatorText[state]
|
|
262
|
+
|
|
263
|
+
const parts = flattenChildren(children)
|
|
264
|
+
const shown = parts.filter((part) => !(isValidElement(part) && part.type === Title && (part.props as StepTitleProps).visuallyHidden))
|
|
265
|
+
const name = parts.map(textOf).filter(Boolean).join(", ")
|
|
266
|
+
const last = _index === total - 1
|
|
267
|
+
|
|
268
|
+
return (
|
|
269
|
+
<View
|
|
270
|
+
// On a phone a step is read as one: its title and description; the current one as selected
|
|
271
|
+
accessible={!web}
|
|
272
|
+
accessibilityLabel={web ? undefined : name || String(_index + 1)}
|
|
273
|
+
accessibilityState={web ? undefined : { selected: state === "active" }}
|
|
274
|
+
{...(web ? { role: "listitem", "aria-current": state === "active" ? "step" : undefined } : null)}
|
|
275
|
+
onLayout={onLayout}
|
|
276
|
+
style={[
|
|
277
|
+
horizontal ? styles.stepHorizontal : styles.stepVertical,
|
|
278
|
+
{ gap: metrics.contentGap, minHeight: metrics.indicator + metrics.rowGap },
|
|
279
|
+
]}
|
|
280
|
+
>
|
|
281
|
+
<Animated.View
|
|
282
|
+
style={[styles.indicator, { width: metrics.indicator, height: metrics.indicator, borderRadius: metrics.indicator / 2, backgroundColor: background }]}
|
|
283
|
+
>
|
|
284
|
+
{state === "completed" ? (
|
|
285
|
+
<CheckMd size={metrics.font} color={ink} />
|
|
286
|
+
) : icon != null ? (
|
|
287
|
+
<IconDefaultsProvider size={metrics.icon} color={ink}>
|
|
288
|
+
{icon}
|
|
289
|
+
</IconDefaultsProvider>
|
|
290
|
+
) : (
|
|
291
|
+
<Text weight="medium" color={ink} style={{ fontSize: metrics.font, lineHeight: Math.round(metrics.font * 1.43) }}>
|
|
292
|
+
{_index + 1}
|
|
293
|
+
</Text>
|
|
294
|
+
)}
|
|
295
|
+
</Animated.View>
|
|
296
|
+
{last ? null : <Connector completed={state === "completed"} width={width} />}
|
|
297
|
+
{parts.length === 0 ? null : (
|
|
298
|
+
<View
|
|
299
|
+
style={
|
|
300
|
+
shown.length === 0
|
|
301
|
+
? styles.hidden
|
|
302
|
+
: horizontal
|
|
303
|
+
? styles.contentHorizontal
|
|
304
|
+
: [styles.contentVertical, { paddingBottom: metrics.rowGap }]
|
|
305
|
+
}
|
|
306
|
+
>
|
|
307
|
+
<StepContext.Provider value={{ state, first: shown[0] }}>{parts}</StepContext.Provider>
|
|
308
|
+
</View>
|
|
309
|
+
)}
|
|
310
|
+
</View>
|
|
311
|
+
)
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
/** The line from a step to the next: under the next indicator's edge, solid once completed, else as `connectorStyle`. */
|
|
315
|
+
function Connector({ completed, width }: { completed: boolean; width: number }) {
|
|
316
|
+
const { orientation, connectorStyle, metrics, colors, connectorWidth, dash, gap, motion } = useSteps()
|
|
317
|
+
const horizontal = orientation === "horizontal"
|
|
318
|
+
const color = useColorTransition(completed ? colors.connectorCompleted : colors.connector, motion)
|
|
319
|
+
const [length, setLength] = useState(0)
|
|
320
|
+
const dashed = connectorStyle === "dashed" && !completed
|
|
321
|
+
const place: ViewStyle = horizontal
|
|
322
|
+
? {
|
|
323
|
+
top: metrics.indicator / 2 - connectorWidth / 2,
|
|
324
|
+
start: "50%",
|
|
325
|
+
marginStart: metrics.indicator / 2,
|
|
326
|
+
width: Math.max(0, width + metrics.rowGap - metrics.indicator),
|
|
327
|
+
height: connectorWidth,
|
|
328
|
+
}
|
|
329
|
+
: { top: metrics.indicator, start: metrics.indicator / 2 - connectorWidth / 2, width: connectorWidth, bottom: -metrics.rowGap }
|
|
330
|
+
return (
|
|
331
|
+
<Animated.View
|
|
332
|
+
pointerEvents="none"
|
|
333
|
+
onLayout={dashed ? (event: LayoutChangeEvent) => setLength(horizontal ? event.nativeEvent.layout.width : event.nativeEvent.layout.height) : undefined}
|
|
334
|
+
style={[styles.connector, place, { borderRadius: connectorWidth / 2 }, dashed ? null : { backgroundColor: color }]}
|
|
335
|
+
>
|
|
336
|
+
{dashed
|
|
337
|
+
? Array.from({ length: Math.ceil(length / (dash + gap)) }, (_, index) => (
|
|
338
|
+
<View
|
|
339
|
+
key={index}
|
|
340
|
+
style={[
|
|
341
|
+
styles.dash,
|
|
342
|
+
horizontal ? { start: index * (dash + gap), width: dash, height: connectorWidth } : { top: index * (dash + gap), height: dash, width: connectorWidth },
|
|
343
|
+
{ backgroundColor: colors.connector },
|
|
344
|
+
]}
|
|
345
|
+
/>
|
|
346
|
+
))
|
|
347
|
+
: null}
|
|
348
|
+
</Animated.View>
|
|
349
|
+
)
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
/** The title of a step: 14/20 medium, beside the indicator's middle in a column, under it in a row. */
|
|
353
|
+
function Title({ children, visuallyHidden = false }: StepTitleProps) {
|
|
354
|
+
const { orientation, metrics, colors } = useSteps()
|
|
355
|
+
const step = useContext(StepContext)
|
|
356
|
+
if (visuallyHidden) {
|
|
357
|
+
// On a phone the step reads its title; on the web it stays in the page for screen readers
|
|
358
|
+
return web ? (
|
|
359
|
+
<View style={styles.visuallyHidden}>
|
|
360
|
+
<Text size="sm">{children}</Text>
|
|
361
|
+
</View>
|
|
362
|
+
) : null
|
|
363
|
+
}
|
|
364
|
+
const inactive = step?.state === "inactive"
|
|
365
|
+
return (
|
|
366
|
+
<Text
|
|
367
|
+
size="sm"
|
|
368
|
+
weight="medium"
|
|
369
|
+
color={inactive ? colors.titleInactive : colors.title}
|
|
370
|
+
align={orientation === "horizontal" ? "center" : undefined}
|
|
371
|
+
style={orientation === "horizontal" ? null : { paddingTop: (metrics.indicator - metrics.line) / 2 }}
|
|
372
|
+
>
|
|
373
|
+
{children}
|
|
374
|
+
</Text>
|
|
375
|
+
)
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
/** The description of a step: 14/20, 2 below the title (beside the indicator's middle when it stands alone). */
|
|
379
|
+
function Description({ children }: StepDescriptionProps) {
|
|
380
|
+
const { orientation, metrics, colors } = useSteps()
|
|
381
|
+
const step = useContext(StepContext)
|
|
382
|
+
const inactive = step?.state === "inactive"
|
|
383
|
+
const first = isValidElement(step?.first) && step.first.type === Description
|
|
384
|
+
const horizontal = orientation === "horizontal"
|
|
385
|
+
return (
|
|
386
|
+
<Text
|
|
387
|
+
size="sm"
|
|
388
|
+
color={inactive ? colors.descriptionInactive : colors.description}
|
|
389
|
+
align={horizontal ? "center" : undefined}
|
|
390
|
+
style={first ? { paddingTop: horizontal ? 0 : (metrics.indicator - metrics.line) / 2 } : { marginTop: 2 }}
|
|
391
|
+
>
|
|
392
|
+
{children}
|
|
393
|
+
</Text>
|
|
394
|
+
)
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
ProgressSteps.Step = Step
|
|
398
|
+
ProgressSteps.Title = Title
|
|
399
|
+
ProgressSteps.Description = Description
|
|
400
|
+
|
|
401
|
+
const styles = StyleSheet.create({
|
|
402
|
+
root: { width: "100%" },
|
|
403
|
+
row: { flexDirection: "row", width: "100%" },
|
|
404
|
+
column: { flexDirection: "column", width: "100%" },
|
|
405
|
+
stepVertical: { flexDirection: "row", alignItems: "flex-start" },
|
|
406
|
+
stepHorizontal: { flex: 1, flexBasis: 0, minWidth: 0, flexDirection: "column", alignItems: "center" },
|
|
407
|
+
indicator: { zIndex: 1, alignItems: "center", justifyContent: "center", flexShrink: 0 },
|
|
408
|
+
connector: { position: "absolute", overflow: "hidden" },
|
|
409
|
+
dash: { position: "absolute" },
|
|
410
|
+
contentVertical: { flex: 1, minWidth: 0 },
|
|
411
|
+
contentHorizontal: { width: "100%", paddingTop: 8 },
|
|
412
|
+
hidden: { position: "absolute", width: 1, height: 1, overflow: "hidden", opacity: 0 },
|
|
413
|
+
visuallyHidden: { position: "absolute", width: 1, height: 1, margin: -1, overflow: "hidden", opacity: 0 },
|
|
414
|
+
minimal: { flexDirection: "row" },
|
|
415
|
+
})
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import { useEffect, useLayoutEffect, useState } from "react"
|
|
2
|
+
import { Animated, Platform, StyleSheet, View, type LayoutChangeEvent, type StyleProp, type TextStyle, type ViewStyle } from "react-native"
|
|
3
|
+
import { curves } from "../../internal/motion"
|
|
4
|
+
import { cssEasing, nativeDriver, useTransitionValue } from "../../internal/transition"
|
|
5
|
+
import { useTheme } from "../../theme/ThemeProvider"
|
|
6
|
+
import { Text, textStyle, type TextProps } from "../Text/Text"
|
|
7
|
+
|
|
8
|
+
export type RollingNumberProps = Omit<TextProps, "children"> & {
|
|
9
|
+
/** The number shown. A new value rolls each changed digit to it. */
|
|
10
|
+
value: number
|
|
11
|
+
/** The locale the number is written in, as `Intl.NumberFormat` takes it. @default "en-US" */
|
|
12
|
+
locales?: Intl.LocalesArgument
|
|
13
|
+
/** How the number is written: currency, percent, fraction digits, grouping, as `Intl.NumberFormat` takes it. */
|
|
14
|
+
format?: Intl.NumberFormatOptions
|
|
15
|
+
/** The style of the row of characters. */
|
|
16
|
+
containerStyle?: StyleProp<ViewStyle>
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
type Character = { character: string; digit: number | null; key: string }
|
|
20
|
+
|
|
21
|
+
/** The parts of a written number, or, where the engine has no `formatToParts`, its characters read as one integer. */
|
|
22
|
+
function partsOf(formatter: Intl.NumberFormat, value: number, glyphs: string[]): Intl.NumberFormatPart[] {
|
|
23
|
+
if (typeof formatter.formatToParts === "function") return formatter.formatToParts(value)
|
|
24
|
+
return Array.from(formatter.format(value)).map((character) => ({ type: glyphs.includes(character) ? "integer" : "literal", value: character }) as Intl.NumberFormatPart)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* A written number's characters, each keyed by its place around the decimal separator, so a digit keeps its reel
|
|
29
|
+
* when the number grows or its fraction gets longer; digits are the locale's own (Arabic-Indic, Bengali…).
|
|
30
|
+
*/
|
|
31
|
+
export const rollingNumberParts = (value: number, locales: Intl.LocalesArgument = "en-US", format?: Intl.NumberFormatOptions) => {
|
|
32
|
+
const formatter = new Intl.NumberFormat(locales, format)
|
|
33
|
+
const { locale, numberingSystem } = formatter.resolvedOptions()
|
|
34
|
+
const digitFormatter = new Intl.NumberFormat(locale, { numberingSystem, useGrouping: false })
|
|
35
|
+
const glyphs = Array.from({ length: 10 }, (_, digit) => digitFormatter.format(digit))
|
|
36
|
+
const parts = partsOf(formatter, value, glyphs)
|
|
37
|
+
let integers = parts.reduce((count, part) => count + (part.type === "integer" ? Array.from(part.value).length : 0), 0)
|
|
38
|
+
let fractions = 0
|
|
39
|
+
const seen: Record<string, number> = {}
|
|
40
|
+
const characters: Character[] = parts.flatMap((part) =>
|
|
41
|
+
Array.from(part.value).map((character) => {
|
|
42
|
+
const glyph = glyphs.indexOf(character)
|
|
43
|
+
const digit = (part.type === "integer" || part.type === "fraction") && glyph >= 0 ? glyph : null
|
|
44
|
+
let key: string
|
|
45
|
+
if (part.type === "integer") key = `i${integers--}`
|
|
46
|
+
else if (part.type === "fraction") key = `f${++fractions}`
|
|
47
|
+
else if (part.type === "group") key = `g${integers}`
|
|
48
|
+
else {
|
|
49
|
+
seen[part.type] = (seen[part.type] ?? 0) + 1
|
|
50
|
+
key = `${part.type}${seen[part.type]}-${character}`
|
|
51
|
+
}
|
|
52
|
+
return { character, digit, key }
|
|
53
|
+
}),
|
|
54
|
+
)
|
|
55
|
+
return { text: parts.map((part) => part.value).join(""), glyphs, characters }
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* A number whose digits roll to a new value, as an odometer, and @plexui/ui's RollingNumber: for a total, a balance, a
|
|
60
|
+
* count or a KPI that changes while it is watched. It takes the props of `Text` for its type and color, in tabular
|
|
61
|
+
* figures. iOS, as the web: each changed digit's reel of 0 to 9 turns to it in `--rolling-number-duration` (600ms) on
|
|
62
|
+
* `--rolling-number-ease`. Android, the Plex Android look's animated text: a changed character fades out and away by
|
|
63
|
+
* 0.3 of the font size while the new one comes in, in 320ms (ease-out quint), up for a larger number. With reduced
|
|
64
|
+
* motion it changes at once. A screen reader reads the written number.
|
|
65
|
+
*/
|
|
66
|
+
export function RollingNumber({ value, locales, format, containerStyle, style, size, variant, weight, mono, testID, accessibilityLabel, ...text }: RollingNumberProps) {
|
|
67
|
+
const theme = useTheme()
|
|
68
|
+
const { text: written, glyphs, characters } = rollingNumberParts(value, locales, format)
|
|
69
|
+
const flat = StyleSheet.flatten(style) as TextStyle | undefined
|
|
70
|
+
const base = textStyle(theme, { variant, size, weight, mono })
|
|
71
|
+
const lineHeight = flat?.lineHeight ?? base.lineHeight ?? 20
|
|
72
|
+
const fontSize = flat?.fontSize ?? base.fontSize ?? 16
|
|
73
|
+
// The direction a change moves on Android: up for a larger number
|
|
74
|
+
const [last, setLast] = useState({ value, up: true })
|
|
75
|
+
if (last.value !== value) setLast({ value, up: value > last.value })
|
|
76
|
+
const glyphProps: TextProps = { ...text, size, variant, weight, mono, tabularNums: true, style }
|
|
77
|
+
return (
|
|
78
|
+
<View
|
|
79
|
+
accessible
|
|
80
|
+
accessibilityRole="text"
|
|
81
|
+
accessibilityLabel={accessibilityLabel ?? written}
|
|
82
|
+
testID={testID}
|
|
83
|
+
// The digits keep left to right in a right-to-left layout, as they are written (the web takes it as `dir`)
|
|
84
|
+
{...(Platform.OS === "web" ? { dir: "ltr" } : null)}
|
|
85
|
+
style={[styles.row, styles.ltr, containerStyle]}
|
|
86
|
+
>
|
|
87
|
+
{characters.map(({ character, digit, key }) =>
|
|
88
|
+
theme.isAndroid ? (
|
|
89
|
+
<Changing key={key} character={character} up={last.up} lift={0.3 * fontSize} reduced={theme.reducedMotion} text={glyphProps} />
|
|
90
|
+
) : digit === null ? (
|
|
91
|
+
<Text key={key} {...glyphProps}>
|
|
92
|
+
{character}
|
|
93
|
+
</Text>
|
|
94
|
+
) : (
|
|
95
|
+
<Reel key={key} digit={digit} character={character} glyphs={glyphs} lineHeight={lineHeight} text={glyphProps} />
|
|
96
|
+
),
|
|
97
|
+
)}
|
|
98
|
+
</View>
|
|
99
|
+
)
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** iOS's reel: the digit itself, hidden, keeps the width and height; a column of 0 to 9 turns over it. */
|
|
103
|
+
function Reel({ digit, character, glyphs, lineHeight, text }: { digit: number; character: string; glyphs: string[]; lineHeight: number; text: TextProps }) {
|
|
104
|
+
const theme = useTheme()
|
|
105
|
+
const [line, setLine] = useState(lineHeight)
|
|
106
|
+
const position = useTransitionValue(digit, {
|
|
107
|
+
duration: theme.duration("--rolling-number-duration", 600),
|
|
108
|
+
easing: cssEasing(theme.token("--rolling-number-ease"), curves.easeInOut),
|
|
109
|
+
native: true,
|
|
110
|
+
reducedMotion: theme.reducedMotion,
|
|
111
|
+
})
|
|
112
|
+
const onLayout = (event: LayoutChangeEvent) => {
|
|
113
|
+
const next = event.nativeEvent.layout.height
|
|
114
|
+
if (next > 0) setLine((old) => (old === next ? old : next))
|
|
115
|
+
}
|
|
116
|
+
return (
|
|
117
|
+
<View style={styles.digit}>
|
|
118
|
+
<Text {...text} onLayout={onLayout} style={[text.style, styles.ghost]}>
|
|
119
|
+
{character}
|
|
120
|
+
</Text>
|
|
121
|
+
<Animated.View
|
|
122
|
+
style={[styles.reel, { transform: [{ translateY: position.interpolate({ inputRange: [0, 9], outputRange: [0, -9 * line] }) }] }]}
|
|
123
|
+
>
|
|
124
|
+
{glyphs.map((glyph) => (
|
|
125
|
+
<Text key={glyph} {...text} style={[text.style, { height: line }]}>
|
|
126
|
+
{glyph}
|
|
127
|
+
</Text>
|
|
128
|
+
))}
|
|
129
|
+
</Animated.View>
|
|
130
|
+
</View>
|
|
131
|
+
)
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** Android's animated text: a new character comes in as the old one leaves, both fading and moving by `lift`. */
|
|
135
|
+
function Changing({ character, up, lift, reduced, text }: { character: string; up: boolean; lift: number; reduced: boolean; text: TextProps }) {
|
|
136
|
+
const [pair, setPair] = useState({ old: null as string | null, now: character, up })
|
|
137
|
+
if (pair.now !== character) setPair({ old: pair.now, now: character, up })
|
|
138
|
+
const [progress] = useState(() => new Animated.Value(1))
|
|
139
|
+
useLayoutEffect(() => {
|
|
140
|
+
if (pair.old == null) return
|
|
141
|
+
progress.setValue(reduced ? 1 : 0)
|
|
142
|
+
}, [pair, progress, reduced])
|
|
143
|
+
useEffect(() => {
|
|
144
|
+
if (pair.old == null || reduced) return
|
|
145
|
+
const animation = Animated.timing(progress, { toValue: 1, duration: 320, easing: curves.easeOutQuint, useNativeDriver: nativeDriver, isInteraction: false })
|
|
146
|
+
animation.start(({ finished }) => {
|
|
147
|
+
if (finished) setPair((current) => (current === pair ? { ...current, old: null } : current))
|
|
148
|
+
})
|
|
149
|
+
return () => animation.stop()
|
|
150
|
+
}, [pair, progress, reduced])
|
|
151
|
+
const direction = pair.up ? 1 : -1
|
|
152
|
+
return (
|
|
153
|
+
<View>
|
|
154
|
+
<Animated.View
|
|
155
|
+
style={
|
|
156
|
+
pair.old == null
|
|
157
|
+
? null
|
|
158
|
+
: { opacity: progress, transform: [{ translateY: progress.interpolate({ inputRange: [0, 1], outputRange: [direction * lift, 0] }) }] }
|
|
159
|
+
}
|
|
160
|
+
>
|
|
161
|
+
<Text {...text}>{pair.now}</Text>
|
|
162
|
+
</Animated.View>
|
|
163
|
+
{pair.old == null || reduced ? null : (
|
|
164
|
+
<Animated.View
|
|
165
|
+
style={[
|
|
166
|
+
styles.leaving,
|
|
167
|
+
{
|
|
168
|
+
opacity: progress.interpolate({ inputRange: [0, 1], outputRange: [1, 0] }),
|
|
169
|
+
transform: [{ translateY: progress.interpolate({ inputRange: [0, 1], outputRange: [0, -direction * lift] }) }],
|
|
170
|
+
},
|
|
171
|
+
]}
|
|
172
|
+
>
|
|
173
|
+
<Text {...text}>{pair.old}</Text>
|
|
174
|
+
</Animated.View>
|
|
175
|
+
)}
|
|
176
|
+
</View>
|
|
177
|
+
)
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
const styles = StyleSheet.create({
|
|
181
|
+
row: { flexDirection: "row", alignItems: "flex-start" },
|
|
182
|
+
// The web takes dir="ltr" (react-native-web has no direction style, and checks every style it is given here)
|
|
183
|
+
ltr: Platform.OS === "web" ? {} : { direction: "ltr" },
|
|
184
|
+
// Clips the reel to one line
|
|
185
|
+
digit: { overflow: "hidden" },
|
|
186
|
+
ghost: { opacity: 0 },
|
|
187
|
+
reel: { position: "absolute", top: 0, start: 0, end: 0, alignItems: "center" },
|
|
188
|
+
leaving: { position: "absolute", top: 0, start: 0 },
|
|
189
|
+
})
|