This commit is contained in:
Philipp Dormann 2025-05-20 00:44:20 +02:00
parent 564a971c63
commit 0cb1193269
Signed by: philipp
GPG Key ID: 3BB9ADD52DCA4314

View File

@ -1,5 +1,5 @@
<script> <script>
import { createEventDispatcher, onMount } from "svelte"; import { createEventDispatcher, onMount, tick } from "svelte";
// Props // Props
export let options = []; export let options = [];
@ -14,7 +14,7 @@
let visibleItems = []; let visibleItems = [];
let startIndex = 0; let startIndex = 0;
let itemHeight = 40; // Fixed height for each option (in pixels) let itemHeight = 40; // Fixed height for each option (in pixels)
let visibleCount = 10; // Number of items to render (adjusted based on container height) let visibleCount = 10; // Default number of items to render
let focusedIndex = -1; // Track the focused option index (-1 means no focus) let focusedIndex = -1; // Track the focused option index (-1 means no focus)
const dispatch = createEventDispatcher(); const dispatch = createEventDispatcher();
@ -50,8 +50,9 @@
} }
// Calculate visible item count based on container height // Calculate visible item count based on container height
function updateVisibleCount() { async function updateVisibleCount() {
if (container) { if (container) {
await tick(); // Wait for DOM to render
visibleCount = Math.ceil(container.clientHeight / itemHeight) + 2; // Buffer of 2 items visibleCount = Math.ceil(container.clientHeight / itemHeight) + 2; // Buffer of 2 items
updateVisibleItems(); updateVisibleItems();
} }
@ -67,12 +68,11 @@
} }
// Toggle dropdown // Toggle dropdown
function toggleDropdown() { async function toggleDropdown() {
isOpen = !isOpen; isOpen = !isOpen;
if (isOpen) { if (isOpen) {
// Update visible count when dropdown opens // Update visible count when dropdown opens
setTimeout(updateVisibleCount, 0); // Ensure container is rendered await updateVisibleCount(); // Ensure container is rendered
focusedIndex = -1; // Reset focus when opening
} }
} }
@ -85,8 +85,9 @@
} }
// Handle input focus to open dropdown // Handle input focus to open dropdown
function handleInputFocus() { async function handleInputFocus() {
isOpen = true; isOpen = true;
await updateVisibleCount(); // Ensure items render on focus
} }
// Handle keyboard navigation // Handle keyboard navigation