Add segmented control component
This commit is contained in:
1 parent
6f75657a74
commit
adb3ff170c
1 file changed
+36
@@ -0,0 +1,36 @@
|
||||
<script lang="ts" generics="T extends string | number">
|
||||
import type { Component } from 'svelte';
|
||||
|
||||
let {
|
||||
value = $bindable(),
|
||||
options,
|
||||
label,
|
||||
}: { value: T; options: { value: T; label: string; icon?: Component<any> }[]; label: string } = $props();
|
||||
</script>
|
||||
|
||||
<!-- A segmented control: radio buttons underneath, so arrow keys and screen readers work as usual. -->
|
||||
<div class="segmented" role="radiogroup" aria-label={label}>
|
||||
{#each options as o (o.value)}
|
||||
<label class:on={value === o.value}>
|
||||
<input type="radio" class="visually-hidden" bind:group={value} value={o.value} />
|
||||
{#if o.icon}<o.icon size={16} aria-hidden="true" />{/if}
|
||||
<span>{o.label}</span>
|
||||
</label>
|
||||
{/each}
|
||||
</div>
|
||||
|
||||
<style>
|
||||
.segmented {
|
||||
display: inline-flex; padding: 3px; gap: 2px; border-radius: var(--r-md);
|
||||
background: var(--surface-2); border: 1px solid var(--border);
|
||||
}
|
||||
label {
|
||||
display: inline-flex; align-items: center; justify-content: center; gap: 6px;
|
||||
min-height: 40px; padding: 0 var(--s3); border-radius: 9px; cursor: pointer;
|
||||
font-size: var(--fs-sm); font-weight: 600; color: var(--text-2);
|
||||
transition: background-color var(--dur) var(--ease), color var(--dur) var(--ease);
|
||||
}
|
||||
label:hover { color: var(--text); }
|
||||
label.on { background: var(--surface); color: var(--text); box-shadow: var(--shadow-1); }
|
||||
label:has(:focus-visible) { outline: 3px solid var(--primary); outline-offset: 1px; }
|
||||
</style>
|
||||
Reference in new issue
Block a user