Skip to content

Commit 8a009fe

Browse files
authored
Adjust onSettle index of BottomSheet (#946)
* adjust onchange index bottomsheet * support enableDynamicSizing for bottomsheet * update example
1 parent 696b647 commit 8a009fe

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

example/src/BottomSheetExample.tsx

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,22 @@ import { Button } from "@draftbit/core";
55

66
const BottomSheetExample: React.FC = () => {
77
const bottomSheetRef = React.useRef<any>();
8+
const [snapIndex, setSnapIndex] = React.useState(0);
89
return (
910
<View style={{ flex: 1 }}>
10-
<BottomSheet style={{ alignItems: "center" }} ref={bottomSheetRef}>
11+
<BottomSheet
12+
enableDynamicSizing={false}
13+
style={{ alignItems: "center" }}
14+
ref={bottomSheetRef}
15+
onSettle={(newIndex: number) => {
16+
try {
17+
console.log("onSettle", newIndex);
18+
setSnapIndex(newIndex);
19+
} catch (error) {
20+
console.error("Error in onSettle:", error);
21+
}
22+
}}
23+
>
1124
<Text>This is a bottom Sheet</Text>
1225
</BottomSheet>
1326
<Button
@@ -42,6 +55,7 @@ const BottomSheetExample: React.FC = () => {
4255
}
4356
}}
4457
/>
58+
<Text>Snap index: {snapIndex}</Text>
4559
</View>
4660
);
4761
};

packages/core/src/components/BottomSheet/BottomSheet.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export interface BottomSheetProps extends ScrollViewProps {
3535
topBorderRadius?: number;
3636
borderWidth?: number;
3737
borderColor?: string;
38+
enableDynamicSizing?: boolean;
3839
onSettle?: (index: number) => void;
3940
style?: StyleProp<ViewStyle>;
4041
}
@@ -56,6 +57,7 @@ const BottomSheet = React.forwardRef<BottomSheetComponent, BottomSheetProps>(
5657
topBorderRadius = 20,
5758
borderWidth = 1,
5859
borderColor,
60+
enableDynamicSizing = true,
5961
onSettle,
6062
style,
6163
children,
@@ -92,6 +94,7 @@ const BottomSheet = React.forwardRef<BottomSheetComponent, BottomSheetProps>(
9294
return (
9395
<BottomSheetComponent
9496
ref={ref}
97+
enableDynamicSizing={enableDynamicSizing}
9598
snapPoints={mappedSnapPoints}
9699
index={
97100
initialSnapIndex !== undefined
@@ -109,7 +112,13 @@ const BottomSheet = React.forwardRef<BottomSheetComponent, BottomSheetProps>(
109112
borderWidth,
110113
borderColor: borderColor ?? theme.colors.border.brand,
111114
}}
112-
onChange={(index) => onSettle?.(mappedSnapPoints.length - index - 1)}
115+
onChange={(index) =>
116+
// Convert bottom-sheet index to match our top-to-bottom ordering
117+
// When dynamic sizing is enabled, we don't need to subtract 1 since an extra snap point may be added
118+
enableDynamicSizing
119+
? onSettle?.(mappedSnapPoints.length - index)
120+
: onSettle?.(mappedSnapPoints.length - index - 1)
121+
}
113122
>
114123
<BottomSheetScrollView
115124
contentContainerStyle={[styles.contentContainerStyle, style]}

0 commit comments

Comments
 (0)