Files
bytesize/src/lib/HotkeyInput.svelte
T

144 lines
3.5 KiB
Svelte

<script lang="ts">
let {
value = $bindable(),
placeholder = "press a key...",
}: {
value: string | null
placeholder?: string
} = $props()
let capturing = $state(false)
let btn: HTMLButtonElement | null = $state(null)
const MODIFIERS = new Set(["Control","Shift","Alt","Meta",
"ControlLeft","ControlRight","ShiftLeft","ShiftRight",
"AltLeft","AltRight","MetaLeft","MetaRight"])
function start() {
capturing = true
btn?.focus()
}
function keydown(e: KeyboardEvent) {
if (!capturing) return
e.preventDefault()
if (e.key === "Escape") {
capturing = false
btn?.blur()
return
}
if (MODIFIERS.has(e.key)) return
const parts: string[] = []
if (e.ctrlKey) parts.push("Ctrl")
if (e.altKey) parts.push("Alt")
if (e.shiftKey) parts.push("Shift")
if (e.metaKey) parts.push("Meta")
let key = e.key
if (key === " ") key = "Space"
else if (key.length === 1) key = key.toUpperCase()
parts.push(key)
value = parts.join("+")
capturing = false
btn?.blur()
}
function clear(e: MouseEvent) {
e.stopPropagation()
value = null
capturing = false
}
</script>
<div class="row">
<button
bind:this={btn}
class="key"
class:capturing
onclick={start}
onkeydown={keydown}
onblur={() => { capturing = false }}
type="button"
aria-label={capturing ? "recording shortcut - press a key combo" : "click to assign shortcut"}
>
{#if capturing}
<span class="blink">type a combo</span>
{:else if value}
<span class="val">{value}</span>
{:else}
<span class="ph">{placeholder}</span>
{/if}
</button>
{#if value && !capturing}
<button class="x" onclick={clear} type="button" aria-label="clear shortcut">
<svg width="10" height="10" viewBox="0 0 10 10" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round">
<line x1="2" y1="2" x2="8" y2="8"/>
<line x1="8" y1="2" x2="2" y2="8"/>
</svg>
</button>
{/if}
</div>
<style>
.row {
display: flex;
gap: 4px;
align-items: stretch;
}
.key {
flex: 1;
height: 34px;
padding: 0 12px;
background: var(--bg-3);
border: 1px solid var(--border-2);
color: var(--text);
border-radius: var(--r-sm);
cursor: pointer;
font-family: var(--mono);
font-size: 12px;
font-weight: 500;
letter-spacing: 0.02em;
transition: border-color 80ms, background 80ms;
text-align: left;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.key:hover { border-color: var(--border-3); background: var(--bg-4); }
.key:focus-visible { box-shadow: 0 0 0 1px var(--accent), 0 0 0 3px var(--accent-glow); outline: none; }
.key.capturing {
border-color: var(--accent);
background: var(--accent-dim);
color: var(--accent);
}
.val { color: var(--text); }
.ph { color: var(--text-3); font-style: italic; font-family: var(--sans); font-weight: 400; }
.blink { animation: blink 1s step-end infinite; color: var(--accent); }
@keyframes blink {
0%, 100% { opacity: 1; }
50% { opacity: 0.3; }
}
.x {
display: grid;
place-items: center;
width: 34px;
height: 34px;
background: transparent;
border: 1px solid var(--border);
color: var(--text-3);
border-radius: var(--r-sm);
cursor: pointer;
flex-shrink: 0;
transition: color 80ms, border-color 80ms;
}
.x:hover { color: var(--err); border-color: var(--err-border); }
</style>