Skip to content

Commit

Permalink
fix: match native iOS header height in stack
Browse files Browse the repository at this point in the history
  • Loading branch information
satya164 committed Nov 27, 2022
1 parent 92a2eea commit 7496a87
Show file tree
Hide file tree
Showing 8 changed files with 221 additions and 179 deletions.
11 changes: 10 additions & 1 deletion packages/elements/src/Header/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import HeaderShownContext from './HeaderShownContext';
import HeaderTitle from './HeaderTitle';

type Props = HeaderOptions & {
/**
* Whether the header is in a modal
*/
modal?: boolean;
/**
* Layout of the screen.
*/
Expand Down Expand Up @@ -46,6 +50,7 @@ export default function Header(props: Props) {

const {
layout = frame,
modal = false,
title,
headerTitle: customTitle,
headerTitleAlign = Platform.select({
Expand All @@ -69,7 +74,11 @@ export default function Header(props: Props) {
headerStatusBarHeight = isParentHeaderShown ? 0 : insets.top,
} = props;

const defaultHeight = getDefaultHeaderHeight(layout, headerStatusBarHeight);
const defaultHeight = getDefaultHeaderHeight(
layout,
modal,
headerStatusBarHeight
);

const {
height = defaultHeight,
Expand Down
23 changes: 18 additions & 5 deletions packages/elements/src/Header/getDefaultHeaderHeight.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,30 @@ import type { Layout } from '../types';

export default function getDefaultHeaderHeight(
layout: Layout,
modal: boolean,
statusBarHeight: number
): number {
const isLandscape = layout.width > layout.height;

let headerHeight;

const isLandscape = layout.width > layout.height;

if (Platform.OS === 'ios') {
if (isLandscape && !Platform.isPad) {
headerHeight = 32;
if (Platform.isPad) {
if (modal) {
headerHeight = 56;
} else {
headerHeight = 50;
}
} else {
headerHeight = 44;
if (isLandscape) {
headerHeight = 32;
} else {
if (modal) {
headerHeight = 56;
} else {
headerHeight = 44;
}
}
}
} else if (Platform.OS === 'android') {
headerHeight = 56;
Expand Down
4 changes: 3 additions & 1 deletion packages/elements/src/Screen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import HeaderShownContext from './Header/HeaderShownContext';

type Props = {
focused: boolean;
modal?: boolean;
navigation: NavigationProp<ParamListBase>;
route: RouteProp<ParamListBase>;
header: React.ReactNode;
Expand All @@ -37,6 +38,7 @@ export default function Screen(props: Props) {

const {
focused,
modal = false,
header,
headerShown = true,
headerStatusBarHeight = isParentHeaderShown ? 0 : insets.top,
Expand All @@ -47,7 +49,7 @@ export default function Screen(props: Props) {
} = props;

const [headerHeight, setHeaderHeight] = React.useState(() =>
getDefaultHeaderHeight(dimensions, headerStatusBarHeight)
getDefaultHeaderHeight(dimensions, modal, headerStatusBarHeight)
);

return (
Expand Down
4 changes: 4 additions & 0 deletions packages/stack/src/types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ type SceneOptionsDefaults = TransitionPreset & {
};

export type Scene = {
/**
* Route object for the current screen.
*/
route: Route<string>;
/**
* Descriptor object for the screen.
*/
Expand Down
1 change: 1 addition & 0 deletions packages/stack/src/views/Header/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export default React.memo(function Header({
progress={progress}
insets={insets}
layout={layout}
modal={isModal}
headerBackTitle={
options.headerBackTitle !== undefined
? options.headerBackTitle
Expand Down
9 changes: 8 additions & 1 deletion packages/stack/src/views/Header/HeaderSegment.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type Props = StackHeaderOptions & {
layout: Layout;
title: string;
insets: EdgeInsets;
modal: boolean;
onGoBack?: () => void;
progress: SceneProgress;
styleInterpolator: StackHeaderStyleInterpolator;
Expand Down Expand Up @@ -99,6 +100,7 @@ export default function HeaderSegment(props: Props) {
progress,
insets,
layout,
modal,
onGoBack,
headerTitle: title,
headerLeft: left,
Expand All @@ -120,7 +122,11 @@ export default function HeaderSegment(props: Props) {
...rest
} = props;

const defaultHeight = getDefaultHeaderHeight(layout, headerStatusBarHeight);
const defaultHeight = getDefaultHeaderHeight(
layout,
modal,
headerStatusBarHeight
);

const { height = defaultHeight } = StyleSheet.flatten(
customHeaderStyle || {}
Expand Down Expand Up @@ -172,6 +178,7 @@ export default function HeaderSegment(props: Props) {

return (
<Header
modal={modal}
layout={layout}
headerTitle={headerTitle}
headerLeft={headerLeft}
Expand Down
11 changes: 1 addition & 10 deletions packages/stack/src/views/ModalStatusBarManager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ export default function ModalStatusBarManager({
const { dark: darkTheme } = useTheme();
const [overlapping, setOverlapping] = React.useState(true);

const enabled = layout.width && layout.height > layout.width;
const scale = 1 - 20 / layout.width;
const offset = (insets.top - 34) * scale;

Expand All @@ -31,22 +30,14 @@ export default function ModalStatusBarManager({
)?.translateY;

React.useEffect(() => {
if (!enabled) {
return;
}

const listener = ({ value }: { value: number }) => {
setOverlapping(value < offset);
};

const sub = translateY?.addListener(listener);

return () => translateY?.removeListener(sub);
}, [enabled, offset, translateY]);

if (!enabled) {
return null;
}
}, [offset, translateY]);

const darkContent = dark ?? !darkTheme;

Expand Down

0 comments on commit 7496a87

Please sign in to comment.