Skip to content

Select Examples

The examples below assume that you have imported the component and set up the theme.

Basic

Build a basic dropdown from a trigger and option buttons. The trigger slot holds the trigger button, each other button is an option with a value, and data-sv-select-empty and data-sv-select-filled swap the placeholder for the chosen label.

--opened
State
<div class="select-basic">
<label class="field-label" for="basic-fruit-trigger">Fruit</label>
<sv-select close-on-select>
<button id="basic-fruit-trigger" type="button" slot="trigger">
<span data-sv-select-empty>Pick a fruit</span>
<span data-sv-select-filled data-sv-select-label></span>
</button>
<button value="apple">Apple</button>
<button value="banana">Banana</button>
<button value="cherry">Cherry</button>
</sv-select>
</div>
<style>
.select-basic {
display: grid;
grid-template-columns: min(100%, 16rem);
gap: 0.5em;
place-content: center;
color: var(--sv-gray-2);
.field-label {
font-weight: 600;
}
}
</style>

Icon

Replace the arrow in the trigger with your own icon. content: none on the trigger's ::after removes the default arrow, and sv-select:state(--opened) rotates the new icon while the list is open.

<div class="select-icon">
<label class="field-label" for="icon-size-trigger">Size</label>
<sv-select close-on-select>
<button id="icon-size-trigger" type="button" class="trigger" slot="trigger">
<span data-sv-select-empty>Pick a size</span>
<span data-sv-select-filled data-sv-select-label></span>
<svg
class="icon"
aria-hidden="true"
viewBox="0 -960 960 960"
xmlns="http://www.w3.org/2000/svg"
>
<path d="M480-345 240-585l56-56 184 184 184-184 56 56-240 240Z"></path>
</svg>
</button>
<button value="small">Small</button>
<button value="medium">Medium</button>
<button value="large">Large</button>
</sv-select>
</div>
<style>
.select-icon {
display: grid;
gap: 0.5em;
inline-size: min(100%, 16rem);
margin-inline: auto;
color: var(--sv-gray-2);
.field-label {
font-weight: 600;
}
.trigger::after {
content: none;
}
.icon {
flex: none;
inline-size: 1.25em;
block-size: 1.25em;
fill: currentcolor;
transition: rotate 200ms ease;
}
sv-select:state(--opened) .icon {
rotate: 180deg;
}
@media (prefers-reduced-motion: reduce) {
.icon {
transition: none;
}
}
}
</style>

Content

Put icons or other markup in the options. data-sv-select-clone shows a copy of the selected option in the trigger.


<div class="select-content">
<label class="field-label" for="content-status-trigger">Status</label>
<sv-select close-on-select value="online">
<button id="content-status-trigger" type="button" class="trigger" slot="trigger">
<span data-sv-select-clone></span>
</button>
<button class="option" value="online">
<!-- Online -->
<svg
class="icon"
aria-hidden="true"
viewBox="0 -960 960 960"
xmlns="http://www.w3.org/2000/svg"
>
<circle cx="480" cy="-480" r="380"></circle>
</svg>
Online
</button>
<button class="option" value="away">
<!-- Away -->
<svg
class="icon"
aria-hidden="true"
viewBox="0 -960 960 960"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M480-860a380 380 0 1 0 0 760 380 380 0 0 0 0-760Zm0 100a280 280 0 1 1 0 560 280 280 0 0 1 0-560Z"
fill-rule="evenodd"
></path>
<path
d="M480-660v180h140"
fill="none"
stroke="currentcolor"
stroke-linecap="round"
stroke-width="90"
></path>
</svg>
Away
</button>
<button class="option" value="busy">
<!-- Busy -->
<svg
class="icon"
aria-hidden="true"
viewBox="0 -960 960 960"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M480-860a380 380 0 1 0 0 760 380 380 0 0 0 0-760Zm0 100a280 280 0 1 1 0 560 280 280 0 0 1 0-560Z"
fill-rule="evenodd"
></path>
<rect height="90" rx="45" width="360" x="300" y="-525"></rect>
</svg>
Busy
</button>
<hr class="separator" />
<button class="option" value="offline">
<!-- Offline -->
<svg
class="icon"
aria-hidden="true"
viewBox="0 -960 960 960"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M480-860a380 380 0 1 0 0 760 380 380 0 0 0 0-760Zm0 100a280 280 0 1 1 0 560 280 280 0 0 1 0-560Z"
fill-rule="evenodd"
></path>
</svg>
Appear offline
</button>
</sv-select>
</div>
<style>
.select-content {
display: grid;
gap: 0.5em;
inline-size: min(100%, 16rem);
margin-inline: auto;
color: var(--sv-gray-2);
.field-label {
font-weight: 600;
}
.option {
display: flex;
gap: 0.6em;
align-items: center;
padding: 0.5em 0.75em;
border-radius: var(--sv-bdr-rad-md);
}
.trigger .option {
padding: 0;
font: inherit;
color: inherit;
cursor: inherit;
background: none;
border: none;
}
.icon {
flex: none;
inline-size: 1.25em;
block-size: 1.25em;
fill: currentcolor;
}
.separator {
flex: none;
block-size: var(--sv-bdr-sz-sm);
margin: 0.25em 0;
background-color: var(--sv-gray-5);
border: none;
}
sv-select:state(--opened) .trigger {
border-color: var(--sv-accent);
}
}
</style>

Multiple

Let users pick several options. multiple allows many picks, and aria-selected="true" marks the initial ones.

<div class="select-multiple">
<label class="field-label" for="multiple-toppings-trigger">Toppings</label>
<sv-select multiple>
<button id="multiple-toppings-trigger" type="button" class="trigger" slot="trigger">
<span data-sv-select-empty>No toppings</span>
<span class="label" data-sv-select-filled data-sv-select-label></span>
</button>
<button aria-selected="true" value="cheese">Extra cheese</button>
<button value="mushrooms">Mushrooms</button>
<button aria-selected="true" value="olives">Olives</button>
<button value="peppers">Peppers</button>
<button value="anchovies">Anchovies</button>
</sv-select>
</div>
<style>
.select-multiple {
display: grid;
gap: 0.5em;
inline-size: min(100%, 18rem);
margin-inline: auto;
color: var(--sv-gray-2);
.field-label {
font-weight: 600;
}
.trigger {
min-inline-size: 0;
.label {
min-inline-size: 0;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
}
sv-select:state(--opened) .trigger {
border-color: var(--sv-accent);
}
}
</style>