@delacour/react-native-ui 0.1.0-alpha.20260925053522 → 0.1.0-alpha.20260925055550
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/package.json +5 -3
- package/src/components/progress/AGENTS.md +120 -0
- package/src/components/progress/index.ts +40 -0
- package/src/components/progress/progress-fill.tsx +152 -0
- package/src/components/progress/progress-header.tsx +19 -0
- package/src/components/progress/progress-label.tsx +27 -0
- package/src/components/progress/progress-output.tsx +47 -0
- package/src/components/progress/progress-track.tsx +38 -0
- package/src/components/progress/progress.context.tsx +79 -0
- package/src/components/progress/progress.tsx +171 -0
- package/src/components/progress/progress.types.ts +23 -0
- package/src/components/progress/progress.variants.test.ts +335 -0
- package/src/components/progress/progress.variants.ts +284 -0
- package/src/components/surface/AGENTS.md +72 -0
- package/src/components/surface/index.ts +15 -0
- package/src/components/surface/surface.context.tsx +54 -0
- package/src/components/surface/surface.tsx +70 -0
- package/src/components/surface/surface.variants.test.ts +180 -0
- package/src/components/surface/surface.variants.ts +133 -0
- package/src/lib/cn.test.ts +14 -0
- package/src/lib/cn.ts +2 -2
- package/src/styles/tokens.ts +10 -0
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@delacour/react-native-ui",
|
|
3
|
-
"version": "0.1.0-alpha.
|
|
4
|
-
"description": "React Native UI components
|
|
3
|
+
"version": "0.1.0-alpha.20260925055550",
|
|
4
|
+
"description": "React Native UI components — Uniwind, Reanimated, Gesture Handler",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"repository": {
|
|
@@ -44,12 +44,14 @@
|
|
|
44
44
|
"./input": "./src/components/input/index.ts",
|
|
45
45
|
"./list-group": "./src/components/list-group/index.ts",
|
|
46
46
|
"./pressable": "./src/components/pressable/index.ts",
|
|
47
|
+
"./progress": "./src/components/progress/index.ts",
|
|
47
48
|
"./provider": "./src/components/provider/index.ts",
|
|
48
49
|
"./radio": "./src/components/radio/index.ts",
|
|
49
50
|
"./screen": "./src/components/screen/index.ts",
|
|
50
51
|
"./separator": "./src/components/separator/index.ts",
|
|
51
52
|
"./slider": "./src/components/slider/index.ts",
|
|
52
53
|
"./spinner": "./src/components/spinner/index.ts",
|
|
54
|
+
"./surface": "./src/components/surface/index.ts",
|
|
53
55
|
"./switch": "./src/components/switch/index.ts",
|
|
54
56
|
"./tabs": "./src/components/tabs/index.ts",
|
|
55
57
|
"./text": "./src/components/text/index.ts",
|
|
@@ -81,7 +83,7 @@
|
|
|
81
83
|
},
|
|
82
84
|
"peerDependencies": {
|
|
83
85
|
"@central-icons-react-native/round-outlined-radius-1-stroke-1.5": "^1.1",
|
|
84
|
-
"@delacour/react-native-charts": "0.1.0-alpha.
|
|
86
|
+
"@delacour/react-native-charts": "0.1.0-alpha.20260925055550",
|
|
85
87
|
"@gorhom/bottom-sheet": "^5.2.8",
|
|
86
88
|
"@legendapp/list": ">=3.3",
|
|
87
89
|
"expo-linear-gradient": ">=15",
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
# Progress
|
|
2
|
+
|
|
3
|
+
How far a task has got, or — indeterminate — that it is under way. Compound root
|
|
4
|
+
plus `Progress.Header`, `Progress.Label`, `Progress.Output`, `Progress.Track` and
|
|
5
|
+
`Progress.Fill`. An output, not a control: nothing here is pressed or dragged.
|
|
6
|
+
|
|
7
|
+
`import { Progress } from "@delacour/react-native-ui/progress";`
|
|
8
|
+
|
|
9
|
+
## Files
|
|
10
|
+
|
|
11
|
+
| File | What it holds |
|
|
12
|
+
| --- | --- |
|
|
13
|
+
| `index.ts` | → `@delacour/react-native-ui/progress` |
|
|
14
|
+
| `progress.tsx` | Root + the `Object.assign` compound surface, and the accessibility surface |
|
|
15
|
+
| `progress-header.tsx` | `Progress.Header`, the label-and-readout row |
|
|
16
|
+
| `progress-label.tsx` | `Progress.Label`, a `Text.Label` naming what is measured |
|
|
17
|
+
| `progress-output.tsx` | `Progress.Output`, the formatted value |
|
|
18
|
+
| `progress-track.tsx` | `Progress.Track` — the clip, and the width measurement |
|
|
19
|
+
| `progress-fill.tsx` | `Progress.Fill` — every animation the component runs |
|
|
20
|
+
| `progress.context.tsx` | `ProgressContext`, `useProgress()`, `useProgressContext()`, `useProgressPart()` |
|
|
21
|
+
| `progress.types.ts` | `ProgressRenderProps`, shared by the context and the readout |
|
|
22
|
+
| `progress.variants.ts` | Pure `tv()` slots + the ratio, geometry, formatting and accessibility resolvers |
|
|
23
|
+
| `progress.variants.test.ts` | |
|
|
24
|
+
|
|
25
|
+
## Design
|
|
26
|
+
|
|
27
|
+
- **Colours**: `default`, `primary`, `success`, `warning`, `destructive`, `info` —
|
|
28
|
+
the slider's set, and a test pins the two lists equal so a bar and a slider in
|
|
29
|
+
one form name a colour with the same word. **Sizes**: `sm`, `md`, `lg`, the
|
|
30
|
+
track's thickness (`h-1`/`h-2`/`h-3`) and the header's type step.
|
|
31
|
+
- **The colour paints the fill, never the track.** An empty bar is the slider's
|
|
32
|
+
groove, `bg-secondary`, at every colour. A test asserts the track's class does
|
|
33
|
+
not move with `color`.
|
|
34
|
+
- **A bare `<Progress value={40} />` draws the bar.** The anatomy is written out
|
|
35
|
+
when a label or readout is wanted, but a compound root with no default children
|
|
36
|
+
renders an empty `View` — the failure the CLI's verify screen already hit once
|
|
37
|
+
with `Slider`, and one no error reports.
|
|
38
|
+
- **No `isDisabled`, no `isInvalid`, no `Field`.** A progress bar reports; it
|
|
39
|
+
does not accept input, so there is nothing to disable and no value to reject.
|
|
40
|
+
`resolveProgressAxes` takes one argument and a test pins that, so a `Field`
|
|
41
|
+
rung cannot be added by accident. A failed task is `color="destructive"`.
|
|
42
|
+
|
|
43
|
+
## Motion
|
|
44
|
+
|
|
45
|
+
- **The fill is a full-length bar slid under a clipping track.** Its position is
|
|
46
|
+
a `translateX` of `(ratio − 1) × trackWidth` — a transform, composited on the UI
|
|
47
|
+
thread with no layout pass. Animating `width` re-lays the bar out on every frame
|
|
48
|
+
of every update. The track's `overflow-hidden rounded-full` is what makes the
|
|
49
|
+
slide read as growth, and it keeps the leading end's round cap because that end
|
|
50
|
+
is the fill's own, not a cut.
|
|
51
|
+
- **One timing per value, on the UI thread.** `useEffect` hands the new ratio to
|
|
52
|
+
`withTiming` once (`PROGRESS_FILL_DURATION_MS`, ease-out); the frames that
|
|
53
|
+
follow never touch React. An upload reporting ten times a second costs ten
|
|
54
|
+
renders.
|
|
55
|
+
- **The first frame is at the value, not animated up from zero.** The shared
|
|
56
|
+
value is seeded with the ratio, so a list of bars scrolling in does not
|
|
57
|
+
replay a fill on every row that mounts.
|
|
58
|
+
- **Hidden until measured.** The track reports its width in `onLayout`; before
|
|
59
|
+
that the fill is `opacity: 0`, because a bar positioned against a width of 0
|
|
60
|
+
flashes at full length for one frame.
|
|
61
|
+
- **Indeterminate is a segment sweeping from wholly off-left to wholly
|
|
62
|
+
off-right.** `indeterminateSegment` puts both ends of the loop outside the
|
|
63
|
+
track, so the frame where `withRepeat` restarts is never on screen. A sweep
|
|
64
|
+
that started at the track's left edge pops into view every pass.
|
|
65
|
+
- **Reduce motion swaps the sweep for a pulse, and does not stop it.** With the
|
|
66
|
+
system setting on, the fill covers the whole track and breathes between
|
|
67
|
+
`PROGRESS_PULSE.from` and `to`. Both animations opt out with
|
|
68
|
+
`ReduceMotion.Never`: under the default policy a timing completes instantly and
|
|
69
|
+
`withRepeat(-1)` spins a zero-length animation forever — a frozen bar, which
|
|
70
|
+
reads as hung. `Spinner` makes the same call for the same reason. **Both layers
|
|
71
|
+
opt out** — the timing *and* the `withRepeat`, which has a policy of its own:
|
|
72
|
+
left at `System`, a reversing repeat never starts and a forward one stops after
|
|
73
|
+
one pass. The pulse sat at full opacity on a simulator until the repeat's own
|
|
74
|
+
argument was set. The pulse
|
|
75
|
+
never reaches zero opacity, because a bar that disappears reads as finished.
|
|
76
|
+
- **The animated style returns the same keys in every mode** — `opacity`,
|
|
77
|
+
`transform`, `width` — so flipping `isIndeterminate` never asks Reanimated to
|
|
78
|
+
change a style's shape mid-animation.
|
|
79
|
+
- **Both effects clean up.** `cancelAnimation` on unmount and on every flip back
|
|
80
|
+
to a value, or the repeat outlives the bar.
|
|
81
|
+
|
|
82
|
+
## Values and formatting
|
|
83
|
+
|
|
84
|
+
- **`progressRatio` clamps, and returns 0 for a degenerate range or a
|
|
85
|
+
non-finite value.** An upload dividing by a total it has not learned yet
|
|
86
|
+
produces `NaN`, and `NaN` written into a shared value freezes the fill for good.
|
|
87
|
+
- **The default readout is the share of the range** — `72%`, or `75%` for 18 of
|
|
88
|
+
24. With `formatOptions` whose style is not `percent`, it formats the value
|
|
89
|
+
itself (`$1,250`). A `percent` style still formats the *ratio*, merging the
|
|
90
|
+
caller's other options, because `Intl` multiplies by a hundred and a value of 72
|
|
91
|
+
would read `7,200%`.
|
|
92
|
+
- **`valueLabel` replaces the readout and the spoken text alike.** One string,
|
|
93
|
+
so the screen and the screen reader cannot disagree.
|
|
94
|
+
- **`Progress.Output` takes a function child** handed `ProgressRenderProps`, for
|
|
95
|
+
a readout the formatter cannot express — `18 of 24 seats`. While
|
|
96
|
+
indeterminate the default readout renders nothing; a function still runs.
|
|
97
|
+
|
|
98
|
+
## Accessibility
|
|
99
|
+
|
|
100
|
+
- **The root is the one accessible element**, `accessibilityRole="progressbar"`,
|
|
101
|
+
with the range and value in `accessibilityValue`. `resolveProgressAccessibility`
|
|
102
|
+
decides the spoken text: the percentage over 0–100, `18 of 24` over any other
|
|
103
|
+
range (formatted like the readout), or `valueLabel`. The count is what the
|
|
104
|
+
screen is about; "seventy-five percent" makes the listener divide back.
|
|
105
|
+
- **`Progress.Label` stays visible to assistive technology** and becomes the
|
|
106
|
+
element's name; `Progress.Output` is hidden (`accessibilityElementsHidden`,
|
|
107
|
+
`importantForAccessibility="no-hide-descendants"`) because the root already
|
|
108
|
+
speaks the value — left visible, it is read twice.
|
|
109
|
+
- **Indeterminate reports `busy: true` and no value.** A `now` of 0 is read out
|
|
110
|
+
as "zero percent", a lie about work that is under way. A bar with no label
|
|
111
|
+
needs an `accessibilityLabel`.
|
|
112
|
+
|
|
113
|
+
## Reuse
|
|
114
|
+
|
|
115
|
+
- **`Progress.Track` and `Progress.Fill` read only `ratio`, `isIndeterminate`,
|
|
116
|
+
`color`, `size` and `trackSize` from context.** A meter is the same track and
|
|
117
|
+
fill with a different root deciding the ratio and the colour, so a root that
|
|
118
|
+
publishes `ProgressContextValue` through `ProgressProvider` reuses both parts
|
|
119
|
+
as they stand. Keep anything a meter would not have — formatting, the
|
|
120
|
+
indeterminate loop's *decision* — out of those two parts' reads.
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
export { Progress, type ProgressProps } from "./progress";
|
|
2
|
+
export {
|
|
3
|
+
type ProgressContextValue,
|
|
4
|
+
ProgressProvider,
|
|
5
|
+
useProgress,
|
|
6
|
+
useProgressContext,
|
|
7
|
+
} from "./progress.context";
|
|
8
|
+
export type { ProgressRenderChildren, ProgressRenderProps } from "./progress.types";
|
|
9
|
+
export {
|
|
10
|
+
fillTranslate,
|
|
11
|
+
formatProgressValue,
|
|
12
|
+
indeterminateSegment,
|
|
13
|
+
PROGRESS_COLORS,
|
|
14
|
+
PROGRESS_DEFAULT_COLOR,
|
|
15
|
+
PROGRESS_DEFAULT_SIZE,
|
|
16
|
+
PROGRESS_FILL_DURATION_MS,
|
|
17
|
+
PROGRESS_INDETERMINATE_DURATION_MS,
|
|
18
|
+
PROGRESS_INDETERMINATE_SEGMENT,
|
|
19
|
+
PROGRESS_MAX_VALUE,
|
|
20
|
+
PROGRESS_MIN_VALUE,
|
|
21
|
+
PROGRESS_OUTPUT_TEXT_SIZE,
|
|
22
|
+
PROGRESS_PULSE,
|
|
23
|
+
PROGRESS_SIZES,
|
|
24
|
+
type ProgressAccessibility,
|
|
25
|
+
type ProgressAxes,
|
|
26
|
+
type ProgressColor,
|
|
27
|
+
type ProgressFormatInput,
|
|
28
|
+
type ProgressOwnAxes,
|
|
29
|
+
type ProgressSize,
|
|
30
|
+
type ProgressVariantProps,
|
|
31
|
+
progressRatio,
|
|
32
|
+
progressVariants,
|
|
33
|
+
resolveProgressAccessibility,
|
|
34
|
+
resolveProgressAxes,
|
|
35
|
+
} from "./progress.variants";
|
|
36
|
+
export type { ProgressFillProps } from "./progress-fill";
|
|
37
|
+
export type { ProgressHeaderProps } from "./progress-header";
|
|
38
|
+
export type { ProgressLabelProps } from "./progress-label";
|
|
39
|
+
export type { ProgressOutputProps } from "./progress-output";
|
|
40
|
+
export type { ProgressTrackProps } from "./progress-track";
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { type ReactElement, useEffect } from "react";
|
|
2
|
+
import type { ViewProps } from "react-native";
|
|
3
|
+
import Animated, {
|
|
4
|
+
cancelAnimation,
|
|
5
|
+
Easing,
|
|
6
|
+
ReduceMotion,
|
|
7
|
+
useAnimatedStyle,
|
|
8
|
+
useReducedMotion,
|
|
9
|
+
useSharedValue,
|
|
10
|
+
withRepeat,
|
|
11
|
+
withTiming,
|
|
12
|
+
} from "react-native-reanimated";
|
|
13
|
+
import { useProgressPart } from "./progress.context";
|
|
14
|
+
import {
|
|
15
|
+
fillTranslate,
|
|
16
|
+
indeterminateSegment,
|
|
17
|
+
PROGRESS_FILL_DURATION_MS,
|
|
18
|
+
PROGRESS_INDETERMINATE_DURATION_MS,
|
|
19
|
+
PROGRESS_INDETERMINATE_SEGMENT,
|
|
20
|
+
PROGRESS_PULSE,
|
|
21
|
+
progressVariants,
|
|
22
|
+
} from "./progress.variants";
|
|
23
|
+
|
|
24
|
+
export type ProgressFillProps = Omit<ViewProps, "children" | "style"> & {
|
|
25
|
+
className?: string;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* The painted bar.
|
|
30
|
+
*
|
|
31
|
+
* **Everything it animates runs on the UI thread.** A new value is handed to
|
|
32
|
+
* `withTiming` once, from an effect, and the frames that follow never touch
|
|
33
|
+
* React — so an upload reporting ten times a second costs ten renders, not six
|
|
34
|
+
* hundred.
|
|
35
|
+
*
|
|
36
|
+
* **Determinate, it is a full-length bar parked left of the track** by what is
|
|
37
|
+
* still to go, and a `translateX` slides it in. A transform composites without a
|
|
38
|
+
* layout pass; an animated `width` would re-lay the bar out on every frame. The
|
|
39
|
+
* track clips what is parked.
|
|
40
|
+
*
|
|
41
|
+
* **Indeterminate, it is a segment sweeping across**, from wholly off the left
|
|
42
|
+
* edge to wholly off the right, so the loop's seam is never on screen. With the
|
|
43
|
+
* system's reduce-motion setting on, the sweep becomes the whole track breathing
|
|
44
|
+
* between two opacities instead: still visibly alive — which a status indicator
|
|
45
|
+
* must be, the same reasoning `Spinner` gives for opting out of the policy — but
|
|
46
|
+
* with nothing travelling across the screen.
|
|
47
|
+
*
|
|
48
|
+
* It is hidden until the track has been measured. A fill positioned against a
|
|
49
|
+
* width of 0 would flash at full length for the frame before layout lands.
|
|
50
|
+
*/
|
|
51
|
+
export function ProgressFill({ className, ...props }: ProgressFillProps): ReactElement {
|
|
52
|
+
const { ratio, isIndeterminate, color, size, trackSize } = useProgressPart("Progress.Fill");
|
|
53
|
+
const isReducedMotion = useReducedMotion();
|
|
54
|
+
|
|
55
|
+
// Seeded at the current ratio, so a bar that mounts at 60% is drawn at 60%
|
|
56
|
+
// rather than animating up from nothing every time a list row scrolls in.
|
|
57
|
+
const progress = useSharedValue(ratio);
|
|
58
|
+
const phase = useSharedValue(0);
|
|
59
|
+
const pulse = useSharedValue<number>(PROGRESS_PULSE.to);
|
|
60
|
+
|
|
61
|
+
useEffect(() => {
|
|
62
|
+
if (isIndeterminate) return;
|
|
63
|
+
progress.value = withTiming(ratio, {
|
|
64
|
+
duration: PROGRESS_FILL_DURATION_MS,
|
|
65
|
+
easing: Easing.out(Easing.cubic),
|
|
66
|
+
});
|
|
67
|
+
}, [isIndeterminate, progress, ratio]);
|
|
68
|
+
|
|
69
|
+
useEffect(() => {
|
|
70
|
+
if (!isIndeterminate) return;
|
|
71
|
+
|
|
72
|
+
if (isReducedMotion) {
|
|
73
|
+
pulse.value = PROGRESS_PULSE.to;
|
|
74
|
+
pulse.value = withRepeat(
|
|
75
|
+
withTiming(PROGRESS_PULSE.from, {
|
|
76
|
+
duration: PROGRESS_PULSE.durationMs,
|
|
77
|
+
easing: Easing.inOut(Easing.ease),
|
|
78
|
+
// Under the default `System` policy the timing completes instantly
|
|
79
|
+
// while reduce motion is on, and `withRepeat(-1)` would spin a
|
|
80
|
+
// zero-length animation forever — a frozen bar, which reads as hung.
|
|
81
|
+
reduceMotion: ReduceMotion.Never,
|
|
82
|
+
}),
|
|
83
|
+
-1,
|
|
84
|
+
true,
|
|
85
|
+
undefined,
|
|
86
|
+
// The repeat has a reduce-motion policy of its own, separate from the
|
|
87
|
+
// timing's. Left at `System`, it declines to start a reversing loop at
|
|
88
|
+
// all and stops a forward one after a single pass.
|
|
89
|
+
ReduceMotion.Never
|
|
90
|
+
);
|
|
91
|
+
} else {
|
|
92
|
+
phase.value = 0;
|
|
93
|
+
phase.value = withRepeat(
|
|
94
|
+
withTiming(1, {
|
|
95
|
+
duration: PROGRESS_INDETERMINATE_DURATION_MS,
|
|
96
|
+
easing: Easing.inOut(Easing.ease),
|
|
97
|
+
reduceMotion: ReduceMotion.Never,
|
|
98
|
+
}),
|
|
99
|
+
-1,
|
|
100
|
+
false,
|
|
101
|
+
undefined,
|
|
102
|
+
ReduceMotion.Never
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// Without this the repeat outlives the bar, or the switch back to a value.
|
|
107
|
+
return () => {
|
|
108
|
+
cancelAnimation(phase);
|
|
109
|
+
cancelAnimation(pulse);
|
|
110
|
+
pulse.value = PROGRESS_PULSE.to;
|
|
111
|
+
};
|
|
112
|
+
}, [isIndeterminate, isReducedMotion, phase, pulse]);
|
|
113
|
+
|
|
114
|
+
const fillStyle = useAnimatedStyle(() => {
|
|
115
|
+
const track = trackSize.value;
|
|
116
|
+
const isMeasured = track > 0;
|
|
117
|
+
|
|
118
|
+
// The same keys every frame, so Reanimated is never asked to swap a style's
|
|
119
|
+
// shape when the bar flips between modes.
|
|
120
|
+
if (isIndeterminate && !isReducedMotion) {
|
|
121
|
+
const segment = indeterminateSegment({
|
|
122
|
+
phase: phase.value,
|
|
123
|
+
segment: PROGRESS_INDETERMINATE_SEGMENT,
|
|
124
|
+
trackSize: track,
|
|
125
|
+
});
|
|
126
|
+
return {
|
|
127
|
+
opacity: isMeasured ? 1 : 0,
|
|
128
|
+
transform: [{ translateX: segment.translate }],
|
|
129
|
+
width: segment.width,
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
if (isIndeterminate) {
|
|
134
|
+
return { opacity: isMeasured ? pulse.value : 0, transform: [{ translateX: 0 }], width: track };
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
return {
|
|
138
|
+
opacity: isMeasured ? 1 : 0,
|
|
139
|
+
transform: [{ translateX: fillTranslate({ ratio: progress.value, trackSize: track }) }],
|
|
140
|
+
width: track,
|
|
141
|
+
};
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
return (
|
|
145
|
+
<Animated.View
|
|
146
|
+
className={progressVariants({ color, isIndeterminate, size }).fill({ className })}
|
|
147
|
+
style={fillStyle}
|
|
148
|
+
{...props}
|
|
149
|
+
/>
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
ProgressFill.displayName = "DelacourUI.Progress.Fill";
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { ReactElement } from "react";
|
|
2
|
+
import { View, type ViewProps } from "react-native";
|
|
3
|
+
import { useProgressPart } from "./progress.context";
|
|
4
|
+
import { progressVariants } from "./progress.variants";
|
|
5
|
+
|
|
6
|
+
export type ProgressHeaderProps = ViewProps;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* The row above the track: a label at the start, a readout at the end.
|
|
10
|
+
*
|
|
11
|
+
* A part rather than a `View` the caller writes because every labelled bar
|
|
12
|
+
* wants exactly this row, and a hand-written one drifts — a missing `gap` lets a
|
|
13
|
+
* long label run into the percentage.
|
|
14
|
+
*/
|
|
15
|
+
export function ProgressHeader({ className, ...props }: ProgressHeaderProps): ReactElement {
|
|
16
|
+
const { color, size, isIndeterminate } = useProgressPart("Progress.Header");
|
|
17
|
+
return <View className={progressVariants({ color, isIndeterminate, size }).header({ className })} {...props} />;
|
|
18
|
+
}
|
|
19
|
+
ProgressHeader.displayName = "DelacourUI.Progress.Header";
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { ReactElement } from "react";
|
|
2
|
+
import { Text, type TextPresetProps } from "../text";
|
|
3
|
+
import { useProgressPart } from "./progress.context";
|
|
4
|
+
import { PROGRESS_OUTPUT_TEXT_SIZE, progressVariants } from "./progress.variants";
|
|
5
|
+
|
|
6
|
+
export type ProgressLabelProps = TextPresetProps;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* What the bar is measuring — "Uploading", "Storage".
|
|
10
|
+
*
|
|
11
|
+
* Renders `Text.Label` and names a size step, never a scale of its own, so the
|
|
12
|
+
* weight and colour stay in the preset. It is left visible to assistive
|
|
13
|
+
* technology: the root is one accessible element, and a label inside it is what
|
|
14
|
+
* VoiceOver and TalkBack read as that element's name.
|
|
15
|
+
*/
|
|
16
|
+
export function ProgressLabel({ className, size, ...props }: ProgressLabelProps): ReactElement {
|
|
17
|
+
const { color, size: progressSize, isIndeterminate } = useProgressPart("Progress.Label");
|
|
18
|
+
return (
|
|
19
|
+
<Text.Label
|
|
20
|
+
className={progressVariants({ color, isIndeterminate, size: progressSize }).label({ className })}
|
|
21
|
+
numberOfLines={1}
|
|
22
|
+
size={size ?? PROGRESS_OUTPUT_TEXT_SIZE[progressSize]}
|
|
23
|
+
{...props}
|
|
24
|
+
/>
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
ProgressLabel.displayName = "DelacourUI.Progress.Label";
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { ReactElement } from "react";
|
|
2
|
+
import { Text, type TextPresetProps } from "../text";
|
|
3
|
+
import { useProgressPart } from "./progress.context";
|
|
4
|
+
import type { ProgressRenderChildren } from "./progress.types";
|
|
5
|
+
import { PROGRESS_OUTPUT_TEXT_SIZE, progressVariants } from "./progress.variants";
|
|
6
|
+
|
|
7
|
+
export type ProgressOutputProps = Omit<TextPresetProps, "children"> & {
|
|
8
|
+
/** Custom content, or a function called with the bar's settled state. */
|
|
9
|
+
children?: ProgressRenderChildren;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* The current value, formatted — `72%` by default.
|
|
14
|
+
*
|
|
15
|
+
* A function child is handed the bar's state, which is how a count reads
|
|
16
|
+
* `18 of 24` rather than a percentage.
|
|
17
|
+
*
|
|
18
|
+
* **Hidden from assistive technology on purpose.** The root already publishes
|
|
19
|
+
* the value as `accessibilityValue`, so a readout left visible inside it would be
|
|
20
|
+
* read twice — once as part of the element's name, once as its value.
|
|
21
|
+
*
|
|
22
|
+
* **An indeterminate bar has no value**, so the default readout renders nothing.
|
|
23
|
+
* A function child still runs, which is where a "Waiting…" belongs.
|
|
24
|
+
*/
|
|
25
|
+
export function ProgressOutput({ children, className, size, ...props }: ProgressOutputProps): ReactElement | null {
|
|
26
|
+
const { color, size: progressSize, isIndeterminate, renderProps } = useProgressPart("Progress.Output");
|
|
27
|
+
|
|
28
|
+
const content =
|
|
29
|
+
typeof children === "function"
|
|
30
|
+
? children(renderProps)
|
|
31
|
+
: (children ?? (isIndeterminate ? null : renderProps.formatted));
|
|
32
|
+
|
|
33
|
+
if (content === null || content === undefined) return null;
|
|
34
|
+
|
|
35
|
+
return (
|
|
36
|
+
<Text.Label
|
|
37
|
+
accessibilityElementsHidden
|
|
38
|
+
className={progressVariants({ color, isIndeterminate, size: progressSize }).output({ className })}
|
|
39
|
+
importantForAccessibility="no-hide-descendants"
|
|
40
|
+
size={size ?? PROGRESS_OUTPUT_TEXT_SIZE[progressSize]}
|
|
41
|
+
{...props}
|
|
42
|
+
>
|
|
43
|
+
{content}
|
|
44
|
+
</Text.Label>
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
ProgressOutput.displayName = "DelacourUI.Progress.Output";
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { type ReactElement, type ReactNode, useCallback } from "react";
|
|
2
|
+
import { type LayoutChangeEvent, View, type ViewProps } from "react-native";
|
|
3
|
+
import { useProgressPart } from "./progress.context";
|
|
4
|
+
import { progressVariants } from "./progress.variants";
|
|
5
|
+
|
|
6
|
+
export type ProgressTrackProps = Omit<ViewProps, "children"> & {
|
|
7
|
+
/** The fill. */
|
|
8
|
+
children?: ReactNode;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* The clipped, rounded groove the fill slides along.
|
|
13
|
+
*
|
|
14
|
+
* It measures its own width into the shared value the fill reads, because the
|
|
15
|
+
* fill is positioned by a transform in points and a transform cannot name a
|
|
16
|
+
* percentage. The clip is what turns a full-length bar parked off to the left
|
|
17
|
+
* into a bar that appears to grow — see `progressVariants`.
|
|
18
|
+
*/
|
|
19
|
+
export function ProgressTrack({ className, onLayout, ...props }: ProgressTrackProps): ReactElement {
|
|
20
|
+
const { color, size, isIndeterminate, trackSize } = useProgressPart("Progress.Track");
|
|
21
|
+
|
|
22
|
+
const handleLayout = useCallback(
|
|
23
|
+
(event: LayoutChangeEvent) => {
|
|
24
|
+
trackSize.value = event.nativeEvent.layout.width;
|
|
25
|
+
onLayout?.(event);
|
|
26
|
+
},
|
|
27
|
+
[onLayout, trackSize]
|
|
28
|
+
);
|
|
29
|
+
|
|
30
|
+
return (
|
|
31
|
+
<View
|
|
32
|
+
className={progressVariants({ color, isIndeterminate, size }).track({ className })}
|
|
33
|
+
onLayout={handleLayout}
|
|
34
|
+
{...props}
|
|
35
|
+
/>
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
ProgressTrack.displayName = "DelacourUI.Progress.Track";
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { createContext, type ReactElement, type ReactNode, use } from "react";
|
|
2
|
+
import type { SharedValue } from "react-native-reanimated";
|
|
3
|
+
import type { ProgressRenderProps } from "./progress.types";
|
|
4
|
+
import type { ProgressColor, ProgressSize } from "./progress.variants";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* What a progress bar's parts read.
|
|
8
|
+
*
|
|
9
|
+
* **The track and the fill read only `ratio`, `isIndeterminate`, `color`, `size`
|
|
10
|
+
* and `trackSize`.** That is deliberate: a meter is the same track and the same
|
|
11
|
+
* fill with a different root deciding the ratio and the colour, so a root that
|
|
12
|
+
* publishes this shape can reuse `Progress.Track` and `Progress.Fill` as they
|
|
13
|
+
* stand. Nothing the parts draw reaches back to how the ratio was arrived at.
|
|
14
|
+
*/
|
|
15
|
+
export type ProgressContextValue = {
|
|
16
|
+
/** Where the value sits in the range, clamped to 0–1, on the JS thread. */
|
|
17
|
+
ratio: number;
|
|
18
|
+
/** Loops a segment along the track instead of showing a value. */
|
|
19
|
+
isIndeterminate: boolean;
|
|
20
|
+
color: ProgressColor;
|
|
21
|
+
size: ProgressSize;
|
|
22
|
+
/** The track's own width, from its `onLayout`. `0` until it has been measured. */
|
|
23
|
+
trackSize: SharedValue<number>;
|
|
24
|
+
/** The settled state `Progress.Output` formats and a render function is handed. */
|
|
25
|
+
renderProps: ProgressRenderProps;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
const ProgressContext = createContext<ProgressContextValue | null>(null);
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Supplies one progress bar's state to its own parts.
|
|
32
|
+
*
|
|
33
|
+
* Lives in its own module, importing nothing but React and types, so a part can
|
|
34
|
+
* read it without importing `./progress` — that import would close a cycle, and
|
|
35
|
+
* Metro serves a partially initialised module for a cycle.
|
|
36
|
+
*/
|
|
37
|
+
export function ProgressProvider({
|
|
38
|
+
value,
|
|
39
|
+
children,
|
|
40
|
+
}: {
|
|
41
|
+
value: ProgressContextValue;
|
|
42
|
+
children: ReactNode;
|
|
43
|
+
}): ReactElement {
|
|
44
|
+
return <ProgressContext value={value}>{children}</ProgressContext>;
|
|
45
|
+
}
|
|
46
|
+
ProgressProvider.displayName = "DelacourUI.Progress.Provider";
|
|
47
|
+
|
|
48
|
+
/** The enclosing progress bar's state, or null outside a `<Progress>`. */
|
|
49
|
+
export function useProgressContext(): ProgressContextValue | null {
|
|
50
|
+
return use(ProgressContext);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Reads the enclosing progress bar's state.
|
|
55
|
+
*
|
|
56
|
+
* For a custom readout or a custom fill that has to match the bar it sits in.
|
|
57
|
+
* Throws outside one — use {@link useProgressContext} where the bar is optional.
|
|
58
|
+
*/
|
|
59
|
+
export function useProgress(): ProgressContextValue {
|
|
60
|
+
const context = useProgressContext();
|
|
61
|
+
if (!context) {
|
|
62
|
+
throw new Error("useProgress must be called inside a <Progress>.");
|
|
63
|
+
}
|
|
64
|
+
return context;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* The enclosing bar's state, for a compound part that cannot work without one.
|
|
69
|
+
*
|
|
70
|
+
* Internal: deliberately not re-exported from `index.ts`. A caller outside the
|
|
71
|
+
* library wants {@link useProgress}, whose error names the hook rather than a part.
|
|
72
|
+
*/
|
|
73
|
+
export function useProgressPart(component: string): ProgressContextValue {
|
|
74
|
+
const context = useProgressContext();
|
|
75
|
+
if (!context) {
|
|
76
|
+
throw new Error(`${component} must be rendered inside a <Progress>.`);
|
|
77
|
+
}
|
|
78
|
+
return context;
|
|
79
|
+
}
|