@delacour/react-native-ui 0.1.0-alpha.20261009011521 → 0.1.0-alpha.20261009012154

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (22) hide show
  1. package/package.json +5 -3
  2. package/src/components/progress-button/AGENTS.md +116 -0
  3. package/src/components/progress-button/index.ts +36 -0
  4. package/src/components/progress-button/progress-button-done.tsx +33 -0
  5. package/src/components/progress-button/progress-button-label.tsx +27 -0
  6. package/src/components/progress-button/progress-button.context.tsx +93 -0
  7. package/src/components/progress-button/progress-button.tsx +414 -0
  8. package/src/components/progress-button/progress-button.variants.test.ts +314 -0
  9. package/src/components/progress-button/progress-button.variants.ts +236 -0
  10. package/src/components/selection-mode/AGENTS.md +155 -0
  11. package/src/components/selection-mode/index.ts +43 -0
  12. package/src/components/selection-mode/selection-mode-action.tsx +65 -0
  13. package/src/components/selection-mode/selection-mode-bar.tsx +79 -0
  14. package/src/components/selection-mode/selection-mode-group.tsx +123 -0
  15. package/src/components/selection-mode/selection-mode-header.tsx +85 -0
  16. package/src/components/selection-mode/selection-mode-indicator.tsx +118 -0
  17. package/src/components/selection-mode/selection-mode-item.tsx +182 -0
  18. package/src/components/selection-mode/selection-mode.context.tsx +89 -0
  19. package/src/components/selection-mode/selection-mode.tsx +207 -0
  20. package/src/components/selection-mode/selection-mode.types.ts +36 -0
  21. package/src/components/selection-mode/selection-mode.variants.test.ts +292 -0
  22. package/src/components/selection-mode/selection-mode.variants.ts +329 -0
@@ -0,0 +1,414 @@
1
+ import {
2
+ Children,
3
+ type ComponentRef,
4
+ isValidElement,
5
+ type ReactElement,
6
+ type ReactNode,
7
+ type Ref,
8
+ useCallback,
9
+ useEffect,
10
+ useMemo,
11
+ useState,
12
+ } from "react";
13
+ import type { AccessibilityActionEvent, LayoutChangeEvent, ViewProps } from "react-native";
14
+ import { Gesture, GestureDetector } from "react-native-gesture-handler";
15
+ import Animated, {
16
+ Easing,
17
+ ReduceMotion,
18
+ useAnimatedStyle,
19
+ useReducedMotion,
20
+ useSharedValue,
21
+ withTiming,
22
+ } from "react-native-reanimated";
23
+ import { scheduleOnRN } from "react-native-worklets";
24
+ import { useControllableState } from "../../hooks/use-controllable-state";
25
+ import { IconDefaultsProvider } from "../icon";
26
+ import { type HapticFeedback, playHaptic } from "../pressable/pressable";
27
+ import {
28
+ type ProgressButtonContextValue,
29
+ ProgressButtonLayerProvider,
30
+ ProgressButtonProvider,
31
+ } from "./progress-button.context";
32
+ import {
33
+ PROGRESS_BUTTON_CROSSFADE_MS,
34
+ PROGRESS_BUTTON_DEFAULT_HINT,
35
+ PROGRESS_BUTTON_FILL_FOREGROUND_TOKEN,
36
+ PROGRESS_BUTTON_LABEL_TOKEN,
37
+ PROGRESS_BUTTON_MAX_DRIFT,
38
+ PROGRESS_BUTTON_REDUCED_MOTION_STEPS,
39
+ PROGRESS_BUTTON_TRAVEL_MS,
40
+ type ProgressButtonShape,
41
+ type ProgressButtonSize,
42
+ type ProgressButtonVariant,
43
+ progressButtonVariants,
44
+ resolveAutoResetDelay,
45
+ resolveHoldDuration,
46
+ resolveProgressButtonAccessibilityState,
47
+ resolveRemainingDuration,
48
+ } from "./progress-button.variants";
49
+ import { ProgressButtonDone } from "./progress-button-done";
50
+ import { ProgressButtonLabel } from "./progress-button-label";
51
+
52
+ export type ProgressButtonProps = Omit<ViewProps, "children"> & {
53
+ children: ReactNode;
54
+ className?: string;
55
+ /** The colour the label and the fill carry. Every variant rests on the same surface. */
56
+ variant?: ProgressButtonVariant;
57
+ /** The button's own height, padding, label step and icon step. */
58
+ size?: ProgressButtonSize;
59
+ /** `pill` (the default) takes the button's capsule corner; `rounded` takes `rounded-lg`. */
60
+ shape?: ProgressButtonShape;
61
+ /** Stretch to the parent's width. */
62
+ isFullWidth?: boolean;
63
+ /** How long a full hold takes, in milliseconds. Defaults to 2000, floored at 200. */
64
+ holdDuration?: number;
65
+ /** Called once, when the fill reaches the end. */
66
+ onComplete?: () => void;
67
+ /** Controlled completion. */
68
+ isCompleted?: boolean;
69
+ /** Starting completion while uncontrolled. */
70
+ defaultCompleted?: boolean;
71
+ /** Called with `true` when a hold completes, and `false` when the button resets. */
72
+ onCompletedChange?: (isCompleted: boolean) => void;
73
+ /** Rewind by itself after `autoResetDelay`. Off by default. */
74
+ isAutoReset?: boolean;
75
+ /** How long a completed button waits before `isAutoReset` rewinds it. Defaults to 1000. */
76
+ autoResetDelay?: number;
77
+ isDisabled?: boolean;
78
+ /** Played when the hold takes; a `success` knock follows on completion. Off by default. */
79
+ haptic?: false | HapticFeedback;
80
+ /** Defaults to saying the button has to be held. */
81
+ accessibilityHint?: string;
82
+ ref?: Ref<ComponentRef<typeof Animated.View>>;
83
+ };
84
+
85
+ /**
86
+ * Splits the children into the content drawn on each layer and the caller's
87
+ * `ProgressButton.Done`, if one was written.
88
+ *
89
+ * Consecutive strings collapse into one label, so `Erase {count} files` stays a
90
+ * single piece of text rather than three spaced apart by the row's gap.
91
+ */
92
+ function splitChildren(children: ReactNode): { content: ReactNode[]; done: ReactElement | null } {
93
+ const content: ReactNode[] = [];
94
+ let done: ReactElement | null = null;
95
+ let text: string[] = [];
96
+
97
+ const flushText = () => {
98
+ if (text.length === 0) return;
99
+ content.push(<ProgressButtonLabel key={`label-${content.length}`}>{text.join("")}</ProgressButtonLabel>);
100
+ text = [];
101
+ };
102
+
103
+ for (const child of Children.toArray(children)) {
104
+ if (typeof child === "string" || typeof child === "number") {
105
+ text.push(String(child));
106
+ continue;
107
+ }
108
+ flushText();
109
+ if (isValidElement(child) && child.type === ProgressButtonDone) {
110
+ done = child;
111
+ continue;
112
+ }
113
+ content.push(child);
114
+ }
115
+ flushText();
116
+
117
+ return { content, done };
118
+ }
119
+
120
+ function ProgressButtonRoot({
121
+ children,
122
+ className,
123
+ variant = "primary",
124
+ size = "md",
125
+ shape = "pill",
126
+ isFullWidth = false,
127
+ holdDuration: holdDurationProp,
128
+ onComplete,
129
+ isCompleted: isCompletedProp,
130
+ defaultCompleted = false,
131
+ onCompletedChange,
132
+ isAutoReset = false,
133
+ autoResetDelay: autoResetDelayProp,
134
+ isDisabled = false,
135
+ haptic = false,
136
+ accessibilityHint = PROGRESS_BUTTON_DEFAULT_HINT,
137
+ onLayout,
138
+ ref,
139
+ ...props
140
+ }: ProgressButtonProps): ReactElement {
141
+ const holdDuration = resolveHoldDuration(holdDurationProp);
142
+ const autoResetDelay = resolveAutoResetDelay(autoResetDelayProp);
143
+ const isReducedMotion = useReducedMotion();
144
+
145
+ const [isCompleted, setCompleted] = useControllableState<boolean>({
146
+ defaultValue: defaultCompleted,
147
+ onChange: onCompletedChange,
148
+ value: isCompletedProp,
149
+ });
150
+
151
+ // Bumped by every completed hold. A controlled caller that does not accept
152
+ // the completion leaves `isCompleted` false, and without a change to key on
153
+ // the button would sit full and locked forever; this is the change.
154
+ const [holdCount, setHoldCount] = useState(0);
155
+
156
+ const progress = useSharedValue(isCompleted ? 1 : 0);
157
+ // 1 while completed: the gesture worklets read it to refuse a new hold and to
158
+ // leave a completed fill where it is on release.
159
+ const isLocked = useSharedValue(isCompleted ? 1 : 0);
160
+ const isHolding = useSharedValue(0);
161
+ const completion = useSharedValue(isCompleted ? 1 : 0);
162
+ const boxWidth = useSharedValue(0);
163
+ const [measuredWidth, setMeasuredWidth] = useState(0);
164
+
165
+ const complete = useCallback(() => {
166
+ setCompleted(true);
167
+ onComplete?.();
168
+ setHoldCount((count) => count + 1);
169
+ }, [onComplete, setCompleted]);
170
+
171
+ // Follows `isCompleted` whichever side moved it. A completion travels the
172
+ // fill to the end (already there after a hold); a reset travels it back.
173
+ // Neither is ever a jump.
174
+ // biome-ignore lint/correctness/useExhaustiveDependencies: `holdCount` is the re-run trigger, not a value read
175
+ useEffect(() => {
176
+ completion.value = withTiming(isCompleted ? 1 : 0, {
177
+ duration: PROGRESS_BUTTON_CROSSFADE_MS,
178
+ reduceMotion: ReduceMotion.Never,
179
+ });
180
+
181
+ if (isCompleted) {
182
+ isLocked.value = 1;
183
+ const remaining = resolveRemainingDuration({
184
+ direction: "forward",
185
+ holdDuration: PROGRESS_BUTTON_TRAVEL_MS,
186
+ progress: progress.value,
187
+ });
188
+ if (remaining > 0) {
189
+ progress.value = withTiming(1, {
190
+ duration: remaining,
191
+ easing: Easing.out(Easing.cubic),
192
+ reduceMotion: ReduceMotion.Never,
193
+ });
194
+ }
195
+ return;
196
+ }
197
+
198
+ if (isLocked.value === 1) {
199
+ isLocked.value = 0;
200
+ progress.value = withTiming(0, {
201
+ duration: resolveRemainingDuration({
202
+ direction: "reverse",
203
+ holdDuration: PROGRESS_BUTTON_TRAVEL_MS,
204
+ progress: progress.value,
205
+ }),
206
+ easing: Easing.inOut(Easing.cubic),
207
+ reduceMotion: ReduceMotion.Never,
208
+ });
209
+ }
210
+ }, [completion, holdCount, isCompleted, isLocked, progress]);
211
+
212
+ useEffect(() => {
213
+ if (!isCompleted || !isAutoReset) return;
214
+ const timer = setTimeout(() => setCompleted(false), autoResetDelay);
215
+ return () => clearTimeout(timer);
216
+ }, [autoResetDelay, isAutoReset, isCompleted, setCompleted]);
217
+
218
+ // A pan with no minimum distance begins on touch-down and finalizes on
219
+ // release or cancel, which is the whole hold. A long-press gesture fires once
220
+ // and cannot tell "still holding" from "held long enough".
221
+ const gesture = useMemo(
222
+ () =>
223
+ Gesture.Pan()
224
+ .minDistance(0)
225
+ .enabled(!isDisabled)
226
+ .shouldCancelWhenOutside(false)
227
+ .onBegin(() => {
228
+ "worklet";
229
+ if (isLocked.value === 1) return;
230
+ isHolding.value = 1;
231
+ if (haptic) playHaptic(haptic);
232
+ const from = Math.min(1, Math.max(0, progress.value));
233
+ progress.value = withTiming(
234
+ 1,
235
+ { duration: holdDuration * (1 - from), easing: Easing.linear, reduceMotion: ReduceMotion.Never },
236
+ (finished) => {
237
+ "worklet";
238
+ if (!finished || isLocked.value === 1) return;
239
+ isLocked.value = 1;
240
+ isHolding.value = 0;
241
+ if (haptic) playHaptic("success");
242
+ scheduleOnRN(complete);
243
+ }
244
+ );
245
+ })
246
+ .onUpdate((event) => {
247
+ "worklet";
248
+ if (isHolding.value === 0 || isLocked.value === 1) return;
249
+ const drift = event.translationX * event.translationX + event.translationY * event.translationY;
250
+ if (drift <= PROGRESS_BUTTON_MAX_DRIFT * PROGRESS_BUTTON_MAX_DRIFT) return;
251
+ isHolding.value = 0;
252
+ const from = Math.min(1, Math.max(0, progress.value));
253
+ progress.value = withTiming(0, {
254
+ duration: holdDuration * from,
255
+ easing: Easing.linear,
256
+ reduceMotion: ReduceMotion.Never,
257
+ });
258
+ })
259
+ .onFinalize(() => {
260
+ "worklet";
261
+ if (isHolding.value === 0 || isLocked.value === 1) return;
262
+ isHolding.value = 0;
263
+ const from = Math.min(1, Math.max(0, progress.value));
264
+ progress.value = withTiming(0, {
265
+ duration: holdDuration * from,
266
+ easing: Easing.linear,
267
+ reduceMotion: ReduceMotion.Never,
268
+ });
269
+ }),
270
+ [complete, haptic, holdDuration, isDisabled, isHolding, isLocked, progress]
271
+ );
272
+
273
+ const handleLayout = useCallback(
274
+ (event: LayoutChangeEvent) => {
275
+ const { width } = event.nativeEvent.layout;
276
+ boxWidth.value = width;
277
+ setMeasuredWidth(width);
278
+ onLayout?.(event);
279
+ },
280
+ [boxWidth, onLayout]
281
+ );
282
+
283
+ // A screen reader cannot hold, so activating completes outright. The hint
284
+ // has already said what the button does.
285
+ const handleAccessibilityAction = useCallback(
286
+ (event: AccessibilityActionEvent) => {
287
+ if (event.nativeEvent.actionName !== "activate") return;
288
+ if (isDisabled || isCompleted) return;
289
+ isLocked.value = 1;
290
+ complete();
291
+ },
292
+ [complete, isCompleted, isDisabled, isLocked]
293
+ );
294
+
295
+ const fillStyle = useAnimatedStyle(() => {
296
+ const p = progress.value;
297
+ const steps = PROGRESS_BUTTON_REDUCED_MOTION_STEPS;
298
+ const shown = isReducedMotion && p < 1 ? Math.floor(Math.max(0, p) * steps) / steps : p;
299
+ return { width: shown * boxWidth.value };
300
+ });
301
+
302
+ // One style per view: each copy of the labels fades on its own binding.
303
+ const labelsStyle = useAnimatedStyle(() => ({ opacity: 1 - completion.value }));
304
+ const fillLabelsStyle = useAnimatedStyle(() => ({ opacity: 1 - completion.value }));
305
+
306
+ const doneStyle = useAnimatedStyle(() => ({
307
+ opacity: completion.value,
308
+ transform: [{ scale: isReducedMotion ? 1 : 0.6 + 0.4 * completion.value }],
309
+ }));
310
+
311
+ const context = useMemo<ProgressButtonContextValue>(
312
+ () => ({ isCompleted, isDisabled, progress, size, variant }),
313
+ [isCompleted, isDisabled, progress, size, variant]
314
+ );
315
+
316
+ const { content, done } = useMemo(() => splitChildren(children), [children]);
317
+
318
+ const slots = progressButtonVariants({ isDisabled, isFullWidth, shape, size, variant });
319
+ const iconClass = slots.icon();
320
+ const surfaceIcons = useMemo(
321
+ () => ({ className: iconClass, color: PROGRESS_BUTTON_LABEL_TOKEN[variant] }),
322
+ [iconClass, variant]
323
+ );
324
+ const fillIcons = useMemo(
325
+ () => ({ className: iconClass, color: PROGRESS_BUTTON_FILL_FOREGROUND_TOKEN[variant] }),
326
+ [iconClass, variant]
327
+ );
328
+ const innerWidth = { width: measuredWidth };
329
+
330
+ // The fill copy and the done mark are hidden from assistive technology: the
331
+ // root is the one accessible element, and the surface copy already names it.
332
+ return (
333
+ <ProgressButtonProvider value={context}>
334
+ <GestureDetector gesture={gesture}>
335
+ <Animated.View
336
+ accessibilityActions={[{ name: "activate" }]}
337
+ accessibilityHint={accessibilityHint}
338
+ accessibilityRole="button"
339
+ accessibilityState={resolveProgressButtonAccessibilityState({ isCompleted, isDisabled })}
340
+ accessible
341
+ className={slots.root({ className })}
342
+ onAccessibilityAction={handleAccessibilityAction}
343
+ onLayout={handleLayout}
344
+ ref={ref}
345
+ {...props}
346
+ >
347
+ <Animated.View className={slots.content()} style={labelsStyle}>
348
+ <ProgressButtonLayerProvider value="surface">
349
+ <IconDefaultsProvider value={surfaceIcons}>{content}</IconDefaultsProvider>
350
+ </ProgressButtonLayerProvider>
351
+ </Animated.View>
352
+ <Animated.View
353
+ accessibilityElementsHidden
354
+ className={slots.fill()}
355
+ importantForAccessibility="no-hide-descendants"
356
+ pointerEvents="none"
357
+ style={fillStyle}
358
+ >
359
+ <Animated.View className={slots.fillContent()} style={[innerWidth, fillLabelsStyle]}>
360
+ <ProgressButtonLayerProvider value="fill">
361
+ <IconDefaultsProvider value={fillIcons}>{content}</IconDefaultsProvider>
362
+ </ProgressButtonLayerProvider>
363
+ </Animated.View>
364
+ <Animated.View className={slots.done()} style={[innerWidth, doneStyle]}>
365
+ <ProgressButtonLayerProvider value="fill">
366
+ <IconDefaultsProvider value={fillIcons}>{done ?? <ProgressButtonDone />}</IconDefaultsProvider>
367
+ </ProgressButtonLayerProvider>
368
+ </Animated.View>
369
+ </Animated.View>
370
+ </Animated.View>
371
+ </GestureDetector>
372
+ </ProgressButtonProvider>
373
+ );
374
+ }
375
+
376
+ /**
377
+ * A button that has to be held, not tapped.
378
+ *
379
+ * A fill grows from the leading edge for `holdDuration` and the action fires
380
+ * when it reaches the end — never before, with no tolerance near it. Released
381
+ * early, the fill plays back at the rate it went in; pressed again, it resumes
382
+ * from where it is. For an irreversible action that would otherwise need a
383
+ * confirmation dialog: two taps are a rhythm a hand falls into, a two-second
384
+ * hold cannot be done by habit.
385
+ *
386
+ * Completion is read off the fill's own animation, then the labels cross-fade
387
+ * to `ProgressButton.Done` — a tick unless the caller writes one. It stays
388
+ * completed until `isAutoReset` rewinds it, or a controlled `isCompleted` goes
389
+ * `false`; either way the fill travels back rather than jumping.
390
+ *
391
+ * A screen reader cannot hold, so its activate action completes the button
392
+ * outright; the default hint says it must be held.
393
+ *
394
+ * @example
395
+ * <ProgressButton onComplete={erase} variant="destructive">
396
+ * <ProgressButton.Label>Hold to erase</ProgressButton.Label>
397
+ * </ProgressButton>
398
+ *
399
+ * @example
400
+ * <ProgressButton isAutoReset haptic="medium" onComplete={pay} variant="success">
401
+ * <Icon icon={IconCreditCard1} />
402
+ * <ProgressButton.Label>Hold to pay</ProgressButton.Label>
403
+ * <ProgressButton.Done>
404
+ * <ProgressButton.Label>Paid</ProgressButton.Label>
405
+ * </ProgressButton.Done>
406
+ * </ProgressButton>
407
+ */
408
+ export const ProgressButton = Object.assign(ProgressButtonRoot, {
409
+ /** The button's text, drawn once on the surface and once inside the fill. */
410
+ Label: ProgressButtonLabel,
411
+ /** What shows once the hold completes. A tick when omitted. */
412
+ Done: ProgressButtonDone,
413
+ displayName: "DelacourUI.ProgressButton",
414
+ });