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.
Loading videos...
Custom Max Results
You can specify a different number of videos to display using the maxResults prop.
Loading videos...
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"
/>
// With custom classes:
<({
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"
/>HTML (Server-Side Rendered)
<div
class="lta-youtube-playlist-carousel js-lta-youtube-playlist-carousel"
data-playlist-id="PL6P9iGS4Ir9nWAVdA1DkOHJ7Vrkm_uCEn"
data-max-results="6"
data-cache-duration="10"
>
<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 will be rendered here -->
</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>Props
| Prop | Type | Required | Default | Description |
|---|---|---|---|---|
playlistId | string | Yes | - | The YouTube playlist ID (e.g., from the playlist URL: https://www.youtube.com/playlist?list=PL6P9iGS4Ir9nWAVdA1DkOHJ7Vrkm_uCEn) |
maxResults | number | No | 6 | Maximum number of videos to display in the carousel |
classes | string | No | - | 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:
- Go to Google Cloud Console
- Create a new project or select an existing one
- Enable the YouTube Data API v3
- Create credentials (API Key)
- 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