Tabs Trip Itinerary
Show each day of a trip in one row that the user can swipe, with tabs that follow it. A child without a slot always shows and holds every day in one scroll container, a valuechange listener scrolls it, and a scrollend listener sets value.
Walk the Alfama hills, then take tram 28 up to the castle. Dinner at a fado house.
Train from Rossio at 9:00. Pena Palace before noon, the Moorish Castle after lunch.
A beach morning at Praia da Rainha, then a bike ride along the coast road to Guincho.
Morning train north. Port cellars in Gaia, and the Dom Luís I bridge at sunset.
Code
The following example assumes that you have imported the component and set up the theme.
<div class="tabs-trip-itinerary">25 collapsed lines
<sv-tabs aria-label="Trip days" value="day-1"> <button value="day-1">Day 1</button> <button value="day-2">Day 2</button> <button value="day-3">Day 3</button> <button value="day-4">Day 4</button>
<div class="days"> <section> <strong>Lisbon</strong> <p>Walk the Alfama hills, then take tram 28 up to the castle. Dinner at a fado house.</p> </section> <section> <strong>Sintra</strong> <p>Train from Rossio at 9:00. Pena Palace before noon, the Moorish Castle after lunch.</p> </section> <section> <strong>Cascais</strong> <p>A beach morning at Praia da Rainha, then a bike ride along the coast road to Guincho.</p> </section> <section> <strong>Porto</strong> <p>Morning train north. Port cellars in Gaia, and the Dom Luís I bridge at sunset.</p> </section> </div> </sv-tabs></div>
<script>26 collapsed lines
const dayTabs = document.querySelector(".tabs-trip-itinerary sv-tabs"); const dayButtons = [...dayTabs.querySelectorAll(":scope > button")]; const days = dayTabs.querySelector(".days"); const daySections = [...days.children];
for (const [index, button] of dayButtons.entries()) { button.ariaControlsElements = [daySections[index]]; daySections[index].role = "tabpanel"; daySections[index].ariaLabelledByElements = [button]; daySections[index].inert = index > 0; }
dayTabs.addEventListener("valuechange", () => { const index = dayButtons.findIndex(button => button.value === dayTabs.value); days.scrollTo({ left: index * days.clientWidth }); });
days.addEventListener("scrollend", () => { const index = Math.round(days.scrollLeft / days.clientWidth);
for (const [sectionIndex, section] of daySections.entries()) { section.inert = sectionIndex !== index; }
dayTabs.value = dayButtons[index].value; });</script>
<style>49 collapsed lines
.tabs-trip-itinerary { max-inline-size: 28rem; margin-inline: auto;
sv-tabs { --sv-tabs-bdr-sz: 0px; --sv-tabs-indicator-thickness: 0px; --sv-tabs-active-clr: var(--sv-accent-content); --sv-tabs-hover-bg: transparent;
&::part(indicator), > button { border-radius: 999px; }
&::part(indicator), > [data-sv-active] { background-color: var(--sv-accent); }
> button + button { margin-inline-start: 0.25em; } }
.days { display: grid; grid-auto-columns: 100%; grid-auto-flow: column; overflow-x: auto; scroll-behavior: smooth; scroll-snap-type: x mandatory; scrollbar-width: none;
@media (prefers-reduced-motion: reduce) { scroll-behavior: auto; } }
section { padding-inline: 1em; scroll-snap-align: start; scroll-snap-stop: always; }
p { margin: 0.25em 0 0; } }</style>