UNPKG

1.36 kBTypeScriptView Raw
1import * as Colors from './colors';
2import { Animated } from 'react-native';
3
4const SHADOW_COLOR = Colors.black;
5const SHADOW_OPACITY = 0.24;
6
7export default function shadow(elevation: number | Animated.Value = 0) {
8 if (elevation instanceof Animated.Value) {
9 const inputRange = [0, 1, 2, 3, 8, 24];
10
11 return {
12 shadowColor: SHADOW_COLOR,
13 shadowOffset: {
14 width: new Animated.Value(0),
15 height: elevation.interpolate({
16 inputRange,
17 outputRange: [0, 0.5, 0.75, 2, 7, 23],
18 }),
19 },
20 shadowOpacity: elevation.interpolate({
21 inputRange: [0, 1],
22 outputRange: [0, SHADOW_OPACITY],
23 extrapolate: 'clamp',
24 }),
25 shadowRadius: elevation.interpolate({
26 inputRange,
27 outputRange: [0, 0.75, 1.5, 3, 8, 24],
28 }),
29 };
30 } else {
31 if (elevation === 0) {
32 return {};
33 }
34
35 let height, radius;
36 switch (elevation) {
37 case 1:
38 height = 0.5;
39 radius = 0.75;
40 break;
41 case 2:
42 height = 0.75;
43 radius = 1.5;
44 break;
45 default:
46 height = elevation - 1;
47 radius = elevation;
48 }
49
50 return {
51 shadowColor: SHADOW_COLOR,
52 shadowOffset: {
53 width: 0,
54 height,
55 },
56 shadowOpacity: SHADOW_OPACITY,
57 shadowRadius: radius,
58 };
59 }
60}