Skip to content

Commit 5853f11

Browse files
committed
fix: read window height from a shared value on ui thread
1 parent 782e00c commit 5853f11

4 files changed

Lines changed: 27 additions & 7 deletions

File tree

src/hooks/useAnimatedLayout.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { useMemo, useState } from 'react';
1+
import { useEffect, useMemo, useState } from 'react';
2+
import { Dimensions } from 'react-native';
23
import {
34
makeMutable,
45
type SharedValue,
@@ -8,6 +9,7 @@ import { INITIAL_CONTAINER_LAYOUT, INITIAL_LAYOUT_VALUE } from '../constants';
89
import type { ContainerLayoutState, LayoutState } from '../types';
910

1011
const INITIAL_STATE: LayoutState = {
12+
window: Dimensions.get('window'),
1113
rawContainerHeight: INITIAL_LAYOUT_VALUE,
1214
containerHeight: INITIAL_LAYOUT_VALUE,
1315
containerOffset: INITIAL_CONTAINER_LAYOUT.offset,
@@ -103,6 +105,15 @@ export function useAnimatedLayout(
103105
},
104106
[state, verticalInset, modal]
105107
);
108+
useEffect(() => {
109+
Dimensions.addEventListener('change', ({ window }) => {
110+
state.modify(_state => {
111+
'worklet';
112+
_state.window = window;
113+
return _state;
114+
});
115+
});
116+
}, [state]);
106117
//#endregion
107118

108119
return state;

src/hooks/useGestureEventsHandlersDefault.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useCallback } from 'react';
2-
import { Dimensions, Keyboard, Platform } from 'react-native';
2+
import { Keyboard, Platform } from 'react-native';
33
import { runOnJS, useSharedValue } from 'react-native-reanimated';
44
import {
55
ANIMATION_SOURCE,
@@ -345,13 +345,13 @@ export const useGestureEventsHandlersDefault: GestureEventsHandlersHookType =
345345
*
346346
* because the the keyboard dismiss is interactive in iOS.
347347
*/
348-
const WINDOW_HEIGHT = Dimensions.get('window').height;
348+
const { window } = animatedLayoutState.get();
349349
if (
350350
!(
351351
Platform.OS === 'ios' &&
352352
isScrollable &&
353353
absoluteY >
354-
WINDOW_HEIGHT -
354+
window.height -
355355
animatedKeyboardState.get().heightWithinContainer
356356
)
357357
) {
@@ -419,6 +419,7 @@ export const useGestureEventsHandlersDefault: GestureEventsHandlersHookType =
419419
enablePanDownToClose,
420420
isInTemporaryPosition,
421421
animatedScrollableState,
422+
animatedLayoutState,
422423
animatedDetentsState,
423424
animatedKeyboardState,
424425
animatedPosition,

src/hooks/useGestureEventsHandlersDefault.web.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useCallback } from 'react';
2-
import { Dimensions, Keyboard, Platform } from 'react-native';
2+
import { Keyboard, Platform } from 'react-native';
33
import { runOnJS, useSharedValue } from 'react-native-reanimated';
44
import {
55
ANIMATION_SOURCE,
@@ -328,13 +328,13 @@ export const useGestureEventsHandlersDefault = () => {
328328
*
329329
* because the the keyboard dismiss is interactive in iOS.
330330
*/
331-
const WINDOW_HEIGHT = Dimensions.get('window').height;
331+
const { window } = animatedLayoutState.get();
332332
if (
333333
!(
334334
Platform.OS === 'ios' &&
335335
isScrollable &&
336336
absoluteY >
337-
WINDOW_HEIGHT - animatedKeyboardState.get().heightWithinContainer
337+
window.height - animatedKeyboardState.get().heightWithinContainer
338338
)
339339
) {
340340
dismissKeyboardOnJs();
@@ -396,6 +396,7 @@ export const useGestureEventsHandlersDefault = () => {
396396
animatedDetentsState,
397397
animatedKeyboardState,
398398
animatedPosition,
399+
animatedLayoutState,
399400
animateToPosition,
400401
context,
401402
]

src/types.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,13 @@ export type ContainerLayoutState = {
300300
* Represents the layout state of the bottom sheet components.
301301
*/
302302
export type LayoutState = {
303+
/**
304+
* Getting latest window layout on a shared value
305+
*/
306+
window: {
307+
height: number;
308+
width: number;
309+
};
303310
/**
304311
* The original height of the container before any adjustments.
305312
*/

0 commit comments

Comments
 (0)