Skip to content

Commit 19866ef

Browse files
authored
refactor: separate Screen native props and public API (#2423)
## Description Separating native types and public API for Screen component. The core idea behind this change is that I removed hardcoded as ScreenProps and try to conform public API to NativeProps accepted by Codegen, with some transformations if necessary. ## Changes The biggest change is not casting Native to Component with ScreenProps. ## Screenshots / GIFs N/A ## Checklist - [ ] Included code example that can be used to test this change - [ ] Updated TS types - [ ] Updated documentation: <!-- For adding new props to native-stack --> - [ ] https://github.com/software-mansion/react-native-screens/blob/main/guides/GUIDE_FOR_LIBRARY_AUTHORS.md - [ ] https://github.com/software-mansion/react-native-screens/blob/main/native-stack/README.md - [ ] https://github.com/software-mansion/react-native-screens/blob/main/src/types.tsx - [ ] https://github.com/software-mansion/react-native-screens/blob/main/src/native-stack/types.tsx - [ ] Ensured that CI passes
1 parent b1c9e40 commit 19866ef

File tree

2 files changed

+41
-21
lines changed

2 files changed

+41
-21
lines changed

src/components/Screen.tsx

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,20 @@ import {
1414
} from '../core';
1515

1616
// Native components
17-
import ScreenNativeComponent from '../fabric/ScreenNativeComponent';
18-
import ModalScreenNativeComponent from '../fabric/ModalScreenNativeComponent';
17+
import ScreenNativeComponent, {
18+
NativeProps as ScreenNativeComponentProps,
19+
} from '../fabric/ScreenNativeComponent';
20+
import ModalScreenNativeComponent, {
21+
NativeProps as ModalScreenNativeComponentProps,
22+
} from '../fabric/ModalScreenNativeComponent';
1923
import { usePrevious } from './helpers/usePrevious';
2024

21-
type NativeScreenProps = Omit<
22-
ScreenProps,
23-
'sheetInitialDetentIndex' | 'sheetLargestUndimmedDetentIndex'
24-
> & {
25-
sheetInitialDetent: number;
26-
sheetLargestUndimmedDetent: number;
27-
};
28-
29-
const NativeScreen: React.ComponentType<NativeScreenProps> =
30-
ScreenNativeComponent as React.ComponentType<NativeScreenProps>;
31-
const AnimatedNativeScreen = Animated.createAnimatedComponent(NativeScreen);
25+
type NativeProps = ScreenNativeComponentProps | ModalScreenNativeComponentProps;
26+
const AnimatedNativeScreen = Animated.createAnimatedComponent(
27+
ScreenNativeComponent,
28+
);
3229
const AnimatedNativeModalScreen = Animated.createAnimatedComponent(
33-
ModalScreenNativeComponent as React.ComponentType<NativeScreenProps>,
30+
ModalScreenNativeComponent,
3431
);
3532

3633
// Incomplete type, all accessible properties available at:
@@ -196,6 +193,11 @@ export const InnerScreen = React.forwardRef<View, ScreenProps>(
196193
sheetInitialDetentIndex = 0,
197194
// Other
198195
stackPresentation,
196+
// Events for override
197+
onAppear,
198+
onDisappear,
199+
onWillAppear,
200+
onWillDisappear,
199201
} = rest;
200202

201203
if (enabled && isNativePlatformSupported) {
@@ -273,6 +275,22 @@ export const InnerScreen = React.forwardRef<View, ScreenProps>(
273275
<DelayedFreeze freeze={freezeOnBlur && activityState === 0}>
274276
<AnimatedScreen
275277
{...props}
278+
/**
279+
* This messy override is to conform NativeProps used by codegen and
280+
* our Public API. To see reasoning go to this PR:
281+
* https://github.com/software-mansion/react-native-screens/pull/2423#discussion_r1810616995
282+
*/
283+
onAppear={onAppear as NativeProps['onAppear']}
284+
onDisappear={onDisappear as NativeProps['onDisappear']}
285+
onWillAppear={onWillAppear as NativeProps['onWillAppear']}
286+
onWillDisappear={onWillDisappear as NativeProps['onWillDisappear']}
287+
onGestureCancel={
288+
(onGestureCancel as NativeProps['onGestureCancel']) ??
289+
(() => {
290+
// for internal use
291+
})
292+
}
293+
//
276294
// Hierarchy of screens is handled on the native side and setting zIndex value causes this issue:
277295
// https://github.com/software-mansion/react-native-screens/issues/2345
278296
// With below change of zIndex, we force RN diffing mechanism to NOT include detaching and attaching mutation in one transaction.
@@ -310,12 +328,6 @@ export const InnerScreen = React.forwardRef<View, ScreenProps>(
310328
],
311329
{ useNativeDriver: true },
312330
)
313-
}
314-
onGestureCancel={
315-
onGestureCancel ??
316-
(() => {
317-
// for internal use
318-
})
319331
}>
320332
{!isNativeStack ? ( // see comment of this prop in types.tsx for information why it is needed
321333
children

src/fabric/ModalScreenNativeComponent.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ type HeaderHeightChangeEvent = Readonly<{
2727
headerHeight: Double;
2828
}>;
2929

30+
type SheetDetentChangedEvent = Readonly<{
31+
index: Int32;
32+
isStable: boolean;
33+
}>;
34+
3035
type GestureResponseDistanceType = Readonly<{
3136
start: Float;
3237
end: Float;
@@ -53,7 +58,8 @@ type StackAnimation =
5358
| 'slide_from_left'
5459
| 'slide_from_bottom'
5560
| 'fade_from_bottom'
56-
| 'ios';
61+
| 'ios_from_right'
62+
| 'ios_from_left';
5763

5864
type SwipeDirection = 'vertical' | 'horizontal';
5965

@@ -70,12 +76,14 @@ export interface NativeProps extends ViewProps {
7076
onTransitionProgress?: DirectEventHandler<TransitionProgressEvent>;
7177
onGestureCancel?: DirectEventHandler<ScreenEvent>;
7278
onHeaderBackButtonClicked?: DirectEventHandler<ScreenEvent>;
79+
onSheetDetentChanged?: DirectEventHandler<SheetDetentChangedEvent>;
7380
sheetAllowedDetents?: number[];
7481
sheetLargestUndimmedDetent?: WithDefault<Int32, -1>;
7582
sheetGrabberVisible?: WithDefault<boolean, false>;
7683
sheetCornerRadius?: WithDefault<Float, -1.0>;
7784
sheetExpandsWhenScrolledToEdge?: WithDefault<boolean, false>;
7885
sheetInitialDetent?: WithDefault<Int32, 0>;
86+
sheetElevation?: WithDefault<Int32, 24>;
7987
customAnimationOnSwipe?: boolean;
8088
fullScreenSwipeEnabled?: boolean;
8189
fullScreenSwipeShadowEnabled?: boolean;

0 commit comments

Comments
 (0)