|
| 1 | +<script setup lang="ts"> |
| 2 | +import type { HTMLAttributes } from "vue"; |
| 3 | +import { ref } from "vue"; |
| 4 | +import { cn } from "@/lib/utils"; |
| 5 | +
|
| 6 | +defineOptions({ |
| 7 | + name: "TestimonialsBlock", |
| 8 | +}); |
| 9 | +
|
| 10 | +export interface TestimonialAvatar { |
| 11 | + src: string; |
| 12 | + alt?: string; |
| 13 | + srcset?: string; |
| 14 | +} |
| 15 | +
|
| 16 | +export interface TestimonialAuthor { |
| 17 | + name: string; |
| 18 | + role?: string; |
| 19 | + description?: string; |
| 20 | + avatar?: TestimonialAvatar; |
| 21 | +} |
| 22 | +
|
| 23 | +export interface TestimonialItem { |
| 24 | + id?: string; |
| 25 | + quote: string; |
| 26 | + author: TestimonialAuthor; |
| 27 | +} |
| 28 | +
|
| 29 | +export interface TestimonialsProps { |
| 30 | + /** Section heading shown above the testimonials */ |
| 31 | + title?: string; |
| 32 | + /** Optional supporting copy shown below the title */ |
| 33 | + description?: string; |
| 34 | + /** Testimonials supplied by the consuming app */ |
| 35 | + items?: TestimonialItem[]; |
| 36 | + /** Additional CSS classes for the section */ |
| 37 | + class?: HTMLAttributes["class"]; |
| 38 | +} |
| 39 | +
|
| 40 | +const props = withDefaults(defineProps<TestimonialsProps>(), { |
| 41 | + title: "What our customers say", |
| 42 | + description: "Real feedback from teams using this product to move faster.", |
| 43 | + items: () => [], |
| 44 | +}); |
| 45 | +
|
| 46 | +const failedAvatarKeys = ref(new Set<string>()); |
| 47 | +
|
| 48 | +function itemKey(item: TestimonialItem, index: number) { |
| 49 | + return item.id ?? `${item.author.name}-${index}`; |
| 50 | +} |
| 51 | +
|
| 52 | +function avatarKey(item: TestimonialItem, index: number) { |
| 53 | + return `${itemKey(item, index)}:${item.author.avatar?.src ?? ""}`; |
| 54 | +} |
| 55 | +
|
| 56 | +function initials(name: string) { |
| 57 | + return name |
| 58 | + .split(" ") |
| 59 | + .filter(Boolean) |
| 60 | + .slice(0, 2) |
| 61 | + .map(part => part.charAt(0)) |
| 62 | + .join("") |
| 63 | + .toUpperCase(); |
| 64 | +} |
| 65 | +
|
| 66 | +function onAvatarError(key: string) { |
| 67 | + failedAvatarKeys.value = new Set(failedAvatarKeys.value).add(key); |
| 68 | +} |
| 69 | +</script> |
| 70 | + |
| 71 | +<template> |
| 72 | + <section |
| 73 | + data-slot="testimonials" |
| 74 | + :class="cn('py-12 md:py-16', props.class)" |
| 75 | + > |
| 76 | + <div class="mx-auto w-full max-w-7xl px-4 sm:px-6 lg:px-8"> |
| 77 | + <div class="mx-auto max-w-2xl text-center"> |
| 78 | + <h2 class="text-balance text-3xl font-semibold tracking-tight sm:text-4xl"> |
| 79 | + {{ title }} |
| 80 | + </h2> |
| 81 | + <p |
| 82 | + v-if="description" |
| 83 | + class="mt-3 text-base/7 text-muted-foreground" |
| 84 | + > |
| 85 | + {{ description }} |
| 86 | + </p> |
| 87 | + </div> |
| 88 | + |
| 89 | + <div class="mt-10 grid gap-4 md:grid-cols-3"> |
| 90 | + <figure |
| 91 | + v-for="(item, index) in items" |
| 92 | + :key="itemKey(item, index)" |
| 93 | + class="flex min-w-0 flex-col justify-between rounded-2xl border bg-card p-6 text-card-foreground shadow-sm" |
| 94 | + > |
| 95 | + <blockquote class="text-base/7 text-foreground"> |
| 96 | + <p class="break-words before:mr-1 before:text-muted-foreground before:content-[open-quote] after:ml-1 after:text-muted-foreground after:content-[close-quote]"> |
| 97 | + {{ item.quote }} |
| 98 | + </p> |
| 99 | + </blockquote> |
| 100 | + |
| 101 | + <figcaption class="mt-8 flex items-center gap-3"> |
| 102 | + <span class="relative flex size-12 shrink-0 items-center justify-center overflow-hidden rounded-full bg-muted text-sm font-medium text-muted-foreground ring-1 ring-border"> |
| 103 | + <img |
| 104 | + v-if="item.author.avatar && !failedAvatarKeys.has(avatarKey(item, index))" |
| 105 | + :src="item.author.avatar.src" |
| 106 | + :srcset="item.author.avatar.srcset" |
| 107 | + :alt="item.author.avatar.alt ?? item.author.name" |
| 108 | + class="size-full object-cover" |
| 109 | + @error="onAvatarError(avatarKey(item, index))" |
| 110 | + > |
| 111 | + <span v-else>{{ initials(item.author.name) }}</span> |
| 112 | + </span> |
| 113 | + <span class="min-w-0"> |
| 114 | + <cite class="block truncate text-sm font-medium not-italic"> |
| 115 | + {{ item.author.name }} |
| 116 | + </cite> |
| 117 | + <span |
| 118 | + v-if="item.author.role || item.author.description" |
| 119 | + class="block truncate text-sm text-muted-foreground" |
| 120 | + > |
| 121 | + {{ item.author.role || item.author.description }} |
| 122 | + </span> |
| 123 | + </span> |
| 124 | + </figcaption> |
| 125 | + </figure> |
| 126 | + </div> |
| 127 | + </div> |
| 128 | + </section> |
| 129 | +</template> |
0 commit comments