8.2.1.2 CustomButton에 Secondary 버튼 스타일 만들기

    앱에서 버튼을 만들 때 흔히 사용하는 UI 패턴은 Primary(기본) 버튼과 Secondary(보조) 버튼을 만드는 것입니다.

    CustomButton에서 Props를 통해 Secondary 버튼의 스타일을 적용해주겠습니다.

    components/CustomButton.js

    import React from 'react';
    import {StyleSheet, View, Pressable, Text, Platform} from 'react-native';
    
    function CustomButton({onPress, title, hasMarginBottom, theme}) {
      const isPrimary = theme === 'primary';
    
      return (
        <View style={[styles.block, hasMarginBottom && styles.margin]}>
          <Pressable
            onPress={onPress}
            style={({pressed}) => [
              styles.wrapper,
              isPrimary && styles.primaryWrapper,
              Platform.OS === 'ios' && pressed && {opacity: 0.5},
            ]}
            android_ripple={{
              color: isPrimary ? '#ffffff' : '#6200ee',
            }}>
            <Text
              style={[
                styles.text,
                isPrimary ? styles.primaryText : styles.secondaryText,
              ]}>
              {title}
            </Text>
          </Pressable>
        </View>
      );
    }
    
    CustomButton.defaultProps = {
      theme: 'primary',
    };
    
    const styles = StyleSheet.create({
      overflow: {
          borderRadius: 4,
          overflow: 'hidden',
      },
      wrapper: {
        borderRadius: 4,
        height: 48,
        alignItems: 'center',
        justifyContent: 'center',
        // backgroundColor 제거
      },
      primaryWrapper: {
        backgroundColor: '#6200ee',
      },
      text: {
        fontWeight: 'bold',
        fontSize: 14,
        color: 'white',
      },
      primaryText: {
        color: 'white',
      },
      secondaryText: {
        color: '#6200ee',
      },
      margin: {
        marginBottom: 8,
      },
    });
    
    export default CustomButton;
    신간 소식 구독하기
    뉴스레터에 가입하시고 이메일로 신간 소식을 받아 보세요.