Skip to content
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

feat(iOS): sheetInitialDetent support #2367

Merged
merged 10 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions apps/src/tests/Test2002.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,22 @@ import {
DefaultTheme,
NavigationContainer,
} from '@react-navigation/native';
import { createNativeStackNavigator } from '@react-navigation/native-stack';
import {
createNativeStackNavigator,
NativeStackNavigationProp,
} from '@react-navigation/native-stack';

type StackParamList = {
Home: undefined;
formSheet: undefined;
fullScreenModal: undefined;
};

function HomeScreen({ navigation }) {
function HomeScreen({
navigation,
}: {
navigation: NativeStackNavigationProp<StackParamList>;
}) {
return (
<View style={styles.container}>
<Button
Expand All @@ -22,15 +35,19 @@ function HomeScreen({ navigation }) {
);
}

function ModalScreen({ navigation }) {
function ModalScreen({
navigation,
}: {
navigation: NativeStackNavigationProp<StackParamList>;
}) {
return (
<View style={styles.container}>
<Button onPress={() => navigation.goBack()} title="Dismiss" />
</View>
);
}

const RootStack = createNativeStackNavigator();
const RootStack = createNativeStackNavigator<StackParamList>();

export default function App() {
const scheme = useColorScheme();
Expand All @@ -42,7 +59,11 @@ export default function App() {
<RootStack.Screen
name="formSheet"
component={ModalScreen}
options={{ presentation: 'formSheet' }}
options={{
presentation: 'formSheet',
sheetAllowedDetents: [0.3, 0.5, 0.8],
sheetInitialDetent: 1,
}}
/>
<RootStack.Screen
name="fullScreenModal"
Expand Down
1 change: 1 addition & 0 deletions ios/RNSScreen.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ namespace react = facebook::react;
@property (nonatomic) NSNumber *sheetLargestUndimmedDetent;
@property (nonatomic) BOOL sheetGrabberVisible;
@property (nonatomic) CGFloat sheetCornerRadius;
@property (nonatomic) NSInteger sheetInitialDetent;
@property (nonatomic) BOOL sheetExpandsWhenScrolledToEdge;
#endif // !TARGET_OS_TV

Expand Down
12 changes: 12 additions & 0 deletions ios/RNSScreen.mm
Original file line number Diff line number Diff line change
Expand Up @@ -902,6 +902,13 @@ - (void)updateFormSheetPresentationStyle
[self setAllowedDetentsForSheet:sheet
to:[self detentsFromMaxHeightFractions:_sheetAllowedDetents]
animate:NO];

if (_sheetInitialDetent > 0 && _sheetInitialDetent < _sheetAllowedDetents.count) {
UISheetPresentationControllerDetent *detent = sheet.detents[_sheetInitialDetent];
[self setSelectedDetentForSheet:sheet to:detent.identifier animate:YES];
} else if (_sheetInitialDetent != 0) {
RCTLogError(@"[RNScreens] sheetInitialDetent out of bounds for sheetAllowedDetents array");
}
alduzy marked this conversation as resolved.
Show resolved Hide resolved
}
}
} else
Expand Down Expand Up @@ -1151,6 +1158,10 @@ - (void)updateProps:(react::Props::Shared const &)props oldProps:(react::Props::
[self setSheetAllowedDetents:[RNSConvert detentFractionsArrayFromVector:newScreenProps.sheetAllowedDetents]];
}

if (newScreenProps.sheetInitialDetent != oldScreenProps.sheetInitialDetent) {
[self setSheetInitialDetent:newScreenProps.sheetInitialDetent];
}

if (newScreenProps.sheetLargestUndimmedDetent != oldScreenProps.sheetLargestUndimmedDetent) {
[self setSheetLargestUndimmedDetent:[NSNumber numberWithInt:newScreenProps.sheetLargestUndimmedDetent]];
}
Expand Down Expand Up @@ -1864,6 +1875,7 @@ @implementation RNSScreenManager
RCT_EXPORT_VIEW_PROPERTY(sheetLargestUndimmedDetent, NSNumber *);
RCT_EXPORT_VIEW_PROPERTY(sheetGrabberVisible, BOOL);
RCT_EXPORT_VIEW_PROPERTY(sheetCornerRadius, CGFloat);
RCT_EXPORT_VIEW_PROPERTY(sheetInitialDetent, NSInteger);
RCT_EXPORT_VIEW_PROPERTY(sheetExpandsWhenScrolledToEdge, BOOL);
#endif

Expand Down
4 changes: 3 additions & 1 deletion native-stack/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -287,9 +287,11 @@ Defaults to system default.

#### `sheetInitialDetent`

Initial detent for the sheet.
Index of the detent the sheet should expand to after being opened.
Works only when `presentation` is set to `formSheet`.

Defaults to `0` - which represents first detent in the detents array.

#### `sheetGrabberVisible` (iOS only)

Boolean indicating whether the sheet shows a grabber at the top.
Expand Down
1 change: 1 addition & 0 deletions src/fabric/ModalScreenNativeComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export interface NativeProps extends ViewProps {
sheetGrabberVisible?: WithDefault<boolean, false>;
sheetCornerRadius?: WithDefault<Float, -1.0>;
sheetExpandsWhenScrolledToEdge?: WithDefault<boolean, false>;
sheetInitialDetent?: WithDefault<Int32, 0>;
customAnimationOnSwipe?: boolean;
fullScreenSwipeEnabled?: boolean;
fullScreenSwipeShadowEnabled?: boolean;
Expand Down
Loading