@delacour/react-native-ui 0.1.0-alpha.20261006140348 → 0.1.0-alpha.20261006140628

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 (35) hide show
  1. package/package.json +5 -3
  2. package/src/components/alert/AGENTS.md +3 -2
  3. package/src/components/alert/alert-glyphs.ts +25 -0
  4. package/src/components/alert/alert-indicator.tsx +3 -22
  5. package/src/components/swipe/AGENTS.md +140 -0
  6. package/src/components/swipe/index.ts +31 -0
  7. package/src/components/swipe/swipe-action.tsx +99 -0
  8. package/src/components/swipe/swipe-end.tsx +14 -0
  9. package/src/components/swipe/swipe-group.tsx +55 -0
  10. package/src/components/swipe/swipe-panel.tsx +55 -0
  11. package/src/components/swipe/swipe-start.tsx +15 -0
  12. package/src/components/swipe/swipe.context.tsx +135 -0
  13. package/src/components/swipe/swipe.tsx +393 -0
  14. package/src/components/swipe/swipe.types.ts +7 -0
  15. package/src/components/swipe/swipe.variants.test.ts +337 -0
  16. package/src/components/swipe/swipe.variants.ts +290 -0
  17. package/src/components/toast/AGENTS.md +140 -0
  18. package/src/components/toast/index.ts +74 -0
  19. package/src/components/toast/toast-action.tsx +59 -0
  20. package/src/components/toast/toast-api.ts +32 -0
  21. package/src/components/toast/toast-close.tsx +44 -0
  22. package/src/components/toast/toast-content.tsx +11 -0
  23. package/src/components/toast/toast-description.tsx +11 -0
  24. package/src/components/toast/toast-indicator.tsx +34 -0
  25. package/src/components/toast/toast-item.tsx +308 -0
  26. package/src/components/toast/toast-title.tsx +13 -0
  27. package/src/components/toast/toast-viewport.tsx +135 -0
  28. package/src/components/toast/toast.context.tsx +69 -0
  29. package/src/components/toast/toast.store.test.ts +259 -0
  30. package/src/components/toast/toast.store.ts +338 -0
  31. package/src/components/toast/toast.timer.test.ts +64 -0
  32. package/src/components/toast/toast.timer.ts +51 -0
  33. package/src/components/toast/toast.tsx +132 -0
  34. package/src/components/toast/toast.variants.test.ts +393 -0
  35. package/src/components/toast/toast.variants.ts +304 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@delacour/react-native-ui",
3
- "version": "0.1.0-alpha.20261006140348",
3
+ "version": "0.1.0-alpha.20261006140628",
4
4
  "description": "React Native UI components — Uniwind, Reanimated, Gesture Handler",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -66,10 +66,12 @@
66
66
  "./spinner": "./src/components/spinner/index.ts",
67
67
  "./steps": "./src/components/steps/index.ts",
68
68
  "./surface": "./src/components/surface/index.ts",
69
+ "./swipe": "./src/components/swipe/index.ts",
69
70
  "./switch": "./src/components/switch/index.ts",
70
71
  "./tabs": "./src/components/tabs/index.ts",
71
72
  "./text": "./src/components/text/index.ts",
72
73
  "./textarea": "./src/components/textarea/index.ts",
74
+ "./toast": "./src/components/toast/index.ts",
73
75
  "./toggle-button": "./src/components/toggle-button/index.ts",
74
76
  "./expo/navigation-theme": "./src/expo/navigation-theme.tsx",
75
77
  "./hooks/use-calm-motion": "./src/hooks/use-calm-motion.tsx",
@@ -101,8 +103,8 @@
101
103
  },
102
104
  "peerDependencies": {
103
105
  "@central-icons-react-native/round-outlined-radius-1-stroke-1.5": "^1.1",
104
- "@delacour/react-native-bottom-sheet": "0.1.0-alpha.20261006140348",
105
- "@delacour/react-native-charts": "0.1.0-alpha.20261006140348",
106
+ "@delacour/react-native-bottom-sheet": "0.1.0-alpha.20261006140628",
107
+ "@delacour/react-native-charts": "0.1.0-alpha.20261006140628",
106
108
  "@legendapp/list": ">=3.3",
107
109
  "expo-linear-gradient": ">=15",
108
110
  "expo-router": ">=57",
@@ -13,7 +13,8 @@ plus `Alert.Indicator`, `Alert.Content`, `Alert.Title`, `Alert.Description`,
13
13
  | --- | --- |
14
14
  | `index.ts` | → `@delacour/react-native-ui/alert` |
15
15
  | `alert.tsx` | Root + the `Object.assign` compound surface |
16
- | `alert-indicator.tsx` | `Alert.Indicator`, and the status → glyph map |
16
+ | `alert-indicator.tsx` | `Alert.Indicator` |
17
+ | `alert-glyphs.ts` | **Leaf.** `ALERT_GLYPHS`, the status → glyph map — `toast/` imports it |
17
18
  | `alert-content.tsx` | `Alert.Content` |
18
19
  | `alert-title.tsx` | `Alert.Title` |
19
20
  | `alert-description.tsx` | `Alert.Description` |
@@ -66,7 +67,7 @@ plus `Alert.Indicator`, `Alert.Content`, `Alert.Title`, `Alert.Description`,
66
67
  - **Glyphs differ in shape, not just colour.** `warning` is a triangle and
67
68
  `destructive` a circle, so the two stay apart for anyone who cannot tell amber
68
69
  from red. `default` shares `info`'s circle. The map lives in
69
- `alert-indicator.tsx` because the glyphs are RN SVG components and
70
+ `alert-glyphs.ts`, a leaf `Toast` shares, because the glyphs are RN SVG components and
70
71
  `alert.variants.ts` must stay importable from `bun test`.
71
72
  - **The indicator is hidden from assistive technology.** The title says what
72
73
  happened; "image" announced before it adds nothing. Children replace the
@@ -0,0 +1,25 @@
1
+ import { IconCircleCheck, IconCircleInfo, IconExclamationCircle, IconExclamationTriangle } from "../../icons/central";
2
+ import type { IconComponent } from "../icon";
3
+ import type { AlertStatus } from "./alert.variants";
4
+
5
+ /**
6
+ * The glyph each status draws.
7
+ *
8
+ * `warning` and `destructive` differ in shape as well as colour — a triangle
9
+ * against a circle — so the two stay distinguishable to anyone who cannot tell
10
+ * amber from red. `default` shares `info`'s glyph: a neutral note is still a
11
+ * note, and a status of its own would need a meaning it does not have.
12
+ *
13
+ * A leaf of its own rather than a constant in `alert-indicator.tsx`: `Toast`
14
+ * draws the same glyph per status, so the two read as one family, and it may
15
+ * import a leaf across folders where it may not import a part (package rule 3).
16
+ * Not in `alert.variants.ts` because the glyphs are React Native SVG components,
17
+ * and that file must stay importable from `bun test`.
18
+ */
19
+ export const ALERT_GLYPHS: Record<AlertStatus, IconComponent> = {
20
+ default: IconCircleInfo,
21
+ info: IconCircleInfo,
22
+ success: IconCircleCheck,
23
+ warning: IconExclamationTriangle,
24
+ destructive: IconExclamationCircle,
25
+ };
@@ -1,28 +1,9 @@
1
1
  import type { ReactElement, ReactNode } from "react";
2
2
  import { View, type ViewProps } from "react-native";
3
- import { IconCircleCheck, IconCircleInfo, IconExclamationCircle, IconExclamationTriangle } from "../../icons/central";
4
- import { Icon, type IconComponent } from "../icon";
3
+ import { Icon } from "../icon";
5
4
  import { useAlertPart } from "./alert.context";
6
- import { type AlertStatus, alertVariants } from "./alert.variants";
7
-
8
- /**
9
- * The glyph each status draws.
10
- *
11
- * `warning` and `destructive` differ in shape as well as colour — a triangle
12
- * against a circle — so the two stay distinguishable to anyone who cannot tell
13
- * amber from red. `default` shares `info`'s glyph: a neutral note is still a
14
- * note, and a status of its own would need a meaning it does not have.
15
- *
16
- * Lives here rather than in `alert.variants.ts` because the glyphs are React
17
- * Native SVG components, and that file must stay importable from `bun test`.
18
- */
19
- const ALERT_GLYPHS: Record<AlertStatus, IconComponent> = {
20
- default: IconCircleInfo,
21
- info: IconCircleInfo,
22
- success: IconCircleCheck,
23
- warning: IconExclamationTriangle,
24
- destructive: IconExclamationCircle,
25
- };
5
+ import { alertVariants } from "./alert.variants";
6
+ import { ALERT_GLYPHS } from "./alert-glyphs";
26
7
 
27
8
  export type AlertIndicatorProps = ViewProps & {
28
9
  className?: string;
@@ -0,0 +1,140 @@
1
+ # Swipe
2
+
3
+ A row that slides aside to reveal actions behind it — delete, archive, snooze, mark done. Compound
4
+ root plus `Swipe.Start`, `Swipe.End`, `Swipe.Action` and `Swipe.Group`, with `useSwipe()` and
5
+ `useSwipeGroup()`. It acts on one row; for many rows at once, a selection mode is the pattern.
6
+
7
+ `import { Swipe } from "@delacour/react-native-ui/swipe";`
8
+
9
+ ## Files
10
+
11
+ | File | What it holds |
12
+ | --- | --- |
13
+ | `index.ts` | → `@delacour/react-native-ui/swipe` |
14
+ | `swipe.tsx` | Root — the partition, the pan, the release, the imperative handle and the accessibility actions |
15
+ | `swipe-start.tsx` | `Swipe.Start`, a marker that renders nothing |
16
+ | `swipe-end.tsx` | `Swipe.End`, a marker that renders nothing |
17
+ | `swipe-action.tsx` | `Swipe.Action`, one tile, positioned by its own animated style |
18
+ | `swipe-panel.tsx` | The layer behind one edge — internal, one per side that has tiles |
19
+ | `swipe-group.tsx` | `Swipe.Group`, the registry that keeps one row open |
20
+ | `swipe.context.tsx` | Row, tile and group contexts, `useSwipe()`, `useSwipeGroup()` |
21
+ | `swipe.types.ts` | `SwipePanelProps`, shared by the two markers |
22
+ | `swipe.variants.ts` | Pure `tv()` slots + the partition, release and tile-layout resolvers, no RN imports |
23
+ | `swipe.variants.test.ts` | |
24
+
25
+ ## Design
26
+
27
+ - **Colours**: `default`, `primary`, `success`, `warning`, `info`, `destructive` — Badge's set. A
28
+ tile is `bg-{color}` with its glyph and label in `{color}-foreground`; `default` is `bg-muted`
29
+ with `text-foreground`, since there is no `muted` surface ink stronger than that.
30
+ - **Tiles are 72pt, all of them.** A panel's width is a count times `SWIPE_TILE_WIDTH`, so nothing
31
+ is measured and the release can be judged on the UI thread the moment the finger lifts. The
32
+ `tileContent` slot spells the same number as `w-18`, and a test pins the pair.
33
+ - **The panel is exactly as wide as the gap the row has left.** Never wider, so it cannot paint
34
+ over a row that has no background of its own; never narrower, so no hole opens. The row slot
35
+ sets no `bg-*` and a test holds it to that — the two never overlap, so the row needs none.
36
+ - **Past the tiles' total the outermost tile grows, and the panel wears its colour.** An overshoot
37
+ then reads as *more of that action* rather than as an empty strip. The outermost is the last
38
+ tile on `end` and the first on `start` — `resolveOutermostIndex`.
39
+ - **Tiles emerge from under the row.** Short of fully open each is overlapped by the one outside
40
+ it, spread in proportion to the gap; they are whole and edge to edge exactly when the gap
41
+ reaches their total. `resolveTileLayout` is the rule, and a test sweeps it to prove the tiles
42
+ always cover the gap and never reach past it once open.
43
+ - **A tile's content box is pinned to its inner edge**, so when the outermost grows its glyph
44
+ stays beside its neighbours rather than drifting to the middle of a widening slab.
45
+
46
+ ## Anatomy
47
+
48
+ - **`Swipe.Start` and `Swipe.End` are markers, lifted out by element type.** Only the root knows
49
+ the gap, so only the root can lay the tiles out; a marker that rendered its own children would
50
+ need the offset passed back down to it anyway. Everything that is not a marker is the row, and
51
+ order does not matter.
52
+ - **The match is on `displayName`, not on function identity.** The package ships raw source, so
53
+ React Compiler or a second module instance can hand the root an element whose `type` is a
54
+ different object standing for the same component — the failure
55
+ [Switch](../switch/AGENTS.md)'s thumb detection records. `partitionSwipeChildren` reads only
56
+ `type.displayName` and `props.children`, which is also what lets the test feed it plain objects.
57
+ - **A tile has to be written directly inside its marker.** `Swipe.Action` reads a tile context the
58
+ root supplies as it lays each tile out, and throws by name if it is rendered anywhere else.
59
+ Wrapping actions in a custom component inside the marker therefore does not work — the wrapper
60
+ would be the tile. This is the trade the lift-out makes.
61
+ - **The icon is a component prop, and that bends the house rule on purpose.** Icons are composed
62
+ everywhere else, but a tile has to size its glyph to `size-icon-md` and tint it to its
63
+ foreground. It takes the same `IconComponent` type `Icon`'s `icon` prop takes and renders
64
+ `<Icon icon={…} />` inside an `IconDefaultsProvider`, so the glyph still flows through the one
65
+ icon path the package has.
66
+
67
+ ## Gesture
68
+
69
+ - **`activeOffsetX([-10, 10])` under `failOffsetY([-8, 8])`**, [Tabs](../tabs/AGENTS.md)' pairing.
70
+ The pan waits for clearly sideways intent and fails the moment the finger commits vertically, so
71
+ a list of rows scrolls normally. This is the opposite of [Slider](../slider/AGENTS.md)'s
72
+ `minDistance(0)`: a slider owns every touch on its track, a row owns only the sideways ones.
73
+ - **A side with no panel rubber-bands at 0.2 and returns.** The row still answers the finger, so
74
+ the touch does not feel dead, but it never promises an action that is not there.
75
+ - **The release is judged on a projection**: `offset + velocity × 0.1` against half the panel. A
76
+ projection that crosses the rest position closes rather than flinging the row open on the other
77
+ side.
78
+ - **A full swipe needs the finger itself past the point.** `|offset| > panel + 0.35 × row`, with
79
+ the projection still beyond it. A flick alone never fires the outermost action, because that
80
+ action is routinely Delete; and pulling back from past the point before letting go cancels it.
81
+ This is the one reading of "or a projected equivalent" that cannot delete by accident. The
82
+ haptic ticks once at exactly that crossing and re-arms if the finger goes back, so the person
83
+ letting go knows what will happen.
84
+ - **The fly-off fires the action when it lands, then brings the row back a beat later.** An action
85
+ that removes the row from state unmounts it during that beat, so it never visibly slides back
86
+ in; one that does not (Done, Archive into the same list) sees its row return closed.
87
+ - **Tapping the open row closes it and swallows the tap.** A transparent shield covers the row
88
+ only while a side is open, behind its own `Gesture.Tap()`, so the row's own `Pressable`s never
89
+ see that tap.
90
+ - **The release runs in `onFinalize`, gated on the pan having started.** `onFinalize` also fires
91
+ for a touch that never activated; without the gate a stray tap would re-judge, and stomp on, an
92
+ imperative `open()` still springing.
93
+ - **Nothing re-renders during a drag.** The offset, the grab, the full-swipe latch and the measured
94
+ width are shared values; React state changes only when a release settles a side.
95
+ - **Every worklet body is self-contained.** `resolveSwipeRelease`, `resolveSwipeDrag` and
96
+ `resolveTileLayout` are restated inside the gesture and the animated styles rather than called,
97
+ and the tests pin the pure versions. A module-scope worklet calling a sibling is the crash
98
+ [Slider](../slider/AGENTS.md) documents.
99
+
100
+ ## Group
101
+
102
+ - **Rows register through context, never by the group walking its children.** A row nested in a
103
+ `ListGroup`, a `.map()` or a custom component still belongs. The registry is a ref, so joining
104
+ or leaving it re-renders nothing.
105
+ - **The group closes only rows that are open.** A close aimed at a shut row would restart its
106
+ spring and cancel a drag already under way on it.
107
+ - **`useSwipeGroup()` outside a group is a no-op, not a throw**, so a screen can call
108
+ `closeAll()` whether or not its rows are grouped.
109
+
110
+ ## Motion
111
+
112
+ - **Under reduced motion every spring becomes a 160ms timing, and the fly-off becomes a fade.**
113
+ The row still follows the finger — direct manipulation is not decorative — and the fade keeps
114
+ the outcome visible rather than cutting.
115
+
116
+ ## RTL
117
+
118
+ - **`I18nManager.isRTL` flips one sign — the row's translation and the pan's reading of it.** The
119
+ offset is held in logical points (positive reveals `start`), and the panels and tiles are pinned
120
+ with Yoga's logical `start` / `end`, which mirror on their own. `end` is always the edge text
121
+ runs toward.
122
+
123
+ ## Accessibility
124
+
125
+ - **Every action is published on the row as an accessibility action**, named and labelled by its
126
+ `label`, and dispatched by name. A swipe is invisible to a screen reader, so without this the
127
+ actions would not exist for one.
128
+ - **The row is a single accessibility element** (`accessible`), the shape a list row with custom
129
+ actions takes; `accessibilityLabel` and `accessibilityHint` passed to `Swipe` land on it. A row
130
+ that needs its own separately focusable controls is not a swipe row.
131
+ - **Tiles are `button`s, hidden while the row is shut** (`accessibilityElementsHidden` and
132
+ `importantForAccessibility="no-hide-descendants"` on each panel), so a screen reader does not
133
+ walk into actions nobody can see.
134
+
135
+ ## Out of scope
136
+
137
+ - A controlled `openSide` prop. The imperative handle and `onOpenChange` cover the cases that came
138
+ up; a controlled pair can follow through `useControllableState` if one appears.
139
+ - Wrapped tiles (see Anatomy), per-tile widths, and vertical swipes.
140
+ - Undo. A demo keeps local state; a toast belongs to the overlays.
@@ -0,0 +1,31 @@
1
+ export { Swipe, type SwipeHandle, type SwipeProps } from "./swipe";
2
+ export {
3
+ type SwipeContextValue,
4
+ type SwipeGroupContextValue,
5
+ useSwipe,
6
+ useSwipeContext,
7
+ useSwipeGroup,
8
+ } from "./swipe.context";
9
+ export type { SwipePanelProps } from "./swipe.types";
10
+ export {
11
+ partitionSwipeChildren,
12
+ resolveOutermostIndex,
13
+ resolveSwipeActionForegroundToken,
14
+ resolveSwipeDrag,
15
+ resolveSwipeRelease,
16
+ resolveTileLayout,
17
+ SWIPE_ACTION_COLORS,
18
+ SWIPE_ACTION_FOREGROUND_TOKEN,
19
+ SWIPE_SIDES,
20
+ SWIPE_TILE_WIDTH,
21
+ type SwipeActionColor,
22
+ type SwipeChildren,
23
+ type SwipeOpenSide,
24
+ type SwipeRelease,
25
+ type SwipeSide,
26
+ type SwipeVariantProps,
27
+ swipeVariants,
28
+ toPhysicalOffset,
29
+ } from "./swipe.variants";
30
+ export type { SwipeActionProps } from "./swipe-action";
31
+ export type { SwipeGroupProps } from "./swipe-group";
@@ -0,0 +1,99 @@
1
+ import { type ReactElement, useCallback, useMemo } from "react";
2
+ import { View } from "react-native";
3
+ import Animated, { useAnimatedStyle } from "react-native-reanimated";
4
+ import { Icon, type IconComponent } from "../icon/icon";
5
+ import { IconDefaultsProvider } from "../icon/icon.context";
6
+ import { Pressable } from "../pressable/pressable";
7
+ import { Text } from "../text/text";
8
+ import { useSwipePart, useSwipeTilePart } from "./swipe.context";
9
+ import {
10
+ resolveSwipeActionForegroundToken,
11
+ SWIPE_TILE_WIDTH,
12
+ type SwipeActionColor,
13
+ swipeVariants,
14
+ } from "./swipe.variants";
15
+
16
+ export type SwipeActionProps = {
17
+ /** The tile's caption, and the name a screen reader offers the action under. Required. */
18
+ label: string;
19
+ /** A Central Icon component, drawn above the label at the tile's size and ink. */
20
+ icon?: IconComponent;
21
+ color?: SwipeActionColor;
22
+ onPress: () => void;
23
+ /** Leave the row open after the action runs — for a toggle such as a bookmark. */
24
+ isKeptOpen?: boolean;
25
+ className?: string;
26
+ labelClassName?: string;
27
+ /** Forwarded to the tile's pressable, for an end-to-end test or a capture flow to find it. */
28
+ testID?: string;
29
+ };
30
+
31
+ /**
32
+ * One tile behind the row.
33
+ *
34
+ * Positioned by its own animated style off the row's offset: short of fully
35
+ * open it is overlapped by the tile outside it, so the set emerges from under
36
+ * the row; past fully open the outermost tile grows into the overshoot. The
37
+ * worklet restates `resolveTileLayout`, which the tests pin.
38
+ *
39
+ * Takes the icon as a **component** rather than composed children, because the
40
+ * tile has to size and tint the glyph itself; it renders it through
41
+ * `IconDefaultsProvider`, the same inheritance a `Button` uses.
42
+ */
43
+ export function SwipeAction({
44
+ label,
45
+ icon,
46
+ color = "default",
47
+ onPress,
48
+ isKeptOpen = false,
49
+ className,
50
+ labelClassName,
51
+ testID,
52
+ }: SwipeActionProps): ReactElement {
53
+ const swipe = useSwipePart("Swipe.Action");
54
+ const { side, index, count } = useSwipeTilePart("Swipe.Action");
55
+ const { offset, runAction } = swipe;
56
+ const slots = swipeVariants({ color, side });
57
+ const order = side === "end" ? index : count - 1 - index;
58
+
59
+ const tileStyle = useAnimatedStyle(() => {
60
+ const tileWidth = SWIPE_TILE_WIDTH;
61
+ const reveal = Math.max(0, side === "start" ? offset.value : -offset.value);
62
+ const total = count * tileWidth;
63
+ const isPacked = reveal <= total;
64
+ const x = isPacked ? (count > 0 ? (order * reveal) / count : 0) : order * tileWidth;
65
+ const width = !isPacked && order === count - 1 ? reveal - order * tileWidth : tileWidth;
66
+ return side === "end" ? { start: x, width } : { end: x, width };
67
+ });
68
+
69
+ const handlePress = useCallback(() => runAction(onPress, isKeptOpen), [isKeptOpen, onPress, runAction]);
70
+ const iconDefaults = useMemo(
71
+ () => ({ className: "size-icon-md", color: resolveSwipeActionForegroundToken(color) }),
72
+ [color]
73
+ );
74
+
75
+ return (
76
+ <Animated.View className={slots.tile({ className })} style={[{ zIndex: order }, tileStyle]}>
77
+ <Pressable
78
+ accessibilityLabel={label}
79
+ accessibilityRole="button"
80
+ className={slots.tilePressable()}
81
+ feedback="fade"
82
+ onPress={handlePress}
83
+ testID={testID}
84
+ >
85
+ <View className={slots.tileContent()}>
86
+ {icon ? (
87
+ <IconDefaultsProvider value={iconDefaults}>
88
+ <Icon icon={icon} />
89
+ </IconDefaultsProvider>
90
+ ) : null}
91
+ <Text className={slots.tileLabel({ className: labelClassName })} numberOfLines={1}>
92
+ {label}
93
+ </Text>
94
+ </View>
95
+ </Pressable>
96
+ </Animated.View>
97
+ );
98
+ }
99
+ SwipeAction.displayName = "DelacourUI.Swipe.Action";
@@ -0,0 +1,14 @@
1
+ import type { ReactElement } from "react";
2
+ import type { SwipePanelProps } from "./swipe.types";
3
+
4
+ /**
5
+ * Marks the tiles behind the row's end edge — the edge text runs toward —
6
+ * revealed by sliding the row toward `start`.
7
+ *
8
+ * A marker, never rendered: the root lifts its children out by element type and
9
+ * lays them out itself. On its own it renders nothing.
10
+ */
11
+ export function SwipeEnd(_props: SwipePanelProps): ReactElement | null {
12
+ return null;
13
+ }
14
+ SwipeEnd.displayName = "DelacourUI.Swipe.End";
@@ -0,0 +1,55 @@
1
+ import { type ReactElement, useCallback, useMemo, useRef } from "react";
2
+ import { View, type ViewProps } from "react-native";
3
+ import { cn } from "../../lib/cn";
4
+ import { type SwipeGroupContextValue, SwipeGroupProvider } from "./swipe.context";
5
+
6
+ export type SwipeGroupProps = ViewProps & {
7
+ className?: string;
8
+ /** One row open at a time: opening one closes the rest. Default `true`. */
9
+ isExclusive?: boolean;
10
+ };
11
+
12
+ /**
13
+ * Coordinates the rows beneath it. Renders a plain `View`.
14
+ *
15
+ * **Rows register themselves through context**, rather than the group walking
16
+ * its children, so a row nested in a `ListGroup`, a `.map()` or a custom
17
+ * component still belongs. The registry is a ref — joining or leaving it
18
+ * re-renders nothing.
19
+ */
20
+ export function SwipeGroup({ className, isExclusive = true, children, ...props }: SwipeGroupProps): ReactElement {
21
+ const rows = useRef(new Map<string, () => void>());
22
+
23
+ const register = useCallback((id: string, close: () => void) => {
24
+ rows.current.set(id, close);
25
+ return () => {
26
+ if (rows.current.get(id) === close) rows.current.delete(id);
27
+ };
28
+ }, []);
29
+
30
+ const notifyOpen = useCallback(
31
+ (id: string) => {
32
+ if (!isExclusive) return;
33
+ for (const [other, close] of rows.current) if (other !== id) close();
34
+ },
35
+ [isExclusive]
36
+ );
37
+
38
+ const closeAll = useCallback(() => {
39
+ for (const close of rows.current.values()) close();
40
+ }, []);
41
+
42
+ const value = useMemo<SwipeGroupContextValue>(
43
+ () => ({ closeAll, notifyOpen, register }),
44
+ [closeAll, notifyOpen, register]
45
+ );
46
+
47
+ return (
48
+ <SwipeGroupProvider value={value}>
49
+ <View className={cn(className)} {...props}>
50
+ {children}
51
+ </View>
52
+ </SwipeGroupProvider>
53
+ );
54
+ }
55
+ SwipeGroup.displayName = "DelacourUI.Swipe.Group";
@@ -0,0 +1,55 @@
1
+ import type { ReactElement } from "react";
2
+ import Animated, { useAnimatedStyle } from "react-native-reanimated";
3
+ import { SwipeTileProvider, useSwipePart } from "./swipe.context";
4
+ import { resolveOutermostIndex, type SwipeSide, type SwipeTileProps, swipeVariants } from "./swipe.variants";
5
+
6
+ type SwipePanelLayerProps = {
7
+ side: SwipeSide;
8
+ /** The `Swipe.Action` elements lifted out of the side's marker. */
9
+ tiles: ReactElement<SwipeTileProps>[];
10
+ /** Hidden from assistive technology while the row is shut. */
11
+ isHidden: boolean;
12
+ };
13
+
14
+ const START_EDGE = { start: 0 } as const;
15
+ const END_EDGE = { end: 0 } as const;
16
+
17
+ /**
18
+ * The layer behind one edge of the row. Internal: the root renders one per side
19
+ * that has tiles.
20
+ *
21
+ * **Exactly as wide as the gap the row has left**, animated off the same offset
22
+ * the row moves on — never wider, so it cannot paint over a row that has no
23
+ * background of its own, and never narrower, so there is no hole. Its own
24
+ * colour is the outermost tile's, so the overshoot that tile grows into reads
25
+ * as the same action.
26
+ *
27
+ * Pinned by logical `start` / `end` rather than `left` / `right`, so the layout
28
+ * mirrors under right to left with no sign of its own; only the row's
29
+ * translation has to flip.
30
+ */
31
+ export function SwipePanelLayer({ side, tiles, isHidden }: SwipePanelLayerProps): ReactElement {
32
+ const { offset } = useSwipePart("Swipe.Panel");
33
+ const outermost = tiles[resolveOutermostIndex(side, tiles.length)];
34
+ const slots = swipeVariants({ color: outermost?.props.color ?? "default", side });
35
+
36
+ const panelStyle = useAnimatedStyle(() => ({
37
+ width: Math.max(0, side === "start" ? offset.value : -offset.value),
38
+ }));
39
+
40
+ return (
41
+ <Animated.View
42
+ accessibilityElementsHidden={isHidden}
43
+ className={slots.panel()}
44
+ importantForAccessibility={isHidden ? "no-hide-descendants" : "auto"}
45
+ style={[side === "start" ? START_EDGE : END_EDGE, panelStyle]}
46
+ >
47
+ {tiles.map((tile, index) => (
48
+ <SwipeTileProvider key={tile.key ?? index} value={{ count: tiles.length, index, side }}>
49
+ {tile}
50
+ </SwipeTileProvider>
51
+ ))}
52
+ </Animated.View>
53
+ );
54
+ }
55
+ SwipePanelLayer.displayName = "DelacourUI.Swipe.Panel";
@@ -0,0 +1,15 @@
1
+ import type { ReactElement } from "react";
2
+ import type { SwipePanelProps } from "./swipe.types";
3
+
4
+ /**
5
+ * Marks the tiles behind the row's start edge — revealed by sliding the row
6
+ * toward `end`.
7
+ *
8
+ * A marker, never rendered: the root lifts its children out by element type and
9
+ * lays them out itself, because only the root knows how wide the gap is. On its
10
+ * own it renders nothing.
11
+ */
12
+ export function SwipeStart(_props: SwipePanelProps): ReactElement | null {
13
+ return null;
14
+ }
15
+ SwipeStart.displayName = "DelacourUI.Swipe.Start";
@@ -0,0 +1,135 @@
1
+ import { createContext, type ReactElement, type ReactNode, use } from "react";
2
+ import type { SharedValue } from "react-native-reanimated";
3
+ import type { SwipeOpenSide, SwipeSide } from "./swipe.variants";
4
+
5
+ export type SwipeContextValue = {
6
+ /** The side that is settled open, or null. Changes on a settle, never during a drag. */
7
+ openSide: SwipeOpenSide;
8
+ /** Slides the row aside to reveal `side`. A side with no panel stays shut. */
9
+ open: (side: SwipeSide) => void;
10
+ /** Slides the row back over its panels. */
11
+ close: () => void;
12
+ /** The row's logical offset — positive reveals `start`. Internal to the parts. */
13
+ offset: SharedValue<number>;
14
+ /** Whether the layout runs right to left, read once from `I18nManager`. */
15
+ isRTL: boolean;
16
+ /** Runs a tile's action, then closes the row unless it asks to stay open. */
17
+ runAction: (onPress: () => void, isKeptOpen: boolean) => void;
18
+ };
19
+
20
+ const SwipeContext = createContext<SwipeContextValue | null>(null);
21
+
22
+ /**
23
+ * Supplies a row's state to its tiles.
24
+ *
25
+ * In a leaf of its own, importing nothing but types, so a part can read it
26
+ * without importing `./swipe` and closing a cycle (package AGENTS.md rule 3).
27
+ */
28
+ export function SwipeProvider({ value, children }: { value: SwipeContextValue; children: ReactNode }): ReactElement {
29
+ return <SwipeContext value={value}>{children}</SwipeContext>;
30
+ }
31
+ SwipeProvider.displayName = "DelacourUI.Swipe.Provider";
32
+
33
+ /** The enclosing row, or null outside a `<Swipe>`. */
34
+ export function useSwipeContext(): SwipeContextValue | null {
35
+ return use(SwipeContext);
36
+ }
37
+
38
+ /**
39
+ * Reads the enclosing row: which side is open, and `open` / `close`.
40
+ *
41
+ * For a custom child of the row that has to close it — a button inside the row
42
+ * that acts and then tucks the actions away. Throws outside a `<Swipe>`.
43
+ */
44
+ export function useSwipe(): SwipeContextValue {
45
+ const context = useSwipeContext();
46
+ if (!context) throw new Error("useSwipe must be called inside a <Swipe>.");
47
+ return context;
48
+ }
49
+
50
+ /**
51
+ * The enclosing row, for a part that cannot work without it.
52
+ *
53
+ * Internal: deliberately not re-exported from `index.ts`.
54
+ */
55
+ export function useSwipePart(component: string): SwipeContextValue {
56
+ const context = useSwipeContext();
57
+ if (!context) throw new Error(`${component} must be rendered inside a <Swipe>.`);
58
+ return context;
59
+ }
60
+
61
+ export type SwipeTileContextValue = {
62
+ side: SwipeSide;
63
+ /** Source order within its panel. */
64
+ index: number;
65
+ /** How many tiles share the panel. */
66
+ count: number;
67
+ };
68
+
69
+ const SwipeTileContext = createContext<SwipeTileContextValue | null>(null);
70
+
71
+ /** Where a tile sits in its panel. The root wraps every tile it lifts out in one. */
72
+ export function SwipeTileProvider({
73
+ value,
74
+ children,
75
+ }: {
76
+ value: SwipeTileContextValue;
77
+ children: ReactNode;
78
+ }): ReactElement {
79
+ return <SwipeTileContext value={value}>{children}</SwipeTileContext>;
80
+ }
81
+ SwipeTileProvider.displayName = "DelacourUI.Swipe.TileProvider";
82
+
83
+ /**
84
+ * A tile's place in its panel. Internal.
85
+ *
86
+ * Throws when a `Swipe.Action` was rendered anywhere but inside `Swipe.Start`
87
+ * or `Swipe.End` — the root lifts tiles out of those two markers only.
88
+ */
89
+ export function useSwipeTilePart(component: string): SwipeTileContextValue {
90
+ const context = use(SwipeTileContext);
91
+ if (!context) throw new Error(`${component} must be written inside <Swipe.Start> or <Swipe.End>.`);
92
+ return context;
93
+ }
94
+
95
+ export type SwipeGroupContextValue = {
96
+ /** Adds a row's close to the group. Returns the matching unregister. */
97
+ register: (id: string, close: () => void) => () => void;
98
+ /** Tells the group `id` has opened, so an exclusive group closes the rest. */
99
+ notifyOpen: (id: string) => void;
100
+ /** Closes every row in the group. */
101
+ closeAll: () => void;
102
+ };
103
+
104
+ const SwipeGroupContext = createContext<SwipeGroupContextValue | null>(null);
105
+
106
+ /** Supplies a group's registry to the rows beneath it, however deeply they are nested. */
107
+ export function SwipeGroupProvider({
108
+ value,
109
+ children,
110
+ }: {
111
+ value: SwipeGroupContextValue;
112
+ children: ReactNode;
113
+ }): ReactElement {
114
+ return <SwipeGroupContext value={value}>{children}</SwipeGroupContext>;
115
+ }
116
+ SwipeGroupProvider.displayName = "DelacourUI.Swipe.Group.Provider";
117
+
118
+ /** The enclosing group, or null for a row standing on its own. */
119
+ export function useSwipeGroupContext(): SwipeGroupContextValue | null {
120
+ return use(SwipeGroupContext);
121
+ }
122
+
123
+ const NO_GROUP = { closeAll: () => {} } as const;
124
+
125
+ /**
126
+ * Closes every row in the enclosing `Swipe.Group` — before a list scrolls away,
127
+ * or when a screen loses focus.
128
+ *
129
+ * Outside a group it returns a no-op rather than throwing, so a screen can call
130
+ * it whether or not its rows are grouped.
131
+ */
132
+ export function useSwipeGroup(): { closeAll: () => void } {
133
+ const group = useSwipeGroupContext();
134
+ return group ? { closeAll: group.closeAll } : NO_GROUP;
135
+ }