mirror of
https://github.com/whoswhip/Sharpbin.git
synced 2026-12-06 02:48:43 +00:00
fix: adjust dropdown max height dynamically and improve search input positioning
This commit is contained in:
@@ -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(() => {
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -70,8 +80,11 @@
|
||||
<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}
|
||||
<div style="position: sticky; top: 0; z-index: 1; background: inherit;">
|
||||
<input
|
||||
type="text"
|
||||
placeholder="Search..."
|
||||
@@ -80,9 +93,9 @@
|
||||
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"
|
||||
|
||||
Reference in New Issue
Block a user