fix: adjust dropdown max height dynamically and improve search input positioning

This commit is contained in:
2026-01-25 04:24:54 -05:00
parent a6b98a97f8
commit 2d01e726a9
@@ -16,6 +16,8 @@
let isOpen = false;
let searchQuery = '';
let dropdownElement: HTMLDivElement;
let dropdownListElement: HTMLDivElement;
let dropdownMaxHeight = '12rem';
$: filteredOptions = searchable
? options.filter(
@@ -39,10 +41,18 @@
function toggleDropdown() {
isOpen = !isOpen;
if (isOpen && searchable) {
if (isOpen) {
setTimeout(() => {
const input = dropdownElement?.querySelector('input[type="text"]') as HTMLInputElement;
input?.focus();
if (dropdownElement && dropdownListElement) {
const rect = dropdownElement.getBoundingClientRect();
const spaceBelow = window.innerHeight - rect.bottom - 8;
const maxCap = 416;
dropdownMaxHeight = Math.max(120, Math.min(spaceBelow, maxCap)) + 'px';
}
if (searchable) {
const input = dropdownElement?.querySelector('input[type="text"]') as HTMLInputElement;
input?.focus();
}
}, 0);
}
}
@@ -56,7 +66,7 @@
<svelte:window on:click={handleClickOutside} />
<div bind:this={dropdownElement} class="relative mb-2 w-full" class:dropdown-sm={variant === 'sm'}>
<div bind:this={dropdownElement} class="relative mb-2 w-full" class:dropdown-sm={variant === 'sm'}>
<button
type="button"
on:click={toggleDropdown}
@@ -70,19 +80,22 @@
<div
class="absolute top-full z-50 mt-1 w-full rounded border border-neutral-700 bg-neutral-800 shadow-lg"
class:dropdown-sm-list={variant === 'sm'}
bind:this={dropdownListElement}
style="max-height: {dropdownMaxHeight}; width: 100%;"
>
{#if searchable}
<input
type="text"
placeholder="Search..."
class="w-full border-b border-neutral-700 bg-neutral-800 p-2 outline-none"
class:dropdown-sm-input={variant === 'sm'}
bind:value={searchQuery}
on:keydown={handleKeyDown}
/>
<div style="position: sticky; top: 0; z-index: 1; background: inherit;">
<input
type="text"
placeholder="Search..."
class="w-full border-b border-neutral-700 bg-neutral-800 p-2 outline-none"
class:dropdown-sm-input={variant === 'sm'}
bind:value={searchQuery}
on:keydown={handleKeyDown}
/>
</div>
{/if}
<div class="max-h-48 overflow-y-auto">
<div style="overflow-y: auto; max-height: calc({dropdownMaxHeight} - 3rem);">
{#each filteredOptions as option (option.value)}
<button
type="button"