Chat Bubble in React Native

前端 未结 1 463
盖世英雄少女心
盖世英雄少女心 2021-02-08 21:09

I am currently making a chat bubble in react-native. Since I am new to react-native I first tried to make the chat bubble on a browser and then tried to replicate the same in re

相关标签:
1条回答
  • 2021-02-08 21:52

    Making the arrows appear was harder than I thought it would be, especially when you consider different screen sizes, different platforms (iOS and android). Yes, @Panagiotis Vrs is correct when he mentioned that it won't look 100% same on both platforms. Nevertheless, I tried to achieve the same using react-native-svg and react-native-size-matters.

    I am sharing my code, maybe someone can improvise this and make this even better.

    The HTML Part

    <View style={[styles.item, styles.itemIn]}>
            <View style={[styles.balloon, {backgroundColor: 'grey'}]}>
              <Text style={{paddingTop: 5, color: 'white'}}>Hey! How are you?</Text>
              <View
              style={[
                styles.arrowContainer,
                styles.arrowLeftContainer,
              ]}
            >
    
               <Svg style={styles.arrowLeft} width={moderateScale(15.5, 0.6)} height={moderateScale(17.5, 0.6)} viewBox="32.484 17.5 15.515 17.5"  enable-background="new 32.485 17.5 15.515 17.5">
                    <Path
                        d="M38.484,17.5c0,8.75,1,13.5-6,17.5C51.484,35,52.484,17.5,38.484,17.5z"
                        fill="grey"
                        x="0"
                        y="0"
                    />
                </Svg>
            </View>
            </View>
          </View>
    
    
          <View style={[styles.item, styles.itemOut]}>
            <View style={[styles.balloon, {backgroundColor: '#1084ff'}]}>
              <Text style={{paddingTop: 5, color: 'white'}}>Hey! I am good. How are you?</Text>
              <View
              style={[
                styles.arrowContainer,
                styles.arrowRightContainer,
              ]}
            >
               <Svg style={styles.arrowRight} width={moderateScale(15.5, 0.6)} height={moderateScale(17.5, 0.6)} viewBox="32.485 17.5 15.515 17.5"  enable-background="new 32.485 17.5 15.515 17.5">
                    <Path
                        d="M48,35c-7-4-6-8.75-6-17.5C28,17.5,29,35,48,35z"
                        fill="#1084ff"
                        x="0"
                        y="0"
                    />
                </Svg>
            </View>
            </View>
          </View>
    
    
    
    
    
           <View style={[styles.item, styles.itemOut]}>
            <View style={[styles.balloon, {backgroundColor: '#1084ff'}]}>
              <Text style={{paddingTop: 5, color: 'white'}}>Check this Image out !!!</Text>
              <View
              style={[
                styles.arrowContainer,
                styles.arrowRightContainer,
              ]}
            >
               <Svg style={styles.arrowRight} width={moderateScale(15.5, 0.6)} height={moderateScale(17.5, 0.6)} viewBox="32.485 17.5 15.515 17.5"  enable-background="new 32.485 17.5 15.515 17.5">
                    <Path
                        d="M48,35c-7-4-6-8.75-6-17.5C28,17.5,29,35,48,35z"
                        fill="#1084ff"
                        x="0"
                        y="0"
                    />
                </Svg>
            </View>
            </View>
          </View>
    
    
           <View style={[styles.item, styles.itemOut]}>
            <View style={[styles.balloon, {backgroundColor: '#1084ff'}]}>
    
            <Image
                styleName="small"
                borderRadius={5}
                source={{ uri: 'https://shoutem.github.io/img/ui-toolkit/examples/image-3.png'}}
                />
              <View
              style={[
                styles.arrowContainer,
                styles.arrowRightContainer,
              ]}
            >
               <Svg style={styles.arrowRight} width={moderateScale(15.5, 0.6)} height={moderateScale(17.5, 0.6)} viewBox="32.485 17.5 15.515 17.5"  enable-background="new 32.485 17.5 15.515 17.5">
                    <Path
                        d="M48,35c-7-4-6-8.75-6-17.5C28,17.5,29,35,48,35z"
                        fill="#1084ff"
                        x="0"
                        y="0"
                    />
                </Svg>
            </View>
            </View>
          </View>
    
    
         <View style={[styles.item, styles.itemIn]}>
            <View style={[styles.balloon, {backgroundColor: 'grey'}]}>
              <Text style={{paddingTop: 5, color: 'white'}}>Nice Picture</Text>
              <View
              style={[
                styles.arrowContainer,
                styles.arrowLeftContainer,
              ]}
            >
    
               <Svg style={styles.arrowLeft} width={moderateScale(15.5, 0.6)} height={moderateScale(17.5, 0.6)} viewBox="32.484 17.5 15.515 17.5"  enable-background="new 32.485 17.5 15.515 17.5">
                    <Path
                        d="M38.484,17.5c0,8.75,1,13.5-6,17.5C51.484,35,52.484,17.5,38.484,17.5z"
                        fill="grey"
                        x="0"
                        y="0"
                    />
                </Svg>
            </View>
            </View>
          </View>
    

    The CSS Part

     item: {
           marginVertical: moderateScale(7, 2),
           flexDirection: 'row'
        },
        itemIn: {
            marginLeft: 20
        },
        itemOut: {
           alignSelf: 'flex-end',
           marginRight: 20
        },
        balloon: {
           maxWidth: moderateScale(250, 2),
           paddingHorizontal: moderateScale(10, 2),
           paddingTop: moderateScale(5, 2),
           paddingBottom: moderateScale(7, 2),
           borderRadius: 20,
        },
        arrowContainer: {
            position: 'absolute',
            top: 0,
            left: 0,
            right: 0,
            bottom: 0,
            zIndex: -1,
            flex: 1
        },
        arrowLeftContainer: {
            justifyContent: 'flex-end',
            alignItems: 'flex-start'
        },
    
        arrowRightContainer: {
            justifyContent: 'flex-end',
            alignItems: 'flex-end',
        },
    
        arrowLeft: {
            left: moderateScale(-6, 0.5),
        },
    
        arrowRight: {
            right:moderateScale(-6, 0.5),
        }
    

    Now scaling it on different device screens was just trial and error for me. The output right now is good enough for me. Maybe when I have time I will try to improve on this current design.

    This is how it looks on iOS:

    iPhone 7:

    iPhone X:

    Now on android unfortunately I wasn't able to make the curve of the arrow appear. Maybe I am doing something wrong. This is how it looks in the end.

    Pixel 2:

    Nexus 6:

    Maybe we can use flex to make the arrow curves look good on android.

    In case someone has made a better version of this. Do share. :)

    0 讨论(0)
提交回复
热议问题