-
-
Notifications
You must be signed in to change notification settings - Fork 767
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dynamic Bottom Sheet height based on Children height #32
Comments
hi @SatyaFariz , thanks for submitting this feature request, i will pick it up as soon as i get free 👍 |
This is a good one. |
Another similar enhancement would be to allow the snapPoints to change (with something like an onLayout callback) and then have the sheet handle animating to the new position |
I have sort of a singleton Bottom Sheet component and So I am wondering whether dynamic |
@mrmarktyy I've managed to get it working by setting a key prop to BottomSheet component. So, when it's children view is indeed calculated, it invalidates the layout:
|
Thanks @fabcall good one, i'll give it try. |
The only issue is that it goes instantly to the new point instead of animating there. It would be nice to have the |
my initial idea is to add a new value type i'll look into it this weekend :) |
@gorhom sounds nice to me. Keep in mind that, if content height is bigger than screen, you could replace "auto" with "100%" and keep the default behavior ;) |
hi guys @SatyaFariz, @EricPKerr, @fabcall, @mrmarktyy i have submitted a draft pr for this feature and while i finish it up, i would appreciate if you could test it yarn add ssh://git@github.com:gorhom/react-native-bottom-sheet#feature/dynamic-snap-point Sample Code/* eslint-disable react-native/no-inline-styles */
import React, { useMemo, useRef } from 'react';
import { View, StyleSheet } from 'react-native';
import BottomSheet from '@gorhom/bottom-sheet';
const BasicExample = () => {
// hooks
const bottomSheetRef = useRef<BottomSheet>(null);
// variables
const snapPoints = useMemo(() => [100, 200], []);
return (
<View style={styles.container}>
<BottomSheet
ref={bottomSheetRef}
snapPoints={snapPoints}
initialSnapIndex={0}
shouldMeasureContentHeight={true} // < new prop 🔥
>
<View
style={{
height: 500,
backgroundColor: 'white',
}}
/>
</BottomSheet>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
padding: 24,
backgroundColor: 'grey',
},
contentContainer: {
height: 500,
backgroundColor: 'white',
},
});
export default BasicExample; |
@gorhom thank you Code example
|
thanks @avet-m for spotting this issue,, i have pushed the fix already, please try again and let me know. yarn add ssh://git@github.com:gorhom/react-native-bottom-sheet#feature/dynamic-snap-point |
@gorhom now the scroll of |
@gorhom using
will show the component at the specified point (20). Even though you can drag it up to its measured height, it animates back to point 20. |
a quick update, managed to get |
This ia simply beautiful 😍. Amazing work, @gorhom |
quick update:
i can't promise a specific date,, but i will try my best to get out asap :) |
hi there 👋 here is a final pr to test this feature: #81 |
I just pushed an alpha release for v2, please try it and let me know :) also check out the new documents website ( still in progress ) 🎉 yarn add @gorhom/bottom-sheet@2.0.0-alpha.2 |
Hey @gorhom is this available in v2? I don't see it on the docs. Thanks! |
@nandorojo yes! it is available for |
Still not working for BottomSheetScrollView? |
Hey there - I may be totally missing something on the example @gorhom, but is contentHeight being set when the size of the content changes? As far as I can tell, onLayout only fires on the first render, not when the content size changes. |
@gorhom Thank you very much for this fix... I would like to point out 2 issues with dynamic heights.
|
@gorhom I have BottomSheet with dynamic height content inside. This content is actually a separate component that shows some data. I set this data by clicking on an element outside the bottom sheet and then open the bottom sheet. Dynamic height is calculated but BottomSheet behaves very oddly: it jumps up/down or even closes immediately after content height change. Please help. |
I'm also facing the problem(v3). The bottom sheet goes down immediately when calculating the new height. Please help. Thanks |
Could you guys test on ‘v4’ ? Thanks |
I had some issues with |
For me scrollables with CONTENT_HEGIHT still aren't working My code is looking like this: interface ContentHeightBottomSheetProps {
children: any;
bottomSheetModalRef: React.MutableRefObject<BottomSheetModalMethods>;
initialSnapPoints?: (string | number)[];
allowClosingOnPanDown?: boolean;
}
export function ContentHeightBottomSheet({
children,
bottomSheetModalRef,
initialSnapPoints = ['5%', 'CONTENT_HEIGHT'],
allowClosingOnPanDown = true,
}: ContentHeightBottomSheetProps) {
const snapPoints = React.useMemo(() => [...initialSnapPoints], []);
const {
animatedHandleHeight,
animatedSnapPoints,
animatedContentHeight,
handleContentLayout,
} = useBottomSheetDynamicSnapPoints(snapPoints);
return (
<BottomSheetModal
ref={bottomSheetModalRef}
index={1}
backdropComponent={BottomSheetBackdrop}
snapPoints={animatedSnapPoints}
handleHeight={animatedHandleHeight}
contentHeight={animatedContentHeight}
enablePanDownToClose={allowClosingOnPanDown}
>
<BottomSheetScrollView onLayout={handleContentLayout}>
{children}
</BottomSheetScrollView>
</BottomSheetModal>
);
} It works perfectly when the content doesn't cover 100%+ of the screen, but when it does I cannot scroll. Seems like this is the same issue I am experiencing Note that I am using |
@gorhom Any updates on this? Tbh I don't really know if I am doing something I am not supposed to or if scrollables with content height are not available yet :/ Thanks in advance :) |
Hey guys, I was able to make the scrollable content to work by setting the import React, { useMemo, forwardRef, useCallback } from 'react'
import { Dimensions, ScrollView, StyleSheet } from 'react-native'
import RNBottomSheet, {
useBottomSheetDynamicSnapPoints,
BottomSheetBackdrop,
BottomSheetBackdropProps,
BottomSheetScrollView,
} from '@gorhom/bottom-sheet'
interface BottomSheetProps {
children: React.ReactNode
onClose(): void
}
const styles = StyleSheet.create({
backgroundStyle: {
backgroundColor: 'transparent',
},
handleIndicator: {
backgroundColor: 'white',
},
})
const { height } = Dimensions.get('window')
function CustomBottomSheet(
{ children, onClose }: BottomSheetProps,
ref: React.Ref<RNBottomSheet>,
): React.ReactElement {
const initialSnapPoints = useMemo(() => ['CONTENT_HEIGHT'], [])
const {
animatedHandleHeight,
animatedSnapPoints,
animatedContentHeight,
handleContentLayout,
} = useBottomSheetDynamicSnapPoints(initialSnapPoints)
const renderBackdrop = useCallback(
(props: BottomSheetBackdropProps) => (
<BottomSheetBackdrop
{...props}
animatedIndex={animatedContentHeight}
pressBehavior="close"
/>
),
[],
)
return (
<RNBottomSheet
ref={ref}
snapPoints={animatedSnapPoints}
handleHeight={animatedHandleHeight}
contentHeight={animatedContentHeight}
animateOnMount={false}
onClose={onClose}
backdropComponent={renderBackdrop}
backgroundStyle={styles.backgroundStyle}
handleIndicatorStyle={styles.handleIndicator}
enablePanDownToClose
>
<BottomSheetScrollView
onLayout={handleContentLayout}
style={{ maxHeight: height * 0.9 }}
>
{children}
</BottomSheetScrollView>
</RNBottomSheet>
)
} |
I have many screen in same BottomSheet and also I use bottomsheet on map so how can I give dynamic height of all screens.? |
This approach seemed to work but the bottom sheet start crashing after opening it multiple times. Is there a way to fix it? |
Hey @vitorsilvalima Please could you explain, why does this work? making this maxHeight * 0.9 does what? |
is this feature available already? i don't see any in the docs. I have tried this https://gorhom.github.io/react-native-bottom-sheet/hooks/#usebottomsheetdynamicsnappoints but it doesn't work with also, i use this example and it doesn't work well with |
I am confused on this as well, |
Just sharing here that there is a PR ready for this exact feature. |
The solution I opted for with a dynamic snap point bottom sheet with max height is: // ....
const initialSnapPoints = useMemo(() => ["CONTENT_HEIGHT"], [])
const {
animatedHandleHeight,
animatedSnapPoints,
animatedContentHeight,
handleContentLayout,
} = useBottomSheetDynamicSnapPoints(initialSnapPoints)
const maxHeightStyle = useBottomSheetMaxHeight(maxHeight)
return (
<BottomSheetModal
// .... other props
snapPoints={animatedSnapPoints as any}
handleHeight={animatedHandleHeight}
contentHeight={animatedContentHeight}
>
<BottomSheetScrollView
onLayout={handleContentLayout}
style={{
maxHeight: maxHeightStyle,
}}
>
{children}
</BottomSheetScrollView>
</BottomSheetModal>
)
// ... With the custom /*
Designed to take the specified max height, take away top safe
area inset and convert to a decimal
*/
import { useWindowDimensions } from "react-native"
import { useSafeAreaInsets } from "react-native-safe-area-context"
export const useBottomSheetMaxHeight = (maxHeight = "85%"): number => {
const { height: deviceHeight } = useWindowDimensions()
const maxHeightDecimal = parseFloat(`${maxHeight}`) / 100.0
const { top: topSafeAreaInset } = useSafeAreaInsets()
return (deviceHeight - topSafeAreaInset) * maxHeightDecimal
} Hope that helps someone 😄 |
and now that the function useBottomSheetDynamicSnapPoints() becomes deprecated? There is no documentation about it |
@puniker When using the hooks, your IDE should show the JSDoc annotation to inform you that it will soon be deprecated. |
One can try to use |
Please refer to this comment: |
Feature Request
It would be awesome if you add dynamic height feature based on the children height (and still able to define max height of the bottom sheet). Also the snapPoints prop should be relative to the total Bottom Sheet height, not the screen height.
Why it is needed
Sometimes we only need to render only a few items inside the bottom sheet. So there's no need to define the bottom sheet height. It should adjust itself according to the children height. And the snapPoints should be relative to the height of the bottom sheet, not the screen height like in the example.
The text was updated successfully, but these errors were encountered: