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 isOpen = false;
let searchQuery = ''; let searchQuery = '';
let dropdownElement: HTMLDivElement; let dropdownElement: HTMLDivElement;
let dropdownListElement: HTMLDivElement;
let dropdownMaxHeight = '12rem';
$: filteredOptions = searchable $: filteredOptions = searchable
? options.filter( ? options.filter(
@@ -39,10 +41,18 @@
function toggleDropdown() { function toggleDropdown() {
isOpen = !isOpen; isOpen = !isOpen;
if (isOpen && searchable) { if (isOpen) {
setTimeout(() => { setTimeout(() => {
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; const input = dropdownElement?.querySelector('input[type="text"]') as HTMLInputElement;
input?.focus(); input?.focus();
}
}, 0); }, 0);
} }
} }
@@ -70,8 +80,11 @@
<div <div
class="absolute top-full z-50 mt-1 w-full rounded border border-neutral-700 bg-neutral-800 shadow-lg" 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'} class:dropdown-sm-list={variant === 'sm'}
bind:this={dropdownListElement}
style="max-height: {dropdownMaxHeight}; width: 100%;"
> >
{#if searchable} {#if searchable}
<div style="position: sticky; top: 0; z-index: 1; background: inherit;">
<input <input
type="text" type="text"
placeholder="Search..." placeholder="Search..."
@@ -80,9 +93,9 @@
bind:value={searchQuery} bind:value={searchQuery}
on:keydown={handleKeyDown} on:keydown={handleKeyDown}
/> />
</div>
{/if} {/if}
<div style="overflow-y: auto; max-height: calc({dropdownMaxHeight} - 3rem);">
<div class="max-h-48 overflow-y-auto">
{#each filteredOptions as option (option.value)} {#each filteredOptions as option (option.value)}
<button <button
type="button" type="button"