180 lines
5.5 KiB
Svelte
180 lines
5.5 KiB
Svelte
<script lang="ts">
|
|
import { tick } from "svelte";
|
|
import Icon from "./Icon.svelte";
|
|
import type { IconName } from "./IconButton.svelte";
|
|
|
|
type Option = { value: string; label: string; icon: IconName };
|
|
let { label, options, value, onchange, shortcut = "" }: {
|
|
label: string;
|
|
options: Option[];
|
|
value: string;
|
|
onchange: (value: string) => void;
|
|
shortcut?: string;
|
|
} = $props();
|
|
|
|
let expanded = $state(false);
|
|
let tooltipDismissed = $state(false);
|
|
let hovered = false;
|
|
let keyboardFocus = false;
|
|
let group: HTMLDivElement;
|
|
const selected = $derived(Math.max(0, options.findIndex(option => option.value === value)));
|
|
|
|
function scheduleExpansion() {
|
|
if (hovered || keyboardFocus) expanded = true;
|
|
}
|
|
|
|
function collapse() {
|
|
expanded = false;
|
|
}
|
|
|
|
function leave() {
|
|
hovered = false;
|
|
if (!keyboardFocus) collapse();
|
|
}
|
|
|
|
async function choose(index: number) {
|
|
scheduleExpansion();
|
|
const hadFocus = group.contains(document.activeElement);
|
|
onchange(options[index].value);
|
|
await tick();
|
|
// A compact click changes which button is visible; carry focus with it.
|
|
if (hadFocus) group.querySelector<HTMLButtonElement>(`[data-value="${options[index].value}"]`)?.focus({ preventScroll: true });
|
|
}
|
|
|
|
function keydown(event: KeyboardEvent) {
|
|
keyboardFocus = true;
|
|
if (event.key === "Escape") {
|
|
event.preventDefault();
|
|
event.stopPropagation();
|
|
tooltipDismissed = true;
|
|
collapse();
|
|
group.querySelector<HTMLButtonElement>(`[data-value="${value}"]`)?.focus();
|
|
} else if (["ArrowLeft", "ArrowRight", "Home", "End"].includes(event.key)) {
|
|
event.preventDefault();
|
|
expanded = true;
|
|
const index = event.key === "Home" ? 0 : event.key === "End" ? options.length - 1
|
|
: (selected + (event.key === "ArrowRight" ? 1 : -1) + options.length) % options.length;
|
|
void choose(index);
|
|
}
|
|
}
|
|
|
|
</script>
|
|
|
|
<div
|
|
bind:this={group}
|
|
class="mode-switcher"
|
|
class:expanded
|
|
class:tooltip-dismissed={tooltipDismissed}
|
|
role="group"
|
|
aria-label={label}
|
|
style:--count={expanded ? options.length : 1}
|
|
style:--selected={expanded ? selected : 0}
|
|
onmouseenter={() => { hovered = true; scheduleExpansion(); }}
|
|
onmouseleave={leave}
|
|
onfocusin={(event) => {
|
|
if ((event.target as HTMLElement).matches(":focus-visible") && !group.contains(event.relatedTarget as Node)) {
|
|
keyboardFocus = true;
|
|
scheduleExpansion();
|
|
}
|
|
}}
|
|
onfocusout={(event) => {
|
|
if (!group.contains(event.relatedTarget as Node)) {
|
|
keyboardFocus = false;
|
|
if (!hovered) collapse();
|
|
}
|
|
}}
|
|
>
|
|
<span class="selection" aria-hidden="true"></span>
|
|
{#each options as option, index (option.value)}
|
|
<button
|
|
type="button"
|
|
class:current={option.value === value}
|
|
class:concealed={!expanded && option.value !== value}
|
|
style:--index={expanded ? index : 0}
|
|
data-value={option.value}
|
|
aria-label={option.label}
|
|
aria-pressed={option.value === value}
|
|
tabindex={expanded || option.value === value ? 0 : -1}
|
|
onmouseenter={() => { tooltipDismissed = false; }}
|
|
onfocus={() => { tooltipDismissed = false; }}
|
|
onpointerdown={() => { keyboardFocus = false; }}
|
|
onpointerup={scheduleExpansion}
|
|
onpointercancel={scheduleExpansion}
|
|
onkeydown={keydown}
|
|
onkeyup={(event) => { if (event.key === "Enter" || event.key === " ") scheduleExpansion(); }}
|
|
onclick={() => void choose(expanded ? index : (selected + 1) % options.length)}
|
|
>
|
|
<Icon icon={option.icon} />
|
|
<span class="tooltip" role="tooltip" aria-hidden="true">{option.label}{shortcut}</span>
|
|
</button>
|
|
{/each}
|
|
</div>
|
|
|
|
<style>
|
|
.mode-switcher {
|
|
--segment: 32px;
|
|
position: relative;
|
|
flex: 0 0 auto;
|
|
width: calc(var(--segment) * var(--count) + 6px);
|
|
height: 34px;
|
|
border: 1px solid transparent;
|
|
border-radius: var(--rs-control-radius);
|
|
}
|
|
.selection {
|
|
position: absolute;
|
|
inset: 2px auto 2px 2px;
|
|
width: var(--segment);
|
|
border-radius: var(--rs-control-radius);
|
|
background: var(--rs-bg-active);
|
|
transform: translateX(calc(var(--selected) * var(--segment)));
|
|
transition: transform 180ms ease;
|
|
pointer-events: none;
|
|
}
|
|
button {
|
|
position: absolute;
|
|
top: 2px;
|
|
left: 2px;
|
|
width: var(--segment);
|
|
height: 28px;
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
padding: 0;
|
|
border: 0;
|
|
border-radius: var(--rs-control-radius);
|
|
background: transparent;
|
|
color: var(--rs-text-dim);
|
|
font-size: 12px;
|
|
transform: translateX(calc(var(--index) * var(--segment)));
|
|
transition: opacity 120ms ease;
|
|
}
|
|
button.current { color: var(--rs-text-strong); }
|
|
button:hover { color: var(--rs-text-strong); }
|
|
button.concealed { visibility: hidden; opacity: 0; pointer-events: none; }
|
|
button:hover, button:focus-visible { z-index: 1; }
|
|
.tooltip {
|
|
position: absolute;
|
|
top: calc(100% + 9px);
|
|
right: 0;
|
|
padding: 6px 10px;
|
|
border: 1px solid var(--rs-border);
|
|
border-radius: 6px;
|
|
background: var(--rs-bg-active);
|
|
box-shadow: 0 4px 16px #0005;
|
|
color: var(--rs-text-strong);
|
|
font: 12px/1.5 var(--rs-font-ui);
|
|
white-space: nowrap;
|
|
pointer-events: none;
|
|
visibility: hidden;
|
|
opacity: 0;
|
|
}
|
|
.mode-switcher:not(.tooltip-dismissed) button:not(.concealed):is(:hover, :focus-visible) .tooltip {
|
|
visibility: visible;
|
|
opacity: 1;
|
|
transition: opacity 120ms ease 200ms, visibility 0s linear 200ms;
|
|
}
|
|
@media (prefers-reduced-motion: reduce) {
|
|
.mode-switcher, .selection, button { transition: none; }
|
|
}
|
|
</style>
|