Skip to content

Commit

Permalink
✨ 단축키로 검색 인풋 포커스되도록 설정
Browse files Browse the repository at this point in the history
  • Loading branch information
이재민 committed Jan 27, 2024
1 parent 72cc179 commit 6f72e72
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/lib/components/ui/command/command-input.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<script lang="ts">
import { Command as CommandPrimitive } from 'cmdk-sv';
import { Search } from 'lucide-svelte';
import { cn } from '$lib/utils';
type $$Props = CommandPrimitive.InputProps;
Expand All @@ -20,4 +21,5 @@
{...$$restProps}
bind:value
/>
<slot name="suffix" />
</div>
1 change: 1 addition & 0 deletions src/lib/kbd.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export const kbd = {
BACKSPACE: 'Backspace',
SLASH: '/',
K: 'k',
S: 's',
E: 'e'
Expand Down
17 changes: 16 additions & 1 deletion src/lib/tabs/tabs-cmdk.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import SubCommand from './sub-command.svelte';
import TabList from './tab-list.svelte';
import { chromeApi, tabKeys } from './utils.tabs';
import { onMount } from 'svelte';
const syncTabIds = createQuery({
queryKey: tabKeys.sync(),
Expand Down Expand Up @@ -51,7 +52,11 @@
}, 100);
};
let cmdkInputEl: HTMLInputElement | null = null;
const handleKeydown = (e: KeyboardEvent) => {
if (e.key === kbd.SLASH && (e.metaKey || e.ctrlKey)) {
cmdkInputEl?.focus();
}
const currentTarget = e.currentTarget;
if (!isHTMLElement(currentTarget)) return;
Expand All @@ -64,6 +69,10 @@
bounce(currentTarget);
}
};
onMount(() => {
cmdkInputEl = document.querySelector('[data-cmdk-input]') as HTMLInputElement;
});
</script>

<Command.Root
Expand All @@ -77,11 +86,17 @@
>
<SelectedTabs />
<Command.Input
data-cmdk-input
autofocus
class="py-1"
placeholder={getLocalMessage('searchPlaceholder')}
bind:value={inputValue}
/>
>
<div slot="suffix" class="ml-2 flex cursor-default items-center gap-1">
<Command.Shortcut class="size-5">⌘</Command.Shortcut>
<Command.Shortcut class="size-5">/</Command.Shortcut>
</div>
</Command.Input>
<TabList {isSyncing} />
<SubCommand {isSyncing} {resetInputValue} />
</Command.Root>

0 comments on commit 6f72e72

Please sign in to comment.