tooot/src/components/Header/Right.tsx

117 lines
3.0 KiB
TypeScript
Raw Normal View History

import Icon from '@components/Icon'
import CustomText from '@components/Text'
import { StyleConstants } from '@utils/styles/constants'
import { useTheme } from '@utils/styles/ThemeManager'
2021-02-08 23:47:20 +01:00
import React, { useMemo } from 'react'
import { AccessibilityProps, Pressable, View } from 'react-native'
2021-02-08 23:47:20 +01:00
import { Flow } from 'react-native-animated-spinkit'
2020-11-30 00:24:53 +01:00
2020-12-26 23:27:53 +01:00
export interface Props {
2021-04-09 21:43:12 +02:00
accessibilityLabel?: string
accessibilityHint?: string
accessibilityState?: AccessibilityProps['accessibilityState']
2020-12-26 23:27:53 +01:00
type?: 'icon' | 'text'
content: string
2021-01-30 01:29:15 +01:00
native?: boolean
2021-03-22 00:25:53 +01:00
background?: boolean
2020-11-30 00:24:53 +01:00
2020-12-26 23:27:53 +01:00
loading?: boolean
disabled?: boolean
2022-12-22 01:21:51 +01:00
destructive?: 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> = ({
2021-04-09 21:43:12 +02:00
// Accessibility - Start
accessibilityLabel,
accessibilityHint,
accessibilityState,
// Accessibility - End
2020-12-26 23:27:53 +01:00
type = 'icon',
content,
2021-01-30 01:29:15 +01:00
native = true,
2021-03-22 00:25:53 +01:00
background = false,
2020-12-26 23:27:53 +01:00
loading,
2020-12-03 01:28:56 +01:00
disabled,
2022-12-22 01:21:51 +01:00
destructive = false,
2020-12-26 23:27:53 +01:00
onPress
2020-11-30 00:24:53 +01:00
}) => {
2022-02-12 14:51:01 +01:00
const { colors, theme } = useTheme()
2020-11-30 00:24:53 +01:00
2020-12-26 23:27:53 +01:00
const loadingSpinkit = useMemo(
() => (
<View style={{ position: 'absolute' }}>
2022-12-22 01:21:51 +01:00
<Flow size={StyleConstants.Font.Size.M * 1.25} color={colors.secondary} />
2020-12-26 23:27:53 +01:00
</View>
),
[theme]
)
const children = useMemo(() => {
switch (type) {
case 'icon':
return (
<>
<Icon
name={content}
2020-12-26 23:27:53 +01:00
style={{ opacity: loading ? 0 : 1 }}
size={StyleConstants.Spacing.M * 1.25}
2022-12-22 01:21:51 +01:00
color={disabled ? colors.secondary : destructive ? colors.red : colors.primaryDefault}
2020-12-26 23:27:53 +01:00
/>
{loading && loadingSpinkit}
</>
)
case 'text':
return (
<>
<CustomText
fontStyle='M'
2022-12-22 01:21:51 +01:00
fontWeight={destructive ? 'Bold' : 'Normal'}
style={{
2022-12-22 01:21:51 +01:00
color: disabled
? colors.secondary
: destructive
? colors.red
: colors.primaryDefault,
opacity: loading ? 0 : 1
}}
2020-12-26 23:27:53 +01:00
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
2021-04-09 21:43:12 +02:00
accessibilityLabel={accessibilityLabel}
accessibilityHint={accessibilityHint}
accessibilityRole='button'
accessibilityState={accessibilityState}
2020-12-28 16:54:19 +01:00
onPress={onPress}
2020-12-26 23:27:53 +01:00
children={children}
2020-12-28 16:54:19 +01:00
disabled={disabled || loading}
style={{
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center',
2022-12-22 01:21:51 +01:00
backgroundColor: background ? colors.backgroundOverlayDefault : undefined,
minHeight: 44,
minWidth: 44,
2022-12-22 01:21:51 +01:00
marginRight: native ? -StyleConstants.Spacing.S : StyleConstants.Spacing.S,
...(type === 'icon' && {
borderRadius: 100
}),
...(type === 'text' && {
paddingHorizontal: StyleConstants.Spacing.S
})
}}
2020-12-26 23:27:53 +01:00
/>
2020-11-30 00:24:53 +01:00
)
}
2020-12-12 12:49:29 +01:00
export default HeaderRight