All files / src/components/shared ClubLogo.vue

83.52% Statements 71/85
57.14% Branches 8/14
0% Functions 0/1
83.52% Lines 71/85

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 1181x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x       1x   1x                           1x   1x 1x   1x 64x     1x   1x 64x 64x 1x   1x 64x 64x 1x   1x                   1x   1x   1x   1x 1x   1x   1x 64x 64x 64x 64x 64x 64x 64x 64x 64x 64x 64x 64x 1x   1x 64x 64x 64x 1x   1x 64x 64x 64x 64x   1x   1x 64x 1x   1x   1x    
<template>
  <div
    v-if="hasLogo || showInitials"
    :class="containerClass"
    :style="containerStyle"
  >
    <!-- Logo image (sized variant with fallback to base) -->
    <img
      v-if="hasLogo"
      :src="activeSrc"
      :alt="`${name} logo`"
      :class="imgClass"
      @error="onImgError"
    />
    <!-- Initials fallback for md/lg/xl when no logo -->
    <span v-else-if="showInitials" :class="initialsClass">
      {{ initial }}
    </span>
  </div>
</template>
 
<script setup>
import { computed, ref } from 'vue';
 
const props = defineProps({
  logoUrl: {
    type: String,
    default: '',
  },
  name: {
    type: String,
    default: '',
  },
  size: {
    type: String,
    default: 'sm',
    validator: v => ['xs', 'sm', 'md', 'lg', 'xl'].includes(v),
  },
});
 
const imgFailed = ref(false);
const variantFailed = ref(false);
 
const sizeSuffix = computed(() => {
  if (['xs', 'sm'].includes(props.size)) return '_sm';
  if (['md', 'lg'].includes(props.size)) return '_md';
  return ''; // xl uses base
});
 
const sizedLogoUrl = computed(() => {
  if (!props.logoUrl || !sizeSuffix.value) return props.logoUrl;
  return props.logoUrl.replace(/\.png$/, `${sizeSuffix.value}.png`);
});
 
const activeSrc = computed(() => {
  if (variantFailed.value || !sizeSuffix.value) return props.logoUrl;
  return sizedLogoUrl.value;
});
 
const onImgError = () => {
  // If the sized variant failed, fall back to base URL
  if (!variantFailed.value && sizeSuffix.value) {
    variantFailed.value = true;
    return;
  }
  // Base URL also failed — no logo available
  imgFailed.value = true;
};
 
const hasLogo = computed(() => props.logoUrl && !imgFailed.value);
 
const initial = computed(() => {
  return props.name ? props.name.charAt(0).toUpperCase() : '?';
});
 
// Only show initials fallback at md/lg/xl sizes
const showInitials = computed(() => {
  return !hasLogo.value && ['md', 'lg', 'xl'].includes(props.size);
});
 
const sizeConfig = computed(() => {
  const sizes = {
    xs: { container: 'w-6 h-6', img: 'w-5 h-5', text: 'text-[10px]' },
    sm: { container: 'w-8 h-8', img: 'w-7 h-7', text: 'text-xs' },
    md: { container: 'w-10 h-10', img: 'w-9 h-9', text: 'text-sm' },
    lg: { container: 'w-14 h-14', img: 'w-12 h-12', text: 'text-lg' },
    xl: {
      container: 'w-[140px] h-[140px]',
      img: 'w-[120px] h-[120px]',
      text: 'text-4xl',
    },
  };
  return sizes[props.size];
});
 
const containerClass = computed(() => {
  const base =
    'rounded-full flex items-center justify-center overflow-hidden flex-shrink-0';
  return `${base} ${sizeConfig.value.container}`;
});
 
const containerStyle = computed(() => {
  // Only add background for visible content (logo or initials)
  if (hasLogo.value || showInitials.value) {
    return { backgroundColor: 'rgba(255, 255, 255, 0.1)' };
  }
  return {};
});
 
const imgClass = computed(() => {
  return `${sizeConfig.value.img} object-contain`;
});
 
const initialsClass = computed(() => {
  return `${sizeConfig.value.text} font-bold text-slate-400`;
});
</script>