All files / components HorizontalCarousel.vue

96.42% Statements 81/84
88.63% Branches 39/44
100% Functions 6/6
96.42% Lines 81/84

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  1x 1x 1x 1x 1x 1x 1x 1x 1x   15x 15x 15x 5x 5x 1x 1x 1x 1x 1x 1x 1x 1x 1x 15x   18x 18x 18x 17x 18x 15x 18x   3x 3x 3x 3x       3x 1x 1x 1x 3x 3x 3x 3x 5x 5x 5x 5x 4x 4x 4x 3x 3x 3x   4x 4x 4x 4x 4x 4x 4x 4x 4x   1x 4x 4x 4x 1x 1x 1x     1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                                                                                                                                
<script setup lang="ts">
const props = defineProps<{ trackClass?: string; label: string; role?: string }>()
const track = ref<HTMLElement | null>(null);
const overflow = ref(false);
const index = ref(0);
const canPrev = computed(() => overflow.value && index.value > 0);
const canNext = computed(() => overflow.value && index.value < (track.value?.children.length || 0) - 1);
const compact = computed(() => props.label.includes('onglets'));
let observer: ResizeObserver | undefined
let initialActiveCentered = false
 
function centerInitialActive() {
  const el = track.value;
  if (!el || !overflow.value || !compact.value || initialActiveCentered) return;
  const active = el.querySelector<HTMLElement>('[aria-selected="true"]');
  if (!active) return;
  const activeIndex = Array.from(el.children).indexOf(active);
  if (activeIndex < 0) return;
  const target = Math.max(0, Math.min(
      el.scrollWidth - el.clientWidth,
      active.offsetLeft - (el.clientWidth - active.offsetWidth) / 2
  ));
  index.value = activeIndex;
  initialActiveCentered = true;
  el.scrollTo({left: target, behavior: 'auto'})
}
 
function updateOverflow() {
  const el = track.value;
  if (!el) return;
  overflow.value = el.scrollWidth > el.clientWidth + 2;
  if (!overflow.value) index.value = 0
  else nextTick(centerInitialActive)
}
 
function updateIndex() {
  const el = track.value;
  if (!el || !el.children.length) return;
  if (el.scrollLeft <= 2) {
    index.value = 0;
    return
  }
  if (el.scrollLeft >= el.scrollWidth - el.clientWidth - 2) {
    index.value = el.children.length - 1;
    return
  }
  const reference = compact.value ? el.scrollLeft : el.scrollLeft + el.clientWidth / 2;
  let closest = 0;
  let distance = Infinity;
  Array.from(el.children).forEach((child, i) => {
    const item = child as HTMLElement;
    const point = compact.value ? item.offsetLeft : item.offsetLeft + item.offsetWidth / 2;
    const next = Math.abs(point - reference);
    if (next < distance) {
      distance = next;
      closest = i
    }
  });
  index.value = closest
}
 
function move(direction: -1 | 1) {
  const el = track.value;
  if (!el) return;
  const next = Math.max(0, Math.min(el.children.length - 1, index.value + direction));
  const item = el.children[next] as HTMLElement;
  index.value = next;
  const centered = item.offsetLeft - (el.clientWidth - item.offsetWidth) / 2;
  el.scrollTo({left: compact.value ? item.offsetLeft : centered, behavior: 'smooth'})
}
 
onMounted(() => {
  observer = new ResizeObserver(updateOverflow);
  if (track.value) observer.observe(track.value);
  nextTick(updateOverflow)
});
onUpdated(() => nextTick(updateOverflow));
onBeforeUnmount(() => observer?.disconnect())
</script>
<template>
  <div :class="$style.root">
    <button v-show="canPrev" type="button" :class="[$style.arrow,$style.previous]"
            :aria-label="`Faire défiler ${label} vers la gauche`" @click="move(-1)"><
    </button>
    <div ref="track" :class="[$style.track,compact&&$style.compact,trackClass]"
         :role="role||(compact?'tablist':undefined)" @scrollend="updateIndex">
      <slot/>
    </div>
    <button v-show="canNext" type="button" :class="[$style.arrow,$style.next]"
            :aria-label="`Faire défiler ${label} vers la droite`" @click="move(1)">>
    </button>
  </div>
</template>
<style module>
.root {
  position: relative;
  min-width: 0
}
 
.track {
  scroll-behavior: smooth;
  scrollbar-width: none;
  -ms-overflow-style: none;
  touch-action: pan-x;
  overscroll-behavior-inline: contain;
  -webkit-overflow-scrolling: touch
}
 
.track::-webkit-scrollbar {
  display: none
}
 
.arrow {
  position: absolute;
  z-index: 4;
  top: 50%;
  display: grid;
  place-items: center;
  width: 38px;
  height: 38px;
  padding: 0;
  transform: translateY(-50%);
  border: 1px solid var(--line);
  border-radius: 50%;
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 3px 14px #0003;
  cursor: pointer;
  font-size: 20px
}
 
.previous {
  left: 6px
}
 
.next {
  right: 6px
}
 
@media (max-width: 1000px) {
  .track {
    display: flex !important;
    gap: 1rem;
    overflow-x: auto !important;
    padding-inline: 1rem;
    margin-inline: -1rem;
    scroll-snap-type: x proximity;
    scroll-padding-inline: 1rem
  }
 
  .track > * {
    flex: 0 0 250px;
    min-width: 250px;
    scroll-snap-align: center
  }
 
  .track.compact > * {
    flex-basis: auto;
    min-width: max-content
  }
}
 
</style>