Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix for the ComboBox positioning issue #48

Merged
merged 1 commit into from
Jun 4, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions src/lib/ComboBox/ComboBox.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,34 @@

let inputFocused = false;
let itemHeight = 36;
const maxItems = 14; // 504 (`max-block-size` in ComboBox.scss) / 36 (itemHeight)
let menuOffset =
itemHeight * -(selection ? items.indexOf(selection) : Math.floor(items.length / 2));
itemHeight * -(selection ? items.indexOf(selection) : Math.floor(items.length > maxItems ? (maxItems / 2) : items.length / 2));

onMount(() => {
if (!searchValue) searchValue = value;
});

function updateOffset(target: HTMLElement) {
menuOffset = -(
target.offsetTop - parseInt(getComputedStyle(target).getPropertyValue("margin-top"))
);
requestAnimationFrame(() => {
// this literally moves the menu so that the `containerElement` matches `target`
let { top: containerTop } = containerElement.getBoundingClientRect();
let { top: targetTop } = target.getBoundingClientRect();

menuOffset += containerTop - targetTop;

/* This will also make sure that the `menuElement` can't go beyond the top of the page
requestAnimationFrame(() => {
let { top } = menuElement.getBoundingClientRect()

if (top < 0) {
menuOffset += -top
}

// Not sure how to do this for bottom
})
*/
})
}

function selectItem(item: Item) {
Expand Down