Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement base button with pressable component #29

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
101 changes: 101 additions & 0 deletions template/src/components/pressables/BaseButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import React from 'react'
import {
ActivityIndicator,
Insets,
Pressable,
StyleProp,
StyleSheet,
Text,
TextStyle,
View,
ViewStyle,
} from 'react-native'
import colors from '@config/ui/colors'
import { FC } from '@utils/types'

const BORDER_RADIUS = 5

type RoundButtonType = 'primary' | 'secondary'

export type BaseButtonPropsType = {
disabled?: boolean | null
onPress?: () => void
variant?: RoundButtonType
style?: StyleProp<ViewStyle>
styleText?: StyleProp<TextStyle>
rootStyle?: StyleProp<ViewStyle>
hitSlop?: null | Insets | number
loading?: boolean
}

const BaseButton: FC<BaseButtonPropsType> = ({
disabled,
onPress,
variant = 'primary',
style,
styleText,
rootStyle,
hitSlop,
loading,
children,
}) => {
const wrapperStyles = [
styles.wrapper,
variant === 'primary' && styles.primary,
variant === 'secondary' && styles.secondary,
style,
]
const textStyles = [
variant === 'primary' && styles.textPrimary,
variant === 'secondary' && styles.textSecondary,
styleText,
]

return (
<Pressable
hitSlop={hitSlop}
onPress={onPress}
disabled={disabled}
style={({ pressed }) => [rootStyle, { opacity: pressed ? 0.6 : 1 }]}
>
<View style={wrapperStyles}>
<Text style={textStyles}>{children}</Text>
{loading ? <ActivityIndicator size="small" style={styles.loadingIndicator} /> : null}
{disabled ? <View style={styles.disabled} /> : null}
</View>
</Pressable>
)
}

export default BaseButton

const styles = StyleSheet.create({
disabled: {
...StyleSheet.absoluteFillObject,
backgroundColor: colors.transparentBlack,
},
loadingIndicator: {
marginLeft: 5,
},
primary: {
backgroundColor: colors.primary,
},
secondary: {
backgroundColor: colors.transparent,
borderWidth: 0,
},
textPrimary: {
color: colors.onPrimary,
},
textSecondary: {
color: colors.secondary,
},
wrapper: {
alignItems: 'center',
borderRadius: BORDER_RADIUS,
borderWidth: 1,
flexDirection: 'row',
justifyContent: 'center',
padding: 8,
},
})
2 changes: 2 additions & 0 deletions template/src/config/ui/colors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ enum Palette {
ANGRY = '#dd3333',
DEEP_PURPLE = '#5D2555',
TRANSPARENT = 'rgba(0, 0, 0, 0)',
TRANSPARENT_BLACK = 'rgba(0,0,1,0.14)',
}

const Colors = {
Expand All @@ -23,6 +24,7 @@ const Colors = {
error: Palette.ANGRY,
onError: Palette.WHITE,
transparent: Palette.TRANSPARENT,
transparentBlack: Palette.TRANSPARENT_BLACK,
}

export default Colors