From 1ab39eb577c8e71c2bd6efe4edd3966e46f8d0e2 Mon Sep 17 00:00:00 2001 From: Julian Hundeloh Date: Mon, 8 Jul 2019 10:02:34 +0200 Subject: [PATCH] fix: consistent elevation behavior across platforms (#1176) --- src/components/Surface.tsx | 4 +-- src/index.tsx | 1 + src/styles/shadow.tsx | 54 ++++++++++++++++++++++++-------------- 3 files changed, 37 insertions(+), 22 deletions(-) diff --git a/src/components/Surface.tsx b/src/components/Surface.tsx index 3e81c6a..c530d1d 100644 --- a/src/components/Surface.tsx +++ b/src/components/Surface.tsx @@ -56,14 +56,14 @@ class Surface extends React.Component { render() { const { style, theme, ...rest } = this.props; const flattenedStyles = StyleSheet.flatten(style) || {}; - const { elevation }: any = flattenedStyles; + const { elevation }: ViewStyle = flattenedStyles; return ( diff --git a/src/index.tsx b/src/index.tsx index 32c6278..dcfdc8e 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -10,6 +10,7 @@ export { withTheme, ThemeProvider } from './core/theming'; export { default as Provider } from './core/Provider'; export { default as DefaultTheme } from './styles/DefaultTheme'; export { default as DarkTheme } from './styles/DarkTheme'; +export { default as shadow } from './styles/shadow'; import * as Avatar from './components/Avatar/Avatar'; import * as List from './components/List/List'; diff --git a/src/styles/shadow.tsx b/src/styles/shadow.tsx index f278a63..36a09d0 100644 --- a/src/styles/shadow.tsx +++ b/src/styles/shadow.tsx @@ -1,20 +1,34 @@ import * as Colors from './colors'; import { Animated } from 'react-native'; -export default function shadow(elevation: number | Animated.Value) { - let height, radius; +const SHADOW_COLOR = Colors.black; +const SHADOW_OPACITY = 0.24; +export default function shadow(elevation: number | Animated.Value = 0) { if (elevation instanceof Animated.Value) { - height = elevation.interpolate({ - inputRange: [0, 1, 2, 3, 8, 24], - outputRange: [0, 0.5, 0.75, 2, 7, 23], - }); + const inputRange = [0, 1, 2, 3, 8, 24]; - radius = elevation.interpolate({ - inputRange: [0, 1, 2, 3, 8, 24], - outputRange: [0, 0.75, 1.5, 3, 8, 24], - }); + return { + shadowColor: SHADOW_COLOR, + shadowOffset: { + width: new Animated.Value(0), + height: elevation.interpolate({ + inputRange, + outputRange: [0, 0.5, 0.75, 2, 7, 23], + }), + }, + shadowOpacity: new Animated.Value(SHADOW_OPACITY), + shadowRadius: elevation.interpolate({ + inputRange, + outputRange: [0, 0.75, 1.5, 3, 8, 24], + }), + }; } else { + if (elevation === 0) { + return {}; + } + + let height, radius; switch (elevation) { case 1: height = 0.5; @@ -28,15 +42,15 @@ export default function shadow(elevation: number | Animated.Value) { height = elevation - 1; radius = elevation; } - } - return { - shadowColor: Colors.black, - shadowOffset: { - width: 0, - height, - }, - shadowOpacity: 0.24, - shadowRadius: radius, - }; + return { + shadowColor: SHADOW_COLOR, + shadowOffset: { + width: 0, + height, + }, + shadowOpacity: SHADOW_OPACITY, + shadowRadius: radius, + }; + } }