All files / src/components TournamentDayStrip.vue

100% Statements 126/126
94.73% Branches 36/38
100% Functions 6/6
100% Lines 126/126

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 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 1801x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x             1x   1x 1x               1x 1x 1x 1x 1x 1x 219x 219x       219x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x       1x         1x   1x           1x   1x   1x 69x 206x 206x 206x 206x 206x 206x 206x 206x 206x 206x 206x 206x 69x 1x   1x 1x   1x 1x 1x 367x 137x 367x   1x 1x 1x 77x 77x 77x 77x 77x 77x 77x 77x 77x 77x   1x 1x 1x 1x 1x                        
<template>
  <!-- One chip per tournament day (SB-1865), swipeable, with arrows that stop
       at either end. The open day scrolls itself into view. -->
  <nav
    class="flex items-center gap-1"
    aria-label="Tournament days"
    data-testid="tournament-day-strip"
  >
    <button
      type="button"
      class="shrink-0 w-11 h-11 flex items-center justify-center rounded-full text-fg-muted transition-colors hover:bg-surface-alt hover:text-fg focus:outline-none focus-visible:ring-2 focus-visible:ring-brand-500 disabled:opacity-30 disabled:hover:bg-transparent disabled:cursor-default"
      :disabled="!prevDay"
      aria-label="Previous day"
      data-testid="day-prev"
      @click="prevDay && emit('update:day', prevDay)"
    >
      <svg viewBox="0 0 20 20" class="w-5 h-5" aria-hidden="true">
        <path
          d="M12.5 15l-5-5 5-5"
          fill="none"
          stroke="currentColor"
          stroke-width="2"
          stroke-linecap="round"
          stroke-linejoin="round"
        />
      </svg>
    </button>
 
    <div
      ref="scroller"
      class="day-scroller relative flex-1 sm:flex-initial min-w-0 flex gap-1.5 overflow-x-auto snap-x snap-mandatory scroll-px-2 py-1"
    >
      <button
        v-for="d in chips"
        :key="d.date"
        :ref="el => setChipRef(d.date, el)"
        type="button"
        :aria-pressed="d.date === selected ? 'true' : 'false'"
        :aria-current="d.date === selected ? 'date' : undefined"
        :aria-label="d.ariaLabel"
        :data-date="d.date"
        :data-today="d.isToday ? 'true' : 'false'"
        data-testid="day-chip"
        :class="[
          'day-chip relative snap-start shrink-0 flex flex-col items-center justify-center min-w-[3.5rem] h-14 px-2 rounded-xl border text-center transition-colors',
          'focus:outline-none focus-visible:ring-2 focus-visible:ring-brand-500 focus-visible:ring-offset-1 focus-visible:ring-offset-surface',
          d.date === selected
            ? 'bg-brand-600 border-brand-600 text-white shadow-sm'
            : 'bg-card border-line text-fg hover:border-brand-400',
        ]"
        @click="emit('update:day', d.date)"
      >
        <span
          :class="[
            'text-[10px] font-semibold uppercase tracking-wider leading-none',
            d.date === selected
              ? 'text-brand-100'
              : d.isToday
                ? 'text-accent-600 dark:text-accent-400'
                : 'text-fg-muted',
          ]"
          >{{ d.isToday ? 'Today' : d.weekday }}</span
        >
        <span class="text-lg font-bold leading-tight tabular-nums">{{
          d.day
        }}</span>
        <span
          v-if="d.count != null"
          :class="[
            'text-[10px] leading-none tabular-nums',
            d.date === selected ? 'text-brand-100' : 'text-fg-muted',
          ]"
          >{{ d.count }}</span
        >
        <span
          v-if="d.isToday && d.date !== selected"
          class="absolute top-1 right-1 w-1.5 h-1.5 rounded-full bg-accent-400"
          aria-hidden="true"
        ></span>
      </button>
    </div>
 
    <button
      type="button"
      class="shrink-0 w-11 h-11 flex items-center justify-center rounded-full text-fg-muted transition-colors hover:bg-surface-alt hover:text-fg focus:outline-none focus-visible:ring-2 focus-visible:ring-brand-500 disabled:opacity-30 disabled:hover:bg-transparent disabled:cursor-default"
      :disabled="!nextDay"
      aria-label="Next day"
      data-testid="day-next"
      @click="nextDay && emit('update:day', nextDay)"
    >
      <svg viewBox="0 0 20 20" class="w-5 h-5" aria-hidden="true">
        <path
          d="M7.5 5l5 5-5 5"
          fill="none"
          stroke="currentColor"
          stroke-width="2"
          stroke-linecap="round"
          stroke-linejoin="round"
        />
      </svg>
    </button>
  </nav>
</template>
 
<script setup>
import { computed, nextTick, onMounted, ref, watch } from 'vue';
import {
  adjacentDay,
  dayChipParts,
  formatDayLong,
} from '../utils/tournamentDays';
 
const props = defineProps({
  // [{ date: 'YYYY-MM-DD', count }] ascending. `count` null = not counted.
  days: { type: Array, default: () => [] },
  selected: { type: String, default: null },
  // The venue's calendar day, so "Today" is the day at the fields.
  today: { type: String, default: null },
});
 
const emit = defineEmits(['update:day']);
 
const chips = computed(() =>
  props.days.map(d => {
    const parts = dayChipParts(d.date);
    const count = typeof d.count === 'number' ? d.count : null;
    const isToday = d.date === props.today;
    const countText =
      count == null ? '' : `, ${count} ${count === 1 ? 'match' : 'matches'}`;
    return {
      date: d.date,
      ...parts,
      count,
      isToday,
      ariaLabel: `${isToday ? 'Today, ' : ''}${formatDayLong(d.date)}${countText}`,
    };
  })
);
 
const prevDay = computed(() => adjacentDay(props.days, props.selected, -1));
const nextDay = computed(() => adjacentDay(props.days, props.selected, 1));
 
const scroller = ref(null);
const chipEls = new Map();
const setChipRef = (date, el) => {
  if (el) chipEls.set(date, el);
  else chipEls.delete(date);
};
 
// Centre the open day in the strip without scrolling the page: only the
// strip's own scrollLeft moves.
const scrollSelectedIntoView = async (smooth = true) => {
  await nextTick();
  const box = scroller.value;
  const chip = chipEls.get(props.selected);
  if (!box || !chip || typeof box.scrollTo !== 'function') return;
  const left = chip.offsetLeft - box.clientWidth / 2 + chip.clientWidth / 2;
  box.scrollTo({
    left: Math.max(0, left),
    behavior: smooth ? 'smooth' : 'auto',
  });
};
 
onMounted(() => scrollSelectedIntoView(false));
watch(
  () => [props.selected, props.days.length],
  () => scrollSelectedIntoView()
);
</script>
 
<style scoped>
.day-scroller {
  scrollbar-width: none;
  -webkit-overflow-scrolling: touch;
}
.day-scroller::-webkit-scrollbar {
  display: none;
}
</style>