Skip to content
This repository has been archived by the owner on Feb 8, 2020. It is now read-only.

Commit

Permalink
fix: expose the header height even if not floating
Browse files Browse the repository at this point in the history
  • Loading branch information
satya164 committed Jan 5, 2020
1 parent edeb2e8 commit 12d9083
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 39 deletions.
4 changes: 2 additions & 2 deletions packages/stack/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ export {
* Utilities
*/
export { default as CardAnimationContext } from './utils/CardAnimationContext';
export { default as FloatingHeaderHeightContext } from './utils/FloatingHeaderHeightContext';
export { default as HeaderHeightContext } from './utils/HeaderHeightContext';
export { default as GestureHandlerRefContext } from './utils/GestureHandlerRefContext';

export { default as useCardAnimation } from './utils/useCardAnimation';
export { default as useFloatingHeaderHeight } from './utils/useFloatingHeaderHeight';
export { default as useHeaderHeight } from './utils/useHeaderHeight';
export { default as useGestureHandlerRef } from './utils/useGestureHandlerRef';

/**
Expand Down
3 changes: 0 additions & 3 deletions packages/stack/src/utils/FloatingHeaderHeightContext.tsx

This file was deleted.

3 changes: 3 additions & 0 deletions packages/stack/src/utils/HeaderHeightContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import * as React from 'react';

export default React.createContext<number | undefined>(undefined);
14 changes: 0 additions & 14 deletions packages/stack/src/utils/useFloatingHeaderHeight.tsx

This file was deleted.

14 changes: 14 additions & 0 deletions packages/stack/src/utils/useHeaderHeight.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import * as React from 'react';
import HeaderHeightContext from './HeaderHeightContext';

export default function useFloatingHeaderHeight() {
const height = React.useContext(HeaderHeightContext);

if (height === undefined) {
throw new Error(
"Couldn't find the header height. Are you inside a screen in Stack?"
);
}

return height;
}
18 changes: 12 additions & 6 deletions packages/stack/src/views/Stack/CardContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { StackNavigationState } from '@react-navigation/routers';
import { Route, useTheme } from '@react-navigation/native';
import { Props as HeaderContainerProps } from '../Header/HeaderContainer';
import Card from './Card';
import FloatingHeaderHeightContext from '../../utils/FloatingHeaderHeightContext';
import HeaderHeightContext from '../../utils/HeaderHeightContext';
import { Scene, Layout, StackHeaderMode, TransitionPreset } from '../../types';

type Props = TransitionPreset & {
Expand Down Expand Up @@ -48,7 +48,11 @@ type Props = TransitionPreset & {
headerMode: StackHeaderMode;
headerShown?: boolean;
headerTransparent?: boolean;
floatingHeaderHeight: number;
headerHeight: number;
onHeaderHeightChange: (props: {
route: Route<string>;
height: number;
}) => void;
};

export default function CardContainer({
Expand All @@ -59,7 +63,6 @@ export default function CardContainer({
cardStyleInterpolator,
closing,
gesture,
floatingHeaderHeight,
focused,
gestureDirection,
gestureEnabled,
Expand All @@ -70,6 +73,8 @@ export default function CardContainer({
headerShown,
headerStyleInterpolator,
headerTransparent,
headerHeight,
onHeaderHeightChange,
index,
layout,
onCloseRoute,
Expand Down Expand Up @@ -150,17 +155,17 @@ export default function CardContainer({
pointerEvents="box-none"
containerStyle={
headerMode === 'float' && !headerTransparent && headerShown !== false
? { marginTop: floatingHeaderHeight }
? { marginTop: headerHeight }
: null
}
contentStyle={[{ backgroundColor: colors.background }, cardStyle]}
style={StyleSheet.absoluteFill}
>
<View style={styles.container}>
<View style={styles.scene}>
<FloatingHeaderHeightContext.Provider value={floatingHeaderHeight}>
<HeaderHeightContext.Provider value={headerHeight}>
{renderScene({ route: scene.route })}
</FloatingHeaderHeightContext.Provider>
</HeaderHeightContext.Provider>
</View>
{headerMode === 'screen'
? renderHeader({
Expand All @@ -171,6 +176,7 @@ export default function CardContainer({
state,
getPreviousRoute,
styleInterpolator: headerStyleInterpolator,
onContentHeightChange: onHeaderHeightChange,
})
: null}
</View>
Expand Down
29 changes: 15 additions & 14 deletions packages/stack/src/views/Stack/CardStack.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ type State = {
scenes: Scene<Route<string>>[];
gestures: GestureValues;
layout: Layout;
floatingHeaderHeights: Record<string, number>;
headerHeights: Record<string, number>;
};

const EPSILON = 1e-5;
Expand Down Expand Up @@ -112,7 +112,7 @@ const MaybeScreen = ({

const FALLBACK_DESCRIPTOR = Object.freeze({ options: {} });

const getFloatingHeaderHeights = (
const getHeaderHeights = (
routes: Route<string>[],
insets: EdgeInsets,
descriptors: StackDescriptorMap,
Expand Down Expand Up @@ -283,12 +283,12 @@ export default class CardStack extends React.Component<Props, State> {
}),
gestures,
descriptors: props.descriptors,
floatingHeaderHeights: getFloatingHeaderHeights(
headerHeights: getHeaderHeights(
props.routes,
props.insets,
state.descriptors,
state.layout,
state.floatingHeaderHeights
state.headerHeights
),
};
}
Expand All @@ -304,7 +304,7 @@ export default class CardStack extends React.Component<Props, State> {
// This is not a great heuristic here. We don't know synchronously
// on mount what the header height is so we have just used the most
// common cases here.
floatingHeaderHeights: {},
headerHeights: {},
};

private handleLayout = (e: LayoutChangeEvent) => {
Expand All @@ -319,7 +319,7 @@ export default class CardStack extends React.Component<Props, State> {

return {
layout,
floatingHeaderHeights: getFloatingHeaderHeights(
headerHeights: getHeaderHeights(
props.routes,
props.insets,
state.descriptors,
Expand All @@ -330,23 +330,23 @@ export default class CardStack extends React.Component<Props, State> {
});
};

private handleFloatingHeaderLayout = ({
private handleHeaderLayout = ({
route,
height,
}: {
route: Route<string>;
height: number;
}) => {
this.setState(({ floatingHeaderHeights }) => {
const previousHeight = floatingHeaderHeights[route.key];
this.setState(({ headerHeights }) => {
const previousHeight = headerHeights[route.key];

if (previousHeight === height) {
return null;
}

return {
floatingHeaderHeights: {
...floatingHeaderHeights,
headerHeights: {
...headerHeights,
[route.key]: height,
},
};
Expand Down Expand Up @@ -375,7 +375,7 @@ export default class CardStack extends React.Component<Props, State> {
onPageChangeCancel,
} = this.props;

const { scenes, layout, gestures, floatingHeaderHeights } = this.state;
const { scenes, layout, gestures, headerHeights } = this.state;

const focusedRoute = state.routes[state.index];
const focusedDescriptor = descriptors[focusedRoute.key];
Expand Down Expand Up @@ -521,7 +521,8 @@ export default class CardStack extends React.Component<Props, State> {
onPageChangeConfirm={onPageChangeConfirm}
onPageChangeCancel={onPageChangeCancel}
gestureResponseDistance={gestureResponseDistance}
floatingHeaderHeight={floatingHeaderHeights[route.key]}
headerHeight={headerHeights[route.key]}
onHeaderHeightChange={this.handleHeaderLayout}
getPreviousRoute={getPreviousRoute}
headerMode={headerMode}
headerShown={headerShown}
Expand All @@ -548,7 +549,7 @@ export default class CardStack extends React.Component<Props, State> {
scenes,
state,
getPreviousRoute,
onContentHeightChange: this.handleFloatingHeaderLayout,
onContentHeightChange: this.handleHeaderLayout,
styleInterpolator:
focusedOptions.headerStyleInterpolator !== undefined
? focusedOptions.headerStyleInterpolator
Expand Down

0 comments on commit 12d9083

Please sign in to comment.