panelui-native 0.44.0 → 0.49.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.
Files changed (108) hide show
  1. package/README.md +6 -1
  2. package/lib/module/components/accordion/index.js +32 -4
  3. package/lib/module/components/accordion/index.js.map +1 -1
  4. package/lib/module/components/button/index.js +83 -15
  5. package/lib/module/components/button/index.js.map +1 -1
  6. package/lib/module/components/button-group/index.js +186 -0
  7. package/lib/module/components/button-group/index.js.map +1 -0
  8. package/lib/module/components/color-picker/index.js +110 -1
  9. package/lib/module/components/color-picker/index.js.map +1 -1
  10. package/lib/module/components/combobox/index.js +7 -1
  11. package/lib/module/components/combobox/index.js.map +1 -1
  12. package/lib/module/components/date-time-picker/index.js +272 -0
  13. package/lib/module/components/date-time-picker/index.js.map +1 -0
  14. package/lib/module/components/fab/index.js +514 -0
  15. package/lib/module/components/fab/index.js.map +1 -0
  16. package/lib/module/components/grid-item/index.js +486 -0
  17. package/lib/module/components/grid-item/index.js.map +1 -0
  18. package/lib/module/components/{kpi-chart → kpi}/index.js +62 -62
  19. package/lib/module/components/kpi/index.js.map +1 -0
  20. package/lib/module/components/line-chart/index.js +92 -10
  21. package/lib/module/components/line-chart/index.js.map +1 -1
  22. package/lib/module/components/markdown-editor/index.js +406 -0
  23. package/lib/module/components/markdown-editor/index.js.map +1 -0
  24. package/lib/module/components/markdown-editor/markdown-transforms.js +243 -0
  25. package/lib/module/components/markdown-editor/markdown-transforms.js.map +1 -0
  26. package/lib/module/components/pie-chart/index.js +625 -0
  27. package/lib/module/components/pie-chart/index.js.map +1 -0
  28. package/lib/module/components/questionnaire/index.js +1312 -0
  29. package/lib/module/components/questionnaire/index.js.map +1 -0
  30. package/lib/module/components/scatter-chart/index.js +1173 -0
  31. package/lib/module/components/scatter-chart/index.js.map +1 -0
  32. package/lib/module/components/tabs/index.js +359 -41
  33. package/lib/module/components/tabs/index.js.map +1 -1
  34. package/lib/module/components/time-picker/index.js +34 -6
  35. package/lib/module/components/time-picker/index.js.map +1 -1
  36. package/lib/module/components/tree/index.js +500 -0
  37. package/lib/module/components/tree/index.js.map +1 -0
  38. package/lib/module/icons/index.js +217 -0
  39. package/lib/module/icons/index.js.map +1 -1
  40. package/lib/module/index.js +11 -2
  41. package/lib/module/index.js.map +1 -1
  42. package/lib/module/utils/chart.js +81 -0
  43. package/lib/module/utils/chart.js.map +1 -1
  44. package/lib/typescript/src/components/accordion/index.d.ts +21 -0
  45. package/lib/typescript/src/components/accordion/index.d.ts.map +1 -1
  46. package/lib/typescript/src/components/button/index.d.ts +21 -0
  47. package/lib/typescript/src/components/button/index.d.ts.map +1 -1
  48. package/lib/typescript/src/components/button-group/index.d.ts +212 -0
  49. package/lib/typescript/src/components/button-group/index.d.ts.map +1 -0
  50. package/lib/typescript/src/components/color-picker/index.d.ts +82 -1
  51. package/lib/typescript/src/components/color-picker/index.d.ts.map +1 -1
  52. package/lib/typescript/src/components/combobox/index.d.ts.map +1 -1
  53. package/lib/typescript/src/components/date-time-picker/index.d.ts +127 -0
  54. package/lib/typescript/src/components/date-time-picker/index.d.ts.map +1 -0
  55. package/lib/typescript/src/components/fab/index.d.ts +285 -0
  56. package/lib/typescript/src/components/fab/index.d.ts.map +1 -0
  57. package/lib/typescript/src/components/grid-item/index.d.ts +292 -0
  58. package/lib/typescript/src/components/grid-item/index.d.ts.map +1 -0
  59. package/lib/typescript/src/components/{kpi-chart → kpi}/index.d.ts +65 -65
  60. package/lib/typescript/src/components/kpi/index.d.ts.map +1 -0
  61. package/lib/typescript/src/components/line-chart/index.d.ts +25 -1
  62. package/lib/typescript/src/components/line-chart/index.d.ts.map +1 -1
  63. package/lib/typescript/src/components/markdown-editor/index.d.ts +102 -0
  64. package/lib/typescript/src/components/markdown-editor/index.d.ts.map +1 -0
  65. package/lib/typescript/src/components/markdown-editor/markdown-transforms.d.ts +76 -0
  66. package/lib/typescript/src/components/markdown-editor/markdown-transforms.d.ts.map +1 -0
  67. package/lib/typescript/src/components/pie-chart/index.d.ts +245 -0
  68. package/lib/typescript/src/components/pie-chart/index.d.ts.map +1 -0
  69. package/lib/typescript/src/components/questionnaire/index.d.ts +336 -0
  70. package/lib/typescript/src/components/questionnaire/index.d.ts.map +1 -0
  71. package/lib/typescript/src/components/scatter-chart/index.d.ts +309 -0
  72. package/lib/typescript/src/components/scatter-chart/index.d.ts.map +1 -0
  73. package/lib/typescript/src/components/tabs/index.d.ts +29 -2
  74. package/lib/typescript/src/components/tabs/index.d.ts.map +1 -1
  75. package/lib/typescript/src/components/time-picker/index.d.ts +19 -1
  76. package/lib/typescript/src/components/time-picker/index.d.ts.map +1 -1
  77. package/lib/typescript/src/components/tree/index.d.ts +125 -0
  78. package/lib/typescript/src/components/tree/index.d.ts.map +1 -0
  79. package/lib/typescript/src/icons/index.d.ts +22 -0
  80. package/lib/typescript/src/icons/index.d.ts.map +1 -1
  81. package/lib/typescript/src/index.d.ts +14 -5
  82. package/lib/typescript/src/index.d.ts.map +1 -1
  83. package/lib/typescript/src/utils/chart.d.ts +34 -0
  84. package/lib/typescript/src/utils/chart.d.ts.map +1 -1
  85. package/package.json +1 -1
  86. package/src/components/accordion/index.tsx +48 -6
  87. package/src/components/button/index.tsx +97 -15
  88. package/src/components/button-group/index.tsx +199 -0
  89. package/src/components/color-picker/index.tsx +140 -3
  90. package/src/components/combobox/index.tsx +7 -1
  91. package/src/components/date-time-picker/index.tsx +411 -0
  92. package/src/components/fab/index.tsx +583 -0
  93. package/src/components/grid-item/index.tsx +515 -0
  94. package/src/components/{kpi-chart → kpi}/index.tsx +85 -85
  95. package/src/components/line-chart/index.tsx +98 -8
  96. package/src/components/markdown-editor/index.tsx +526 -0
  97. package/src/components/markdown-editor/markdown-transforms.ts +228 -0
  98. package/src/components/pie-chart/index.tsx +863 -0
  99. package/src/components/questionnaire/index.tsx +1615 -0
  100. package/src/components/scatter-chart/index.tsx +1401 -0
  101. package/src/components/tabs/index.tsx +392 -50
  102. package/src/components/time-picker/index.tsx +42 -6
  103. package/src/components/tree/index.tsx +564 -0
  104. package/src/icons/index.tsx +154 -0
  105. package/src/index.ts +142 -18
  106. package/src/utils/chart.ts +110 -0
  107. package/lib/module/components/kpi-chart/index.js.map +0 -1
  108. package/lib/typescript/src/components/kpi-chart/index.d.ts.map +0 -1
@@ -0,0 +1,583 @@
1
+ /**
2
+ * Fab — the floating action button: one screen, one thing it is mostly for.
3
+ *
4
+ * ```tsx
5
+ * <Fab icon={<PlusIcon size={24} />} accessibilityLabel="New note" onPress={compose} />
6
+ * ```
7
+ *
8
+ * ## It floats, which is the whole problem with it
9
+ *
10
+ * A button pinned over the content is a button covering some of it, and the
11
+ * bottom-right corner of a scrolling list is exactly where the last row goes. So
12
+ * a Fab is right when a screen has *one* action worth that trade and wrong when
13
+ * it has three — a corner with three buttons in it is a toolbar that has been
14
+ * put in the wrong place. Where a screen has several, the honest shapes are a
15
+ * `ButtonGroup` in a bar or, if one of them really does lead, this with a
16
+ * `Fab.Group` behind it.
17
+ *
18
+ * It positions itself absolutely against its nearest positioned ancestor, which
19
+ * on a screen means the screen. Give the content below it enough bottom padding
20
+ * to scroll clear — this cannot know how tall your list is, and a Fab sitting on
21
+ * the last row forever is the failure people actually hit.
22
+ *
23
+ * ## Extended, and when to bother
24
+ *
25
+ * ```tsx
26
+ * <Fab extended icon={<PencilIcon size={20} />}>Write</Fab>
27
+ * ```
28
+ *
29
+ * A lone glyph is a guess unless the glyph is a plus. `extended` spells the
30
+ * action out, costs the width, and is worth it for anything a plus would not
31
+ * have said.
32
+ *
33
+ * ## The speed dial
34
+ *
35
+ * ```tsx
36
+ * <Fab.Group icon={<PlusIcon size={24} />} accessibilityLabel="Add">
37
+ * <Fab.Action icon={<ImageIcon size={18} />} label="Photo" onPress={addPhoto} />
38
+ * <Fab.Action icon={<FileIcon size={18} />} label="File" onPress={addFile} />
39
+ * </Fab.Group>
40
+ * ```
41
+ *
42
+ * The actions come out of the button one after another rather than together —
43
+ * a stagger of a few frames each, which is enough to read as a list unfolding
44
+ * instead of a menu appearing. Every action carries its label beside it, because
45
+ * a column of unlabelled circles is a quiz.
46
+ *
47
+ * Opening also drops a scrim over the screen. Not for looks: an open dial is
48
+ * modal — the next tap either picks something or closes it — and a scrim is what
49
+ * says so, as well as what catches the tap that closes it.
50
+ *
51
+ * A group draws itself through a portal, above everything else — the scrim and
52
+ * the buttons together, so the backdrop cannot end up on top of the dial it is
53
+ * behind. That means its `offset` is measured from the screen's edges rather
54
+ * than from whatever it is written inside, which is what lets it be declared
55
+ * next to the content it belongs to instead of at the root of the screen.
56
+ */
57
+ import {
58
+ Children,
59
+ createContext,
60
+ forwardRef,
61
+ isValidElement,
62
+ useCallback,
63
+ useContext,
64
+ useMemo,
65
+ useState,
66
+ type ReactNode,
67
+ } from 'react';
68
+ import { Pressable, StyleSheet, View, type ViewProps } from 'react-native';
69
+ import Animated, {
70
+ interpolate,
71
+ useAnimatedStyle,
72
+ useDerivedValue,
73
+ withTiming,
74
+ type SharedValue,
75
+ } from 'react-native-reanimated';
76
+ import { tv, type VariantProps } from 'tailwind-variants';
77
+ import { useCSSVariable } from 'uniwind';
78
+ import { IconColorProvider } from '../../icons';
79
+ import { AnimatedPressable } from '../../primitives/animated-pressable';
80
+ import { Portal } from '../../primitives/portal';
81
+ import { Scrim } from '../../primitives/scrim';
82
+ import { Text } from '../../primitives/text';
83
+ import { cn } from '../../utils/cn';
84
+ import { selectionTick } from '../../utils/haptics';
85
+
86
+ /** Which corner the button sits in. */
87
+ export type FabPlacement = 'bottom-right' | 'bottom-center' | 'bottom-left';
88
+
89
+ /** How far from the edges a floating button sits, in points. */
90
+ const DEFAULT_OFFSET = 16;
91
+
92
+ /** How long the dial takes to open, and how much each action lags the one above it. */
93
+ const OPEN_DURATION = 220;
94
+ const STAGGER = 45;
95
+
96
+ /** How far an action starts below its resting place, in points. */
97
+ const ACTION_TRAVEL = 12;
98
+
99
+ /** A quarter turn on the trigger while the dial is open — a plus becomes a cross. */
100
+ const OPEN_ROTATION = 45;
101
+
102
+ const fabVariants = tv({
103
+ slots: {
104
+ root: 'flex-row items-center justify-center gap-2 rounded-full bg-primary shadow-lg',
105
+ label: 'font-medium text-primary-foreground',
106
+ },
107
+ variants: {
108
+ size: {
109
+ sm: { root: 'h-11 min-w-11', label: 'text-sm' },
110
+ md: { root: 'h-14 min-w-14', label: 'text-base' },
111
+ lg: { root: 'h-16 min-w-16', label: 'text-lg' },
112
+ },
113
+ /* An icon-only button is a circle, so its width is its height and it takes
114
+ no padding. Extended, it is a stadium and the padding is what makes it
115
+ one. */
116
+ extended: {
117
+ true: {},
118
+ false: {},
119
+ },
120
+ variant: {
121
+ primary: { root: 'bg-primary', label: 'text-primary-foreground' },
122
+ secondary: { root: 'bg-secondary', label: 'text-secondary-foreground' },
123
+ surface: { root: 'border border-border bg-popover', label: 'text-foreground' },
124
+ destructive: { root: 'bg-destructive', label: 'text-white' },
125
+ },
126
+ disabled: {
127
+ true: { root: 'opacity-[0.64] shadow-none' },
128
+ },
129
+ },
130
+ compoundVariants: [
131
+ { extended: true, size: 'sm', class: { root: 'px-4' } },
132
+ { extended: true, size: 'md', class: { root: 'px-5' } },
133
+ { extended: true, size: 'lg', class: { root: 'px-6' } },
134
+ { extended: false, size: 'sm', class: { root: 'w-11' } },
135
+ { extended: false, size: 'md', class: { root: 'w-14' } },
136
+ { extended: false, size: 'lg', class: { root: 'w-16' } },
137
+ ],
138
+ defaultVariants: {
139
+ size: 'md',
140
+ variant: 'primary',
141
+ extended: false,
142
+ },
143
+ });
144
+
145
+ type FabVariantProps = VariantProps<typeof fabVariants>;
146
+
147
+ /** How big the button is. */
148
+ export type FabSize = NonNullable<FabVariantProps['size']>;
149
+ /** What the button is drawn in. */
150
+ export type FabVariant = NonNullable<FabVariantProps['variant']>;
151
+
152
+ /** The theme token each variant's icon reads against. */
153
+ const CONTENT_COLOR_VAR: Record<Exclude<FabVariant, 'destructive'>, string> = {
154
+ primary: '--color-primary-foreground',
155
+ secondary: '--color-secondary-foreground',
156
+ surface: '--color-foreground',
157
+ };
158
+
159
+ /** Where a floating button parks itself, given its offset. */
160
+ function anchor(placement: FabPlacement, offset: number) {
161
+ const base = { position: 'absolute' as const, bottom: offset };
162
+ if (placement === 'bottom-left') return { ...base, left: offset };
163
+ if (placement === 'bottom-center') return { ...base, alignSelf: 'center' as const };
164
+ return { ...base, right: offset };
165
+ }
166
+
167
+ /* -------------------------------------------------------------------------- *
168
+ * Fab
169
+ * -------------------------------------------------------------------------- */
170
+
171
+ export interface FabProps extends Omit<ViewProps, 'children'>, FabVariantProps {
172
+ className?: string;
173
+ /** The glyph. Sized by you — this is the one thing that should not guess. */
174
+ icon?: ReactNode;
175
+ /**
176
+ * The label, which turns the circle into a stadium. Needs `extended`; a
177
+ * label with nowhere to go is a label that gets clipped by the circle.
178
+ */
179
+ children?: ReactNode;
180
+ /** Spell the action out beside the glyph. */
181
+ extended?: boolean;
182
+ /**
183
+ * Pin it over the content, in a corner.
184
+ *
185
+ * Left out, it is an ordinary button in the flow — which is what you want
186
+ * inside a `Fab.Group`, or when the screen already has somewhere for it to
187
+ * sit.
188
+ */
189
+ placement?: FabPlacement;
190
+ /** Distance from the edges when `placement` is set, in points. */
191
+ offset?: number;
192
+ onPress?: () => void;
193
+ disabled?: boolean;
194
+ /**
195
+ * A tick on press. Off by default — needs the optional `expo-haptics`, and
196
+ * is silent without it.
197
+ */
198
+ haptics?: boolean;
199
+ /** Required for an icon-only button. A lone glyph reads out as nothing. */
200
+ accessibilityLabel?: string;
201
+ }
202
+
203
+ const FabRoot = forwardRef<View, FabProps>(
204
+ (
205
+ {
206
+ className,
207
+ icon,
208
+ children,
209
+ extended = false,
210
+ placement,
211
+ offset = DEFAULT_OFFSET,
212
+ size,
213
+ variant,
214
+ disabled = false,
215
+ haptics = false,
216
+ onPress,
217
+ style,
218
+ ...props
219
+ },
220
+ ref
221
+ ) => {
222
+ const { root, label } = fabVariants({
223
+ size,
224
+ variant,
225
+ extended: extended && !!children,
226
+ disabled,
227
+ });
228
+
229
+ const themed = useCSSVariable(
230
+ CONTENT_COLOR_VAR[
231
+ variant === 'destructive' ? 'primary' : (variant ?? 'primary')
232
+ ]
233
+ );
234
+ const contentColor =
235
+ variant === 'destructive'
236
+ ? '#ffffff'
237
+ : typeof themed === 'string'
238
+ ? themed
239
+ : undefined;
240
+
241
+ const handlePress = useCallback(() => {
242
+ if (haptics) selectionTick();
243
+ onPress?.();
244
+ }, [haptics, onPress]);
245
+
246
+ return (
247
+ <IconColorProvider color={contentColor}>
248
+ <AnimatedPressable
249
+ ref={ref}
250
+ accessibilityRole="button"
251
+ accessibilityState={{ disabled }}
252
+ disabled={disabled}
253
+ onPress={handlePress}
254
+ className={root({ className })}
255
+ style={[placement ? anchor(placement, offset) : null, style]}
256
+ {...props}
257
+ >
258
+ {icon}
259
+ {extended && children ? (
260
+ <Text className={label()}>{children}</Text>
261
+ ) : null}
262
+ </AnimatedPressable>
263
+ </IconColorProvider>
264
+ );
265
+ }
266
+ );
267
+
268
+ FabRoot.displayName = 'Fab';
269
+
270
+ /* -------------------------------------------------------------------------- *
271
+ * Group — the speed dial
272
+ * -------------------------------------------------------------------------- */
273
+
274
+ interface FabGroupContextValue {
275
+ /** 0 closed, 1 open. Every action reads it and its own index off it. */
276
+ progress: SharedValue<number>;
277
+ count: number;
278
+ size: FabSize;
279
+ close: () => void;
280
+ }
281
+
282
+ const FabGroupContext = createContext<FabGroupContextValue | null>(null);
283
+
284
+ function useFabGroup(part: string): FabGroupContextValue {
285
+ const ctx = useContext(FabGroupContext);
286
+ if (!ctx) throw new Error(`${part} must be used inside <Fab.Group>.`);
287
+ return ctx;
288
+ }
289
+
290
+ export interface FabGroupProps extends Omit<ViewProps, 'children'> {
291
+ className?: string;
292
+ /** The glyph on the trigger. */
293
+ icon?: ReactNode;
294
+ /** The trigger's label, if it should be extended while closed. */
295
+ label?: string;
296
+ /** `Fab.Action` children, in the order they should unfold. */
297
+ children?: ReactNode;
298
+ /** Controlled open state. */
299
+ open?: boolean;
300
+ onOpenChange?: (open: boolean) => void;
301
+ /** Which corner of the *screen* the whole dial parks in. */
302
+ placement?: FabPlacement;
303
+ /** Distance from the screen's edges, in points. Add your safe-area inset. */
304
+ offset?: number;
305
+ size?: FabSize;
306
+ variant?: FabVariant;
307
+ disabled?: boolean;
308
+ haptics?: boolean;
309
+ /** Frost the screen behind the open dial instead of dimming it. */
310
+ blur?: boolean;
311
+ /** Required — the trigger is a lone glyph until it is opened. */
312
+ accessibilityLabel?: string;
313
+ /**
314
+ * Turn the trigger's glyph a quarter circle while the dial is open.
315
+ *
316
+ * On by default, and it is doing real work when the glyph is a plus: the
317
+ * same mark becomes a cross, which says "this closes now" without a second
318
+ * icon that has to be swapped in. Turn it off for a glyph that means
319
+ * something at one angle only.
320
+ */
321
+ rotateOnOpen?: boolean;
322
+ }
323
+
324
+ /**
325
+ * A trigger with actions behind it.
326
+ *
327
+ * The scrim goes through a portal so it covers the screen rather than the
328
+ * group's own corner of it; the actions do not, because they belong to the
329
+ * button and have to stay above the scrim in the same coordinate space as the
330
+ * trigger they unfold from.
331
+ */
332
+ const FabGroup = forwardRef<View, FabGroupProps>(
333
+ (
334
+ {
335
+ className,
336
+ icon,
337
+ label,
338
+ children,
339
+ open: openProp,
340
+ onOpenChange,
341
+ placement = 'bottom-right',
342
+ offset = DEFAULT_OFFSET,
343
+ size = 'md',
344
+ variant,
345
+ disabled = false,
346
+ haptics = false,
347
+ blur = false,
348
+ rotateOnOpen = true,
349
+ accessibilityLabel,
350
+ style,
351
+ ...props
352
+ },
353
+ ref
354
+ ) => {
355
+ const [internalOpen, setInternalOpen] = useState(false);
356
+ const open = openProp ?? internalOpen;
357
+
358
+ const setOpen = useCallback(
359
+ (next: boolean) => {
360
+ if (openProp === undefined) setInternalOpen(next);
361
+ onOpenChange?.(next);
362
+ },
363
+ [openProp, onOpenChange]
364
+ );
365
+
366
+ const actions = Children.toArray(children).filter(isValidElement);
367
+ /*
368
+ * The count, pulled out as a plain number before any worklet sees it.
369
+ *
370
+ * A worklet captures every variable it *references*, whole — so reading
371
+ * `actions.length` inside one would drag the array of React elements onto
372
+ * the UI thread with it, and an element holds a fibre, which cannot be
373
+ * serialised. The error that produces names neither this file nor this
374
+ * line, so: never reach through an object into a worklet.
375
+ */
376
+ const actionCount = actions.length;
377
+
378
+ /*
379
+ * One shared value for the whole dial rather than one per action.
380
+ *
381
+ * The stagger is a function of the action's index, applied on the UI
382
+ * thread inside each action's own style — so opening is a single timing
383
+ * driving every row, and there is no chain of JavaScript timeouts to get
384
+ * out of step with itself when the dial is closed halfway through opening.
385
+ */
386
+ const progress = useDerivedValue<number>(
387
+ () => withTiming(open ? 1 : 0, { duration: OPEN_DURATION + STAGGER * actionCount }),
388
+ [open, actionCount]
389
+ );
390
+
391
+ const rotation = useAnimatedStyle(() => ({
392
+ transform: [
393
+ { rotate: `${interpolate(progress.value, [0, 1], [0, OPEN_ROTATION])}deg` },
394
+ ],
395
+ }));
396
+
397
+ const close = useCallback(() => setOpen(false), [setOpen]);
398
+
399
+ const context = useMemo<FabGroupContextValue>(
400
+ () => ({ progress, count: actionCount, size, close }),
401
+ [progress, actionCount, size, close]
402
+ );
403
+
404
+ const toggle = useCallback(() => {
405
+ if (haptics) selectionTick();
406
+ setOpen(!open);
407
+ }, [haptics, open, setOpen]);
408
+
409
+ /*
410
+ * The scrim and the buttons go through the same portal, in that order.
411
+ *
412
+ * They have to travel together. Portalled content is drawn above
413
+ * everything in the ordinary tree, so a portalled scrim over an
414
+ * in-place trigger covers the trigger and its actions — the dial opens
415
+ * and immediately disappears behind its own backdrop. One portal, scrim
416
+ * first, keeps the stacking honest: within it they are siblings, and the
417
+ * later sibling wins.
418
+ *
419
+ * The provider goes inside the portalled subtree for the same reason
420
+ * every other overlay here re-provides its context — the portal renders
421
+ * its children at the host, which is nowhere below this component, so a
422
+ * `Fab.Action` looking for the dial would not find it.
423
+ *
424
+ * One consequence worth knowing: `offset` is measured from the screen's
425
+ * edges rather than from whatever this is written inside. That is what a
426
+ * floating button wants, and it is why a group can be declared next to
427
+ * the content it belongs to instead of at the root of the screen.
428
+ */
429
+ return (
430
+ <Portal>
431
+ {open ? (
432
+ // Catches the tap that closes the dial, and says it is modal.
433
+ <Pressable
434
+ accessibilityRole="button"
435
+ accessibilityLabel="Close"
436
+ onPress={close}
437
+ style={StyleSheet.absoluteFill}
438
+ >
439
+ <Scrim blur={blur} dimClassName="bg-black/32" />
440
+ </Pressable>
441
+ ) : null}
442
+
443
+ <FabGroupContext.Provider value={context}>
444
+ <View
445
+ ref={ref}
446
+ className={cn('items-end gap-3', className)}
447
+ style={[anchor(placement, offset), style]}
448
+ {...props}
449
+ >
450
+ {/* Mounted only while open: a column of actions kept alive behind
451
+ the trigger would still be in the accessibility tree, and a
452
+ screen reader would walk into four buttons nobody can see. */}
453
+ {open
454
+ ? actions.map((action, index) => (
455
+ <FabActionSlot key={index} index={index}>
456
+ {action}
457
+ </FabActionSlot>
458
+ ))
459
+ : null}
460
+
461
+ <FabRoot
462
+ icon={
463
+ rotateOnOpen ? (
464
+ <Animated.View style={rotation}>{icon}</Animated.View>
465
+ ) : (
466
+ icon
467
+ )
468
+ }
469
+ extended={!!label && !open}
470
+ size={size}
471
+ variant={variant}
472
+ disabled={disabled}
473
+ accessibilityLabel={accessibilityLabel}
474
+ accessibilityState={{ disabled, expanded: open }}
475
+ onPress={toggle}
476
+ >
477
+ {label}
478
+ </FabRoot>
479
+ </View>
480
+ </FabGroupContext.Provider>
481
+ </Portal>
482
+ );
483
+ }
484
+ );
485
+
486
+ FabGroup.displayName = 'Fab.Group';
487
+
488
+ /**
489
+ * One action's slot in the unfolding.
490
+ *
491
+ * The stagger runs bottom-up: the action nearest the trigger arrives first,
492
+ * which is the order a hand travelling away from the button meets them in.
493
+ */
494
+ function FabActionSlot({ index, children }: { index: number; children: ReactNode }) {
495
+ const { progress, count } = useFabGroup('Fab.Action');
496
+
497
+ const style = useAnimatedStyle(() => {
498
+ const steps = Math.max(1, count);
499
+ const from = (count - 1 - index) / (steps + 1);
500
+ const to = from + 1 / (steps + 1) + 0.35;
501
+ const t = interpolate(progress.value, [from, Math.min(1, to)], [0, 1], 'clamp');
502
+ return {
503
+ opacity: t,
504
+ transform: [{ translateY: interpolate(t, [0, 1], [ACTION_TRAVEL, 0]) }],
505
+ };
506
+ });
507
+
508
+ return <Animated.View style={style}>{children}</Animated.View>;
509
+ }
510
+
511
+ /* -------------------------------------------------------------------------- *
512
+ * Action
513
+ * -------------------------------------------------------------------------- */
514
+
515
+ export interface FabActionProps extends Omit<ViewProps, 'children'> {
516
+ className?: string;
517
+ /** The glyph. */
518
+ icon?: ReactNode;
519
+ /** What it does, beside the glyph. A column of unlabelled circles is a quiz. */
520
+ label?: string;
521
+ onPress?: () => void;
522
+ disabled?: boolean;
523
+ /** Draws it in the destructive colour, for the one that removes something. */
524
+ destructive?: boolean;
525
+ /** Extra classes for the label chip. */
526
+ labelClassName?: string;
527
+ }
528
+
529
+ /**
530
+ * One choice in an open dial: a small round button with its name beside it.
531
+ *
532
+ * The label is a chip of its own rather than text on the background, because
533
+ * the background is whatever the screen behind the scrim happens to be and
534
+ * plain text over it is legible on some screens and not others.
535
+ *
536
+ * Pressing one closes the dial before running the action. Leaving it open over
537
+ * whatever the action just did is the wrong default: the dial is a menu, and a
538
+ * menu that stays up after a choice reads as the choice not having registered.
539
+ */
540
+ const FabAction = forwardRef<View, FabActionProps>(
541
+ (
542
+ { className, icon, label, onPress, disabled = false, destructive = false, labelClassName, ...props },
543
+ ref
544
+ ) => {
545
+ const { size, close } = useFabGroup('Fab.Action');
546
+
547
+ const handlePress = useCallback(() => {
548
+ close();
549
+ onPress?.();
550
+ }, [close, onPress]);
551
+
552
+ return (
553
+ <View className="flex-row items-center justify-end gap-3" {...props}>
554
+ {label ? (
555
+ <View className="rounded-lg bg-popover px-2.5 py-1 shadow-sm">
556
+ <Text size="sm" className={cn('text-foreground', labelClassName)}>
557
+ {label}
558
+ </Text>
559
+ </View>
560
+ ) : null}
561
+ <FabRoot
562
+ ref={ref}
563
+ icon={icon}
564
+ // A step down from the trigger, so the trigger stays the one that
565
+ // leads even while the dial it opened is on screen.
566
+ size={size === 'lg' ? 'md' : 'sm'}
567
+ variant={destructive ? 'destructive' : 'surface'}
568
+ disabled={disabled}
569
+ accessibilityLabel={label}
570
+ onPress={handlePress}
571
+ className={className}
572
+ />
573
+ </View>
574
+ );
575
+ }
576
+ );
577
+
578
+ FabAction.displayName = 'Fab.Action';
579
+
580
+ export const Fab = Object.assign(FabRoot, {
581
+ Group: FabGroup,
582
+ Action: FabAction,
583
+ });