Search code examples
javascriptreactjsreact-nativeshadow

Creating shadow design in react native


I am trying to create a UI in react-native like in the below picture:

enter image description here

Until now I can do only like this:

enter image description here

But is there any proper way to do that?

  <View style={styles.frame1}>
    <View  style={styles.frameBefore}></View>
    <View style={styles.frame2}>
      <TextInput/>
    </View>
  </View>

  frame1: {
    flexDirection: 'row',
    margin: 10,
    borderColor: LightColorLine,
    borderStyle:'solid',
    borderWidth: 0.5,
    backgroundColor: LightColorBackgr,
    padding:10,
  },
  frame2:{
    backgroundColor: LightColorTextBackgr,
    padding: -50,
    flex: 0.98,
  },
  frameBefore:{
    width: 0,
    height: 60,
    borderRightWidth: 0.9,
    borderColor: LightColorLine,
    borderStyle:'solid',
    shadowColor: '#000000',
    shadowOffset: {
      width: 0,
      height: 10,
    },
    shadowOpacity: 0.25,
    shadowRadius: 3.84,
    elevation: 5,
    flex: 0.01,
    transform: [{ 
      rotate: '45deg'
    }]
  },

Solution

  • Here is the closest I was able to get

    enter image description here

    The tricky part was creating the shadow on an angle, especially considering the API's React-Native provide for shadows differ so much between iOS and Android.

    To overcome this I used a linear gradient which is not included by React Native by default. I used expo-linear-gradient but there a few others you can use if you would prefer.

    export const Triangle = props => (
      <View style={props.style}>
        <LinearGradient
          colors={['black', 'transparent']}
          start={[0, 0]}
          end={[0.5, 0.5]}
          style={styles.triangleShadow}
        />
        <View style={styles.triangle} />
      </View>
    );
    
    export default class App extends React.Component {
      render() {
        return (
          <View style={styles.container}>
            <View style={styles.card}>
              <Text style={styles.text}>Be Cool.</Text>
              <Triangle style={styles.topLeftTriangle} />
              <Triangle style={styles.bottomRightTriangle} />
            </View>
          </View>
        );
      }
    }
    
    const styles = StyleSheet.create({
      container: {
        flex: 1,
        justifyContent: 'center',
        paddingTop: Constants.statusBarHeight,
        backgroundColor: '#ecf0f1',
        padding: 32,
      },
      text: {
        fontSize: 18,
        padding: 32,
        paddingVertical: 48,
        fontWeight: 'bold',
        borderWidth: 1,
        backgroundColor: '#F66F6F',
        color: 'white',
        borderColor: 'gray',
      },
      card: {
        borderWidth: 1,
        padding: 8,
        borderColor: 'gray',
      },
      triangle: {
        width: 0,
        height: 0,
        backgroundColor: 'transparent',
        borderStyle: 'solid',
        borderRightWidth: 80,
        borderTopWidth: 80,
        borderRightColor: 'transparent',
        borderTopColor: '#ecf0f1',
      },
      triangleShadow: {
        height: 90,
        width: 90,
        position: 'absolute',
        top: 0,
        left: 0,
      },
      topLeftTriangle: {
        position: 'absolute',
        top: -10,
        left: -10,
      },
      bottomRightTriangle: {
        position: 'absolute',
        bottom: -10,
        right: -10,
        transform: [{ rotate: '180deg' }],
      },
    });