Tabs Product Details
Load the content of a panel the first time its tab is selected, like reviews on a product page. A valuechange listener fills the child whose slot is the new value.
Since hidden panels do not render, a loading="lazy" image or iframe in a panel loads only when its panel shows.
A light rain jacket that packs into its own pocket. Taped seams keep the water out, and vents under the arms let the heat out.
Code
The following example assumes that you have imported the component and set up the theme.
<div class="tabs-product-details">29 collapsed lines
<sv-tabs aria-label="Product details" value="description"> <button value="description">Description</button> <button value="reviews">Reviews</button> <button value="questions">Questions</button>
<div slot="description"> <p> A light rain jacket that packs into its own pocket. Taped seams keep the water out, and vents under the arms let the heat out. </p> </div> <div slot="reviews"> <div class="skeleton"></div> <div class="skeleton"></div> <div class="skeleton"></div> </div> <div slot="questions"> <div class="skeleton"></div> <div class="skeleton"></div> <div class="skeleton"></div> </div> </sv-tabs>
<template> <div class="entry"> <strong></strong> <p></p> </div> </template></div>
<script>42 collapsed lines
const productTabs = document.querySelector(".tabs-product-details sv-tabs"); const entryTemplate = document.querySelector(".tabs-product-details template"); const requestedValues = new Set(["description"]);
// Stands in for a fetch request async function fetchEntries(value) { await new Promise(resolve => setTimeout(resolve, 1000));
const entriesByValue = { reviews: [ ["★★★★★ Dry all day", "Wore it on a full day of hiking in the rain and stayed dry."], ["★★★★☆ Runs small", "Good jacket, but order one size up if you wear a sweater under it."], ], questions: [ ["Does it have a hood?", "Yes, and it rolls into the collar when you do not need it."], [ "Can I wash it in a machine?", "Yes, on a cold and gentle cycle. Do not use fabric softener.", ], ], };
return entriesByValue[value]; }
function createEntry([title, text]) { const entry = entryTemplate.content.cloneNode(true); entry.querySelector("strong").textContent = title; entry.querySelector("p").textContent = text; return entry; }
productTabs.addEventListener("valuechange", async () => { const { value } = productTabs; if (requestedValues.has(value)) return;
requestedValues.add(value);
const panel = productTabs.querySelector(`[slot="${value}"]`); const entries = await fetchEntries(value); panel.replaceChildren(...entries.map(createEntry)); });</script>
<style>43 collapsed lines
.tabs-product-details { max-inline-size: 32rem; margin-inline: auto;
p { margin: 0.25em 0 0; }
.entry + .entry { margin-block-start: 1em; }
.skeleton { block-size: 1em; background-image: linear-gradient( 90deg, var(--sv-gray-6) 30%, var(--sv-gray-5), var(--sv-gray-6) 70% ); background-size: 300% 100%; border-radius: var(--sv-bdr-rad-sm); animation: tabs-product-details-shimmer 1.2s linear infinite;
& + & { margin-block-start: 0.6em; }
&:last-of-type { inline-size: 60%; }
@media (prefers-reduced-motion: reduce) { animation: none; } } }
@keyframes tabs-product-details-shimmer { to { background-position-x: -150%; } }</style>