panelui-native 0.16.0 → 0.20.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.
Files changed (76) hide show
  1. package/lib/module/components/bottom-sheet/index.js +38 -10
  2. package/lib/module/components/bottom-sheet/index.js.map +1 -1
  3. package/lib/module/components/checkbox/index.js +24 -11
  4. package/lib/module/components/checkbox/index.js.map +1 -1
  5. package/lib/module/components/field/index.js +337 -0
  6. package/lib/module/components/field/index.js.map +1 -0
  7. package/lib/module/components/flow/flow-paths.js +378 -0
  8. package/lib/module/components/flow/flow-paths.js.map +1 -0
  9. package/lib/module/components/flow/index.js +1598 -0
  10. package/lib/module/components/flow/index.js.map +1 -0
  11. package/lib/module/components/form/index.js +77 -0
  12. package/lib/module/components/form/index.js.map +1 -0
  13. package/lib/module/components/form/use-field.js +50 -0
  14. package/lib/module/components/form/use-field.js.map +1 -0
  15. package/lib/module/components/form/use-form.js +217 -0
  16. package/lib/module/components/form/use-form.js.map +1 -0
  17. package/lib/module/components/frame/index.js +124 -22
  18. package/lib/module/components/frame/index.js.map +1 -1
  19. package/lib/module/components/otp-input/index.js +303 -0
  20. package/lib/module/components/otp-input/index.js.map +1 -0
  21. package/lib/module/components/rating/index.js +339 -0
  22. package/lib/module/components/rating/index.js.map +1 -0
  23. package/lib/module/components/signature/index.js +500 -0
  24. package/lib/module/components/signature/index.js.map +1 -0
  25. package/lib/module/components/slider/index.js +14 -1
  26. package/lib/module/components/slider/index.js.map +1 -1
  27. package/lib/module/icons/index.js +247 -0
  28. package/lib/module/icons/index.js.map +1 -1
  29. package/lib/module/index.js +7 -1
  30. package/lib/module/index.js.map +1 -1
  31. package/lib/typescript/src/components/bottom-sheet/index.d.ts +15 -1
  32. package/lib/typescript/src/components/bottom-sheet/index.d.ts.map +1 -1
  33. package/lib/typescript/src/components/checkbox/index.d.ts +8 -0
  34. package/lib/typescript/src/components/checkbox/index.d.ts.map +1 -1
  35. package/lib/typescript/src/components/field/index.d.ts +94 -0
  36. package/lib/typescript/src/components/field/index.d.ts.map +1 -0
  37. package/lib/typescript/src/components/flow/flow-paths.d.ts +89 -0
  38. package/lib/typescript/src/components/flow/flow-paths.d.ts.map +1 -0
  39. package/lib/typescript/src/components/flow/index.d.ts +310 -0
  40. package/lib/typescript/src/components/flow/index.d.ts.map +1 -0
  41. package/lib/typescript/src/components/form/index.d.ts +64 -0
  42. package/lib/typescript/src/components/form/index.d.ts.map +1 -0
  43. package/lib/typescript/src/components/form/use-field.d.ts +16 -0
  44. package/lib/typescript/src/components/form/use-field.d.ts.map +1 -0
  45. package/lib/typescript/src/components/form/use-form.d.ts +37 -0
  46. package/lib/typescript/src/components/form/use-form.d.ts.map +1 -0
  47. package/lib/typescript/src/components/frame/index.d.ts +39 -2
  48. package/lib/typescript/src/components/frame/index.d.ts.map +1 -1
  49. package/lib/typescript/src/components/otp-input/index.d.ts +141 -0
  50. package/lib/typescript/src/components/otp-input/index.d.ts.map +1 -0
  51. package/lib/typescript/src/components/rating/index.d.ts +122 -0
  52. package/lib/typescript/src/components/rating/index.d.ts.map +1 -0
  53. package/lib/typescript/src/components/signature/index.d.ts +254 -0
  54. package/lib/typescript/src/components/signature/index.d.ts.map +1 -0
  55. package/lib/typescript/src/components/slider/index.d.ts +6 -0
  56. package/lib/typescript/src/components/slider/index.d.ts.map +1 -1
  57. package/lib/typescript/src/icons/index.d.ts +25 -0
  58. package/lib/typescript/src/icons/index.d.ts.map +1 -1
  59. package/lib/typescript/src/index.d.ts +8 -2
  60. package/lib/typescript/src/index.d.ts.map +1 -1
  61. package/package.json +9 -1
  62. package/src/components/bottom-sheet/index.tsx +50 -12
  63. package/src/components/checkbox/index.tsx +40 -12
  64. package/src/components/field/index.tsx +329 -0
  65. package/src/components/flow/flow-paths.ts +348 -0
  66. package/src/components/flow/index.tsx +1924 -0
  67. package/src/components/form/index.tsx +85 -0
  68. package/src/components/form/use-field.ts +65 -0
  69. package/src/components/form/use-form.ts +252 -0
  70. package/src/components/frame/index.tsx +162 -14
  71. package/src/components/otp-input/index.tsx +396 -0
  72. package/src/components/rating/index.tsx +416 -0
  73. package/src/components/signature/index.tsx +681 -0
  74. package/src/components/slider/index.tsx +20 -1
  75. package/src/icons/index.tsx +185 -0
  76. package/src/index.ts +78 -0
@@ -0,0 +1,416 @@
1
+ /**
2
+ * Rating — a row of stars you can read or set.
3
+ *
4
+ * A rating is two jobs wearing one shape. Read-only, it is a compact way to
5
+ * *show* a score — four-and-a-half stars next to a product. Interactive, it is
6
+ * an *input*: tap a star to pick a whole value, or with `precision={0.5}` press
7
+ * the left half of a star for a half. Either way the fill is the truth, so the
8
+ * two modes look identical and only differ in whether a finger changes them.
9
+ *
10
+ * Partial fills are drawn by clipping a filled star over an empty one of the
11
+ * same geometry — the filled layer's width is a fraction of the star's, so a
12
+ * half star is exactly half and any precision in between is honest. That fill
13
+ * is animated on the UI thread: setting a value springs the stars to it rather
14
+ * than snapping, and a drag follows the finger with no spring so it never lags.
15
+ *
16
+ * ```tsx
17
+ * <Rating defaultValue={3} onValueChange={setScore} />
18
+ *
19
+ * <Rating value={4.5} precision={0.5} readOnly />
20
+ *
21
+ * <Rating label="Rate your stay" showValue color="warning" defaultValue={0} />
22
+ * ```
23
+ *
24
+ * Works controlled (`value` + `onValueChange`) or uncontrolled (`defaultValue`).
25
+ */
26
+ import { forwardRef, useCallback, useEffect, useRef, useState } from 'react';
27
+ import {
28
+ View,
29
+ type AccessibilityActionEvent,
30
+ type LayoutChangeEvent,
31
+ } from 'react-native';
32
+ import { Gesture, GestureDetector } from 'react-native-gesture-handler';
33
+ import Animated, {
34
+ runOnJS,
35
+ useAnimatedStyle,
36
+ useSharedValue,
37
+ withSpring,
38
+ type SharedValue,
39
+ } from 'react-native-reanimated';
40
+ import { tv, type VariantProps } from 'tailwind-variants';
41
+ import { useCSSVariable } from 'uniwind';
42
+ import { StarIcon } from '../../icons';
43
+ import { Text } from '../../primitives/text';
44
+ import { cn } from '../../utils/cn';
45
+ import { selectionTick } from '../../utils/haptics';
46
+
47
+ /** Springs the fill onto its resting value after a tap or the end of a drag. */
48
+ const SPRING = { damping: 18, stiffness: 220, mass: 0.6 } as const;
49
+
50
+ const ratingVariants = tv({
51
+ slots: {
52
+ root: 'gap-2',
53
+ header: 'flex-row items-center justify-between gap-3',
54
+ label: 'text-sm font-medium text-foreground',
55
+ value: 'text-sm text-muted-foreground',
56
+ row: 'flex-row items-center self-start',
57
+ },
58
+ variants: {
59
+ size: {
60
+ sm: { row: 'gap-0.5' },
61
+ md: { row: 'gap-1' },
62
+ lg: { row: 'gap-1.5' },
63
+ },
64
+ disabled: {
65
+ true: { root: 'opacity-50' },
66
+ },
67
+ },
68
+ defaultVariants: {
69
+ size: 'md',
70
+ },
71
+ });
72
+
73
+ type RatingVariantProps = VariantProps<typeof ratingVariants>;
74
+ export type RatingSize = NonNullable<RatingVariantProps['size']>;
75
+ export type RatingColor =
76
+ | 'warning'
77
+ | 'primary'
78
+ | 'success'
79
+ | 'destructive'
80
+ | 'info'
81
+ | 'foreground';
82
+
83
+ /** Star pixel size per size token — the whole control scales from this. */
84
+ const STAR_SIZE: Record<RatingSize, number> = { sm: 18, md: 24, lg: 32 };
85
+
86
+ /** The CSS token each colour paints its filled stars with. */
87
+ const FILL_VAR: Record<RatingColor, string> = {
88
+ warning: '--color-warning',
89
+ primary: '--color-primary',
90
+ success: '--color-success',
91
+ destructive: '--color-destructive',
92
+ info: '--color-info',
93
+ foreground: '--color-foreground',
94
+ };
95
+
96
+ function clampJS(value: number, min: number, max: number) {
97
+ return Math.min(Math.max(value, min), max);
98
+ }
99
+
100
+ /** Rounds `value` up to the nearest `precision`, so any touch fills at least it. */
101
+ function snap(value: number, precision: number, max: number) {
102
+ const stepped = Math.ceil(value / precision) * precision;
103
+ // Guard the float drift that `ceil` on fractional steps can leave behind.
104
+ return clampJS(Math.round(stepped / precision) * precision, 0, max);
105
+ }
106
+
107
+ export interface RatingProps extends Omit<RatingVariantProps, 'disabled'> {
108
+ className?: string;
109
+ /** Controlled value. Leave unset and pass `defaultValue` to run uncontrolled. */
110
+ value?: number;
111
+ /** Starting value when uncontrolled. */
112
+ defaultValue?: number;
113
+ /** How many stars. */
114
+ max?: number;
115
+ /**
116
+ * Smallest step a tap can pick, as a fraction of a star. `1` is whole stars,
117
+ * `0.5` lets the left half of a star mean a half. Reading a value renders any
118
+ * precision — this only constrains what a finger can set.
119
+ */
120
+ precision?: number;
121
+ /** Fires as the value changes, including live while dragging. */
122
+ onValueChange?: (value: number) => void;
123
+ /** Fires once when a tap or drag ends — the place for expensive side effects. */
124
+ onValueCommit?: (value: number) => void;
125
+ /** Show the stars but ignore touches — the display half of the component. */
126
+ readOnly?: boolean;
127
+ disabled?: boolean;
128
+ /**
129
+ * Let a second tap on the current value clear it back to zero, so a rating
130
+ * given by mistake can be taken back without a separate control.
131
+ */
132
+ allowClear?: boolean;
133
+ /** Which token the filled stars are painted with. */
134
+ color?: RatingColor;
135
+ /** Caption above the stars. Also becomes the accessibility label. */
136
+ label?: string;
137
+ /** Show the numeric value on the caption row, opposite the label. */
138
+ showValue?: boolean;
139
+ /** Format the shown value. Defaults to the number as written. */
140
+ formatValue?: (value: number) => string;
141
+ /**
142
+ * A tick under the finger each time a drag crosses onto a new star. Off by
143
+ * default — needs the optional `expo-haptics`, and is silent without it.
144
+ */
145
+ haptics?: boolean;
146
+ /** Extra classes for the caption row. */
147
+ headerClassName?: string;
148
+ /** Extra classes for the row of stars. */
149
+ rowClassName?: string;
150
+ }
151
+
152
+ /**
153
+ * One star: an empty outline with a filled copy clipped over it. The clip width
154
+ * is driven by the shared `display` value on the UI thread, so the fill of this
155
+ * star animates and drags without a React render.
156
+ */
157
+ function Star({
158
+ index,
159
+ display,
160
+ size,
161
+ gap,
162
+ fillColor,
163
+ emptyColor,
164
+ }: {
165
+ index: number;
166
+ display: SharedValue<number>;
167
+ size: number;
168
+ gap: number;
169
+ fillColor: string;
170
+ emptyColor: string;
171
+ }) {
172
+ // Clamped inline rather than through a helper: this body runs on the UI
173
+ // thread, which can only reach worklets, not module-scope functions.
174
+ const clipStyle = useAnimatedStyle(() => ({
175
+ width: Math.min(Math.max(display.value - index, 0), 1) * size,
176
+ }));
177
+
178
+ return (
179
+ <View style={{ width: size, height: size, marginRight: gap }}>
180
+ <StarIcon size={size} color={emptyColor} />
181
+ {/* The filled layer is pinned to the left and clipped to a fraction of
182
+ the star's width, so a partial value shows a partial star. */}
183
+ <Animated.View
184
+ style={[{ position: 'absolute', left: 0, top: 0, overflow: 'hidden' }, clipStyle]}
185
+ >
186
+ <StarIcon size={size} color={fillColor} filled />
187
+ </Animated.View>
188
+ </View>
189
+ );
190
+ }
191
+
192
+ export const Rating = forwardRef<View, RatingProps>(
193
+ (
194
+ {
195
+ className,
196
+ headerClassName,
197
+ rowClassName,
198
+ value: valueProp,
199
+ defaultValue = 0,
200
+ max = 5,
201
+ precision = 1,
202
+ onValueChange,
203
+ onValueCommit,
204
+ readOnly = false,
205
+ disabled = false,
206
+ allowClear = false,
207
+ color = 'warning',
208
+ size = 'md',
209
+ label,
210
+ showValue = false,
211
+ formatValue,
212
+ haptics = false,
213
+ },
214
+ ref
215
+ ) => {
216
+ const isControlled = valueProp !== undefined;
217
+ const [internal, setInternal] = useState(defaultValue);
218
+ const value = clampJS(isControlled ? valueProp! : internal, 0, max);
219
+
220
+ const interactive = !readOnly && !disabled;
221
+ const slots = ratingVariants({ size, disabled });
222
+ const starSize = STAR_SIZE[size ?? 'md'];
223
+ const gap = size === 'sm' ? 2 : size === 'lg' ? 6 : 4;
224
+
225
+ // SVG paints with real colour strings, not classes — resolve the tokens.
226
+ const fillColor = useCSSVariable(FILL_VAR[color]);
227
+ const emptyColor = useCSSVariable('--color-border');
228
+ const fill = typeof fillColor === 'string' ? fillColor : '#f59e0b';
229
+ const empty = typeof emptyColor === 'string' ? emptyColor : '#d4d4d4';
230
+
231
+ // Drives every star's clipped fill on the UI thread.
232
+ const display = useSharedValue(value);
233
+ const rowWidth = useSharedValue(0);
234
+
235
+ // Keep the change/commit handlers reachable from the gesture without
236
+ // rebuilding it on every render.
237
+ const changeRef = useRef(onValueChange);
238
+ changeRef.current = onValueChange;
239
+ const commitRef = useRef(onValueCommit);
240
+ commitRef.current = onValueCommit;
241
+ // The last whole star a drag crossed, so haptics tick once per star.
242
+ const lastStep = useRef(Math.ceil(value));
243
+
244
+ const emitChange = useCallback(
245
+ (next: number) => {
246
+ if (!isControlled) setInternal(next);
247
+ changeRef.current?.(next);
248
+ },
249
+ [isControlled]
250
+ );
251
+
252
+ // Follow a controlled value that changes elsewhere, springing to it.
253
+ useEffect(() => {
254
+ display.value = withSpring(value, SPRING);
255
+ }, [value, display]);
256
+
257
+ const onRowLayout = (event: LayoutChangeEvent) => {
258
+ rowWidth.value = event.nativeEvent.layout.width;
259
+ };
260
+
261
+ // Maps an x within the row to a snapped rating. Runs on JS off the worklet.
262
+ const valueFromX = useCallback(
263
+ (x: number, width: number) => {
264
+ if (width <= 0) return value;
265
+ const raw = clampJS(x / width, 0, 1) * max;
266
+ return snap(raw, precision, max);
267
+ },
268
+ [max, precision, value]
269
+ );
270
+
271
+ const applyLive = useCallback(
272
+ (x: number, width: number) => {
273
+ let next = valueFromX(x, width);
274
+ if (allowClear && next === value && Math.ceil(next) === lastStep.current) {
275
+ next = 0;
276
+ }
277
+ display.value = next;
278
+ if (haptics && Math.ceil(next) !== lastStep.current) {
279
+ lastStep.current = Math.ceil(next);
280
+ selectionTick();
281
+ }
282
+ emitChange(next);
283
+ },
284
+ [valueFromX, allowClear, value, display, haptics, emitChange]
285
+ );
286
+
287
+ // Resets the haptic step to where the touch started, then applies the first
288
+ // position. Runs on JS: a ref cannot be written from a gesture worklet.
289
+ const begin = useCallback(
290
+ (x: number, width: number) => {
291
+ lastStep.current = Math.ceil(value);
292
+ applyLive(x, width);
293
+ },
294
+ [value, applyLive]
295
+ );
296
+
297
+ const commit = useCallback(
298
+ (x: number, width: number) => {
299
+ let next = valueFromX(x, width);
300
+ if (allowClear && next === value) next = 0;
301
+ display.value = withSpring(next, SPRING);
302
+ lastStep.current = Math.ceil(next);
303
+ emitChange(next);
304
+ commitRef.current?.(next);
305
+ },
306
+ [valueFromX, allowClear, value, display, emitChange]
307
+ );
308
+
309
+ const pan = Gesture.Pan()
310
+ .enabled(interactive)
311
+ .onBegin((event) => {
312
+ runOnJS(begin)(event.x, rowWidth.value);
313
+ })
314
+ .onUpdate((event) => {
315
+ runOnJS(applyLive)(event.x, rowWidth.value);
316
+ })
317
+ .onFinalize((event) => {
318
+ runOnJS(commit)(event.x, rowWidth.value);
319
+ });
320
+
321
+ const tap = Gesture.Tap()
322
+ .enabled(interactive)
323
+ .maxDuration(250)
324
+ .onEnd((event) => {
325
+ runOnJS(commit)(event.x, rowWidth.value);
326
+ });
327
+
328
+ const gesture = Gesture.Race(tap, pan);
329
+
330
+ // VoiceOver / TalkBack move by a single precision step.
331
+ const nudge = (dir: 1 | -1) => {
332
+ const next = clampJS(
333
+ Math.round((value + dir * precision) / precision) * precision,
334
+ 0,
335
+ max
336
+ );
337
+ if (next === value) return;
338
+ display.value = withSpring(next, SPRING);
339
+ lastStep.current = Math.ceil(next);
340
+ emitChange(next);
341
+ commitRef.current?.(next);
342
+ };
343
+
344
+ const onAccessibilityAction = (event: AccessibilityActionEvent) => {
345
+ if (event.nativeEvent.actionName === 'increment') nudge(1);
346
+ else if (event.nativeEvent.actionName === 'decrement') nudge(-1);
347
+ };
348
+
349
+ const shown = formatValue ? formatValue(value) : String(value);
350
+ const a11yLabel = label ?? `Rating, ${shown} out of ${max}`;
351
+
352
+ const stars = (
353
+ <View
354
+ className={slots.row({ className: rowClassName })}
355
+ onLayout={onRowLayout}
356
+ // The row is measured for the touch math, so it must not collapse.
357
+ collapsable={false}
358
+ >
359
+ {Array.from({ length: max }, (_, i) => (
360
+ <Star
361
+ key={i}
362
+ index={i}
363
+ display={display}
364
+ size={starSize}
365
+ // No trailing gap after the last star, so the row width is honest.
366
+ gap={i === max - 1 ? 0 : gap}
367
+ fillColor={fill}
368
+ emptyColor={empty}
369
+ />
370
+ ))}
371
+ </View>
372
+ );
373
+
374
+ const header =
375
+ label || showValue ? (
376
+ <View className={slots.header({ className: headerClassName })}>
377
+ {label ? <Text className={slots.label()}>{label}</Text> : <View />}
378
+ {showValue ? <Text className={slots.value()}>{shown}</Text> : null}
379
+ </View>
380
+ ) : null;
381
+
382
+ return (
383
+ <View ref={ref} className={cn(slots.root({ className }))}>
384
+ {header}
385
+ {interactive ? (
386
+ <GestureDetector gesture={gesture}>
387
+ <View
388
+ accessible
389
+ accessibilityRole="adjustable"
390
+ accessibilityLabel={a11yLabel}
391
+ accessibilityState={{ disabled }}
392
+ accessibilityValue={{ min: 0, max, now: value, text: shown }}
393
+ accessibilityActions={[{ name: 'increment' }, { name: 'decrement' }]}
394
+ onAccessibilityAction={onAccessibilityAction}
395
+ // Hold the stars in a self-starting box so the gesture area is
396
+ // exactly the stars, not the full width of the parent.
397
+ className="self-start"
398
+ >
399
+ {stars}
400
+ </View>
401
+ </GestureDetector>
402
+ ) : (
403
+ <View
404
+ accessible
405
+ accessibilityRole="image"
406
+ accessibilityLabel={a11yLabel}
407
+ >
408
+ {stars}
409
+ </View>
410
+ )}
411
+ </View>
412
+ );
413
+ }
414
+ );
415
+
416
+ Rating.displayName = 'Rating';