current state before improvement pass
This commit is contained in:
+74
-56
@@ -1,36 +1,34 @@
|
||||
<script lang="ts">
|
||||
let {
|
||||
value = $bindable(),
|
||||
placeholder = "click to assign",
|
||||
placeholder = "press a key...",
|
||||
}: {
|
||||
value: string | null
|
||||
placeholder?: string
|
||||
} = $props()
|
||||
|
||||
let capturing: boolean = $state(false)
|
||||
let buttonEl: HTMLButtonElement | null = $state(null)
|
||||
let capturing = $state(false)
|
||||
let btn: HTMLButtonElement | null = $state(null)
|
||||
|
||||
const MODIFIER_KEYS = new Set(["Control", "Shift", "Alt", "Meta", "ControlLeft", "ControlRight"])
|
||||
const MODIFIERS = new Set(["Control","Shift","Alt","Meta",
|
||||
"ControlLeft","ControlRight","ShiftLeft","ShiftRight",
|
||||
"AltLeft","AltRight","MetaLeft","MetaRight"])
|
||||
|
||||
function startCapture() {
|
||||
function start() {
|
||||
capturing = true
|
||||
buttonEl?.focus()
|
||||
btn?.focus()
|
||||
}
|
||||
|
||||
function handleKeydown(e: KeyboardEvent) {
|
||||
function keydown(e: KeyboardEvent) {
|
||||
if (!capturing) return
|
||||
e.preventDefault()
|
||||
e.stopPropagation()
|
||||
|
||||
if (e.key === "Escape") {
|
||||
capturing = false
|
||||
buttonEl?.blur()
|
||||
return
|
||||
}
|
||||
|
||||
if (MODIFIER_KEYS.has(e.key)) {
|
||||
btn?.blur()
|
||||
return
|
||||
}
|
||||
if (MODIFIERS.has(e.key)) return
|
||||
|
||||
const parts: string[] = []
|
||||
if (e.ctrlKey) parts.push("Ctrl")
|
||||
@@ -45,11 +43,7 @@
|
||||
parts.push(key)
|
||||
value = parts.join("+")
|
||||
capturing = false
|
||||
buttonEl?.blur()
|
||||
}
|
||||
|
||||
function handleBlur() {
|
||||
capturing = false
|
||||
btn?.blur()
|
||||
}
|
||||
|
||||
function clear(e: MouseEvent) {
|
||||
@@ -61,65 +55,89 @@
|
||||
|
||||
<div class="row">
|
||||
<button
|
||||
bind:this={buttonEl}
|
||||
bind:this={btn}
|
||||
class="key"
|
||||
class:capturing
|
||||
onclick={startCapture}
|
||||
onkeydown={handleKeydown}
|
||||
onblur={handleBlur}
|
||||
onclick={start}
|
||||
onkeydown={keydown}
|
||||
onblur={() => { capturing = false }}
|
||||
type="button"
|
||||
aria-label={capturing ? "recording shortcut - press a key combo" : "click to assign shortcut"}
|
||||
>
|
||||
{#if capturing}
|
||||
press a combo (Esc to cancel)
|
||||
<span class="blink">type a combo</span>
|
||||
{:else if value}
|
||||
{value}
|
||||
<span class="val">{value}</span>
|
||||
{:else}
|
||||
{placeholder}
|
||||
<span class="ph">{placeholder}</span>
|
||||
{/if}
|
||||
</button>
|
||||
{#if value && !capturing}
|
||||
<button class="clear" onclick={clear} type="button" title="clear">×</button>
|
||||
<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: 0.4rem;
|
||||
gap: 4px;
|
||||
align-items: stretch;
|
||||
}
|
||||
button {
|
||||
font: inherit;
|
||||
padding: 0.45rem 0.7rem;
|
||||
border: 1px solid #444;
|
||||
background: #222;
|
||||
color: inherit;
|
||||
border-radius: 4px;
|
||||
cursor: pointer;
|
||||
text-align: left;
|
||||
|
||||
.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;
|
||||
}
|
||||
button:hover {
|
||||
border-color: #888;
|
||||
|
||||
.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);
|
||||
}
|
||||
button.capturing {
|
||||
border-color: #4a8;
|
||||
background: #1a2a24;
|
||||
color: #4a8;
|
||||
outline: none;
|
||||
|
||||
.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; }
|
||||
}
|
||||
button:focus {
|
||||
outline: none;
|
||||
border-color: #4a8;
|
||||
}
|
||||
button.clear {
|
||||
flex: 0 0 auto;
|
||||
padding: 0.45rem 0.7rem;
|
||||
color: #888;
|
||||
|
||||
.x {
|
||||
display: grid;
|
||||
place-items: center;
|
||||
width: 34px;
|
||||
height: 34px;
|
||||
background: transparent;
|
||||
border-color: #2a2a2a;
|
||||
}
|
||||
button.clear:hover {
|
||||
color: #ec9a9a;
|
||||
border-color: #ec9a9a;
|
||||
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>
|
||||
|
||||
Reference in New Issue
Block a user