All files / src/components/Spinner/variants Bars.vue

100% Statements 17/17
75% Branches 3/4
100% Functions 7/7
100% Lines 13/13

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                    17x           17x 17x   17x   17x   17x       17x     17x   17x 85x         16x                 80x 81x                                                                                
<script setup lang="ts">
import { computed } from "vue";
import { scaleSize, toCssSize } from "../../../utils/size";
 
interface Props {
  size?: string | number;
  color?: string;
  speed?: number;
}
 
const props = withDefaults(defineProps<Props>(), {
  size: 40,
  color: "currentColor",
  speed: 1,
});
 
const containerHeight = computed(() => toCssSize(props.size));
const barWidth = computed(() => scaleSize(props.size, "/", 8));
// five bars of size/8 plus four gaps of size*3/32 fill exactly one `size`
const gapSize = computed(() => scaleSize(props.size, "*", 3 / 32));
 
const duration = computed(() => {
  const safeSpeed =
    Number.isFinite(props.speed) && props.speed > 0
      ? props.speed
      : 1;
 
  return 1 / safeSpeed;
});
 
const formattedDuration = computed(() => `${duration.value}s`);
 
const getAnimationDelay = (index: number) => {
  return `${index * duration.value * 0.1}s`;
};
</script>
 
<template>
  <div
    class="v-spinner-bars"
    :style="{
      width: containerHeight,
      height: containerHeight,
      gap: gapSize,
      color: props.color,
    }"
  >
    <span
      v-for="n in 5"
      :key="n"
      :style="{
        width: barWidth,
        backgroundColor: props.color,
        animationDuration: formattedDuration,
        animationDelay: getAnimationDelay(n - 1),
      }"
    />
  </div>
</template>
 
<style scoped>
.v-spinner-bars {
  display: flex;
  align-items: center;
  justify-content: center;
}
 
.v-spinner-bars span {
  height: 100%;
  border-radius: 999px;
  animation-name: vslk-bars;
  animation-timing-function: ease-in-out;
  animation-iteration-count: infinite;
}
 
@keyframes vslk-bars {
  0%,
  100% {
    transform: scaleY(0.4);
    opacity: 0.4;
  }
 
  50% {
    transform: scaleY(1);
    opacity: 1;
  }
}
</style>