|
1 | 1 | import { useCallback, useEffect, useRef, useState } from 'react'; |
2 | 2 |
|
| 3 | +/** |
| 4 | + * Configuration options for the useAutoHeight hook. |
| 5 | + * |
| 6 | + * @interface UseAutoHeightOptions |
| 7 | + */ |
3 | 8 | export interface UseAutoHeightOptions { |
4 | | - /** Minimum height (in dp) enforced for the WebView container. */ |
| 9 | + /** |
| 10 | + * Minimum height (in dp/points) enforced for the WebView container. |
| 11 | + * |
| 12 | + * Prevents the component from becoming too small when content is minimal. |
| 13 | + * The actual height will be: `max(minHeight, measuredContentHeight)` |
| 14 | + * |
| 15 | + * @default 0 |
| 16 | + */ |
5 | 17 | minHeight: number; |
6 | | - /** Optional callback triggered whenever a new height is committed. */ |
| 18 | + |
| 19 | + /** |
| 20 | + * Optional callback triggered whenever a new height is committed. |
| 21 | + * |
| 22 | + * This callback fires after the height has been validated, throttled by the |
| 23 | + * HEIGHT_DIFF_THRESHOLD, and scheduled for the next animation frame. |
| 24 | + * |
| 25 | + * Use this callback for: |
| 26 | + * - Analytics or logging height changes |
| 27 | + * - Triggering dependent UI updates |
| 28 | + * - Syncing with external state management |
| 29 | + * |
| 30 | + * The callback is not called for changes smaller than 1dp to avoid noise. |
| 31 | + * |
| 32 | + * @param height - The new committed height in dp/points |
| 33 | + * |
| 34 | + * @example |
| 35 | + * ```ts |
| 36 | + * onHeightChange={(height) => { |
| 37 | + * analytics.track('webview_height_change', { height }); |
| 38 | + * }} |
| 39 | + * ``` |
| 40 | + */ |
7 | 41 | onHeightChange?: (height: number) => void; |
8 | 42 | } |
9 | 43 |
|
| 44 | +/** |
| 45 | + * Return value from the useAutoHeight hook. |
| 46 | + * |
| 47 | + * @interface UseAutoHeightResult |
| 48 | + */ |
10 | 49 | export interface UseAutoHeightResult { |
11 | | - /** Current height applied to the WebView container. */ |
| 50 | + /** |
| 51 | + * The current height applied to the WebView container in dp/points. |
| 52 | + * |
| 53 | + * This value is: |
| 54 | + * - At least `minHeight` (respects minimum) |
| 55 | + * - Updated smoothly using requestAnimationFrame |
| 56 | + * - Batched to avoid excessive re-renders |
| 57 | + * - Only committed if change exceeds 1dp threshold |
| 58 | + * |
| 59 | + * @type {number} |
| 60 | + */ |
12 | 61 | height: number; |
| 62 | + |
13 | 63 | /** |
14 | | - * Helper that parses any incoming payload (from the WebView bridge) and |
15 | | - * commits a new height when appropriate. |
| 64 | + * Parser and dispatcher for incoming height payloads from the WebView bridge. |
| 65 | + * |
| 66 | + * This function should be called with any data received from the injected JavaScript |
| 67 | + * bridge. It handles: |
| 68 | + * - Type coercion (number strings are converted to numbers) |
| 69 | + * - Validation (finite positive numbers only) |
| 70 | + * - Throttling (changes < 1dp are ignored) |
| 71 | + * - Scheduling (uses requestAnimationFrame for smooth updates) |
| 72 | + * |
| 73 | + * Safe to call with any value—invalid inputs are silently ignored. |
| 74 | + * |
| 75 | + * @param rawValue - Any value received from the WebView bridge |
| 76 | + * |
| 77 | + * @example |
| 78 | + * ```ts |
| 79 | + * const { height, setHeightFromPayload } = useAutoHeight({ minHeight: 100 }); |
| 80 | + * |
| 81 | + * const handleMessage = (event) => { |
| 82 | + * setHeightFromPayload(event.nativeEvent.data); |
| 83 | + * }; |
| 84 | + * ``` |
16 | 85 | */ |
17 | 86 | setHeightFromPayload: (rawValue: unknown) => void; |
18 | 87 | } |
19 | 88 |
|
| 89 | +/** |
| 90 | + * Threshold (in dp) below which height changes are ignored. |
| 91 | + * |
| 92 | + * Prevents excessive re-renders from minor content layout fluctuations. |
| 93 | + * @internal |
| 94 | + */ |
20 | 95 | const HEIGHT_DIFF_THRESHOLD = 1; |
21 | 96 |
|
22 | 97 | /** |
23 | | - * React hook encapsulating the logic for tracking and updating the WebView height. |
24 | | - * Ensures we avoid unnecessary renders while keeping the implementation testable. |
| 98 | + * React hook for managing automatic WebView height calculation and updates. |
| 99 | + * |
| 100 | + * ## Overview |
| 101 | + * This hook encapsulates all the logic needed to: |
| 102 | + * 1. Track the WebView's content height |
| 103 | + * 2. Validate and normalize incoming height values |
| 104 | + * 3. Throttle updates to prevent layout thrashing |
| 105 | + * 4. Schedule updates on the next animation frame |
| 106 | + * 5. Invoke callbacks when height changes |
| 107 | + * |
| 108 | + * ## Features |
| 109 | + * - 🎯 Enforces minimum height to prevent shrinking below acceptable bounds |
| 110 | + * - ⚡ Batches updates using requestAnimationFrame for smooth 60fps rendering |
| 111 | + * - 🚫 Ignores changes smaller than 1dp to reduce noise |
| 112 | + * - 🔒 Type-safe with strong validation of incoming values |
| 113 | + * - 🧪 Fully testable with deterministic behavior |
| 114 | + * - 🧹 Automatically cleans up animation frame requests on unmount |
| 115 | + * |
| 116 | + * ## How it Works |
| 117 | + * ``` |
| 118 | + * WebView Bridge sends height → setHeightFromPayload() validates |
| 119 | + * → scheduleCommit() batches update |
| 120 | + * → requestAnimationFrame executes flushPendingHeight() |
| 121 | + * → commitHeight() updates state + callback |
| 122 | + * ``` |
| 123 | + * |
| 124 | + * ## Usage Example |
| 125 | + * ```ts |
| 126 | + * import { useAutoHeight } from 'react-native-sized-webview'; |
| 127 | + * |
| 128 | + * function MyComponent() { |
| 129 | + * const { height, setHeightFromPayload } = useAutoHeight({ |
| 130 | + * minHeight: 100, |
| 131 | + * onHeightChange: (h) => console.log(`Height: ${h}dp`), |
| 132 | + * }); |
| 133 | + * |
| 134 | + * const handleMessage = (event) => { |
| 135 | + * setHeightFromPayload(event.nativeEvent.data); |
| 136 | + * }; |
| 137 | + * |
| 138 | + * return ( |
| 139 | + * <View style={{ height }}> |
| 140 | + * <WebView onMessage={handleMessage} /> |
| 141 | + * </View> |
| 142 | + * ); |
| 143 | + * } |
| 144 | + * ``` |
| 145 | + * |
| 146 | + * ## Performance Considerations |
| 147 | + * - Uses requestAnimationFrame to sync with 60fps screen refresh |
| 148 | + * - Only processes height changes exceeding 1dp threshold |
| 149 | + * - Maintains refs to avoid unnecessary re-render triggers |
| 150 | + * - Properly cancels pending frames on unmount |
| 151 | + * |
| 152 | + * @param options - Configuration object with minHeight and optional onHeightChange callback |
| 153 | + * @returns Object containing current height and payload processor function |
| 154 | + * |
| 155 | + * @throws Does not throw—all invalid inputs are silently ignored |
| 156 | + * |
| 157 | + * @example |
| 158 | + * ```ts |
| 159 | + * // Basic usage with defaults |
| 160 | + * const { height, setHeightFromPayload } = useAutoHeight({ minHeight: 50 }); |
| 161 | + * ``` |
| 162 | + * |
| 163 | + * @example |
| 164 | + * ```ts |
| 165 | + * // With callback to track changes |
| 166 | + * const { height, setHeightFromPayload } = useAutoHeight({ |
| 167 | + * minHeight: 100, |
| 168 | + * onHeightChange: (newHeight) => { |
| 169 | + * analytics.track('webview_resized', { newHeight }); |
| 170 | + * }, |
| 171 | + * }); |
| 172 | + * ``` |
25 | 173 | */ |
26 | 174 | export const useAutoHeight = ( |
27 | 175 | options: UseAutoHeightOptions |
|
0 commit comments