All files / src/components PopupTemplate.tsx

92.3% Statements 12/13
87.5% Branches 14/16
100% Functions 3/3
91.66% Lines 11/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161              1x                                                 3x 1x                                 1x                             3x 3x     3x   1x                                     3x 3x                   1x                                                                                                                      
import React from "react";
import { Pressable, StyleSheet, Text, View } from "react-native";
import { Ionicons } from "@expo/vector-icons";
import FontAwesome from "@expo/vector-icons/FontAwesome";
import Foundation from "@expo/vector-icons/Foundation";
import { useTheme } from "../hooks/useTheme";
 
export const BURGUNDY = "#800020";
 
export interface AccessibilityInfo {
  hasElevator: boolean;
  hasParking: boolean;
  isAccessible: boolean;
}
 
interface PopupTemplateProps {
  readonly title: string;
  readonly accessibility?: AccessibilityInfo;
  readonly onClose: () => void;
  readonly dismissOnBackdropPress?: boolean;
  readonly renderTopContent?: () => React.ReactNode;
  readonly renderBody: () => React.ReactNode;
  readonly renderButtons: () => React.ReactNode;
}
 
function AccessibilityIcons({
  acc,
  primaryColor,
}: {
  readonly acc: AccessibilityInfo;
  readonly primaryColor: string;
}) {
  if (!acc.hasParking && !acc.hasElevator && !acc.isAccessible) return null;
  return (
    <View style={styles.iconsRow}>
      {acc.hasParking && (
        <View style={[styles.badge, { backgroundColor: primaryColor }]}>
          <Text style={styles.badgeText}>P</Text>
        </View>
      )}
      {acc.hasElevator && (
        <Foundation name="elevator" size={20} color={primaryColor} />
      )}
      {acc.isAccessible && (
        <FontAwesome name="wheelchair" size={20} color={primaryColor} />
      )}
    </View>
  );
}
 
const defaultAccessibility: AccessibilityInfo = {
  hasElevator: false,
  hasParking: false,
  isAccessible: false,
};
 
export default function PopupTemplate({
  title,
  accessibility,
  onClose,
  dismissOnBackdropPress = false,
  renderTopContent,
  renderBody,
  renderButtons,
}: PopupTemplateProps) {
  const acc = accessibility ?? defaultAccessibility;
  const { colors } = useTheme();
 
  const card = (
    <Pressable
      style={[styles.card, { backgroundColor: colors.card }]}
      onPress={(e) => e.stopPropagation()}
    >
      {renderTopContent?.()}
      <View style={styles.content}>
        <View style={styles.header}>
          <Text style={[styles.title, { color: colors.text }]}>{title}</Text>
          <View style={styles.headerRight}>
            <AccessibilityIcons acc={acc} primaryColor={colors.primary} />
            <Pressable style={styles.closeBtn} onPress={onClose}>
              <Ionicons name="close" size={20} color={colors.text} />
            </Pressable>
          </View>
        </View>
        {renderBody()}
        {renderButtons()}
      </View>
    </Pressable>
  );
 
  Eif (dismissOnBackdropPress) {
    return (
      <Pressable style={styles.backdrop} onPress={onClose}>
        {card}
      </Pressable>
    );
  }
 
  return <View style={styles.backdrop}>{card}</View>;
}
 
const styles = StyleSheet.create({
  backdrop: {
    ...StyleSheet.absoluteFillObject,
    justifyContent: "center",
    alignItems: "center",
    paddingHorizontal: 16,
  },
  card: {
    width: "100%",
    maxWidth: 520,
    borderRadius: 16,
    overflow: "hidden",
    shadowColor: "#000",
    shadowOpacity: 0.18,
    shadowRadius: 18,
    shadowOffset: { width: 0, height: 6 },
    elevation: 10,
  },
  content: {
    paddingHorizontal: 14,
    paddingTop: 12,
    paddingBottom: 14,
  },
  header: {
    flexDirection: "row",
    alignItems: "flex-start",
    justifyContent: "space-between",
    gap: 12,
    marginBottom: 6,
  },
  title: {
    flex: 1,
    fontSize: 18,
    fontWeight: "800",
  },
  headerRight: {
    flexDirection: "row",
    alignItems: "center",
    gap: 10,
  },
  iconsRow: {
    flexDirection: "row",
    alignItems: "center",
    gap: 8,
  },
  badge: {
    borderRadius: 10,
    paddingHorizontal: 8,
    paddingVertical: 2,
  },
  badgeText: { color: "#fff", fontWeight: "900" },
  closeBtn: {
    width: 32,
    height: 32,
    borderRadius: 16,
    alignItems: "center",
    justifyContent: "center",
  },
});