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

fix: Allow Reanimated Screen to check large header #1915

Merged
merged 9 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
22 changes: 22 additions & 0 deletions src/native-stack/utils/getStatusBarHeight.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Rect } from 'react-native-safe-area-context';
import { Platform } from 'react-native';

export default function getStatusBarHeight(
topInset: number,
dimensions: Rect,
isStatusBarTranslucent: boolean
) {
if (Platform.OS === 'ios') {
// It looks like some iOS devices don't have strictly set status bar height to 44.
// Thus, if the top inset is higher than 50, then the device should have a dynamic island.
// On models with Dynamic Island the status bar height is smaller than the safe area top inset by 5 pixels.
// See https://developer.apple.com/forums/thread/662466 for more details about status bar height.
const hasDynamicIsland = topInset > 50;
return hasDynamicIsland ? topInset - 5 : topInset;
} else if (Platform.OS === 'android') {
// On Android we should also rely on frame's y-axis position, as topInset is 0 on visible status bar.
return isStatusBarTranslucent ? topInset : dimensions.y;
}

return topInset;
}
23 changes: 2 additions & 21 deletions src/native-stack/views/NativeStackView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {
PartialState,
} from '@react-navigation/native';
import {
Rect,
useSafeAreaFrame,
useSafeAreaInsets,
} from 'react-native-safe-area-context';
Expand All @@ -31,6 +30,7 @@ import {
import HeaderConfig from './HeaderConfig';
import SafeAreaProviderCompat from '../utils/SafeAreaProviderCompat';
import getDefaultHeaderHeight from '../utils/getDefaultHeaderHeight';
import getStatusBarHeight from '../utils/getStatusBarHeight';
import HeaderHeightContext from '../utils/HeaderHeightContext';
import AnimatedHeaderHeightContext from '../utils/AnimatedHeaderHeightContext';

Expand Down Expand Up @@ -264,6 +264,7 @@ const RouteView = ({
key={route.key}
enabled
isNativeStack
screenDescriptor={descriptors[route.key]}
tboba marked this conversation as resolved.
Show resolved Hide resolved
style={StyleSheet.absoluteFill}
sheetAllowedDetents={sheetAllowedDetents}
sheetLargestUndimmedDetent={sheetLargestUndimmedDetent}
Expand Down Expand Up @@ -422,26 +423,6 @@ export default function NativeStackView(props: Props) {
);
}

function getStatusBarHeight(
topInset: number,
dimensions: Rect,
isStatusBarTranslucent: boolean
) {
if (Platform.OS === 'ios') {
// It looks like some iOS devices don't have strictly set status bar height to 44.
// Thus, if the top inset is higher than 50, then the device should have a dynamic island.
// On models with Dynamic Island the status bar height is smaller than the safe area top inset by 5 pixels.
// See https://developer.apple.com/forums/thread/662466 for more details about status bar height.
const hasDynamicIsland = topInset > 50;
return hasDynamicIsland ? topInset - 5 : topInset;
} else if (Platform.OS === 'android') {
// On Android we should also rely on frame's y-axis position, as topInset is 0 on visible status bar.
return isStatusBarTranslucent ? topInset : dimensions.y;
}

return topInset;
}

const styles = StyleSheet.create({
container: {
flex: 1,
Expand Down
20 changes: 12 additions & 8 deletions src/reanimated/ReanimatedNativeStackScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
useSafeAreaInsets,
} from 'react-native-safe-area-context';
import getDefaultHeaderHeight from '../native-stack/utils/getDefaultHeaderHeight';
import getStatusBarHeight from '../native-stack/utils/getStatusBarHeight';
import ReanimatedHeaderHeightContext from './ReanimatedHeaderHeightContext';

const AnimatedScreen = Animated.createAnimatedComponent(
Expand All @@ -31,23 +32,26 @@ const ReanimatedNativeStackScreen = React.forwardRef<
ScreenProps
>((props, ref) => {
const { children, ...rest } = props;
const { stackPresentation = 'push' } = rest;
const { stackPresentation = 'push', screenDescriptor } = rest;

const dimensions = useSafeAreaFrame();
const topInset = useSafeAreaInsets().top;
let statusBarHeight = topInset;
const hasDynamicIsland = Platform.OS === 'ios' && topInset === 59;
if (hasDynamicIsland) {
// On models with Dynamic Island the status bar height is smaller than the safe area top inset.
statusBarHeight = 54;
}
const isStatusBarTranslucent = rest.statusBarTranslucent ?? false;
const statusBarHeight = getStatusBarHeight(
topInset,
dimensions,
isStatusBarTranslucent
);

const isLargeHeader = screenDescriptor?.options.headerLargeTitle ?? false;

// Default header height, normally used in `useHeaderHeight` hook.
// Here, it is used for returning a default value for shared value.
const defaultHeaderHeight = getDefaultHeaderHeight(
dimensions,
statusBarHeight,
stackPresentation
stackPresentation,
isLargeHeader
);

const cachedHeaderHeight = React.useRef(defaultHeaderHeight);
Expand Down
2 changes: 1 addition & 1 deletion src/reanimated/useReanimatedHeaderHeight.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from 'react';
import ReanimatedHeaderHeightContext from './ReanimatedHeaderHeightContext';

export default function useReanimatedTransitionProgress() {
export default function useReanimatedHeaderHeight() {
tboba marked this conversation as resolved.
Show resolved Hide resolved
const height = React.useContext(ReanimatedHeaderHeightContext);

if (height === undefined) {
Expand Down
2 changes: 2 additions & 0 deletions src/types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
TextInputFocusEventData,
ColorValue,
} from 'react-native';
import { NativeStackDescriptor } from './native-stack/types';

export type SearchBarCommands = {
focus: () => void;
Expand Down Expand Up @@ -97,6 +98,7 @@ export interface ScreenProps extends ViewProps {
active?: 0 | 1 | Animated.AnimatedInterpolation<number>;
activityState?: 0 | 1 | 2 | Animated.AnimatedInterpolation<number>;
children?: React.ReactNode;
screenDescriptor?: NativeStackDescriptor;
/**
* Boolean indicating that swipe dismissal should trigger animation provided by `stackAnimation`. Defaults to `false`.
*
Expand Down
Loading