@oxy.so/bloom 7.19.1 → 7.20.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.
- package/docs/adoption-matrix.mdx +10 -9
- package/docs/dialog.mdx +2 -0
- package/docs/popover.mdx +2 -0
- package/docs/rating.mdx +2 -0
- package/docs/stat-bar.mdx +10 -0
- package/docs/viewport.mdx +47 -0
- package/lib/commonjs/dialog/Dialog.js +5 -3
- package/lib/commonjs/dialog/Dialog.js.map +1 -1
- package/lib/commonjs/dialog/Dialog.web.js +5 -3
- package/lib/commonjs/dialog/Dialog.web.js.map +1 -1
- package/lib/commonjs/dialog/placement.js +8 -1
- package/lib/commonjs/dialog/placement.js.map +1 -1
- package/lib/commonjs/index.js +12 -0
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/index.web.js +12 -0
- package/lib/commonjs/index.web.js.map +1 -1
- package/lib/commonjs/popover/Popover.web.js +11 -1
- package/lib/commonjs/popover/Popover.web.js.map +1 -1
- package/lib/commonjs/popover/context.js.map +1 -1
- package/lib/commonjs/rating/RatingBar.js +2 -0
- package/lib/commonjs/rating/RatingBar.js.map +1 -1
- package/lib/commonjs/rating/RatingBar.web.js +2 -0
- package/lib/commonjs/rating/RatingBar.web.js.map +1 -1
- package/lib/commonjs/stat-bar/Meter.js +7 -4
- package/lib/commonjs/stat-bar/Meter.js.map +1 -1
- package/lib/commonjs/stat-bar/Meter.web.js +11 -4
- package/lib/commonjs/stat-bar/Meter.web.js.map +1 -1
- package/lib/commonjs/stat-bar/use-meter-reveal-policy.js +29 -0
- package/lib/commonjs/stat-bar/use-meter-reveal-policy.js.map +1 -0
- package/lib/commonjs/stat-bar/use-meter-reveal.js +39 -0
- package/lib/commonjs/stat-bar/use-meter-reveal.js.map +1 -0
- package/lib/commonjs/stat-bar/use-meter-reveal.web.js +38 -0
- package/lib/commonjs/stat-bar/use-meter-reveal.web.js.map +1 -0
- package/lib/commonjs/viewport/ViewportProvider.js +28 -0
- package/lib/commonjs/viewport/ViewportProvider.js.map +1 -0
- package/lib/commonjs/viewport/context.js +9 -0
- package/lib/commonjs/viewport/context.js.map +1 -0
- package/lib/commonjs/viewport/geometry.js +31 -0
- package/lib/commonjs/viewport/geometry.js.map +1 -0
- package/lib/commonjs/viewport/index.js +27 -0
- package/lib/commonjs/viewport/index.js.map +1 -0
- package/lib/commonjs/viewport/index.web.js +27 -0
- package/lib/commonjs/viewport/index.web.js.map +1 -0
- package/lib/commonjs/viewport/types.js +6 -0
- package/lib/commonjs/viewport/types.js.map +1 -0
- package/lib/commonjs/viewport/use-in-view.js +106 -0
- package/lib/commonjs/viewport/use-in-view.js.map +1 -0
- package/lib/commonjs/viewport/use-in-view.web.js +49 -0
- package/lib/commonjs/viewport/use-in-view.web.js.map +1 -0
- package/lib/commonjs/viewport/use-viewport-binding.js +55 -0
- package/lib/commonjs/viewport/use-viewport-binding.js.map +1 -0
- package/lib/module/dialog/Dialog.js +6 -4
- package/lib/module/dialog/Dialog.js.map +1 -1
- package/lib/module/dialog/Dialog.web.js +6 -4
- package/lib/module/dialog/Dialog.web.js.map +1 -1
- package/lib/module/dialog/placement.js +7 -1
- package/lib/module/dialog/placement.js.map +1 -1
- package/lib/module/index.js +1 -0
- package/lib/module/index.js.map +1 -1
- package/lib/module/index.web.js +1 -0
- package/lib/module/index.web.js.map +1 -1
- package/lib/module/popover/Popover.web.js +11 -1
- package/lib/module/popover/Popover.web.js.map +1 -1
- package/lib/module/popover/context.js.map +1 -1
- package/lib/module/rating/RatingBar.js +2 -0
- package/lib/module/rating/RatingBar.js.map +1 -1
- package/lib/module/rating/RatingBar.web.js +2 -0
- package/lib/module/rating/RatingBar.web.js.map +1 -1
- package/lib/module/stat-bar/Meter.js +9 -4
- package/lib/module/stat-bar/Meter.js.map +1 -1
- package/lib/module/stat-bar/Meter.web.js +11 -4
- package/lib/module/stat-bar/Meter.web.js.map +1 -1
- package/lib/module/stat-bar/use-meter-reveal-policy.js +25 -0
- package/lib/module/stat-bar/use-meter-reveal-policy.js.map +1 -0
- package/lib/module/stat-bar/use-meter-reveal.js +35 -0
- package/lib/module/stat-bar/use-meter-reveal.js.map +1 -0
- package/lib/module/stat-bar/use-meter-reveal.web.js +34 -0
- package/lib/module/stat-bar/use-meter-reveal.web.js.map +1 -0
- package/lib/module/viewport/ViewportProvider.js +23 -0
- package/lib/module/viewport/ViewportProvider.js.map +1 -0
- package/lib/module/viewport/context.js +5 -0
- package/lib/module/viewport/context.js.map +1 -0
- package/lib/module/viewport/geometry.js +25 -0
- package/lib/module/viewport/geometry.js.map +1 -0
- package/lib/module/viewport/index.js +6 -0
- package/lib/module/viewport/index.js.map +1 -0
- package/lib/module/viewport/index.web.js +11 -0
- package/lib/module/viewport/index.web.js.map +1 -0
- package/lib/module/viewport/types.js +4 -0
- package/lib/module/viewport/types.js.map +1 -0
- package/lib/module/viewport/use-in-view.js +102 -0
- package/lib/module/viewport/use-in-view.js.map +1 -0
- package/lib/module/viewport/use-in-view.web.js +45 -0
- package/lib/module/viewport/use-in-view.web.js.map +1 -0
- package/lib/module/viewport/use-viewport-binding.js +51 -0
- package/lib/module/viewport/use-viewport-binding.js.map +1 -0
- package/lib/typescript/commonjs/dialog/Dialog.web.d.ts.map +1 -1
- package/lib/typescript/commonjs/dialog/placement.d.ts +3 -1
- package/lib/typescript/commonjs/dialog/placement.d.ts.map +1 -1
- package/lib/typescript/commonjs/dialog/types.d.ts +2 -0
- package/lib/typescript/commonjs/dialog/types.d.ts.map +1 -1
- package/lib/typescript/commonjs/index.d.ts +1 -0
- package/lib/typescript/commonjs/index.d.ts.map +1 -1
- package/lib/typescript/commonjs/index.web.d.ts +1 -0
- package/lib/typescript/commonjs/index.web.d.ts.map +1 -1
- package/lib/typescript/commonjs/popover/Popover.web.d.ts.map +1 -1
- package/lib/typescript/commonjs/popover/context.d.ts +16 -2
- package/lib/typescript/commonjs/popover/context.d.ts.map +1 -1
- package/lib/typescript/commonjs/rating/RatingBar.d.ts.map +1 -1
- package/lib/typescript/commonjs/rating/types.d.ts +3 -0
- package/lib/typescript/commonjs/rating/types.d.ts.map +1 -1
- package/lib/typescript/commonjs/stat-bar/Meter.d.ts.map +1 -1
- package/lib/typescript/commonjs/stat-bar/Meter.web.d.ts.map +1 -1
- package/lib/typescript/commonjs/stat-bar/index.d.ts +1 -0
- package/lib/typescript/commonjs/stat-bar/index.d.ts.map +1 -1
- package/lib/typescript/commonjs/stat-bar/index.web.d.ts +1 -0
- package/lib/typescript/commonjs/stat-bar/index.web.d.ts.map +1 -1
- package/lib/typescript/commonjs/stat-bar/types.d.ts +14 -0
- package/lib/typescript/commonjs/stat-bar/types.d.ts.map +1 -1
- package/lib/typescript/commonjs/stat-bar/use-meter-reveal-policy.d.ts +10 -0
- package/lib/typescript/commonjs/stat-bar/use-meter-reveal-policy.d.ts.map +1 -0
- package/lib/typescript/commonjs/stat-bar/use-meter-reveal.d.ts +5 -0
- package/lib/typescript/commonjs/stat-bar/use-meter-reveal.d.ts.map +1 -0
- package/lib/typescript/commonjs/stat-bar/use-meter-reveal.web.d.ts +9 -0
- package/lib/typescript/commonjs/stat-bar/use-meter-reveal.web.d.ts.map +1 -0
- package/lib/typescript/commonjs/viewport/ViewportProvider.d.ts +4 -0
- package/lib/typescript/commonjs/viewport/ViewportProvider.d.ts.map +1 -0
- package/lib/typescript/commonjs/viewport/context.d.ts +8 -0
- package/lib/typescript/commonjs/viewport/context.d.ts.map +1 -0
- package/lib/typescript/commonjs/viewport/geometry.d.ts +10 -0
- package/lib/typescript/commonjs/viewport/geometry.d.ts.map +1 -0
- package/lib/typescript/commonjs/viewport/index.d.ts +5 -0
- package/lib/typescript/commonjs/viewport/index.d.ts.map +1 -0
- package/lib/typescript/commonjs/viewport/index.web.d.ts +5 -0
- package/lib/typescript/commonjs/viewport/index.web.d.ts.map +1 -0
- package/lib/typescript/commonjs/viewport/types.d.ts +25 -0
- package/lib/typescript/commonjs/viewport/types.d.ts.map +1 -0
- package/lib/typescript/commonjs/viewport/use-in-view.d.ts +11 -0
- package/lib/typescript/commonjs/viewport/use-in-view.d.ts.map +1 -0
- package/lib/typescript/commonjs/viewport/use-in-view.web.d.ts +10 -0
- package/lib/typescript/commonjs/viewport/use-in-view.web.d.ts.map +1 -0
- package/lib/typescript/commonjs/viewport/use-viewport-binding.d.ts +9 -0
- package/lib/typescript/commonjs/viewport/use-viewport-binding.d.ts.map +1 -0
- package/lib/typescript/module/dialog/Dialog.web.d.ts.map +1 -1
- package/lib/typescript/module/dialog/placement.d.ts +3 -1
- package/lib/typescript/module/dialog/placement.d.ts.map +1 -1
- package/lib/typescript/module/dialog/types.d.ts +2 -0
- package/lib/typescript/module/dialog/types.d.ts.map +1 -1
- package/lib/typescript/module/index.d.ts +1 -0
- package/lib/typescript/module/index.d.ts.map +1 -1
- package/lib/typescript/module/index.web.d.ts +1 -0
- package/lib/typescript/module/index.web.d.ts.map +1 -1
- package/lib/typescript/module/popover/Popover.web.d.ts.map +1 -1
- package/lib/typescript/module/popover/context.d.ts +16 -2
- package/lib/typescript/module/popover/context.d.ts.map +1 -1
- package/lib/typescript/module/rating/RatingBar.d.ts.map +1 -1
- package/lib/typescript/module/rating/types.d.ts +3 -0
- package/lib/typescript/module/rating/types.d.ts.map +1 -1
- package/lib/typescript/module/stat-bar/Meter.d.ts.map +1 -1
- package/lib/typescript/module/stat-bar/Meter.web.d.ts.map +1 -1
- package/lib/typescript/module/stat-bar/index.d.ts +1 -0
- package/lib/typescript/module/stat-bar/index.d.ts.map +1 -1
- package/lib/typescript/module/stat-bar/index.web.d.ts +1 -0
- package/lib/typescript/module/stat-bar/index.web.d.ts.map +1 -1
- package/lib/typescript/module/stat-bar/types.d.ts +14 -0
- package/lib/typescript/module/stat-bar/types.d.ts.map +1 -1
- package/lib/typescript/module/stat-bar/use-meter-reveal-policy.d.ts +10 -0
- package/lib/typescript/module/stat-bar/use-meter-reveal-policy.d.ts.map +1 -0
- package/lib/typescript/module/stat-bar/use-meter-reveal.d.ts +5 -0
- package/lib/typescript/module/stat-bar/use-meter-reveal.d.ts.map +1 -0
- package/lib/typescript/module/stat-bar/use-meter-reveal.web.d.ts +9 -0
- package/lib/typescript/module/stat-bar/use-meter-reveal.web.d.ts.map +1 -0
- package/lib/typescript/module/viewport/ViewportProvider.d.ts +4 -0
- package/lib/typescript/module/viewport/ViewportProvider.d.ts.map +1 -0
- package/lib/typescript/module/viewport/context.d.ts +8 -0
- package/lib/typescript/module/viewport/context.d.ts.map +1 -0
- package/lib/typescript/module/viewport/geometry.d.ts +10 -0
- package/lib/typescript/module/viewport/geometry.d.ts.map +1 -0
- package/lib/typescript/module/viewport/index.d.ts +5 -0
- package/lib/typescript/module/viewport/index.d.ts.map +1 -0
- package/lib/typescript/module/viewport/index.web.d.ts +5 -0
- package/lib/typescript/module/viewport/index.web.d.ts.map +1 -0
- package/lib/typescript/module/viewport/types.d.ts +25 -0
- package/lib/typescript/module/viewport/types.d.ts.map +1 -0
- package/lib/typescript/module/viewport/use-in-view.d.ts +11 -0
- package/lib/typescript/module/viewport/use-in-view.d.ts.map +1 -0
- package/lib/typescript/module/viewport/use-in-view.web.d.ts +10 -0
- package/lib/typescript/module/viewport/use-in-view.web.d.ts.map +1 -0
- package/lib/typescript/module/viewport/use-viewport-binding.d.ts +9 -0
- package/lib/typescript/module/viewport/use-viewport-binding.d.ts.map +1 -0
- package/package.json +23 -1
- package/src/dialog/Dialog.tsx +7 -4
- package/src/dialog/Dialog.web.tsx +7 -4
- package/src/dialog/placement.ts +9 -1
- package/src/dialog/types.ts +2 -0
- package/src/index.ts +2 -0
- package/src/index.web.ts +2 -0
- package/src/popover/Popover.web.tsx +12 -1
- package/src/popover/context.ts +9 -3
- package/src/rating/RatingBar.tsx +2 -1
- package/src/rating/RatingBar.web.tsx +2 -2
- package/src/rating/types.ts +3 -0
- package/src/stat-bar/Meter.tsx +7 -3
- package/src/stat-bar/Meter.web.tsx +7 -4
- package/src/stat-bar/index.ts +2 -0
- package/src/stat-bar/index.web.ts +2 -0
- package/src/stat-bar/types.ts +15 -0
- package/src/stat-bar/use-meter-reveal-policy.ts +19 -0
- package/src/stat-bar/use-meter-reveal.ts +21 -0
- package/src/stat-bar/use-meter-reveal.web.ts +24 -0
- package/src/viewport/ViewportProvider.tsx +11 -0
- package/src/viewport/context.ts +9 -0
- package/src/viewport/geometry.ts +18 -0
- package/src/viewport/index.ts +4 -0
- package/src/viewport/index.web.ts +9 -0
- package/src/viewport/types.ts +23 -0
- package/src/viewport/use-in-view.ts +69 -0
- package/src/viewport/use-in-view.web.ts +33 -0
- package/src/viewport/use-viewport-binding.ts +34 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { useLayoutEffect, useMemo, useRef } from 'react';
|
|
2
|
+
import { Animated, Easing } from 'react-native';
|
|
3
|
+
import { useMeterRevealPolicy } from './use-meter-reveal-policy';
|
|
4
|
+
import type { MeterReveal } from './types';
|
|
5
|
+
|
|
6
|
+
/** Width animation on the native JS driver; layout properties cannot use the native driver. */
|
|
7
|
+
export function useMeterReveal(fraction: number, reveal?: MeterReveal) {
|
|
8
|
+
const policy = useMeterRevealPolicy(fraction, reveal);
|
|
9
|
+
const progress = useRef(new Animated.Value(reveal && policy.animate ? 0 : policy.fraction)).current;
|
|
10
|
+
const [x1, y1, x2, y2] = policy.easing;
|
|
11
|
+
const easing = useMemo(() => Easing.bezier(x1, y1, x2, y2), [x1, y1, x2, y2]);
|
|
12
|
+
useLayoutEffect(() => {
|
|
13
|
+
progress.stopAnimation();
|
|
14
|
+
if (!policy.animate) { progress.setValue(policy.fraction); return; }
|
|
15
|
+
const animation = Animated.timing(progress, { toValue: policy.fraction, duration: policy.duration,
|
|
16
|
+
delay: policy.delay, easing, useNativeDriver: false, isInteraction: false });
|
|
17
|
+
animation.start();
|
|
18
|
+
return () => animation.stop();
|
|
19
|
+
}, [progress, policy.fraction, policy.animate, policy.duration, policy.delay, easing]);
|
|
20
|
+
return progress.interpolate({ inputRange: [0, 1], outputRange: ['0%', '100%'], extrapolate: 'clamp' });
|
|
21
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { useLayoutEffect, useRef, useState } from 'react';
|
|
2
|
+
import { useMeterRevealPolicy } from './use-meter-reveal-policy';
|
|
3
|
+
import type { MeterReveal } from './types';
|
|
4
|
+
|
|
5
|
+
/** Stage an initial empty paint before the CSS transition, including visible-on-mount. */
|
|
6
|
+
export function useMeterReveal(fraction: number, reveal?: MeterReveal) {
|
|
7
|
+
const policy = useMeterRevealPolicy(fraction, reveal);
|
|
8
|
+
const [painted, setPainted] = useState(reveal && policy.animate ? 0 : policy.fraction);
|
|
9
|
+
const staged = useRef(false);
|
|
10
|
+
useLayoutEffect(() => {
|
|
11
|
+
if (!policy.animate || staged.current) { setPainted(policy.fraction); return; }
|
|
12
|
+
let next = 0;
|
|
13
|
+
const frame = requestAnimationFrame(() => {
|
|
14
|
+
next = requestAnimationFrame(() => { staged.current = true; setPainted(policy.fraction); });
|
|
15
|
+
});
|
|
16
|
+
return () => { cancelAnimationFrame(frame); if (next) cancelAnimationFrame(next); };
|
|
17
|
+
}, [policy.fraction, policy.animate]);
|
|
18
|
+
return {
|
|
19
|
+
fraction: policy.animate ? painted : policy.fraction,
|
|
20
|
+
duration: policy.animate ? policy.duration : 0,
|
|
21
|
+
delay: policy.animate ? policy.delay : 0,
|
|
22
|
+
easing: `cubic-bezier(${policy.easing.join(',')})`,
|
|
23
|
+
};
|
|
24
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import React, { useContext, useMemo } from 'react';
|
|
2
|
+
import { ViewportContext, type ViewportScope } from './context';
|
|
3
|
+
import type { ViewportProviderProps } from './types';
|
|
4
|
+
|
|
5
|
+
/** Scope native clipping geometry without inserting a layout wrapper. */
|
|
6
|
+
export function ViewportProvider({ children, root = false }: ViewportProviderProps) {
|
|
7
|
+
const inherited = useContext(ViewportContext);
|
|
8
|
+
const parent = root ? null : inherited;
|
|
9
|
+
const scope = useMemo<ViewportScope>(() => ({ parent, getNode: null, listeners: new Set() }), [parent]);
|
|
10
|
+
return <ViewportContext.Provider value={scope}>{children}</ViewportContext.Provider>;
|
|
11
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { createContext } from 'react';
|
|
2
|
+
import type { ViewportHandle } from './types';
|
|
3
|
+
|
|
4
|
+
export interface ViewportScope {
|
|
5
|
+
parent: ViewportScope | null;
|
|
6
|
+
getNode: (() => ViewportHandle | null) | null;
|
|
7
|
+
listeners: Set<() => void>;
|
|
8
|
+
}
|
|
9
|
+
export const ViewportContext = createContext<ViewportScope | null>(null);
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export interface VisibilityRect { x: number; y: number; width: number; height: number }
|
|
2
|
+
export function validVisibilityRect(rect: VisibilityRect): boolean {
|
|
3
|
+
return Object.values(rect).every(Number.isFinite) && rect.width > 0 && rect.height > 0;
|
|
4
|
+
}
|
|
5
|
+
export function intersectionRatio(target: VisibilityRect, clips: readonly VisibilityRect[]): number {
|
|
6
|
+
if (!validVisibilityRect(target) || clips.some((clip) => !validVisibilityRect(clip))) return 0;
|
|
7
|
+
let left = target.x, top = target.y, right = left + target.width, bottom = top + target.height;
|
|
8
|
+
for (const clip of clips) {
|
|
9
|
+
left = Math.max(left, clip.x); top = Math.max(top, clip.y);
|
|
10
|
+
right = Math.min(right, clip.x + clip.width); bottom = Math.min(bottom, clip.y + clip.height);
|
|
11
|
+
}
|
|
12
|
+
if (left === target.x && top === target.y && right === target.x + target.width && bottom === target.y + target.height) return 1;
|
|
13
|
+
return Math.max(0, right - left) * Math.max(0, bottom - top) / (target.width * target.height);
|
|
14
|
+
}
|
|
15
|
+
export function visibilityThreshold(value = 0): number {
|
|
16
|
+
if (!Number.isFinite(value) || value < 0 || value > 1) throw new Error('Bloom visibility threshold must be between 0 and 1.');
|
|
17
|
+
return value;
|
|
18
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { ViewportProvider } from './ViewportProvider';
|
|
2
|
+
export { useViewportBinding } from './use-viewport-binding';
|
|
3
|
+
export { useInView } from './use-in-view';
|
|
4
|
+
export type { ViewportProviderProps, ViewportBindingOptions, InViewOptions, VisibilityHandle, ViewportHandle } from './types';
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// AUTO-GENERATED by scripts/generate-platform-exports.mjs — DO NOT EDIT.
|
|
2
|
+
// Source of truth: src/viewport/index.ts.
|
|
3
|
+
// Re-run `bun run generate:exports` (or any `bun run build`) after
|
|
4
|
+
// changing that barrel or the set of web-forked subpaths.
|
|
5
|
+
|
|
6
|
+
export { ViewportProvider } from './ViewportProvider';
|
|
7
|
+
export { useViewportBinding } from './use-viewport-binding';
|
|
8
|
+
export { useInView } from './use-in-view.web';
|
|
9
|
+
export type { ViewportProviderProps, ViewportBindingOptions, InViewOptions, VisibilityHandle, ViewportHandle } from './types';
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { PropsWithChildren, RefObject } from 'react';
|
|
2
|
+
import type { ScrollViewProps } from 'react-native';
|
|
3
|
+
|
|
4
|
+
/** A mounted native viewport or target supporting window-space measurement. */
|
|
5
|
+
export interface VisibilityHandle {
|
|
6
|
+
measureInWindow(callback: (x: number, y: number, width: number, height: number) => void): void;
|
|
7
|
+
}
|
|
8
|
+
/** ScrollView exposes measurement through its native scroll host. */
|
|
9
|
+
export type ViewportHandle = VisibilityHandle | { getNativeScrollRef(): VisibilityHandle | null };
|
|
10
|
+
export interface ViewportProviderProps extends PropsWithChildren {
|
|
11
|
+
/** Start a separate clipping chain, e.g. in a portaled dialog. */
|
|
12
|
+
root?: boolean;
|
|
13
|
+
}
|
|
14
|
+
export interface ViewportBindingOptions extends Pick<ScrollViewProps, 'onScroll' | 'onLayout' | 'onContentSizeChange'> {
|
|
15
|
+
/** The actual scrolling/clipping host. Keep its ref on the ScrollView. */
|
|
16
|
+
viewportRef: RefObject<ViewportHandle | null>;
|
|
17
|
+
}
|
|
18
|
+
export interface InViewOptions {
|
|
19
|
+
/** Required visible area fraction, from 0 to 1. Defaults to 0. */
|
|
20
|
+
threshold?: number;
|
|
21
|
+
/** Stop observing after the threshold is first reached. Defaults to false. */
|
|
22
|
+
once?: boolean;
|
|
23
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { useCallback, useContext, useEffect, useRef, useState } from 'react';
|
|
2
|
+
import { Dimensions } from 'react-native';
|
|
3
|
+
import { ViewportContext, type ViewportScope } from './context';
|
|
4
|
+
import { intersectionRatio, visibilityThreshold, type VisibilityRect } from './geometry';
|
|
5
|
+
import type { InViewOptions, VisibilityHandle, ViewportHandle } from './types';
|
|
6
|
+
|
|
7
|
+
/** Native visibility is measured against every bound ancestor viewport. */
|
|
8
|
+
export function useInView({ threshold: requestedThreshold, once = false }: InViewOptions = {}) {
|
|
9
|
+
const threshold = visibilityThreshold(requestedThreshold);
|
|
10
|
+
const scope = useContext(ViewportContext);
|
|
11
|
+
if (!scope) throw new Error('Bloom useInView requires a bound ViewportProvider on native.');
|
|
12
|
+
const [node, setNode] = useState<VisibilityHandle | null>(null);
|
|
13
|
+
const [inView, setInView] = useState(false);
|
|
14
|
+
const entered = useRef(false);
|
|
15
|
+
const scheduleRef = useRef<() => void>(() => {});
|
|
16
|
+
const ref = useCallback((value: unknown) => {
|
|
17
|
+
const target = value as VisibilityHandle | null;
|
|
18
|
+
if (target && typeof target.measureInWindow !== 'function') throw new Error('Bloom useInView ref must point to a native View with measureInWindow.');
|
|
19
|
+
setNode(target);
|
|
20
|
+
}, []);
|
|
21
|
+
const onLayout = useCallback(() => scheduleRef.current(), []);
|
|
22
|
+
useEffect(() => {
|
|
23
|
+
if (!node || (once && entered.current)) return;
|
|
24
|
+
let disposed = false, frame: ReturnType<typeof requestAnimationFrame> | null = null, generation = 0;
|
|
25
|
+
const scopes: ViewportScope[] = [];
|
|
26
|
+
for (let current: ViewportScope | null = scope; current; current = current.parent) scopes.push(current);
|
|
27
|
+
const measure = (source: ViewportHandle | null, complete: (rect: VisibilityRect | null) => void) => {
|
|
28
|
+
const handle = source && 'getNativeScrollRef' in source ? source.getNativeScrollRef() : source;
|
|
29
|
+
if (!handle) { complete(null); return; }
|
|
30
|
+
handle.measureInWindow((x, y, width, height) => complete({ x, y, width, height }));
|
|
31
|
+
};
|
|
32
|
+
const read = () => {
|
|
33
|
+
frame = null;
|
|
34
|
+
const ticket = ++generation;
|
|
35
|
+
const window = Dimensions.get('window');
|
|
36
|
+
const clips: VisibilityRect[] = [{ x: 0, y: 0, width: window.width, height: window.height }];
|
|
37
|
+
let pending = scopes.length + 1, target: VisibilityRect | null = null, missing = false;
|
|
38
|
+
const finish = () => {
|
|
39
|
+
if (--pending || disposed || ticket !== generation) return;
|
|
40
|
+
const ratio = !missing && target ? intersectionRatio(target, clips) : 0;
|
|
41
|
+
const visible = ratio > 0 && ratio >= threshold;
|
|
42
|
+
if (visible) entered.current = true;
|
|
43
|
+
setInView(visible);
|
|
44
|
+
};
|
|
45
|
+
measure(node, (rect) => { target = rect; finish(); });
|
|
46
|
+
scopes.forEach((owner) => measure(owner.getNode?.() ?? null, (rect) => {
|
|
47
|
+
if (rect) clips.push(rect); else missing = true;
|
|
48
|
+
finish();
|
|
49
|
+
}));
|
|
50
|
+
};
|
|
51
|
+
const schedule = () => {
|
|
52
|
+
if (disposed || frame !== null) return;
|
|
53
|
+
// A newer event invalidates asynchronous native measurements immediately.
|
|
54
|
+
generation++;
|
|
55
|
+
frame = requestAnimationFrame(read);
|
|
56
|
+
};
|
|
57
|
+
scheduleRef.current = schedule;
|
|
58
|
+
scopes.forEach((owner) => owner.listeners.add(schedule));
|
|
59
|
+
const subscription = Dimensions.addEventListener('change', schedule);
|
|
60
|
+
schedule();
|
|
61
|
+
return () => {
|
|
62
|
+
disposed = true; generation++;
|
|
63
|
+
if (frame !== null) cancelAnimationFrame(frame);
|
|
64
|
+
scopes.forEach((owner) => owner.listeners.delete(schedule));
|
|
65
|
+
subscription.remove(); scheduleRef.current = () => {};
|
|
66
|
+
};
|
|
67
|
+
}, [node, scope, threshold, once, inView]);
|
|
68
|
+
return { inView, targetProps: { ref, onLayout, collapsable: false as const } };
|
|
69
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { useCallback, useEffect, useRef, useState } from 'react';
|
|
2
|
+
import { visibilityThreshold } from './geometry';
|
|
3
|
+
import type { InViewOptions } from './types';
|
|
4
|
+
|
|
5
|
+
/** The browser owns ancestor clipping, scrolling and resize observation. */
|
|
6
|
+
export function useInView({ threshold: requestedThreshold, once = false }: InViewOptions = {}) {
|
|
7
|
+
const threshold = visibilityThreshold(requestedThreshold);
|
|
8
|
+
const [node, setNode] = useState<Element | null>(null);
|
|
9
|
+
const [inView, setInView] = useState(false);
|
|
10
|
+
const entered = useRef(false);
|
|
11
|
+
const ref = useCallback((value: unknown) => {
|
|
12
|
+
if (value != null && !(value instanceof Element)) throw new Error('Bloom useInView ref must point to a web host element.');
|
|
13
|
+
setNode(value as Element | null);
|
|
14
|
+
}, []);
|
|
15
|
+
useEffect(() => {
|
|
16
|
+
if (!node || (once && entered.current)) return;
|
|
17
|
+
// Unsupported environments remain unobserved, never pretend mount is visible.
|
|
18
|
+
if (typeof IntersectionObserver === 'undefined') return;
|
|
19
|
+
const observer = new IntersectionObserver((entries) => {
|
|
20
|
+
for (const entry of entries) {
|
|
21
|
+
if (entry.target !== node || (once && entered.current)) continue;
|
|
22
|
+
const visible = entry.boundingClientRect.width > 0 && entry.boundingClientRect.height > 0 && entry.isIntersecting && entry.intersectionRatio > 0 && entry.intersectionRatio >= threshold;
|
|
23
|
+
if (visible) entered.current = true;
|
|
24
|
+
setInView(visible);
|
|
25
|
+
if (visible && once) observer.disconnect();
|
|
26
|
+
}
|
|
27
|
+
}, { threshold });
|
|
28
|
+
observer.observe(node);
|
|
29
|
+
return () => observer.disconnect();
|
|
30
|
+
}, [node, threshold, once]);
|
|
31
|
+
const onLayout = useCallback(() => {}, []);
|
|
32
|
+
return { inView, targetProps: { ref, onLayout } };
|
|
33
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { useCallback, useContext, useLayoutEffect } from 'react';
|
|
2
|
+
import { Platform, type ScrollViewProps } from 'react-native';
|
|
3
|
+
import { ViewportContext } from './context';
|
|
4
|
+
import type { ViewportBindingOptions } from './types';
|
|
5
|
+
|
|
6
|
+
/** Compose with restoration/metrics callbacks; no React renders on scroll. */
|
|
7
|
+
export function useViewportBinding({ viewportRef, onScroll, onLayout, onContentSizeChange }: ViewportBindingOptions) {
|
|
8
|
+
const scope = useContext(ViewportContext);
|
|
9
|
+
if (Platform.OS !== 'web' && !scope) throw new Error('Bloom useViewportBinding requires ViewportProvider on native.');
|
|
10
|
+
if (onScroll != null && typeof onScroll !== 'function') {
|
|
11
|
+
throw new Error('Bloom useViewportBinding onScroll expects a plain RN callback; pass its returned onScroll through the observer slot of your animated binding.');
|
|
12
|
+
}
|
|
13
|
+
const notify = useCallback(() => {
|
|
14
|
+
if (Platform.OS !== 'web') scope?.listeners.forEach((listener) => listener());
|
|
15
|
+
}, [scope]);
|
|
16
|
+
useLayoutEffect(() => {
|
|
17
|
+
if (!scope || Platform.OS === 'web') return;
|
|
18
|
+
if (scope.getNode) throw new Error('Each ViewportProvider supports one viewport binding. Nest another provider for a nested scroller.');
|
|
19
|
+
const getNode = () => viewportRef.current;
|
|
20
|
+
scope.getNode = getNode;
|
|
21
|
+
notify();
|
|
22
|
+
return () => { if (scope.getNode === getNode) scope.getNode = null; notify(); };
|
|
23
|
+
}, [scope, viewportRef, notify]);
|
|
24
|
+
const handleScroll = useCallback<NonNullable<ScrollViewProps['onScroll']>>((event) => {
|
|
25
|
+
notify(); onScroll?.(event);
|
|
26
|
+
}, [notify, onScroll]);
|
|
27
|
+
const handleLayout = useCallback<NonNullable<ScrollViewProps['onLayout']>>((event) => {
|
|
28
|
+
notify(); onLayout?.(event);
|
|
29
|
+
}, [notify, onLayout]);
|
|
30
|
+
const handleContentSizeChange = useCallback<NonNullable<ScrollViewProps['onContentSizeChange']>>((width, height) => {
|
|
31
|
+
notify(); onContentSizeChange?.(width, height);
|
|
32
|
+
}, [notify, onContentSizeChange]);
|
|
33
|
+
return { onScroll: handleScroll, onLayout: handleLayout, onContentSizeChange: handleContentSizeChange, scrollEventThrottle: 16 };
|
|
34
|
+
}
|