Bez popisu

themed-button.tsx 1.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import { Pressable, StyleSheet, Text } from 'react-native';
  2. import { Colors } from '@/constants/theme';
  3. import { useColorScheme } from '@/hooks/use-color-scheme';
  4. type Variant = 'primary' | 'secondary' | 'ghost';
  5. type Props = {
  6. title: string;
  7. onPress: () => void;
  8. variant?: Variant;
  9. disabled?: boolean;
  10. testID?: string;
  11. };
  12. export function ThemedButton({
  13. title,
  14. onPress,
  15. variant = 'primary',
  16. disabled,
  17. testID,
  18. }: Props) {
  19. const theme = useColorScheme() ?? 'light';
  20. const tint = Colors[theme].tint;
  21. const textOnTint = Colors[theme].textOnTint;
  22. const backgroundColor =
  23. variant === 'primary' ? tint : 'transparent';
  24. const borderColor =
  25. variant === 'secondary' ? tint : 'transparent';
  26. const textColor =
  27. variant === 'primary'
  28. ? textOnTint
  29. : tint;
  30. return (
  31. <Pressable
  32. accessibilityRole="button"
  33. testID={testID}
  34. onPress={onPress}
  35. disabled={disabled}
  36. style={({ pressed }) => [
  37. styles.base,
  38. {
  39. backgroundColor,
  40. borderColor,
  41. opacity: disabled ? 0.5 : pressed ? 0.85 : 1,
  42. },
  43. ]}>
  44. <Text style={[styles.text, { color: textColor }]}>{title}</Text>
  45. </Pressable>
  46. );
  47. }
  48. const styles = StyleSheet.create({
  49. base: {
  50. borderRadius: 10,
  51. borderWidth: 1,
  52. paddingHorizontal: 14,
  53. paddingVertical: 10,
  54. alignItems: 'center',
  55. justifyContent: 'center',
  56. },
  57. text: {
  58. fontSize: 15,
  59. fontWeight: '600',
  60. },
  61. });