From 133b59cd175ddc899dff3b56bf3a0514c0c91ae6 Mon Sep 17 00:00:00 2001 From: Satyajit Sahoo Date: Sun, 5 Jan 2020 14:07:16 +0100 Subject: [PATCH] feat: expose header height in context --- packages/stack/src/index.tsx | 9 +- ...onContext.tsx => CardAnimationContext.tsx} | 0 .../src/utils/FloatingHeaderHeightContext.tsx | 3 + ...ntext.tsx => GestureHandlerRefContext.tsx} | 0 packages/stack/src/utils/useCardAnimation.tsx | 14 ++++ .../src/utils/useFloatingHeaderHeight.tsx | 14 ++++ .../stack/src/utils/useGestureHandlerRef.tsx | 14 ++++ packages/stack/src/views/Stack/Card.tsx | 82 +++++++++---------- .../stack/src/views/Stack/CardContainer.tsx | 7 +- 9 files changed, 99 insertions(+), 44 deletions(-) rename packages/stack/src/utils/{StackCardAnimationContext.tsx => CardAnimationContext.tsx} (100%) create mode 100644 packages/stack/src/utils/FloatingHeaderHeightContext.tsx rename packages/stack/src/utils/{StackGestureContext.tsx => GestureHandlerRefContext.tsx} (100%) create mode 100644 packages/stack/src/utils/useCardAnimation.tsx create mode 100644 packages/stack/src/utils/useFloatingHeaderHeight.tsx create mode 100644 packages/stack/src/utils/useGestureHandlerRef.tsx diff --git a/packages/stack/src/index.tsx b/packages/stack/src/index.tsx index 167002ee..11c38fdb 100644 --- a/packages/stack/src/index.tsx +++ b/packages/stack/src/index.tsx @@ -36,8 +36,13 @@ export { /** * Utilities */ -export { default as StackGestureContext } from './utils/StackGestureContext'; -export { default as StackCardAnimationContext } from './utils/StackCardAnimationContext'; +export { default as CardAnimationContext } from './utils/CardAnimationContext'; +export { default as FloatingHeaderHeightContext } from './utils/FloatingHeaderHeightContext'; +export { default as GestureHandlerRefContext } from './utils/GestureHandlerRefContext'; + +export { default as useCardAnimation } from './utils/useCardAnimation'; +export { default as useFloatingHeaderHeight } from './utils/useFloatingHeaderHeight'; +export { default as useGestureHandlerRef } from './utils/useGestureHandlerRef'; /** * Types diff --git a/packages/stack/src/utils/StackCardAnimationContext.tsx b/packages/stack/src/utils/CardAnimationContext.tsx similarity index 100% rename from packages/stack/src/utils/StackCardAnimationContext.tsx rename to packages/stack/src/utils/CardAnimationContext.tsx diff --git a/packages/stack/src/utils/FloatingHeaderHeightContext.tsx b/packages/stack/src/utils/FloatingHeaderHeightContext.tsx new file mode 100644 index 00000000..f31f9dac --- /dev/null +++ b/packages/stack/src/utils/FloatingHeaderHeightContext.tsx @@ -0,0 +1,3 @@ +import * as React from 'react'; + +export default React.createContext(0); diff --git a/packages/stack/src/utils/StackGestureContext.tsx b/packages/stack/src/utils/GestureHandlerRefContext.tsx similarity index 100% rename from packages/stack/src/utils/StackGestureContext.tsx rename to packages/stack/src/utils/GestureHandlerRefContext.tsx diff --git a/packages/stack/src/utils/useCardAnimation.tsx b/packages/stack/src/utils/useCardAnimation.tsx new file mode 100644 index 00000000..26d4529a --- /dev/null +++ b/packages/stack/src/utils/useCardAnimation.tsx @@ -0,0 +1,14 @@ +import * as React from 'react'; +import CardAnimationContext from './CardAnimationContext'; + +export default function useCardAnimation() { + const animation = React.useContext(CardAnimationContext); + + if (animation === undefined) { + throw new Error( + "Couldn't find values for card animation. Are you inside a screen in Stack?" + ); + } + + return animation; +} diff --git a/packages/stack/src/utils/useFloatingHeaderHeight.tsx b/packages/stack/src/utils/useFloatingHeaderHeight.tsx new file mode 100644 index 00000000..c39ddba7 --- /dev/null +++ b/packages/stack/src/utils/useFloatingHeaderHeight.tsx @@ -0,0 +1,14 @@ +import * as React from 'react'; +import FloatingHeaderHeightContext from './FloatingHeaderHeightContext'; + +export default function useFloatingHeaderHeight() { + const height = React.useContext(FloatingHeaderHeightContext); + + if (height === undefined) { + throw new Error( + "Couldn't find the floating header height. Are you inside a screen in Stack?" + ); + } + + return height; +} diff --git a/packages/stack/src/utils/useGestureHandlerRef.tsx b/packages/stack/src/utils/useGestureHandlerRef.tsx new file mode 100644 index 00000000..0a34f174 --- /dev/null +++ b/packages/stack/src/utils/useGestureHandlerRef.tsx @@ -0,0 +1,14 @@ +import * as React from 'react'; +import StackGestureRefContext from './GestureHandlerRefContext'; + +export default function useGestureHandlerRef() { + const ref = React.useContext(StackGestureRefContext); + + if (ref === undefined) { + throw new Error( + "Couldn't find a ref for gesture handler. Are you inside a screen in Stack?" + ); + } + + return ref; +} diff --git a/packages/stack/src/views/Stack/Card.tsx b/packages/stack/src/views/Stack/Card.tsx index 959de784..7fd0cdbb 100755 --- a/packages/stack/src/views/Stack/Card.tsx +++ b/packages/stack/src/views/Stack/Card.tsx @@ -16,8 +16,8 @@ import { } from 'react-native-gesture-handler'; import { EdgeInsets } from 'react-native-safe-area-context'; import Color from 'color'; -import StackGestureContext from '../../utils/StackGestureContext'; -import StackCardAnimationContext from '../../utils/StackCardAnimationContext'; +import StackGestureRefContext from '../../utils/GestureHandlerRefContext'; +import CardAnimationContext from '../../utils/CardAnimationContext'; import getDistanceForDirection from '../../utils/getDistanceForDirection'; import getInvertedMultiplier from '../../utils/getInvertedMultiplier'; import memoize from '../../utils/memoize'; @@ -421,48 +421,48 @@ export default class Card extends React.Component { : false; return ( - - - {overlayEnabled && overlayStyle ? ( - - ) : null} + + {overlayEnabled && overlayStyle ? ( + ) : null} + + - - - {shadowEnabled && shadowStyle && !isTransparent ? ( - - ) : null} - - + + {shadowEnabled && shadowStyle && !isTransparent ? ( + + ) : null} + + + {children} - - - - - - - + + + + + + + ); } } diff --git a/packages/stack/src/views/Stack/CardContainer.tsx b/packages/stack/src/views/Stack/CardContainer.tsx index 033c4965..fe3c095d 100644 --- a/packages/stack/src/views/Stack/CardContainer.tsx +++ b/packages/stack/src/views/Stack/CardContainer.tsx @@ -4,6 +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 { Scene, Layout, StackHeaderMode, TransitionPreset } from '../../types'; type Props = TransitionPreset & { @@ -156,7 +157,11 @@ export default function CardContainer({ style={StyleSheet.absoluteFill} > - {renderScene({ route: scene.route })} + + + {renderScene({ route: scene.route })} + + {headerMode === 'screen' ? renderHeader({ mode: 'screen',