Files
RuneStone/src/lib/ui/IconButton.svelte
T
Knotty Wyvern 9fc1490988 Make menus and hover fills translucent over a background image
With a background image, menus use the surface transparency of Appearance
(82% by default) instead of at least 92%, and they keep the blur.
Hovered rows and controls in the work area, hovered menu rows, and the
workspace button use the new --rs-hover-bg token. With an image, it is a
6% veil of the strong text color. The image stays visible, and the hover
shows over light and dark image areas. Without an image, nothing changes.

Silver Serpent dim text is now #c2c6cc. It keeps 4.5:1 contrast on a
hovered row over a white image.

UI-061 replaces the image rule of UI-043. Card 05b9bfa1.
2026-10-30 13:38:47 -07:00

178 lines
4.3 KiB
Svelte

<script lang="ts">
import { onDestroy } from "svelte";
import Icon from "./Icon.svelte";
export type IconName =
| "globe"
| "check"
| "alert"
| "palette"
| "undo"
| "git"
| "arrow-left"
| "arrow-right"
| "copy"
| "duplicate"
| "stop"
| "settings"
| "sun"
| "help"
| "puzzle"
| "link"
| "kanban"
| "split"
| "more"
| "chevron-down"
| "chevrons-up-down"
| "up"
| "down"
| "maximize"
| "minimize"
| "window-maximize"
| "window-restore"
| "restore"
| "refresh"
| "text"
| "folder-open"
| "file-plus"
| "folder-plus"
| "save"
| "pen"
| "code"
| "terminal"
| "eye"
| "panel-left"
| "panel-right"
| "spark"
| "list"
| "search"
| "bookmark"
| "files"
| "sort"
| "fold"
| "unfold"
| "book-open"
| "close"
| "trash"
| "plus"
| "download"
| "upload"
| "minus"
| "toggle"
| "zoom-in"
| "zoom-out"
| "fit-width"
| "rotate";
type Props = {
icon: IconName;
label: string;
active?: boolean;
disabled?: boolean;
onclick?: () => void;
side?: "bottom" | "right";
type?: "button" | "submit";
};
let { icon, label, active, disabled = false, onclick, side = "bottom", type = "button" }: Props = $props();
const tooltipId = $props.id();
let button: HTMLButtonElement;
let tooltip: HTMLSpanElement;
let showTip = $state(false);
let timer: ReturnType<typeof setTimeout> | undefined;
function onLeave() {
clearTimeout(timer);
showTip = false;
if (tooltip?.matches(":popover-open")) tooltip.hidePopover();
}
function onEnter() {
clearTimeout(timer);
timer = setTimeout(() => {
if (!button?.isConnected || disabled) return;
const bounds = button.getBoundingClientRect();
tooltip.showPopover();
const tip = tooltip.getBoundingClientRect();
let left = side === "right" ? bounds.right + 8 : bounds.left + (bounds.width - tip.width) / 2;
let top = side === "right" ? bounds.top + (bounds.height - tip.height) / 2 : bounds.bottom + 8;
if (top + tip.height > innerHeight - 8) top = bounds.top - tip.height - 8;
left = Math.max(8, Math.min(left, innerWidth - tip.width - 8));
tooltip.style.left = `${left}px`;
tooltip.style.top = `${Math.max(8, top)}px`;
showTip = true;
}, 300);
}
onDestroy(() => clearTimeout(timer));
</script>
<svelte:window onresize={onLeave} onkeydown={(event) => { if (event.key === "Escape") onLeave(); }} />
<button
bind:this={button}
class="icon-btn"
class:active
{type}
{disabled}
aria-label={label}
aria-pressed={active}
aria-describedby={showTip ? tooltipId : undefined}
onclick={() => { onLeave(); onclick?.(); }}
onmouseenter={onEnter}
onmouseleave={onLeave}
onfocus={onEnter}
onblur={onLeave}
>
<Icon {icon} />
</button>
<span bind:this={tooltip} id={tooltipId} class="tip" popover="manual" role="tooltip">{label}</span>
<style>
.icon-btn {
position: relative;
width: 28px;
flex: 0 0 28px;
border-radius: var(--rs-control-radius);
height: 28px;
padding: 0;
border: 1px solid transparent;
background: transparent;
color: var(--rs-text);
display: inline-flex;
align-items: center;
justify-content: center;
}
/* Hover gets a neutral fill. An active toggle also gets the accent color and an accent bar. A disabled button is dim. */
.icon-btn:not(:disabled):hover {
background: var(--rs-hover-bg);
border-color: var(--rs-border);
color: var(--rs-text-strong);
}
.icon-btn.active {
background: var(--rs-item-selection-bg);
color: var(--rs-accent);
box-shadow: inset 0 -2px 0 var(--rs-accent);
}
.icon-btn:disabled { color: var(--rs-text-dim); }
.tip {
position: fixed;
inset: auto;
margin: 0;
max-width: min(280px, calc(100vw - 16px));
white-space: pre-line;
text-align: left;
background: var(--rs-tooltip-bg);
border: 1px solid var(--rs-border);
border-radius: var(--rs-control-radius);
box-shadow: var(--rs-tooltip-shadow);
color: var(--rs-tooltip-text);
padding: var(--rs-tooltip-padding);
font: var(--rs-tooltip-font-size)/1.5 var(--rs-font-ui);
pointer-events: none;
}
</style>