Initial RuneStone project

This commit is contained in:
Knotty Wyvern
2026-09-26 01:25:42 -07:00
commit bdc15e952d
190 changed files with 25986 additions and 0 deletions
+190
View File
@@ -0,0 +1,190 @@
<script lang="ts">
import { onDestroy, 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;
let timer: ReturnType<typeof setTimeout> | undefined;
const expansionDelay = 1500;
const selected = $derived(Math.max(0, options.findIndex(option => option.value === value)));
function scheduleExpansion() {
clearTimeout(timer);
if (!expanded && (hovered || keyboardFocus)) {
timer = setTimeout(() => { expanded = true; }, expansionDelay);
}
}
function collapse() {
clearTimeout(timer);
expanded = false;
}
function leave() {
hovered = false;
if (!keyboardFocus) collapse();
}
async function choose(index: number) {
// Cycling should leave a full pause before the targets start moving.
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 === "Enter" || event.key === " ") clearTimeout(timer);
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();
clearTimeout(timer);
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);
}
}
onDestroy(() => clearTimeout(timer));
</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; clearTimeout(timer); }}
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 var(--rs-border);
border-radius: 8px;
transition: width 180ms ease;
}
.selection {
position: absolute;
inset: 2px auto 2px 2px;
width: var(--segment);
border-radius: 5px;
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: 5px;
background: transparent;
color: var(--rs-text-dim);
font-size: 12px;
transform: translateX(calc(var(--index) * var(--segment)));
transition: transform 180ms ease, opacity 120ms ease;
}
button.current { color: var(--rs-accent); }
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>