Files
classroomio/apps/dashboard/src/lib/features/ui/search.svelte
T

76 lines
1.9 KiB
Svelte
Raw Normal View History

<script lang="ts">
import * as Kbd from '@cio/ui/base/kbd';
import * as InputGroup from '@cio/ui/base/input-group';
import SearchIcon from '@lucide/svelte/icons/search';
2026-03-09 11:18:22 +01:00
import type { ComponentProps } from 'svelte';
import { t } from '$lib/utils/functions/translations';
2026-04-27 11:40:38 +01:00
import { searchStore } from '$features/search/store/search-store.svelte';
import type { SearchScope } from '$features/search/utils/types';
2026-03-09 11:18:22 +01:00
interface Props {
placeholder?: string;
value?: string;
class?: string;
onFocus?: (event: FocusEvent) => void;
onBlur?: (event: FocusEvent) => void;
onKeydown?: (event: KeyboardEvent) => void;
2026-04-27 11:40:38 +01:00
scope?: SearchScope;
2026-03-09 11:18:22 +01:00
}
let {
placeholder = '',
value = $bindable(''),
class: className = '',
onFocus,
onBlur,
onKeydown,
2026-04-27 11:40:38 +01:00
scope = 'org',
2026-03-09 11:18:22 +01:00
...restProps
}: Props & Omit<ComponentProps<typeof InputGroup.Root>, 'class' | 'children'> = $props();
2026-04-27 11:40:38 +01:00
function openCommandPalette() {
searchStore.open(scope);
}
function handleFocus(event: FocusEvent) {
onFocus?.(event);
openCommandPalette();
}
function handlePointerdown(event: PointerEvent) {
event.preventDefault();
openCommandPalette();
}
function handleKeydown(event: KeyboardEvent) {
onKeydown?.(event);
if (event.key === 'Tab') {
return;
}
event.preventDefault();
openCommandPalette();
}
</script>
2026-04-27 11:40:38 +01:00
<InputGroup.Root class={`hidden! max-w-56 md:flex! ${className}`} onpointerdown={handlePointerdown} {...restProps}>
2026-03-09 11:18:22 +01:00
<InputGroup.Input
placeholder={placeholder || $t('app.search.placeholder')}
bind:value
2026-04-27 11:40:38 +01:00
readonly
role="button"
aria-haspopup="dialog"
on:focus={handleFocus}
2026-03-09 11:18:22 +01:00
on:blur={onBlur}
2026-04-27 11:40:38 +01:00
on:keydown={handleKeydown}
2026-03-09 11:18:22 +01:00
/>
<InputGroup.Addon>
<SearchIcon />
</InputGroup.Addon>
<InputGroup.Addon align="inline-end">
<Kbd.Root>⌘</Kbd.Root>
<Kbd.Root>K</Kbd.Root>
</InputGroup.Addon>
</InputGroup.Root>