All files / src/components/Skeleton/variants Card.vue

100% Statements 54/54
87.8% Branches 36/41
100% Functions 16/16
100% Lines 47/47

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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205                                33x       33x 33x 33x   33x 33x     33x 28x 28x     33x 4x 4x     33x   33x 33x 33x     33x 33x 33x     33x   33x   33x 33x     33x 33x 97x 64x 33x             33x                   33x             23x   33x           80x 80x 82x                   32x                 22x   32x           3x 3x 3x                                       32x               22x   32x           12x 12x 12x                                                                                  
<script setup lang="ts">
import { computed } from "vue";
import Skeleton from "../Skeleton.vue";
import type { SkeletonBaseProps } from "../types";
 
type CardOptions = {
  layout?: "simple" | "image" | "horizontal";
  lines?: number;
  imageHeight?: number | string;
  mediaWidth?: number | string;
  titleWidth?: number | string;
  radius?: number | string;
  borderWidth?: number | string;
  borderColor?: string;
};
 
const props = withDefaults(defineProps<SkeletonBaseProps>(), {
  width: 320,
});
 
const options = computed(() => (props.options ?? {}) as CardOptions);
const layout = computed(() => options.value.layout ?? "image");
const lines = computed(() => Math.max(1, options.value.lines ?? 3));
 
const containerWidth = computed(() =>
  typeof props.width === "number" ? `${props.width}px` : props.width ?? "320px"
);
 
const imageHeight = computed(() => {
  const value = options.value.imageHeight ?? 180;
  return typeof value === "number" ? `${value}px` : value;
});
 
const mediaWidth = computed(() => {
  const value = options.value.mediaWidth ?? 120;
  return typeof value === "number" ? `${value}px` : value;
});
 
const titleWidth = computed(() => options.value.titleWidth ?? "72%");
 
const cardRadius = computed(() => {
  const value = options.value.radius ?? "14px";
  return typeof value === "number" ? `${value}px` : value;
});
 
const borderWidth = computed(() => {
  const value = options.value.borderWidth ?? "1px";
  return typeof value === "number" ? `${value}px` : value;
});
 
const borderColor = computed(() => options.value.borderColor ?? "rgba(148, 163, 184, 0.18)");
 
const forwardedProps = computed(() => {
  const { width, height, options, variant, lines: topLevelLines, size, radius, ...rest } =
    props as any;
  return rest;
});
 
const lineWidths = computed(() =>
  Array.from({ length: lines.value }, (_, i) => {
    if (i === 0) return "100%";
    if (i === lines.value - 1) return "55%";
    return "82%";
  })
);
</script>
 
<template>
  <div
    v-if="layout === 'image'"
    class="vslk-card vslk-card--image"
    :style="{
      width: containerWidth,
      borderRadius: cardRadius,
      borderWidth,
      borderColor,
    }"
  >
    <Skeleton
      v-bind="forwardedProps"
      variant="block"
      width="100%"
      :height="imageHeight"
      :radius="cardRadius"
    />
 
    <div class="vslk-card__content">
      <Skeleton
        v-bind="forwardedProps"
        variant="text"
        :width="titleWidth"
        height="14px"
      />
 
      <Skeleton
        v-for="(w, i) in lineWidths"
        :key="i"
        v-bind="forwardedProps"
        variant="text"
        :width="w"
        height="10px"
      />
    </div>
  </div>
 
  <div
    v-else-if="layout === 'simple'"
    class="vslk-card vslk-card--simple"
    :style="{
      width: containerWidth,
      borderRadius: cardRadius,
      borderWidth,
      borderColor,
    }"
  >
    <div class="vslk-card__content">
      <Skeleton
        v-bind="forwardedProps"
        variant="text"
        :width="titleWidth"
        height="14px"
      />
 
      <Skeleton
        v-for="(w, i) in lineWidths"
        :key="i"
        v-bind="forwardedProps"
        variant="text"
        :width="w"
        height="10px"
      />
    </div>
  </div>
 
  <div
    v-else
    class="vslk-card vslk-card--horizontal"
    :style="{
      width: containerWidth,
      borderRadius: cardRadius,
      borderWidth,
      borderColor,
    }"
  >
    <Skeleton
      v-bind="forwardedProps"
      variant="block"
      :width="mediaWidth"
      :height="mediaWidth"
      :radius="cardRadius"
      class="vslk-card__media"
    />
 
    <div class="vslk-card__content">
      <Skeleton
        v-bind="forwardedProps"
        variant="text"
        :width="titleWidth"
        height="14px"
      />
 
      <Skeleton
        v-for="(w, i) in lineWidths"
        :key="i"
        v-bind="forwardedProps"
        variant="text"
        :width="w"
        height="10px"
      />
    </div>
  </div>
</template>
 
<style scoped>
.vslk-card {
  display: flex;
  gap: 16px;
  padding: 16px;
  border-style: solid;
  background: transparent;
  box-sizing: border-box;
  overflow: hidden;
}
 
.vslk-card--image,
.vslk-card--simple {
  flex-direction: column;
}
 
.vslk-card--horizontal {
  flex-direction: row;
  align-items: flex-start;
}
.vslk-card--horizontal :deep(.vslk-card__media) {
  flex-shrink: 0;
}
 
.vslk-card__content {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-width: 0;
}
</style>