Skip to content

Commit b72e275

Browse files
committed
fix: dynamic sizing with detached static views
1 parent 1ef05c7 commit b72e275

File tree

4 files changed

+67
-66
lines changed

4 files changed

+67
-66
lines changed

example/src/Main.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import React from 'react';
21
import { ShowcaseApp } from '@gorhom/showcase-template';
2+
import React from 'react';
3+
import { description, version } from '../../package.json';
34
import { screens } from './screens';
4-
import { version, description } from '../../package.json';
55

66
const author = {
77
username: 'Mo Gorhom',

example/src/screens/modal/DetachedExample.tsx

+28-17
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,53 @@
1-
import React, { useCallback, useMemo, useRef } from 'react';
2-
import { View, StyleSheet, Text } from 'react-native';
3-
import { useSafeAreaInsets } from 'react-native-safe-area-context';
41
import {
2+
BottomSheetFooter,
3+
type BottomSheetFooterProps,
4+
type BottomSheetHandleProps,
55
BottomSheetModal,
66
BottomSheetView,
7-
BottomSheetFooter,
8-
BottomSheetHandleProps,
9-
BottomSheetFooterProps,
107
} from '@gorhom/bottom-sheet';
8+
import React, { useCallback, useMemo, useRef, useState } from 'react';
9+
import { StyleSheet, Text, View } from 'react-native';
10+
import { useSafeAreaInsets } from 'react-native-safe-area-context';
1111
import { Button } from '../../components/button';
1212
import { ContactItem } from '../../components/contactItem';
1313
import { HeaderHandle } from '../../components/headerHandle';
14-
import { withModalProvider } from './withModalProvider';
14+
import type { Contact } from '../../types';
1515
import { createContactListMockData } from '../../utilities/createMockData';
16-
import { Contact } from '../../types';
16+
import { withModalProvider } from './withModalProvider';
17+
18+
const DATA = createContactListMockData(4);
1719

1820
const DetachedExample = () => {
1921
// refs
2022
const bottomSheetRef = useRef<BottomSheetModal>(null);
2123

24+
// state
25+
const [count, setCount] = useState(2);
26+
2227
// variables
23-
const data = useMemo(() => createContactListMockData(2), []);
28+
const data = useMemo(() => DATA.slice(0, count), [count]);
2429

2530
// hooks
2631
const { bottom: safeBottomArea } = useSafeAreaInsets();
2732

2833
// callbacks
2934
const handlePresentPress = useCallback(() => {
30-
bottomSheetRef.current!.present();
35+
bottomSheetRef.current?.present();
3136
}, []);
3237
const handleDismissPress = useCallback(() => {
33-
bottomSheetRef.current!.dismiss();
38+
bottomSheetRef.current?.dismiss();
3439
}, []);
3540
const handleClosePress = useCallback(() => {
3641
bottomSheetRef.current?.close();
3742
}, []);
43+
const handleResizePress = useCallback(() => {
44+
setCount(state => (state === 2 ? 4 : 2));
45+
}, []);
3846

3947
// renders
4048
const renderHeaderHandle = useCallback(
4149
(props: BottomSheetHandleProps) => (
42-
<HeaderHandle {...props} children="Detached Example" />
50+
<HeaderHandle {...props}>Detached Example</HeaderHandle>
4351
),
4452
[]
4553
);
@@ -56,12 +64,15 @@ const DetachedExample = () => {
5664
const renderFooter = useCallback(
5765
(props: BottomSheetFooterProps) => (
5866
<BottomSheetFooter {...props}>
59-
<View style={styles.footer}>
60-
<Text style={styles.footerText}>this is a footer!</Text>
61-
</View>
67+
<Button
68+
label="Resize"
69+
style={styles.footer}
70+
labelStyle={styles.footerText}
71+
onPress={handleResizePress}
72+
/>
6273
</BottomSheetFooter>
6374
),
64-
[]
75+
[handleResizePress]
6576
);
6677
return (
6778
<View style={styles.container}>
@@ -117,7 +128,7 @@ const styles = StyleSheet.create({
117128
justifyContent: 'center',
118129
alignItems: 'center',
119130
marginHorizontal: 12,
120-
padding: 12,
131+
padding: 6,
121132
marginBottom: 12,
122133
borderRadius: 24,
123134
backgroundColor: '#80f',

src/components/bottomSheet/BottomSheet.tsx

+11-10
Original file line numberDiff line numberDiff line change
@@ -1495,6 +1495,7 @@ const BottomSheetComponent = forwardRef<BottomSheet, BottomSheetProps>(
14951495
configs: _providedAnimationConfigs,
14961496
overrideReduceMotion: _providedOverrideReduceMotion,
14971497
}),
1498+
overflow: 'hidden',
14981499
};
14991500
}, [
15001501
enableDynamicSizing,
@@ -1923,13 +1924,12 @@ const BottomSheetComponent = forwardRef<BottomSheet, BottomSheetProps>(
19231924
style={contentContainerStyle}
19241925
>
19251926
{children}
1926-
1927-
{footerComponent && (
1928-
<BottomSheetFooterContainer
1929-
footerComponent={footerComponent}
1930-
/>
1931-
)}
19321927
</DraggableView>
1928+
{footerComponent && (
1929+
<BottomSheetFooterContainer
1930+
footerComponent={footerComponent}
1931+
/>
1932+
)}
19331933
</Animated.View>
19341934
<BottomSheetHandleContainer
19351935
key="BottomSheetHandleContainer"
@@ -1961,10 +1961,11 @@ const BottomSheetComponent = forwardRef<BottomSheet, BottomSheetProps>(
19611961
animatedPosition,
19621962
// animatedHandleGestureState,
19631963
// animatedContentGestureState,
1964-
// animatedContainerHeight,
1965-
// animatedSheetHeight,
1966-
// animatedHandleHeight,
1967-
// animatedContentHeight,
1964+
animatedContainerHeight,
1965+
animatedSheetHeight,
1966+
animatedHandleHeight,
1967+
animatedContentHeight,
1968+
animatedFooterHeight,
19681969
// // keyboardHeight,
19691970
// isLayoutCalculated,
19701971
// isContentHeightFixed,

src/components/bottomSheetView/BottomSheetView.tsx

+26-37
Original file line numberDiff line numberDiff line change
@@ -25,36 +25,24 @@ function BottomSheetViewComponent({
2525
//#endregion
2626

2727
//#region styles
28-
const flattenContainerStyle = useMemo(
29-
() => StyleSheet.flatten(style),
30-
[style]
31-
);
32-
const containerStylePaddingBottom = useMemo(() => {
33-
const paddingBottom =
34-
flattenContainerStyle && 'paddingBottom' in flattenContainerStyle
35-
? flattenContainerStyle.paddingBottom
28+
const flattenStyle = useMemo(() => StyleSheet.flatten(style), [style]);
29+
const containerStyle = useAnimatedStyle(() => {
30+
if (!enableFooterMarginAdjustment) {
31+
return flattenStyle;
32+
}
33+
34+
const marginBottom =
35+
typeof flattenStyle.marginBottom === 'number'
36+
? flattenStyle.marginBottom
3637
: 0;
37-
return typeof paddingBottom === 'number' ? paddingBottom : 0;
38-
}, [flattenContainerStyle]);
39-
const containerStyle = useMemo(() => {
38+
39+
// console.log(paddingBottom, animatedFooterHeight.value);
40+
4041
return {
41-
...flattenContainerStyle,
42-
paddingBottom: 0,
42+
...flattenStyle,
43+
marginBottom: marginBottom + animatedFooterHeight.value,
4344
};
44-
}, [flattenContainerStyle]);
45-
const spaceStyle = useAnimatedStyle(
46-
() => ({
47-
opacity: 0,
48-
height: enableFooterMarginAdjustment
49-
? animatedFooterHeight.value + containerStylePaddingBottom
50-
: containerStylePaddingBottom,
51-
}),
52-
[
53-
enableFooterMarginAdjustment,
54-
containerStylePaddingBottom,
55-
animatedFooterHeight,
56-
]
57-
);
45+
}, [flattenStyle, enableFooterMarginAdjustment, animatedFooterHeight]);
5846
//#endregion
5947

6048
//#region callbacks
@@ -72,14 +60,16 @@ function BottomSheetViewComponent({
7260
onLayout(event);
7361
}
7462

75-
print({
76-
component: BottomSheetView.displayName,
77-
method: 'handleLayout',
78-
category: 'layout',
79-
params: {
80-
height: event.nativeEvent.layout.height,
81-
},
82-
});
63+
if (__DEV__) {
64+
print({
65+
component: BottomSheetView.displayName,
66+
method: 'handleLayout',
67+
category: 'layout',
68+
params: {
69+
height: event.nativeEvent.layout.height,
70+
},
71+
});
72+
}
8373
},
8474
[onLayout, animatedContentHeight, enableDynamicSizing]
8575
);
@@ -90,9 +80,8 @@ function BottomSheetViewComponent({
9080

9181
//render
9282
return (
93-
<Animated.View onLayout={handleLayout} style={containerStyle} {...rest}>
83+
<Animated.View {...rest} onLayout={handleLayout} style={containerStyle}>
9484
{children}
95-
<Animated.View pointerEvents="none" style={spaceStyle} />
9685
</Animated.View>
9786
);
9887
}

0 commit comments

Comments
 (0)