BizzPocket

Testimonials carousel without React

A swipeable row of customer quotes with previous and next buttons and arrow keys, built on sir.carousel. No JavaScript of your own.

On this page

Goal: a row of customer quotes that snaps one card at a time, with previous / next buttons and arrow-key support, and no island: sir.carousel drives it.


The block

blocks/quote-carousel/quote-carousel.tsx
import { block, f, sir } from '@bizzpocket/theme';
import './quote-carousel.css';

/** An id safe for `id=` and `aria-controls`. */
const domId = (raw: string) => raw.replace(/[^A-Za-z0-9_-]/g, '-');

function Stars({ rating }: { rating: number }) {
  return (
    <span className="qc__stars" role="img" aria-label={`${rating} out of 5 stars`}>
      {[1, 2, 3, 4, 5].map((n) => (
        <svg key={n} viewBox="0 0 24 24" width="16" height="16" aria-hidden="true"
          fill={n <= rating ? 'currentColor' : 'none'} stroke="currentColor" strokeWidth="1.5" strokeLinejoin="round">
          <path d="m12 3 2.7 5.5 6.1.9-4.4 4.3 1 6.1-5.4-2.9-5.4 2.9 1-6.1L3.2 9.4l6.1-.9L12 3Z" />
        </svg>
      ))}
    </span>
  );
}

export default block({
  name: 'Quote carousel',
  category: 'social-proof',
  description: 'Quotes from your customers in a swipeable row. Hidden until you add one.',
  fields: {
    heading: f.text({ optional: true, default: 'What customers say' }),
    quotes: f.list(
      {
        text: f.text({ multiline: true, label: 'Quote' }),
        name: f.text({ label: 'Customer name' }),
        detail: f.text({ optional: true, label: 'Place or detail', help: 'Optional, for example a city.' }),
        rating: f.number({ optional: true, min: 1, max: 5, step: 1, label: 'Stars (1 to 5)' }),
      },
      { max: 12, label: 'Quotes', help: 'Only add words your customers really said.' },
    ),
  },
  render: ({ heading, quotes }, ctx) => {
    const items = quotes.filter((q) => q.text.trim() !== '' && q.name.trim() !== '');
    // No quotes: render nothing rather than a heading over an empty row.
    if (items.length === 0) return null;
    const trackId = `qc-${domId(ctx.blockId)}`;
    const many = items.length > 1;
    return (
      // One quote needs no carousel: the behaviour and the buttons are only there for two or more.
      <div className="qc" {...(many ? sir.carousel() : {})}>
        {heading ? <h2 className="qc__heading">{heading}</h2> : null}
        <ul id={trackId} className="qc__track" data-bp-carousel-track="" aria-label="Customer quotes">
          {items.map((q, i) => (
            <li key={i} className="qc__slide" aria-roledescription="slide" aria-label={`${i + 1} of ${items.length}`}>
              <figure className="qc__card">
                {q.rating !== undefined ? <Stars rating={Math.trunc(q.rating)} /> : null}
                <blockquote className="qc__quote">{q.text}</blockquote>
                <figcaption className="qc__who">
                  <strong>{q.name}</strong>
                  {q.detail ? <span>{q.detail}</span> : null}
                </figcaption>
              </figure>
            </li>
          ))}
        </ul>
        {many ? (
          <div className="qc__controls">
            <button type="button" className="qc__nav" data-bp-carousel-prev="" aria-controls={trackId} aria-label="Previous quote">
              ‹
            </button>
            {/* Left empty: the runtime adds one dot per quote. */}
            <div className="qc__dots" data-bp-carousel-dots="" />
            <button type="button" className="qc__nav" data-bp-carousel-next="" aria-controls={trackId} aria-label="Next quote">
              ›
            </button>
          </div>
        ) : null}
      </div>
    );
  },
});
blocks/quote-carousel/quote-carousel.css
[data-theme] .qc {
  padding-block: var(--spacing-section-y, 3rem);
}
[data-theme] .qc__heading {
  margin: 0 0 1.25rem;
  text-align: center;
}
/* The track scrolls by itself: swipe and trackpad work before (and without) the runtime. */
[data-theme] .qc__track {
  display: flex;
  gap: var(--spacing-s, 1rem);
  margin: 0;
  padding: 0 0 0.5rem;
  list-style: none;
  overflow-x: auto;
  scroll-snap-type: x mandatory;
  scroll-padding-inline: 1rem;
  overscroll-behavior-x: contain;
  scrollbar-width: none;
}
[data-theme] .qc__track::-webkit-scrollbar {
  display: none;
}
[data-theme] .qc__track:focus-visible {
  outline: 2px solid var(--color-primary, currentColor);
  outline-offset: 4px;
}
/* One card on phones, two on tablets, three on computers. */
[data-theme] .qc__slide {
  flex: 0 0 85%;
  scroll-snap-align: start;
}
@media (min-width: 48rem) {
  [data-theme] .qc__slide {
    flex-basis: calc((100% - var(--spacing-s, 1rem)) / 2);
  }
}
@media (min-width: 64rem) {
  [data-theme] .qc__slide {
    flex-basis: calc((100% - 2 * var(--spacing-s, 1rem)) / 3);
  }
}
[data-theme] .qc__card {
  height: 100%;
  box-sizing: border-box;
  margin: 0;
  padding: 1.5rem;
  border-radius: var(--radius-card, 0.75rem);
  background: var(--color-surface, rgba(0, 0, 0, 0.04));
}
[data-theme] .qc__stars {
  display: inline-flex;
  gap: 2px;
  color: var(--color-accent, #d99a00);
}
[data-theme] .qc__quote {
  margin: 0.75rem 0 1rem;
}
[data-theme] .qc__who {
  display: flex;
  flex-direction: column;
  font-size: 0.875rem;
}
[data-theme] .qc__controls {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 0.5rem;
  margin-top: 1rem;
}
[data-theme] .qc__dots {
  display: flex;
  gap: 0.25rem;
}
/* A 24px target around an 8px dot. */
[data-theme] .qc__dots button {
  width: 1.5rem;
  height: 1.5rem;
  padding: 0;
  border: 0;
  background: radial-gradient(circle, currentColor 0 0.25rem, transparent 0.3rem);
  opacity: 0.35;
  cursor: pointer;
}
[data-theme] .qc__dots button[aria-current='true'] {
  opacity: 1;
}
[data-theme] .qc__nav:disabled {
  opacity: 0.35;
  cursor: default;
}
[data-theme] .qc__nav {
  width: 2.75rem;
  height: 2.75rem;
  border: 1px solid currentColor;
  border-radius: 999px;
  background: transparent;
  color: inherit;
  font-size: 1.5rem;
  line-height: 1;
  cursor: pointer;
}

Add it to the home page with bizzpocket add-to-layout quote-carousel, then fill in the quotes in the editor (or in props.quotes of the layout node).


How it works

  • sir.carousel() on the root adds data-bp-island="carousel". The SIR runtime that every storefront page already loads finds the root and wires it up. It reads these inside:
    • [data-bp-carousel-track], the element that scrolls;
    • [data-bp-carousel-prev] and [data-bp-carousel-next], the buttons;
    • an empty [data-bp-carousel-dots] (optional), which it fills with one dot per slide.
  • What the runtime does. A button click scrolls the track by 90 % of its width (pass sir.carousel({ 'data-step-px': '320' }) for a fixed step). The left and right arrow keys do the same when the track has focus. It gives the track role="region", aria-roledescription="carousel", tabindex="0" and an aria-label unless you set them, and it jumps instead of scrolling smoothly under prefers-reduced-motion.
  • The CSS does the rest. scroll-snap-type makes each card stop in place. The track is a normal scrolling element, so swipe, trackpad and the scrollbar keys work before the runtime starts, and every quote is in the HTML for search engines and screen readers.
  • Nothing is invented. With no quotes the block renders nothing. With one quote it is a plain card with no buttons and no behaviour.
  • The ends and the dots. At the start the previous button is disabled (with aria-disabled="true"), at the end the next one; it re-checks on scroll and resize, and the CSS above dims them. Each dot is a button labelled "Go to slide 2 of 5" that scrolls its quote into place; the dot of the quote nearest the start has aria-current="true". Before the runtime starts the dots box is empty, and it is inside the controls row, so nothing moves when they appear.
  • What it does not do. There is no autoplay. Add it with an island if you really need it (and give it a pause button).

JS cost

0 bytes from your theme (client gz 0 in the size table). The carousel behaviour is part of the shared SIR runtime the page loads anyway. CSS is about 0.6 KB gzip.

See also

  • Interactivity: sir attributes: every SIR helper and the markup it needs.
  • Fields: f.list and its options.
  • The registry's testimonials block (bizzpocket add testimonials) is a fuller version of this recipe.