Zoeken...  ⌘K GitHub

TestimonialsSlider Social Proof

Testimonials slider met auto-advance, grote quote centraal, dot navigatie met voortgangsbalk. Pauzeer bij hover.

/testimonials-slider
src/components/sections/TestimonialsSlider.astro
---
/**
 * TestimonialsSlider
 * Strak testimonials component: grote quote prominent centraal, avatars
 * als navigatie-pills, auto-advance met pauze op hover. Puur CSS + minimal JS.
 */
interface Testimonial {
  quote: string;
  name: string;
  role?: string;
  company?: string;
  avatar?: string;
  rating?: number;
}

interface Props {
  preHeadline?: string;
  headline?: string;
  testimonials: Testimonial[];
  accentColor?: string;
}

const {
  preHeadline,
  headline,
  testimonials = [],
} = Astro.props;
---

<section class="ts" data-component="testimonials-slider">
  <div class="ts__inner">

    {(preHeadline || headline) && (
      <div class="ts__header">
        {preHeadline && <p class="ts__pre">{preHeadline}</p>}
        {headline && <h2 class="ts__headline" set:html={headline} />}
      </div>
    )}

    <div class="ts__stage">
      <!-- Quotes -->
      {testimonials.map((t, i) => (
        <div class="ts__slide" data-slide={i} class:list={[{ 'ts__slide--active': i === 0 }]}>
          <!-- Stars -->
          {t.rating && (
            <div class="ts__stars" aria-label={`${t.rating} van 5 sterren`}>
              {Array.from({ length: 5 }).map((_, si) => (
                <svg width="16" height="16" viewBox="0 0 24 24" fill={si < t.rating! ? 'currentColor' : 'none'} stroke="currentColor" stroke-width="1.5">
                  <path d="M12 2l3.09 6.26L22 9.27l-5 4.87 1.18 6.88L12 17.77l-6.18 3.25L7 14.14 2 9.27l6.91-1.01L12 2z"/>
                </svg>
              ))}
            </div>
          )}

          <blockquote class="ts__quote">
            <span class="ts__quote-mark">"</span>
            {t.quote}
          </blockquote>

          <div class="ts__author">
            {t.avatar ? (
              <img src={t.avatar} alt={t.name} class="ts__avatar" />
            ) : (
              <div class="ts__avatar-fallback">
                {t.name.charAt(0).toUpperCase()}
              </div>
            )}
            <div class="ts__author-info">
              <span class="ts__name">{t.name}</span>
              {(t.role || t.company) && (
                <span class="ts__role">
                  {t.role}{t.role && t.company && ' · '}{t.company}
                </span>
              )}
            </div>
          </div>
        </div>
      ))}
    </div>

    <!-- Navigation dots + progress -->
    <div class="ts__nav">
      {testimonials.map((t, i) => (
        <button class="ts__dot" data-target={i} aria-label={`Testimonial van ${t.name}`}>
          <span class="ts__dot-fill"></span>
        </button>
      ))}
    </div>

    <!-- Prev/Next -->
    <div class="ts__arrows">
      <button class="ts__arrow ts__arrow--prev" aria-label="Vorige">
        <svg width="18" height="18" viewBox="0 0 18 18" fill="none">
          <path d="M11 4L6 9l5 5" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
        </svg>
      </button>
      <button class="ts__arrow ts__arrow--next" aria-label="Volgende">
        <svg width="18" height="18" viewBox="0 0 18 18" fill="none">
          <path d="M7 4l5 5-5 5" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
        </svg>
      </button>
    </div>
  </div>
</section>

<script>
  const section = document.querySelector('[data-component="testimonials-slider"]');
  if (section) {
    const slides = section.querySelectorAll<HTMLElement>('.ts__slide');
    const dots = section.querySelectorAll<HTMLButtonElement>('.ts__dot');
    const prevBtn = section.querySelector<HTMLButtonElement>('.ts__arrow--prev');
    const nextBtn = section.querySelector<HTMLButtonElement>('.ts__arrow--next');
    let current = 0;
    let timer: ReturnType<typeof setInterval>;

    function go(index: number) {
      slides[current].classList.remove('ts__slide--active');
      dots[current].classList.remove('ts__dot--active');
      current = (index + slides.length) % slides.length;
      slides[current].classList.add('ts__slide--active');
      dots[current].classList.add('ts__dot--active');
    }

    function startTimer() {
      clearInterval(timer);
      timer = setInterval(() => go(current + 1), 5500);
    }

    dots.forEach((dot, i) => dot.addEventListener('click', () => { go(i); startTimer(); }));
    prevBtn?.addEventListener('click', () => { go(current - 1); startTimer(); });
    nextBtn?.addEventListener('click', () => { go(current + 1); startTimer(); });

    section.addEventListener('mouseenter', () => clearInterval(timer));
    section.addEventListener('mouseleave', startTimer);

    go(0);
    startTimer();
  }
</script>

<style>
  .ts {
    padding: 6rem 1.5rem;
    background: var(--color-bg, #fff);
    overflow: hidden;
  }

  .ts__inner {
    max-width: 800px;
    margin: 0 auto;
    position: relative;
  }

  .ts__header {
    text-align: center;
    margin-bottom: 4rem;
  }

  .ts__pre {
    font-size: 0.75rem;
    font-weight: 700;
    letter-spacing: 0.12em;
    text-transform: uppercase;
    color: var(--color-accent, #6366f1);
    margin-bottom: 1rem;
  }

  .ts__headline {
    font-size: clamp(1.75rem, 3.5vw, 2.75rem);
    font-weight: 800;
    letter-spacing: -0.03em;
    color: var(--color-primary, #0a0a0a);
    line-height: 1.1;
  }

  .ts__headline :global(em) {
    font-style: normal;
    color: var(--color-accent, #6366f1);
  }

  /* Stage */
  .ts__stage {
    position: relative;
    min-height: 280px;
    margin-bottom: 3rem;
  }

  .ts__slide {
    position: absolute;
    inset: 0;
    opacity: 0;
    transform: translateY(16px);
    transition: opacity 0.5s cubic-bezier(0.22,1,0.36,1), transform 0.5s cubic-bezier(0.22,1,0.36,1);
    pointer-events: none;
    display: flex;
    flex-direction: column;
    align-items: center;
    text-align: center;
    gap: 1.5rem;
    padding: 0 1rem;
  }

  .ts__slide--active {
    opacity: 1;
    transform: translateY(0);
    pointer-events: auto;
    position: relative;
  }

  /* Stars */
  .ts__stars {
    display: flex;
    gap: 0.25rem;
    color: #f59e0b;
  }

  /* Quote */
  .ts__quote {
    font-size: clamp(1.125rem, 2.5vw, 1.5rem);
    font-weight: 500;
    line-height: 1.6;
    color: var(--color-primary, #0a0a0a);
    letter-spacing: -0.01em;
    position: relative;
    quotes: none;
  }

  .ts__quote-mark {
    font-size: 5rem;
    line-height: 0;
    vertical-align: -0.5em;
    color: var(--color-accent, #6366f1);
    font-weight: 900;
    opacity: 0.15;
    margin-right: -0.1em;
    font-style: normal;
  }

  /* Author */
  .ts__author {
    display: flex;
    align-items: center;
    gap: 0.875rem;
  }

  .ts__avatar {
    width: 48px;
    height: 48px;
    border-radius: 50%;
    object-fit: cover;
    flex-shrink: 0;
    border: 2px solid rgba(0,0,0,0.06);
  }

  .ts__avatar-fallback {
    width: 48px;
    height: 48px;
    border-radius: 50%;
    background: var(--color-accent, #6366f1);
    color: #fff;
    font-weight: 700;
    font-size: 1.125rem;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
  }

  .ts__author-info {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    text-align: left;
  }

  .ts__name {
    font-size: 0.9375rem;
    font-weight: 700;
    color: var(--color-primary, #0a0a0a);
  }

  .ts__role {
    font-size: 0.8125rem;
    color: var(--color-muted, #6b7280);
  }

  /* Navigation dots */
  .ts__nav {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    margin-bottom: 0;
  }

  .ts__dot {
    width: 32px;
    height: 4px;
    border-radius: 2px;
    background: rgba(0,0,0,0.1);
    border: none;
    cursor: pointer;
    padding: 0;
    transition: width 0.3s, background 0.2s;
    position: relative;
    overflow: hidden;
  }

  .ts__dot--active {
    width: 48px;
    background: rgba(0,0,0,0.15);
  }

  .ts__dot-fill {
    position: absolute;
    inset: 0;
    background: var(--color-accent, #6366f1);
    transform: scaleX(0);
    transform-origin: left;
  }

  .ts__dot--active .ts__dot-fill {
    animation: ts-fill 5.5s linear forwards;
  }

  @keyframes ts-fill {
    from { transform: scaleX(0); }
    to   { transform: scaleX(1); }
  }

  /* Arrows */
  .ts__arrows {
    position: absolute;
    top: 50%;
    left: 0;
    right: 0;
    transform: translateY(-50%);
    display: flex;
    justify-content: space-between;
    pointer-events: none;
  }

  .ts__arrow {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    border: 1px solid rgba(0,0,0,0.1);
    background: var(--color-bg, #fff);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    color: var(--color-primary, #0a0a0a);
    transition: border-color 0.2s, box-shadow 0.2s;
    pointer-events: auto;
    transform: translateX(-60px);
  }

  .ts__arrow--next { transform: translateX(60px); }

  .ts__arrow:hover {
    border-color: var(--color-accent, #6366f1);
    box-shadow: 0 2px 12px rgba(99,102,241,0.15);
    color: var(--color-accent, #6366f1);
  }

  @media (max-width: 768px) {
    .ts__arrow { display: none; }
    .ts__stage { min-height: 320px; }
  }
</style>

Props

Prop Type Default Beschrijving
testimonials * Testimonial[] Array van testimonials met quote, name, role, company, rating
preHeadline string Label boven sectie
headline string Sectie headline

* = verplicht