2026-01-16 17:47:05 +03:00
|
|
|
<!--
|
|
|
|
|
Component: SearchBar
|
|
|
|
|
|
|
|
|
|
Search input with popover dropdown for results/suggestions
|
|
|
|
|
- Features keyboard navigation (ArrowDown/Up/Enter) and auto-focus prevention on popover open.
|
|
|
|
|
- The input field serves as the popover trigger.
|
|
|
|
|
-->
|
2026-01-09 16:19:22 +03:00
|
|
|
<script lang="ts">
|
|
|
|
|
import { Input } from '$shared/shadcn/ui/input';
|
|
|
|
|
import { Label } from '$shared/shadcn/ui/label';
|
|
|
|
|
import {
|
|
|
|
|
Content as PopoverContent,
|
|
|
|
|
Root as PopoverRoot,
|
|
|
|
|
Trigger as PopoverTrigger,
|
|
|
|
|
} from '$shared/shadcn/ui/popover';
|
|
|
|
|
import { useId } from 'bits-ui';
|
2026-01-16 17:47:05 +03:00
|
|
|
import type { Snippet } from 'svelte';
|
2026-01-09 16:19:22 +03:00
|
|
|
|
|
|
|
|
interface Props {
|
2026-01-16 17:47:05 +03:00
|
|
|
/** Unique identifier for the input element */
|
2026-01-09 16:19:22 +03:00
|
|
|
id: string;
|
2026-01-16 17:47:05 +03:00
|
|
|
/** Current search value (bindable) */
|
2026-01-09 16:19:22 +03:00
|
|
|
value: string;
|
2026-01-16 17:47:05 +03:00
|
|
|
/** Additional CSS classes for the container */
|
2026-01-09 16:19:22 +03:00
|
|
|
class?: string;
|
2026-01-16 17:47:05 +03:00
|
|
|
/** Placeholder text for the input */
|
2026-01-09 16:19:22 +03:00
|
|
|
placeholder?: string;
|
2026-01-16 17:47:05 +03:00
|
|
|
/** Optional label displayed above the input */
|
2026-01-09 16:19:22 +03:00
|
|
|
label?: string;
|
2026-01-16 17:47:05 +03:00
|
|
|
/** Content to render inside the popover (receives unique content ID) */
|
2026-01-09 16:19:22 +03:00
|
|
|
children: Snippet<[{ id: string }]> | undefined;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let {
|
|
|
|
|
id = 'search-bar',
|
|
|
|
|
value = $bindable(),
|
|
|
|
|
class: className,
|
|
|
|
|
placeholder,
|
|
|
|
|
label,
|
|
|
|
|
children,
|
|
|
|
|
}: Props = $props();
|
|
|
|
|
|
|
|
|
|
let open = $state(false);
|
2026-01-13 20:09:30 +03:00
|
|
|
let triggerRef = $state<HTMLInputElement>(null!);
|
2026-01-14 15:27:41 +03:00
|
|
|
// svelte-ignore state_referenced_locally
|
2026-01-09 16:19:22 +03:00
|
|
|
const contentId = useId(id);
|
|
|
|
|
|
|
|
|
|
function handleKeyDown(event: KeyboardEvent) {
|
|
|
|
|
if (event.key === 'ArrowDown' || event.key === 'ArrowUp' || event.key === 'Enter') {
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function handleInputClick() {
|
|
|
|
|
open = true;
|
|
|
|
|
}
|
|
|
|
|
</script>
|
|
|
|
|
|
2026-01-16 17:47:05 +03:00
|
|
|
<PopoverRoot bind:open>
|
2026-01-09 16:19:22 +03:00
|
|
|
<PopoverTrigger bind:ref={triggerRef}>
|
|
|
|
|
{#snippet child({ props })}
|
2026-01-16 17:47:05 +03:00
|
|
|
{@const { onclick, ...rest } = props}
|
|
|
|
|
<div {...rest} class="flex flex-row flex-1 w-full">
|
2026-01-09 16:19:22 +03:00
|
|
|
{#if label}
|
|
|
|
|
<Label for={id}>{label}</Label>
|
|
|
|
|
{/if}
|
|
|
|
|
<Input
|
|
|
|
|
id={id}
|
|
|
|
|
placeholder={placeholder}
|
|
|
|
|
bind:value={value}
|
|
|
|
|
onkeydown={handleKeyDown}
|
2026-01-16 17:47:05 +03:00
|
|
|
onclick={handleInputClick}
|
2026-01-09 16:19:22 +03:00
|
|
|
class="flex flex-row flex-1"
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
{/snippet}
|
|
|
|
|
</PopoverTrigger>
|
|
|
|
|
|
|
|
|
|
<PopoverContent
|
|
|
|
|
onOpenAutoFocus={e => e.preventDefault()}
|
2026-01-16 17:47:05 +03:00
|
|
|
onInteractOutside={(e => {
|
|
|
|
|
if (e.target === triggerRef) {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
}
|
|
|
|
|
})}
|
|
|
|
|
class="w-(--bits-popover-anchor-width) min-w-(--bits-popover-anchor-width)"
|
2026-01-09 16:19:22 +03:00
|
|
|
>
|
|
|
|
|
{@render children?.({ id: contentId })}
|
|
|
|
|
</PopoverContent>
|
|
|
|
|
</PopoverRoot>
|