Tabs Plan Picker
Animate the height of the panel area when the panels have different heights, like pricing plans with more features in each tier. A tabclicked listener reads the height of sv-tabs, sets value, reads the height again, and animates between the two heights.
Since interpolate-size only animates between auto and a length, the height needs a script. block-size: 0 on [data-sv-leaving] keeps the old panel out of the height while it slides out.
Reduced Motion
Your system is set to reduce motion, so the animations shown on this page will not play for you.
$0 per month
- 1 project
- Community support
$12 per month
- Unlimited projects
- Custom domains
- Email support
$40 per month
- Everything in Pro
- Roles and permissions
- Single sign-on
- Priority support
- Shared storage
Code
The following example assumes that you have imported the component and set up the theme.
<div class="tabs-plan-picker">42 collapsed lines
<sv-tabs aria-label="Plans" value="pro"> <button value="starter">Starter</button> <button value="pro">Pro</button> <button value="team">Team</button>
<div slot="starter"> <p class="price"> <strong>$0</strong> per month </p> <ul> <li>1 project</li> <li>Community support</li> </ul> </div>
<div slot="pro"> <p class="price"> <strong>$12</strong> per month </p> <ul> <li>Unlimited projects</li> <li>Custom domains</li> <li>Email support</li> </ul> </div>
<div slot="team"> <p class="price"> <strong>$40</strong> per month </p> <ul> <li>Everything in Pro</li> <li>Roles and permissions</li> <li>Single sign-on</li> <li>Priority support</li> <li>Shared storage</li> </ul> </div> </sv-tabs></div>
<script type="application/x-typescript" module>18 collapsed lines
const planTabs = document.querySelector<Tabs>(".tabs-plan-picker sv-tabs")!; const reducedMotionQuery = matchMedia("(prefers-reduced-motion: reduce)"); const heightAnimationOptions = { duration: 400, easing: "cubic-bezier(0.16, 1, 0.3, 1)" };
let heightAnimation: Animation | undefined;
planTabs.addEventListener("tabclicked", event => { if (reducedMotionQuery.matches) return;
const startHeight = planTabs.offsetHeight; heightAnimation?.cancel();
planTabs.value = event.detail.value; const endHeight = planTabs.offsetHeight;
const heightKeyframes = { height: [`${startHeight}px`, `${endHeight}px`] }; heightAnimation = planTabs.animate(heightKeyframes, heightAnimationOptions); });</script>
<style>86 collapsed lines
.tabs-plan-picker { max-inline-size: 30rem; margin-inline: auto;
sv-tabs::part(indicator), sv-tabs > button { border: 1px solid transparent; border-start-start-radius: var(--sv-bdr-rad-md); border-start-end-radius: var(--sv-bdr-rad-md); }
sv-tabs::part(indicator), sv-tabs > [data-sv-active], sv-tabs::part(panels) { background-color: var(--sv-gray-7); border: 1px solid var(--sv-gray-5); }
sv-tabs::part(indicator), sv-tabs > [data-sv-active] { border-block-end-color: var(--sv-gray-7); }
sv-tabs::part(panels) { min-block-size: 0; padding: 1.25em; overflow: clip; border-end-start-radius: var(--sv-bdr-rad-md); border-end-end-radius: var(--sv-bdr-rad-md); }
sv-tabs > [slot] { transition: opacity 250ms, translate 400ms cubic-bezier(0.16, 1, 0.3, 1);
@starting-style { opacity: 0; translate: var(--slide-offset, 0) 0; }
&[data-sv-leaving] { block-size: 0; opacity: 0; translate: calc(-1 * var(--slide-offset, 0)) 0; }
@media (prefers-reduced-motion: reduce) { transition: none; } }
sv-tabs:state(--forward) > [slot] { --slide-offset: 3em; }
sv-tabs:state(--backward) > [slot] { --slide-offset: -3em; }
.price { margin-block-start: 0; color: var(--sv-gray-2);
strong { font-size: 2em; line-height: 1; color: var(--sv-gray-1); } }
ul { display: grid; gap: 0.5em; padding: 0; margin: 0; list-style: none; }
li::before { margin-inline-end: 0.5em; font-weight: bold; color: var(--sv-accent); content: "✓"; } }</style>