@oxyhq/bloom 0.79.1 → 0.80.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 (52) hide show
  1. package/lib/commonjs/scroll/index.web.js +52 -18
  2. package/lib/commonjs/scroll/index.web.js.map +1 -1
  3. package/lib/commonjs/scroll/scrollable.web.js +9 -3
  4. package/lib/commonjs/scroll/scrollable.web.js.map +1 -1
  5. package/lib/commonjs/tabs/Tabs.js +263 -63
  6. package/lib/commonjs/tabs/Tabs.js.map +1 -1
  7. package/lib/commonjs/tabs/expo-router/RouterTabs.js +215 -0
  8. package/lib/commonjs/tabs/expo-router/RouterTabs.js.map +1 -0
  9. package/lib/commonjs/tabs/expo-router/index.js +13 -0
  10. package/lib/commonjs/tabs/expo-router/index.js.map +1 -0
  11. package/lib/module/scroll/index.web.js +52 -18
  12. package/lib/module/scroll/index.web.js.map +1 -1
  13. package/lib/module/scroll/scrollable.web.js +9 -3
  14. package/lib/module/scroll/scrollable.web.js.map +1 -1
  15. package/lib/module/tabs/Tabs.js +271 -65
  16. package/lib/module/tabs/Tabs.js.map +1 -1
  17. package/lib/module/tabs/expo-router/RouterTabs.js +210 -0
  18. package/lib/module/tabs/expo-router/RouterTabs.js.map +1 -0
  19. package/lib/module/tabs/expo-router/index.js +10 -0
  20. package/lib/module/tabs/expo-router/index.js.map +1 -0
  21. package/lib/typescript/commonjs/scroll/index.web.d.ts.map +1 -1
  22. package/lib/typescript/commonjs/scroll/scrollable.web.d.ts +18 -5
  23. package/lib/typescript/commonjs/scroll/scrollable.web.d.ts.map +1 -1
  24. package/lib/typescript/commonjs/tabs/Tabs.d.ts +41 -1
  25. package/lib/typescript/commonjs/tabs/Tabs.d.ts.map +1 -1
  26. package/lib/typescript/commonjs/tabs/expo-router/RouterTabs.d.ts +80 -0
  27. package/lib/typescript/commonjs/tabs/expo-router/RouterTabs.d.ts.map +1 -0
  28. package/lib/typescript/commonjs/tabs/expo-router/index.d.ts +9 -0
  29. package/lib/typescript/commonjs/tabs/expo-router/index.d.ts.map +1 -0
  30. package/lib/typescript/commonjs/tabs/types.d.ts +48 -4
  31. package/lib/typescript/commonjs/tabs/types.d.ts.map +1 -1
  32. package/lib/typescript/module/scroll/index.web.d.ts.map +1 -1
  33. package/lib/typescript/module/scroll/scrollable.web.d.ts +18 -5
  34. package/lib/typescript/module/scroll/scrollable.web.d.ts.map +1 -1
  35. package/lib/typescript/module/tabs/Tabs.d.ts +41 -1
  36. package/lib/typescript/module/tabs/Tabs.d.ts.map +1 -1
  37. package/lib/typescript/module/tabs/expo-router/RouterTabs.d.ts +80 -0
  38. package/lib/typescript/module/tabs/expo-router/RouterTabs.d.ts.map +1 -0
  39. package/lib/typescript/module/tabs/expo-router/index.d.ts +9 -0
  40. package/lib/typescript/module/tabs/expo-router/index.d.ts.map +1 -0
  41. package/lib/typescript/module/tabs/types.d.ts +48 -4
  42. package/lib/typescript/module/tabs/types.d.ts.map +1 -1
  43. package/package.json +15 -1
  44. package/src/__tests__/Tabs.test.tsx +187 -0
  45. package/src/__tests__/optional-peer-imports.test.ts +5 -0
  46. package/src/__tests__/scroll-web.test.tsx +255 -2
  47. package/src/scroll/index.web.tsx +55 -19
  48. package/src/scroll/scrollable.web.ts +30 -8
  49. package/src/tabs/Tabs.tsx +352 -77
  50. package/src/tabs/expo-router/RouterTabs.tsx +268 -0
  51. package/src/tabs/expo-router/index.ts +8 -0
  52. package/src/tabs/types.ts +48 -4
@@ -0,0 +1,268 @@
1
+ /**
2
+ * expo-router bindings for the in-screen tab strip.
3
+ *
4
+ * This subpath (`@oxyhq/bloom/tabs/expo-router`) is the ONLY place in the tabs
5
+ * feature allowed to import `expo-router`, which is why it is a separate entry:
6
+ * the core `@oxyhq/bloom/tabs` stays usable in an app with a different router,
7
+ * or none, and the optional peer is never pulled into a bundle that does not
8
+ * ask for it. Same split as `tab-bar` / `tab-bar/expo-router` and
9
+ * `scroll` / `scroll/expo-router`.
10
+ *
11
+ * Everything routing-shaped lives here on purpose: which tab the route means,
12
+ * what a committed swipe navigates to, and which links to warm. The strip
13
+ * itself stays presentational and knows only about measured geometry.
14
+ */
15
+ import { useCallback, useEffect, useMemo, useRef, type ReactNode } from 'react';
16
+ import { Gesture, GestureDetector } from 'react-native-gesture-handler';
17
+ import { runOnJS } from 'react-native-reanimated';
18
+ import { router, usePathname, type Href } from 'expo-router';
19
+
20
+ import { Tabs, TabsTrigger, type TabsDragController } from '../Tabs';
21
+ import type { TabsProps } from '../types';
22
+
23
+ export interface RouterTabItem {
24
+ /** Stable id for the tab. Used as a React key and a callback argument. */
25
+ value: string;
26
+ /** Tab label text. */
27
+ label: string;
28
+ /** The route this tab shows. Must be the tab's EXACT path. */
29
+ href: Href;
30
+ /** Icon rendered before the label. */
31
+ icon?: ReactNode;
32
+ /** Optional tally beside the label; `0` or omitted renders nothing. */
33
+ count?: number;
34
+ disabled?: boolean;
35
+ }
36
+
37
+ export type RouterTabsProps = Omit<
38
+ TabsProps,
39
+ // All three describe a selection the ROUTE already answers, so accepting them
40
+ // here would let a caller contradict the router.
41
+ 'value' | 'onValueChange' | 'hasSelection' | 'children'
42
+ > & {
43
+ items: RouterTabItem[];
44
+ /**
45
+ * Called instead of navigating when the ALREADY-active tab is pressed.
46
+ *
47
+ * Without it a re-tap would push a second history entry for the route the
48
+ * user is already on, so Back would appear to do nothing. Re-tapping the
49
+ * active tab conventionally means "refresh" or "scroll to top", and that is
50
+ * the caller's to define.
51
+ */
52
+ onReselect?: (value: string) => void;
53
+ /**
54
+ * Enable the horizontal swipe between neighbouring tabs. Defaults to `true`.
55
+ *
56
+ * The gesture is INDICATOR-LED: the underline tracks the finger toward the
57
+ * neighbour and commits on release past a threshold, while the content stays
58
+ * where it is. That asymmetry is deliberate — the underline has somewhere
59
+ * real to go and can complete its journey, whereas under a `<Slot/>` there is
60
+ * no neighbouring screen mounted to bring in, so translating the content
61
+ * would only open a blank gutter on the trailing edge.
62
+ */
63
+ swipeEnabled?: boolean;
64
+ };
65
+
66
+ /**
67
+ * Resolve which item the current route belongs to.
68
+ *
69
+ * LONGEST matching prefix, on a segment boundary — not an exact match. All
70
+ * three properties matter:
71
+ *
72
+ * - A prefix is needed because a tab may own a subtree (`/feeds/1/topics` and
73
+ * anything under it), and an exact match would drop the underline the moment
74
+ * the user goes one level deeper.
75
+ * - LONGEST is needed because the default tab's href is a prefix of every
76
+ * sibling's (`/@ana` vs `/@ana/replies`), so a first-match rule would light
77
+ * up the default tab on every tab.
78
+ * - The segment boundary is what stops `/feeds/12` from matching `/feeds/1`.
79
+ *
80
+ * Returns `undefined` when nothing matches, which is a real state — a strip
81
+ * rendered by a LAYOUT keeps rendering while a child route outside the tab set
82
+ * is showing — and leaves the underline hidden rather than parked on a lie.
83
+ */
84
+ function matchActiveValue(items: RouterTabItem[], pathname: string): string | undefined {
85
+ let best: { value: string; length: number } | undefined;
86
+ for (const item of items) {
87
+ // `Href` is a string or an object; only a string form can be compared to a
88
+ // pathname, and an object form describes params we cannot resolve here.
89
+ if (typeof item.href !== 'string') continue;
90
+ // Compare paths only: a query string describes what the tab shows, not
91
+ // which tab it is, and a trailing slash is the same route either way.
92
+ const [path = ''] = item.href.split('?');
93
+ const href = path.replace(/\/$/, '');
94
+ const isMatch = pathname === href || pathname.startsWith(`${href}/`);
95
+ if (!isMatch) continue;
96
+ if (!best || href.length > best.length) {
97
+ best = { value: item.value, length: href.length };
98
+ }
99
+ }
100
+ return best?.value;
101
+ }
102
+
103
+ /**
104
+ * A tab strip whose selection IS the route.
105
+ *
106
+ * Render it in the LAYOUT that owns the tabs, above the `<Slot/>` that renders
107
+ * them — not inside the tab screens themselves. That placement is the whole
108
+ * point, and it is load-bearing twice over: a strip inside the screens is
109
+ * unmounted and rebuilt by every navigation, so its underline has nothing to
110
+ * animate from and the chrome blanks while the incoming route's chunk loads;
111
+ * and a GESTURE inside the screens would be destroyed by the very navigation it
112
+ * commits, mid-release.
113
+ *
114
+ * ```tsx
115
+ * // app/(app)/[username]/_layout.tsx
116
+ * <RouterTabs
117
+ * items={[
118
+ * { value: 'posts', label: 'Posts', href: `/${username}` },
119
+ * { value: 'replies', label: 'Replies', href: `/${username}/replies` },
120
+ * ]}
121
+ * />
122
+ * <Slot />
123
+ * ```
124
+ */
125
+ export function RouterTabs({
126
+ items,
127
+ onReselect,
128
+ swipeEnabled = true,
129
+ ...tabsProps
130
+ }: RouterTabsProps) {
131
+ const pathname = usePathname();
132
+ const activeValue = useMemo(() => matchActiveValue(items, pathname), [items, pathname]);
133
+ const dragRef = useRef<TabsDragController>(null);
134
+ // What releasing right now would commit. A ref rather than state: it is
135
+ // written on every frame of the drag and read once, and re-rendering the
136
+ // strip mid-gesture would fight the animation the gesture is driving.
137
+ const pendingRef = useRef<string | null>(null);
138
+
139
+ const navigateTo = useCallback(
140
+ (value: string) => {
141
+ const target = items.find((item) => item.value === value);
142
+ if (!target) return;
143
+ // PUSH, not replace: a history entry per tab is what makes Back return to
144
+ // the tab the reader came from instead of leaving the screen entirely.
145
+ router.push(target.href);
146
+ },
147
+ [items],
148
+ );
149
+
150
+ const handlePress = useCallback(
151
+ (item: RouterTabItem) => {
152
+ if (item.value === activeValue) {
153
+ onReselect?.(item.value);
154
+ return;
155
+ }
156
+ navigateTo(item.value);
157
+ },
158
+ [activeValue, onReselect, navigateTo],
159
+ );
160
+
161
+ const onDrag = useCallback((translationX: number) => {
162
+ pendingRef.current = dragRef.current?.drag(translationX) ?? null;
163
+ }, []);
164
+
165
+ // `onEnd` does not fire for every terminated gesture, and `onFinalize` fires
166
+ // for all of them — including after `onEnd`. Guarding on the ref being
167
+ // non-null is what makes the second call a no-op instead of a double commit.
168
+ const releasedRef = useRef(true);
169
+ const onRelease = useCallback(() => {
170
+ if (releasedRef.current) return;
171
+ releasedRef.current = true;
172
+ const committed = pendingRef.current;
173
+ pendingRef.current = null;
174
+ // The underline commits first and instantly: it lives in the layout, it
175
+ // survives the swap, and it is the honest signal the gesture was accepted.
176
+ // The content area arrives when its chunk does.
177
+ dragRef.current?.release(committed);
178
+ if (committed !== null) navigateTo(committed);
179
+ }, [navigateTo]);
180
+
181
+ const onBegin = useCallback(() => {
182
+ releasedRef.current = false;
183
+ }, []);
184
+
185
+ const panGesture = useMemo(
186
+ () =>
187
+ Gesture.Pan()
188
+ // Horizontal intent only. This is a hint to the gesture system and is
189
+ // NOT what protects vertical scrolling on web — see `touchAction` below.
190
+ .activeOffsetX([-12, 12])
191
+ .failOffsetY([-12, 12])
192
+ .onBegin(() => {
193
+ runOnJS(onBegin)();
194
+ })
195
+ .onUpdate((event) => {
196
+ runOnJS(onDrag)(event.translationX);
197
+ })
198
+ .onFinalize(() => {
199
+ runOnJS(onRelease)();
200
+ }),
201
+ [onBegin, onDrag, onRelease],
202
+ );
203
+
204
+ // Warm every OTHER tab's async chunk. That fetch is the ~300ms the content
205
+ // area would otherwise spend empty after a commit, and a swipe offers no
206
+ // hover to warm on, so it is done up front rather than on interaction.
207
+ //
208
+ // `router.prefetch` — the public imperative API (declared in expo-router's
209
+ // `global-state/router.d.ts`) — rather than wrapping each trigger in
210
+ // `<Link prefetch asChild>`. Both reasons are about the WRAPPER, not about
211
+ // prefetching: `asChild` is Radix's `Slot`, which COMPOSES event handlers, so
212
+ // the trigger's own `onPress` and the link's navigation would both run — and
213
+ // the link's push knows nothing about `onReselect`, so re-tapping the active
214
+ // tab would push a duplicate entry and make Back appear to do nothing. It
215
+ // also injects an `href` a `Pressable` ignores. The prefetching itself is
216
+ // identical: `<Link prefetch>` renders `<Prefetch href/>` beside its element
217
+ // and does nothing else.
218
+ //
219
+ // Known trade: a real `<Link>` would render a web anchor, which is what gives
220
+ // middle-click and open-in-new-tab. A tab strip arguably wants that; getting
221
+ // both needs the trigger to render an anchor itself, which is a change to the
222
+ // core rather than to this file.
223
+ useEffect(() => {
224
+ for (const item of items) {
225
+ if (item.value === activeValue || item.disabled) continue;
226
+ router.prefetch(item.href);
227
+ }
228
+ }, [items, activeValue]);
229
+
230
+ const strip = (
231
+ // `hasSelection` is what stops the underline asserting a tab while a
232
+ // NON-TAB sibling route is showing — the layout that renders this strip
233
+ // renders those too (a profile's `/followers` beside its tab routes).
234
+ <Tabs {...tabsProps} ref={dragRef} hasSelection={activeValue !== undefined}>
235
+ {items.map((item) => (
236
+ <TabsTrigger
237
+ key={item.value}
238
+ value={item.value}
239
+ label={item.label}
240
+ icon={item.icon}
241
+ count={item.count}
242
+ disabled={item.disabled}
243
+ // Focus, never a press, is what drives the underline — so a deep
244
+ // link, a browser Back and a back gesture animate exactly like a tap.
245
+ isFocused={item.value === activeValue}
246
+ onPress={() => handlePress(item)}
247
+ />
248
+ ))}
249
+ </Tabs>
250
+ );
251
+
252
+ if (!swipeEnabled) return strip;
253
+
254
+ return (
255
+ // `touchAction="pan-y"` is MANDATORY and is the landmine of this file.
256
+ // RNGH sets the detector's view to `touch-action: none` on web
257
+ // (`GestureHandlerWebDelegate.js`), which disables browser-native VERTICAL
258
+ // scrolling on that element. `failOffsetY` above does not protect against
259
+ // it: that is a JS decision taken after `touch-action` has already told the
260
+ // browser not to scroll. Removing this prop is silent on native and breaks
261
+ // scrolling on mobile web.
262
+ <GestureDetector gesture={panGesture} touchAction="pan-y">
263
+ {strip}
264
+ </GestureDetector>
265
+ );
266
+ }
267
+
268
+ RouterTabs.displayName = 'RouterTabs';
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Entry for `@oxyhq/bloom/tabs/expo-router`. The component lives in a sibling
3
+ * module because this file is the published entry path and must stay stable;
4
+ * the split also keeps the router binding in one place, which is what the
5
+ * optional-peer allowlist asserts about.
6
+ */
7
+ export { RouterTabs } from './RouterTabs';
8
+ export type { RouterTabItem, RouterTabsProps } from './RouterTabs';
package/src/tabs/types.ts CHANGED
@@ -3,10 +3,38 @@ import type { StyleProp, ViewStyle, TextStyle } from 'react-native';
3
3
  export type TabsVariant = 'underline' | 'filled' | 'outlined';
4
4
 
5
5
  export interface TabsProps {
6
- /** The currently selected tab value. */
7
- value: string;
8
- /** Called when a tab is selected. */
9
- onValueChange: (value: string) => void;
6
+ /**
7
+ * The currently selected tab value — the CONTROLLED path, where the bar owns
8
+ * the underline and reports presses through {@link TabsProps.onValueChange}.
9
+ *
10
+ * OMIT it for the focus-driven path, where each trigger supplies its own
11
+ * {@link TabsTriggerProps.isFocused} and performs its own navigation. That is
12
+ * how `@oxyhq/bloom/tabs/expo-router` keeps the underline correct through
13
+ * deep links, browser Back and back gestures — none of which pass through a
14
+ * press handler. Providing both would put two writers on one shared value.
15
+ */
16
+ value?: string;
17
+ /** Called when a tab is selected. Controlled path only. */
18
+ onValueChange?: (value: string) => void;
19
+ /**
20
+ * Focus-driven path only: whether ANY trigger is currently focused.
21
+ *
22
+ * `false` fades the underline out where it stands. This is not the same as
23
+ * "the selection moved", and it is not hypothetical: a strip rendered by a
24
+ * LAYOUT keeps rendering while the layout's other, non-tab children are
25
+ * showing — a profile's `/followers` sits beside its tab routes under one
26
+ * layout — and without this the underline stays parked on whichever tab was
27
+ * last visited, asserting a selection that is not there.
28
+ *
29
+ * Fading is the whole response; the underline is deliberately NOT moved,
30
+ * because there is no position that means "nowhere" and travelling to a
31
+ * sentinel would drag it across every tab on the way out.
32
+ *
33
+ * `RouterTabs` sets it, so an app author never passes it by hand. Defaults to
34
+ * `true`, which is correct for the controlled path (a `value` naming no
35
+ * trigger simply leaves the underline where it was, as it always has).
36
+ */
37
+ hasSelection?: boolean;
10
38
  /** Visual variant for the tab bar. */
11
39
  variant?: TabsVariant;
12
40
  /**
@@ -29,8 +57,24 @@ export interface TabsTriggerProps {
29
57
  label: string;
30
58
  /** Icon rendered before the label. */
31
59
  icon?: React.ReactNode;
60
+ /**
61
+ * Optional tally rendered as a small muted number beside the label. Omitted
62
+ * (or `0`) renders nothing. The underline tracks the trigger's measured
63
+ * width, so a count never changes where it sits.
64
+ */
65
+ count?: number;
66
+ /**
67
+ * Whether the ROUTER considers this tab focused — the focus-driven path.
68
+ * Supplying it makes this trigger, not the bar, the writer of the shared
69
+ * underline, and suppresses {@link TabsProps.onValueChange} on press because
70
+ * navigation is then the caller's job. Leave it `undefined` on the controlled
71
+ * path.
72
+ */
73
+ isFocused?: boolean;
32
74
  /** Whether this tab is disabled. */
33
75
  disabled?: boolean;
76
+ /** Extra press handler. Runs on both paths, before any selection is reported. */
77
+ onPress?: () => void;
34
78
  style?: StyleProp<ViewStyle>;
35
79
  textStyle?: StyleProp<TextStyle>;
36
80
  }