147 lines
3.5 KiB
Svelte
147 lines
3.5 KiB
Svelte
<script lang="ts">
|
|
import { onDestroy } from "svelte";
|
|
import Icon from "./Icon.svelte";
|
|
export type IconName =
|
|
| "settings"
|
|
| "help"
|
|
| "puzzle"
|
|
| "split"
|
|
| "more"
|
|
| "chevron-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";
|
|
|
|
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-dim);
|
|
display: inline-flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
}
|
|
|
|
.icon-btn:hover,
|
|
.icon-btn.active {
|
|
background: var(--rs-bg-active);
|
|
border-color: var(--rs-border);
|
|
color: var(--rs-accent);
|
|
}
|
|
|
|
.tip {
|
|
position: fixed;
|
|
inset: auto;
|
|
margin: 0;
|
|
max-width: min(280px, calc(100vw - 16px));
|
|
white-space: pre-line;
|
|
text-align: left;
|
|
background: var(--rs-bg-active);
|
|
border: 1px solid var(--rs-border);
|
|
border-radius: 6px;
|
|
box-shadow: 0 4px 16px #0005;
|
|
color: var(--rs-text-strong);
|
|
padding: 6px 10px;
|
|
font: 12px/1.5 var(--rs-font-ui);
|
|
pointer-events: none;
|
|
}
|
|
</style>
|