Segmented Control Expanding Labels
Show only icons, and reveal the label of the selected option with an animation. button[aria-checked="true"] grows its label's grid column from 0fr to 1fr, and a hidden copy of the longest label keeps the control at one width.
Reduced Motion
Your system is set to reduce motion, so the animations shown on this page will not play for you.
Code
The following example assumes that you have imported the component and set up the theme.
<sv-segmented-control class="segmented-expanding-labels" aria-label="Navigation" value="home">60 collapsed lines
<button value="home"> <!-- Home --> <svg aria-hidden="true" viewBox="0 0 24 24"> <path d="M10 20v-6h4v6h5v-8h3L12 3 2 12h3v8z"></path> </svg> <span class="label"> <span class="label-text"> <span>Home</span> <span aria-hidden="true">Library</span> </span> </span> </button>
<button value="search"> <!-- Search --> <svg aria-hidden="true" viewBox="0 0 24 24"> <path d="M15.5 14h-.79l-.28-.27A6.47 6.47 0 0 0 16 9.5 6.5 6.5 0 1 0 9.5 16c1.61 0 3.09-.59 4.23-1.57l.27.28v.79l5 4.99L20.49 19zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14" ></path> </svg> <span class="label"> <span class="label-text"> <span>Search</span> <span aria-hidden="true">Library</span> </span> </span> </button>
<button aria-label="Library, 3 new" value="library"> <span class="icon"> <!-- Library --> <svg aria-hidden="true" viewBox="0 0 24 24"> <path d="M21 5c-1.11-.35-2.33-.5-3.5-.5-1.95 0-4.05.4-5.5 1.5-1.45-1.1-3.55-1.5-5.5-1.5S2.45 4.9 1 6v14.65c0 .25.25.5.5.5.1 0 .15-.05.25-.05C3.1 20.45 5.05 20 6.5 20c1.95 0 4.05.4 5.5 1.5 1.35-.85 3.8-1.5 5.5-1.5 1.65 0 3.35.3 4.75 1.05.1.05.15.05.25.05.25 0 .5-.25.5-.5V6c-.6-.45-1.25-.75-2-1m0 13.5c-1.1-.35-2.3-.5-3.5-.5-1.7 0-4.15.65-5.5 1.5V8c1.35-.85 3.8-1.5 5.5-1.5 1.2 0 2.4.15 3.5.5z" ></path> </svg> <span class="unread-count" aria-hidden="true">3</span> </span> <span class="label"> <span class="label-text"> <span>Library</span> <span aria-hidden="true">Library</span> </span> </span> </button>
<button value="profile"> <!-- Profile --> <svg aria-hidden="true" viewBox="0 0 24 24"> <path d="M12 12c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4m0 2c-2.67 0-8 1.34-8 4v2h16v-2c0-2.66-5.33-4-8-4" ></path> </svg> <span class="label"> <span class="label-text"> <span>Profile</span> <span aria-hidden="true">Library</span> </span> </span> </button></sv-segmented-control>
<style>132 collapsed lines
@property --label-fade { syntax: "<length>"; inherits: false; initial-value: 0; }
.segmented-expanding-labels { --sv-segmented-control-dur: 400ms; --sv-segmented-control-ease: cubic-bezier(0, 0.55, 0.45, 1); --sv-segmented-control-bdr-rad: 999px; --sv-segmented-control-pad: 0.4em; --sv-segmented-control-active-bg: var(--sv-gray-0); --sv-segmented-control-active-clr: var(--sv-gray-7);
inline-size: fit-content; margin-inline: auto;
&::part(container) { color: var(--sv-gray-3); }
/* The sliding and the resting pill share one shadow */ &::part(indicator), button[data-sv-active] { box-shadow: 0 0.2em 0.6em rgb(0 0 0 / 25%); }
button { display: flex; flex: none; align-items: center; padding-inline: 0.8em; font: inherit; font-weight: 600; }
svg { inline-size: 1.3em; block-size: 1.3em; fill: currentcolor; transition: scale var(--sv-segmented-control-dur) var(--sv-segmented-control-ease); }
.icon { position: relative; display: grid; }
.unread-count { position: absolute; inset-block-start: -0.6em; inset-inline-end: -0.4em; display: grid; place-items: center; inline-size: 1.6em; block-size: 1.6em; font-size: 0.65em; font-weight: 700;
/* Icon color, with the digits in the surface color */ color: var(--sv-gray-6); background-color: var(--sv-gray-3); border-radius: 50%; transition-duration: var(--sv-segmented-control-dur); transition-property: color, background-color; }
.label { --label-fade: 1.5em;
display: grid; grid-template-columns: minmax(0, 0fr);
/* Clips the label and fades its ends. A narrow label fades almost entirely. */ mask-image: linear-gradient( to right, transparent, #000 var(--label-fade) calc(100% - var(--label-fade)), transparent ); transition-timing-function: var(--sv-segmented-control-ease); transition-duration: var(--sv-segmented-control-dur); transition-property: grid-template-columns, --label-fade; }
/* Keeps its full width while the column grows. The text then shows from the start */ .label-text { display: grid; justify-self: start;
span { grid-area: 1 / 1; justify-self: center; padding-inline-start: 0.4em; white-space: nowrap; }
/* Hidden copy of the longest label. Every label opens to its width. */ [aria-hidden] { visibility: hidden; } }
[aria-checked="true"] { svg { scale: 1.1; }
.unread-count { color: var(--sv-segmented-control-active-bg); background-color: var(--sv-segmented-control-active-clr); }
.label { --label-fade: 0px;
grid-template-columns: minmax(0, 1fr); } }
@media (max-width: 400px) { font-size: 0.85em;
button { padding-inline: 0.6em; } }
@media (prefers-reduced-motion: reduce) { --sv-segmented-control-dur: 0s; } }</style>