glacia-native 0.4.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/module/Bars.js +176 -0
- package/lib/module/Carousel.js +103 -0
- package/lib/module/Display.js +240 -0
- package/lib/module/Feedback.js +172 -0
- package/lib/module/Forms2.js +227 -0
- package/lib/module/Overlays.js +166 -0
- package/lib/module/Pickers.js +243 -0
- package/lib/module/Text.js +37 -0
- package/lib/module/index.js +58 -1
- package/lib/module/internal/GlassView.js +23 -7
- package/lib/module/internal/primitives.js +7 -2
- package/lib/module/internal/tokens.js +55 -1
- package/lib/typescript/Bars.d.ts +121 -0
- package/lib/typescript/Carousel.d.ts +47 -0
- package/lib/typescript/Display.d.ts +119 -0
- package/lib/typescript/Feedback.d.ts +138 -0
- package/lib/typescript/Forms2.d.ts +128 -0
- package/lib/typescript/Glass.d.ts +1 -1
- package/lib/typescript/Overlays.d.ts +105 -0
- package/lib/typescript/Pickers.d.ts +105 -0
- package/lib/typescript/Text.d.ts +28 -0
- package/lib/typescript/index.d.ts +9 -1
- package/lib/typescript/internal/GlassView.d.ts +10 -1
- package/lib/typescript/internal/tokens.d.ts +61 -0
- package/package.json +2 -2
- package/src/Bars.tsx +570 -0
- package/src/Carousel.tsx +233 -0
- package/src/Display.tsx +550 -0
- package/src/Feedback.tsx +579 -0
- package/src/Forms2.tsx +608 -0
- package/src/Overlays.tsx +474 -0
- package/src/Pickers.tsx +590 -0
- package/src/Text.tsx +83 -0
- package/src/index.ts +101 -1
- package/src/internal/GlassView.tsx +29 -7
- package/src/internal/primitives.ts +7 -2
- package/src/internal/tokens.ts +62 -0
package/src/Display.tsx
ADDED
|
@@ -0,0 +1,550 @@
|
|
|
1
|
+
import { forwardRef, useEffect, useRef, useState, type ReactNode } from "react";
|
|
2
|
+
import type { StyleProp, ViewStyle } from "react-native";
|
|
3
|
+
|
|
4
|
+
import { Glass } from "./Glass";
|
|
5
|
+
import { colors, radii } from "./internal/tokens";
|
|
6
|
+
import { primitives } from "./internal/primitives";
|
|
7
|
+
import { useIsDark, useSpring } from "./internal/motion";
|
|
8
|
+
|
|
9
|
+
// ---------------------------------------------------------------------------
|
|
10
|
+
// Avatar
|
|
11
|
+
|
|
12
|
+
export type AvatarSize = "sm" | "md" | "lg";
|
|
13
|
+
|
|
14
|
+
export interface AvatarProps {
|
|
15
|
+
/** Image to show. When absent, or when it fails to load, initials render. */
|
|
16
|
+
source?: { uri: string } | number;
|
|
17
|
+
/** The person's name: initials are derived from it when no image loads. */
|
|
18
|
+
name?: string;
|
|
19
|
+
size?: AvatarSize;
|
|
20
|
+
/** Draws a status dot in the corner. */
|
|
21
|
+
status?: "online" | "offline" | "busy";
|
|
22
|
+
style?: StyleProp<ViewStyle>;
|
|
23
|
+
isDark?: boolean;
|
|
24
|
+
testID?: string;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const avatarSizes: Record<AvatarSize, number> = { sm: 28, md: 40, lg: 64 };
|
|
28
|
+
|
|
29
|
+
/** First letters of the first and last word, as an avatar fallback. */
|
|
30
|
+
export function initialsOf(name: string): string {
|
|
31
|
+
const words = name.trim().split(/\s+/).filter(Boolean);
|
|
32
|
+
if (words.length === 0) return "";
|
|
33
|
+
if (words.length === 1) return words[0]!.slice(0, 2).toUpperCase();
|
|
34
|
+
return (words[0]![0]! + words[words.length - 1]![0]!).toUpperCase();
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* A person or entity: their picture, or their initials when there is none.
|
|
39
|
+
*
|
|
40
|
+
* The frame is Glacia's glass so it belongs to the same family as the rest of
|
|
41
|
+
* the library; the initials sit on it in the theme's foreground colour.
|
|
42
|
+
*/
|
|
43
|
+
export const Avatar = forwardRef<any, AvatarProps>(function Avatar(
|
|
44
|
+
{ source, name, size = "md", status, style, isDark: isDarkProp, testID, ...props },
|
|
45
|
+
ref,
|
|
46
|
+
) {
|
|
47
|
+
const isDark = useIsDark(isDarkProp);
|
|
48
|
+
const theme = isDark ? colors.dark : colors.light;
|
|
49
|
+
const { View, Text, Image } = primitives();
|
|
50
|
+
|
|
51
|
+
// A broken remote image falls back to initials rather than an empty circle.
|
|
52
|
+
const [failed, setFailed] = useState(false);
|
|
53
|
+
const dimension = avatarSizes[size];
|
|
54
|
+
const initials = name ? initialsOf(name) : "";
|
|
55
|
+
|
|
56
|
+
const statusColor =
|
|
57
|
+
status === "online" ? theme.on : status === "busy" ? theme.destructive : theme.muted;
|
|
58
|
+
const dot = Math.max(8, Math.round(dimension * 0.26));
|
|
59
|
+
|
|
60
|
+
return (
|
|
61
|
+
<View
|
|
62
|
+
ref={ref}
|
|
63
|
+
testID={testID}
|
|
64
|
+
accessible
|
|
65
|
+
accessibilityRole="image"
|
|
66
|
+
accessibilityLabel={name ? `Avatar of ${name}` : "Avatar"}
|
|
67
|
+
style={{ width: dimension, height: dimension, ...(style as ViewStyle) }}
|
|
68
|
+
{...props}
|
|
69
|
+
>
|
|
70
|
+
<Glass
|
|
71
|
+
effect="regular"
|
|
72
|
+
isDark={isDark}
|
|
73
|
+
style={{
|
|
74
|
+
width: dimension,
|
|
75
|
+
height: dimension,
|
|
76
|
+
borderRadius: dimension / 2,
|
|
77
|
+
alignItems: "center",
|
|
78
|
+
justifyContent: "center",
|
|
79
|
+
overflow: "hidden",
|
|
80
|
+
}}
|
|
81
|
+
>
|
|
82
|
+
{source && !failed ? (
|
|
83
|
+
<Image
|
|
84
|
+
source={source}
|
|
85
|
+
accessibilityLabel={name ?? "Avatar"}
|
|
86
|
+
onError={() => setFailed(true)}
|
|
87
|
+
style={{ width: dimension, height: dimension }}
|
|
88
|
+
/>
|
|
89
|
+
) : (
|
|
90
|
+
<Text
|
|
91
|
+
style={{
|
|
92
|
+
fontSize: Math.round(dimension * 0.38),
|
|
93
|
+
fontWeight: "600",
|
|
94
|
+
color: theme.foreground,
|
|
95
|
+
}}
|
|
96
|
+
>
|
|
97
|
+
{initials}
|
|
98
|
+
</Text>
|
|
99
|
+
)}
|
|
100
|
+
</Glass>
|
|
101
|
+
|
|
102
|
+
{status && (
|
|
103
|
+
<View
|
|
104
|
+
style={{
|
|
105
|
+
position: "absolute",
|
|
106
|
+
right: 0,
|
|
107
|
+
bottom: 0,
|
|
108
|
+
width: dot,
|
|
109
|
+
height: dot,
|
|
110
|
+
borderRadius: dot / 2,
|
|
111
|
+
backgroundColor: statusColor,
|
|
112
|
+
borderWidth: 1.5,
|
|
113
|
+
borderColor: theme.glassActive,
|
|
114
|
+
}}
|
|
115
|
+
/>
|
|
116
|
+
)}
|
|
117
|
+
</View>
|
|
118
|
+
);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
// ---------------------------------------------------------------------------
|
|
122
|
+
// Badge
|
|
123
|
+
|
|
124
|
+
export interface BadgeProps {
|
|
125
|
+
/** The count or short label. Omit to render a plain dot. */
|
|
126
|
+
children?: ReactNode;
|
|
127
|
+
/** Draws the badge in the destructive tone (unread, errors). */
|
|
128
|
+
tone?: "fill" | "destructive" | "success" | "muted";
|
|
129
|
+
/** The element to overlay. Supplying it puts the badge in the corner. */
|
|
130
|
+
overlay?: ReactNode;
|
|
131
|
+
style?: StyleProp<ViewStyle>;
|
|
132
|
+
isDark?: boolean;
|
|
133
|
+
testID?: string;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* A small count or dot.
|
|
138
|
+
*
|
|
139
|
+
* Deliberately not glass: a badge is a micro-element, and a translucent 16pt
|
|
140
|
+
* chip loses its contrast against whatever it sits on. It uses the solid theme
|
|
141
|
+
* tint instead, which is what makes it legible in both themes.
|
|
142
|
+
*/
|
|
143
|
+
export const Badge = forwardRef<any, BadgeProps>(function Badge(
|
|
144
|
+
{ children, tone = "fill", overlay, style, isDark: isDarkProp, testID, ...props },
|
|
145
|
+
ref,
|
|
146
|
+
) {
|
|
147
|
+
const isDark = useIsDark(isDarkProp);
|
|
148
|
+
const theme = isDark ? colors.dark : colors.light;
|
|
149
|
+
const { View, Text } = primitives();
|
|
150
|
+
|
|
151
|
+
const background =
|
|
152
|
+
tone === "destructive"
|
|
153
|
+
? theme.destructive
|
|
154
|
+
: tone === "success"
|
|
155
|
+
? theme.on
|
|
156
|
+
: tone === "muted"
|
|
157
|
+
? theme.muted
|
|
158
|
+
: theme.fill;
|
|
159
|
+
|
|
160
|
+
const isDot = children == null;
|
|
161
|
+
const height = isDot ? 10 : 18;
|
|
162
|
+
|
|
163
|
+
const badge = (
|
|
164
|
+
<View
|
|
165
|
+
ref={ref}
|
|
166
|
+
testID={testID}
|
|
167
|
+
accessibilityRole="text"
|
|
168
|
+
accessibilityLabel={isDot ? undefined : String(children)}
|
|
169
|
+
style={{
|
|
170
|
+
minWidth: height,
|
|
171
|
+
height,
|
|
172
|
+
borderRadius: height / 2,
|
|
173
|
+
paddingHorizontal: isDot ? 0 : 6,
|
|
174
|
+
alignItems: "center",
|
|
175
|
+
justifyContent: "center",
|
|
176
|
+
backgroundColor: background,
|
|
177
|
+
...(style as ViewStyle),
|
|
178
|
+
}}
|
|
179
|
+
{...props}
|
|
180
|
+
>
|
|
181
|
+
{!isDot && (
|
|
182
|
+
<Text style={{ fontSize: 11, fontWeight: "700", color: theme.onTint }} numberOfLines={1}>
|
|
183
|
+
{children}
|
|
184
|
+
</Text>
|
|
185
|
+
)}
|
|
186
|
+
</View>
|
|
187
|
+
);
|
|
188
|
+
|
|
189
|
+
if (overlay === undefined) return badge;
|
|
190
|
+
|
|
191
|
+
// Overlaid: the badge floats at the top-right of whatever it decorates, which
|
|
192
|
+
// needs a positioned box around the child rather than a bare fragment.
|
|
193
|
+
return (
|
|
194
|
+
<View style={{ alignSelf: "flex-start" }}>
|
|
195
|
+
{overlay}
|
|
196
|
+
<View style={{ position: "absolute", top: -4, right: -4 }}>{badge}</View>
|
|
197
|
+
</View>
|
|
198
|
+
);
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
// ---------------------------------------------------------------------------
|
|
202
|
+
// Separator
|
|
203
|
+
|
|
204
|
+
export interface SeparatorProps {
|
|
205
|
+
orientation?: "horizontal" | "vertical";
|
|
206
|
+
/** Inset from the leading edge, matching a list row's leading accessory. */
|
|
207
|
+
inset?: number;
|
|
208
|
+
style?: StyleProp<ViewStyle>;
|
|
209
|
+
isDark?: boolean;
|
|
210
|
+
testID?: string;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* A hairline divider in the theme's separator colour.
|
|
215
|
+
*
|
|
216
|
+
* A plain View rather than a Glass: a 1pt rule is not a surface, and glass on
|
|
217
|
+
* a line has nothing to refract.
|
|
218
|
+
*/
|
|
219
|
+
export const Separator = forwardRef<any, SeparatorProps>(function Separator(
|
|
220
|
+
{ orientation = "horizontal", inset = 0, style, isDark: isDarkProp, testID, ...props },
|
|
221
|
+
ref,
|
|
222
|
+
) {
|
|
223
|
+
const isDark = useIsDark(isDarkProp);
|
|
224
|
+
const theme = isDark ? colors.dark : colors.light;
|
|
225
|
+
const { View } = primitives();
|
|
226
|
+
|
|
227
|
+
const separatorStyle: ViewStyle =
|
|
228
|
+
orientation === "horizontal"
|
|
229
|
+
? { height: 0.5, alignSelf: "stretch", marginLeft: inset, backgroundColor: theme.separator }
|
|
230
|
+
: { width: 0.5, alignSelf: "stretch", backgroundColor: theme.separator };
|
|
231
|
+
|
|
232
|
+
return (
|
|
233
|
+
<View
|
|
234
|
+
ref={ref}
|
|
235
|
+
testID={testID}
|
|
236
|
+
accessibilityRole="none"
|
|
237
|
+
style={{ ...separatorStyle, ...(style as ViewStyle) }}
|
|
238
|
+
{...props}
|
|
239
|
+
/>
|
|
240
|
+
);
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
// ---------------------------------------------------------------------------
|
|
244
|
+
// Skeleton
|
|
245
|
+
|
|
246
|
+
export interface SkeletonProps {
|
|
247
|
+
/** Rectangles for cards and rows, circles for avatars, lines for a run of text. */
|
|
248
|
+
shape?: "rect" | "circle" | "line";
|
|
249
|
+
width?: number | string;
|
|
250
|
+
height?: number;
|
|
251
|
+
style?: StyleProp<ViewStyle>;
|
|
252
|
+
isDark?: boolean;
|
|
253
|
+
testID?: string;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* A loading placeholder.
|
|
258
|
+
*
|
|
259
|
+
* The pulse is opacity only and slow, on the gentle spring, so it reads as
|
|
260
|
+
* "loading" without motion that could bother someone sensitive to movement.
|
|
261
|
+
* Nothing about it is essential to understanding the screen, so it is hidden
|
|
262
|
+
* from screen readers rather than announced.
|
|
263
|
+
*/
|
|
264
|
+
export const Skeleton = forwardRef<any, SkeletonProps>(function Skeleton(
|
|
265
|
+
{ shape = "rect", width, height, style, isDark: isDarkProp, testID, ...props },
|
|
266
|
+
ref,
|
|
267
|
+
) {
|
|
268
|
+
const isDark = useIsDark(isDarkProp);
|
|
269
|
+
const theme = isDark ? colors.dark : colors.light;
|
|
270
|
+
const { View, Animated } = primitives();
|
|
271
|
+
|
|
272
|
+
const { value: pulse, animated } = useSpring(1, "gentle");
|
|
273
|
+
const opacity = animated
|
|
274
|
+
? pulse.interpolate({ inputRange: [0, 1], outputRange: [0.35, 1] })
|
|
275
|
+
: 1;
|
|
276
|
+
|
|
277
|
+
const resolvedWidth = width ?? (shape === "line" ? "100%" : shape === "circle" ? 40 : 120);
|
|
278
|
+
const resolvedHeight = height ?? (shape === "line" ? 12 : shape === "circle" ? 40 : 80);
|
|
279
|
+
const radius =
|
|
280
|
+
shape === "circle" ? Math.min(resolvedHeight, 9999) / 2 : shape === "line" ? radii.sm : radii.md;
|
|
281
|
+
|
|
282
|
+
const skeletonStyle: ViewStyle = {
|
|
283
|
+
width: resolvedWidth as any,
|
|
284
|
+
height: resolvedHeight,
|
|
285
|
+
borderRadius: radius,
|
|
286
|
+
backgroundColor: theme.skeleton,
|
|
287
|
+
...(style as ViewStyle),
|
|
288
|
+
};
|
|
289
|
+
|
|
290
|
+
const a11y = { accessibilityRole: "none" as const, accessible: false, testID };
|
|
291
|
+
|
|
292
|
+
return animated ? (
|
|
293
|
+
<Animated.View ref={ref} style={{ ...skeletonStyle, opacity }} {...a11y} {...props} />
|
|
294
|
+
) : (
|
|
295
|
+
<View ref={ref} style={skeletonStyle} {...a11y} {...props} />
|
|
296
|
+
);
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
// ---------------------------------------------------------------------------
|
|
300
|
+
// Spinner
|
|
301
|
+
|
|
302
|
+
export type SpinnerSize = "sm" | "md" | "lg";
|
|
303
|
+
|
|
304
|
+
export interface SpinnerProps {
|
|
305
|
+
size?: SpinnerSize;
|
|
306
|
+
/** Spinner colour; defaults to the theme's muted foreground. */
|
|
307
|
+
tint?: string;
|
|
308
|
+
/** Announced to screen readers, since a spinner alone has no text. */
|
|
309
|
+
accessibilityLabel?: string;
|
|
310
|
+
style?: StyleProp<ViewStyle>;
|
|
311
|
+
isDark?: boolean;
|
|
312
|
+
testID?: string;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
const spinnerSizes: Record<SpinnerSize, number> = { sm: 16, md: 22, lg: 32 };
|
|
316
|
+
|
|
317
|
+
/**
|
|
318
|
+
* A loading spinner, drawn by Glacia.
|
|
319
|
+
*
|
|
320
|
+
* An SVG arc that rotates on the gentle spring through Animated — not the
|
|
321
|
+
* platform's ActivityIndicator, so it looks the same on every platform and
|
|
322
|
+
* wears the theme's colour instead of inheriting the system tint.
|
|
323
|
+
*/
|
|
324
|
+
export const Spinner = forwardRef<any, SpinnerProps>(function Spinner(
|
|
325
|
+
{
|
|
326
|
+
size = "md",
|
|
327
|
+
tint,
|
|
328
|
+
accessibilityLabel = "Loading",
|
|
329
|
+
style,
|
|
330
|
+
isDark: isDarkProp,
|
|
331
|
+
testID,
|
|
332
|
+
...props
|
|
333
|
+
},
|
|
334
|
+
ref,
|
|
335
|
+
) {
|
|
336
|
+
const isDark = useIsDark(isDarkProp);
|
|
337
|
+
const theme = isDark ? colors.dark : colors.light;
|
|
338
|
+
const { View, Svg, Circle, Animated } = primitives();
|
|
339
|
+
|
|
340
|
+
const dimension = spinnerSizes[size];
|
|
341
|
+
const stroke = Math.max(2, Math.round(dimension / 9));
|
|
342
|
+
const color = tint ?? theme.muted;
|
|
343
|
+
// An arc, not a full ring: a complete circle rotating looks static.
|
|
344
|
+
const circumference = 2 * Math.PI * ((dimension - stroke) / 2);
|
|
345
|
+
const dash = circumference * 0.28;
|
|
346
|
+
|
|
347
|
+
// A continuous spin never settles, so it is driven by Animated.timing on a
|
|
348
|
+
// loop rather than the spring hook, which is built for one-off targets.
|
|
349
|
+
const { Animated: AnimatedAPI, isNative } = primitives();
|
|
350
|
+
const spinning = isNative && Boolean(AnimatedAPI?.timing);
|
|
351
|
+
|
|
352
|
+
const rotation = useRef<any>(null);
|
|
353
|
+
if (rotation.current === null) {
|
|
354
|
+
rotation.current = spinning ? new AnimatedAPI.Value(0) : null;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
useEffect(() => {
|
|
358
|
+
if (!spinning || !rotation.current) return;
|
|
359
|
+
rotation.current.setValue(0);
|
|
360
|
+
const loop = AnimatedAPI.loop(
|
|
361
|
+
AnimatedAPI.timing(rotation.current, {
|
|
362
|
+
toValue: 1,
|
|
363
|
+
duration: 900,
|
|
364
|
+
useNativeDriver: true,
|
|
365
|
+
}),
|
|
366
|
+
);
|
|
367
|
+
loop.start?.();
|
|
368
|
+
return () => loop.stop?.();
|
|
369
|
+
}, [AnimatedAPI, spinning]);
|
|
370
|
+
|
|
371
|
+
const spinStyle = spinning
|
|
372
|
+
? {
|
|
373
|
+
transform: [
|
|
374
|
+
{
|
|
375
|
+
rotate: rotation.current.interpolate({
|
|
376
|
+
inputRange: [0, 1],
|
|
377
|
+
outputRange: ["0deg", "360deg"],
|
|
378
|
+
}),
|
|
379
|
+
},
|
|
380
|
+
],
|
|
381
|
+
}
|
|
382
|
+
: undefined;
|
|
383
|
+
|
|
384
|
+
const svg = (
|
|
385
|
+
<Svg width={dimension} height={dimension} viewBox={`0 0 ${dimension} ${dimension}`}>
|
|
386
|
+
<Circle
|
|
387
|
+
cx={dimension / 2}
|
|
388
|
+
cy={dimension / 2}
|
|
389
|
+
r={(dimension - stroke) / 2}
|
|
390
|
+
stroke={theme.skeleton}
|
|
391
|
+
strokeWidth={stroke}
|
|
392
|
+
fill="none"
|
|
393
|
+
/>
|
|
394
|
+
<Circle
|
|
395
|
+
cx={dimension / 2}
|
|
396
|
+
cy={dimension / 2}
|
|
397
|
+
r={(dimension - stroke) / 2}
|
|
398
|
+
stroke={color}
|
|
399
|
+
strokeWidth={stroke}
|
|
400
|
+
fill="none"
|
|
401
|
+
strokeLinecap="round"
|
|
402
|
+
strokeDasharray={`${dash} ${circumference - dash}`}
|
|
403
|
+
/>
|
|
404
|
+
</Svg>
|
|
405
|
+
);
|
|
406
|
+
|
|
407
|
+
return (
|
|
408
|
+
<View
|
|
409
|
+
ref={ref}
|
|
410
|
+
testID={testID}
|
|
411
|
+
accessibilityRole="progressbar"
|
|
412
|
+
accessibilityLabel={accessibilityLabel}
|
|
413
|
+
style={[{ width: dimension, height: dimension }, style as ViewStyle]}
|
|
414
|
+
{...props}
|
|
415
|
+
>
|
|
416
|
+
{spinning ? <Animated.View style={spinStyle}>{svg}</Animated.View> : svg}
|
|
417
|
+
</View>
|
|
418
|
+
);
|
|
419
|
+
});
|
|
420
|
+
|
|
421
|
+
// ---------------------------------------------------------------------------
|
|
422
|
+
// Progress
|
|
423
|
+
|
|
424
|
+
export type ProgressVariant = "bar" | "ring";
|
|
425
|
+
|
|
426
|
+
export interface ProgressProps {
|
|
427
|
+
/** 0–100. Values outside the range are clamped rather than drawn wrong. */
|
|
428
|
+
value: number;
|
|
429
|
+
variant?: ProgressVariant;
|
|
430
|
+
/** Overrides the fill colour; defaults to the theme accent. */
|
|
431
|
+
tint?: string;
|
|
432
|
+
/** Announced by screen readers, e.g. "Uploading photo". */
|
|
433
|
+
accessibilityLabel?: string;
|
|
434
|
+
style?: StyleProp<ViewStyle>;
|
|
435
|
+
isDark?: boolean;
|
|
436
|
+
testID?: string;
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
const RING = 64;
|
|
440
|
+
|
|
441
|
+
/**
|
|
442
|
+
* Progress, linear or circular.
|
|
443
|
+
*
|
|
444
|
+
* The track is a glass surface, so it refracts what it sits on exactly like the
|
|
445
|
+
* rest of the library; the fill is the theme accent. Both forms report
|
|
446
|
+
* `progressbar` with a value, which is the contract NativeTask relies on
|
|
447
|
+
* internally — this component generalises it.
|
|
448
|
+
*/
|
|
449
|
+
export const Progress = forwardRef<any, ProgressProps>(function Progress(
|
|
450
|
+
{
|
|
451
|
+
value,
|
|
452
|
+
variant = "bar",
|
|
453
|
+
tint,
|
|
454
|
+
accessibilityLabel,
|
|
455
|
+
style,
|
|
456
|
+
isDark: isDarkProp,
|
|
457
|
+
testID,
|
|
458
|
+
...props
|
|
459
|
+
},
|
|
460
|
+
ref,
|
|
461
|
+
) {
|
|
462
|
+
const isDark = useIsDark(isDarkProp);
|
|
463
|
+
const theme = isDark ? colors.dark : colors.light;
|
|
464
|
+
const { View, Text, Svg, Circle } = primitives();
|
|
465
|
+
|
|
466
|
+
const clamped = Math.min(Math.max(value, 0), 100);
|
|
467
|
+
const fill = tint ?? theme.fill;
|
|
468
|
+
const a11y = {
|
|
469
|
+
accessibilityRole: "progressbar" as const,
|
|
470
|
+
accessibilityValue: { min: 0, max: 100, now: Math.round(clamped) },
|
|
471
|
+
accessibilityLabel,
|
|
472
|
+
testID,
|
|
473
|
+
};
|
|
474
|
+
|
|
475
|
+
if (variant === "ring") {
|
|
476
|
+
const stroke = 6;
|
|
477
|
+
const radius = (RING - stroke) / 2;
|
|
478
|
+
const circumference = 2 * Math.PI * radius;
|
|
479
|
+
return (
|
|
480
|
+
<View
|
|
481
|
+
ref={ref}
|
|
482
|
+
style={[{ width: RING, height: RING }, style as ViewStyle]}
|
|
483
|
+
{...a11y}
|
|
484
|
+
{...props}
|
|
485
|
+
>
|
|
486
|
+
<Svg width={RING} height={RING} viewBox={`0 0 ${RING} ${RING}`}>
|
|
487
|
+
<Circle
|
|
488
|
+
cx={RING / 2}
|
|
489
|
+
cy={RING / 2}
|
|
490
|
+
r={radius}
|
|
491
|
+
stroke={theme.skeleton}
|
|
492
|
+
strokeWidth={stroke}
|
|
493
|
+
fill="none"
|
|
494
|
+
/>
|
|
495
|
+
<Circle
|
|
496
|
+
cx={RING / 2}
|
|
497
|
+
cy={RING / 2}
|
|
498
|
+
r={radius}
|
|
499
|
+
stroke={fill}
|
|
500
|
+
strokeWidth={stroke}
|
|
501
|
+
fill="none"
|
|
502
|
+
strokeLinecap="round"
|
|
503
|
+
strokeDasharray={`${(clamped / 100) * circumference} ${circumference}`}
|
|
504
|
+
// Start the arc at twelve o'clock, the way progress dials read.
|
|
505
|
+
transform={`rotate(-90 ${RING / 2} ${RING / 2})`}
|
|
506
|
+
/>
|
|
507
|
+
</Svg>
|
|
508
|
+
<View
|
|
509
|
+
style={{
|
|
510
|
+
position: "absolute",
|
|
511
|
+
width: RING,
|
|
512
|
+
height: RING,
|
|
513
|
+
alignItems: "center",
|
|
514
|
+
justifyContent: "center",
|
|
515
|
+
}}
|
|
516
|
+
>
|
|
517
|
+
<Text style={{ fontSize: 13, fontWeight: "600", color: theme.foreground }}>
|
|
518
|
+
{`${Math.round(clamped)}%`}
|
|
519
|
+
</Text>
|
|
520
|
+
</View>
|
|
521
|
+
</View>
|
|
522
|
+
);
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
return (
|
|
526
|
+
<Glass
|
|
527
|
+
ref={ref}
|
|
528
|
+
effect="regular"
|
|
529
|
+
isDark={isDark}
|
|
530
|
+
style={{
|
|
531
|
+
height: 8,
|
|
532
|
+
borderRadius: radii.pill,
|
|
533
|
+
overflow: "hidden",
|
|
534
|
+
justifyContent: "center",
|
|
535
|
+
...(style as ViewStyle),
|
|
536
|
+
}}
|
|
537
|
+
{...a11y}
|
|
538
|
+
{...props}
|
|
539
|
+
>
|
|
540
|
+
<View
|
|
541
|
+
style={{
|
|
542
|
+
width: `${clamped}%`,
|
|
543
|
+
height: 8,
|
|
544
|
+
borderRadius: radii.pill,
|
|
545
|
+
backgroundColor: fill,
|
|
546
|
+
}}
|
|
547
|
+
/>
|
|
548
|
+
</Glass>
|
|
549
|
+
);
|
|
550
|
+
});
|