YouTube Playlist Carousel

The YouTube Playlist Carousel component automatically fetches and displays videos from a YouTube playlist in a carousel format. Videos are loaded dynamically from the YouTube Data API and displayed using the MediaCarousel component.

Basic Usage

Display the latest 6 videos from a YouTube playlist. The component requires a playlistId parameter. The YouTube API key is configured on the backend via the YOUTUBE_API_KEY environment variable.

Custom Max Results

You can specify a different number of videos to display using the maxResults prop.

Usage Examples

Here's how to use the component in your templates:

11ty Template (Preact/HTM)

const YouTubePlaylistCarousel = require('./_includes/components/YouTubePlaylistCarousel/YouTubePlaylistCarousel');
const { html } = require('htm/preact');

// In your 11ty template:
<({
	playlistId,
	maxResults = 6,
	classes,
	...properties
}) => {
	if (!playlistId) {
		return html`<div class="u-text-error">Error: playlistId is required</div>`;
	}

	const classNames = classnames(
		'lta-youtube-playlist-carousel',
		'js-lta-youtube-playlist-carousel',
		classes
	);

	// Render a placeholder carousel that will be populated by client-side script
	return html`
		<div 
			class="${classNames}" 
			data-playlist-id="${playlistId}"
			data-max-results="${maxResults}"
			data-cache-duration="10"
			"...${properties}"
		>
			<div class="lta-youtube-playlist-carousel__loading u-text-center u-py-4">
				<p>Loading videos...</p>
			</div>
			<div class="lta-youtube-playlist-carousel__content" style="display: none;">
                        <${MediaCarousel} classes="lta-youtube-playlist-carousel__carousel" data-loop="false">
                                <!-- Videos will be inserted here by client-side script as swiper-slide elements with data-is-video attribute -->
                        </${MediaCarousel}>
			</div>
			<div class="lta-youtube-playlist-carousel__error" style="display: none;">
				<p class="u-text-error">Failed to load videos. Please try again later.</p>
			</div>
		</div>
	`;
}playlistId="PL6P9iGS4Ir9nWAVdA1DkOHJ7Vrkm_uCEn"
	maxResults="6"
/&gt;

// With custom classes:
&lt;({
	playlistId,
	maxResults = 6,
	classes,
	...properties
}) => {
	if (!playlistId) {
		return html`<div class="u-text-error">Error: playlistId is required</div>`;
	}

	const classNames = classnames(
		'lta-youtube-playlist-carousel',
		'js-lta-youtube-playlist-carousel',
		classes
	);

	// Render a placeholder carousel that will be populated by client-side script
	return html`
		<div 
			class="${classNames}" 
			data-playlist-id="${playlistId}"
			data-max-results="${maxResults}"
			data-cache-duration="10"
			"...${properties}"
		>
			<div class="lta-youtube-playlist-carousel__loading u-text-center u-py-4">
				<p>Loading videos...</p>
			</div>
			<div class="lta-youtube-playlist-carousel__content" style="display: none;">
                        <${MediaCarousel} classes="lta-youtube-playlist-carousel__carousel" data-loop="false">
                                <!-- Videos will be inserted here by client-side script as swiper-slide elements with data-is-video attribute -->
                        </${MediaCarousel}>
			</div>
			<div class="lta-youtube-playlist-carousel__error" style="display: none;">
				<p class="u-text-error">Failed to load videos. Please try again later.</p>
			</div>
		</div>
	`;
}playlistId="PL6P9iGS4Ir9nWAVdA1DkOHJ7Vrkm_uCEn"
	maxResults="4"
	classes="my-custom-class"
/&gt;

HTML (Server-Side Rendered)

&lt;div 
	class="lta-youtube-playlist-carousel js-lta-youtube-playlist-carousel"
	data-playlist-id="PL6P9iGS4Ir9nWAVdA1DkOHJ7Vrkm_uCEn"
	data-max-results="6"
	data-cache-duration="10"
&gt;
	&lt;div class="lta-youtube-playlist-carousel__loading u-text-center u-py-4"&gt;
		&lt;p&gt;Loading videos...&lt;/p&gt;
	&lt;/div&gt;
	&lt;div class="lta-youtube-playlist-carousel__content" style="display: none;"&gt;
		&lt;!-- MediaCarousel will be rendered here --&gt;
	&lt;/div&gt;
	&lt;div class="lta-youtube-playlist-carousel__error" style="display: none;"&gt;
		&lt;p class="u-text-error"&gt;Failed to load videos. Please try again later.&lt;/p&gt;
	&lt;/div&gt;
&lt;/div&gt;

Props

PropTypeRequiredDefaultDescription
playlistIdstringYes-The YouTube playlist ID (e.g., from the playlist URL: https://www.youtube.com/playlist?list=PL6P9iGS4Ir9nWAVdA1DkOHJ7Vrkm_uCEn)
maxResultsnumberNo6Maximum number of videos to display in the carousel
classesstringNo-Additional CSS classes to apply to the component wrapper

Configuration

Note: This component requires a YouTube Data API v3 key to be configured on the backend. Set the YOUTUBE_API_KEY environment variable with your API key.

To get a YouTube Data API v3 key:

  1. Go to Google Cloud Console
  2. Create a new project or select an existing one
  3. Enable the YouTube Data API v3
  4. Create credentials (API Key)
  5. Restrict the API key to YouTube Data API v3 for security

The component automatically:

  • Fetches videos from the backend API endpoint (which handles CORS and API key securely)
  • Displays them in a carousel using MediaCarousel
  • Shows loading and error states
  • Updates automatically when the page loads
  • Handles YouTube API quota errors gracefully