tooot/src/components/Header/Right.tsx

100 lines
2.2 KiB
TypeScript
Raw Normal View History

2020-12-26 23:27:53 +01:00
import React, { useMemo } from 'react'
import { Pressable, StyleSheet, Text, View } from 'react-native'
import { Chase } from 'react-native-animated-spinkit'
2020-11-30 00:24:53 +01:00
import { Feather } from '@expo/vector-icons'
2020-12-13 14:04:25 +01:00
import { StyleConstants } from '@utils/styles/constants'
2020-12-26 23:27:53 +01:00
import { useTheme } from '@utils/styles/ThemeManager'
2020-11-30 00:24:53 +01:00
2020-12-26 23:27:53 +01:00
export interface Props {
type?: 'icon' | 'text'
content?: string
2020-11-30 00:24:53 +01:00
2020-12-26 23:27:53 +01:00
loading?: boolean
disabled?: boolean
2020-11-30 00:24:53 +01:00
2020-12-26 23:27:53 +01:00
onPress: () => void
2020-11-30 00:24:53 +01:00
}
2020-12-26 23:27:53 +01:00
const HeaderRight: React.FC<Props> = ({
type = 'icon',
content,
loading,
2020-12-03 01:28:56 +01:00
disabled,
2020-12-26 23:27:53 +01:00
onPress
2020-11-30 00:24:53 +01:00
}) => {
const { theme } = useTheme()
2020-12-26 23:27:53 +01:00
const loadingSpinkit = useMemo(
() => (
<View style={{ position: 'absolute' }}>
<Chase
size={StyleConstants.Font.Size.M * 1.25}
color={theme.secondary}
/>
</View>
),
[theme]
)
const children = useMemo(() => {
switch (type) {
case 'icon':
return (
<>
<Feather
name={content as any}
color={disabled ? theme.secondary : theme.primary}
size={StyleConstants.Spacing.M * 1.25}
style={{ opacity: loading ? 0 : 1 }}
/>
{loading && loadingSpinkit}
</>
)
case 'text':
return (
<>
<Text
style={[
styles.text,
{
color: disabled ? theme.secondary : theme.primary,
opacity: loading ? 0 : 1
}
]}
children={content}
/>
{loading && loadingSpinkit}
</>
)
}
}, [theme, loading, disabled])
2020-11-30 00:24:53 +01:00
return (
2020-12-13 23:38:37 +01:00
<Pressable
2020-12-26 23:27:53 +01:00
{...(!disabled && !loading && { onPress })}
children={children}
2020-12-13 23:38:37 +01:00
style={[
styles.base,
{
backgroundColor: theme.backgroundGradientStart,
2020-12-26 23:27:53 +01:00
...(type === 'icon' && { height: 44, width: 44, marginRight: -9 })
2020-12-13 23:38:37 +01:00
}
]}
2020-12-26 23:27:53 +01:00
/>
2020-11-30 00:24:53 +01:00
)
}
const styles = StyleSheet.create({
base: {
2020-12-13 23:38:37 +01:00
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center',
borderRadius: 100
2020-11-30 00:24:53 +01:00
},
text: {
2020-12-03 01:28:56 +01:00
fontSize: StyleConstants.Font.Size.M
2020-11-30 00:24:53 +01:00
}
})
2020-12-12 12:49:29 +01:00
export default HeaderRight