Skip to content

Commit f3aead0

Browse files
author
Tom Adam
committed
[pstanoev#177] fixed scroll position update - needs to happen in beforeUpdate
1 parent b9ab15c commit f3aead0

File tree

1 file changed

+25
-13
lines changed

1 file changed

+25
-13
lines changed

src/SimpleAutocomplete.svelte

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<script>
22
import { flip } from "svelte/animate"
33
import { fade } from "svelte/transition"
4+
import {afterUpdate} from 'svelte'
45
56
// the list of items the user can select from
67
export let items = []
@@ -203,6 +204,17 @@
203204
// other state
204205
let inputDelayTimeout
205206
207+
let setPositionOnNextUpdate = false;
208+
209+
// --- Lifecycle events ---
210+
211+
afterUpdate(() => {
212+
if(setPositionOnNextUpdate) {
213+
setScrollAwareListPosition()
214+
}
215+
setPositionOnNextUpdate = false
216+
})
217+
206218
// --- Functions ---
207219
208220
function safeFunction(theFunction, argument) {
@@ -912,23 +924,11 @@
912924
return
913925
}
914926
915-
setScrollAwareListPosition()
927+
setPositionOnNextUpdate = true
916928
917929
opened = true
918930
}
919931
920-
function setScrollAwareListPosition() {
921-
const { height: viewPortHeight } = window.visualViewport
922-
const { bottom: inputButtom, height: inputHeight } = input.getBoundingClientRect()
923-
const { height: listHeight } = list.getBoundingClientRect()
924-
925-
if (inputButtom + listHeight > viewPortHeight) {
926-
list.style.top = `-${inputHeight + listHeight}px`
927-
} else {
928-
list.style.top = "0px"
929-
}
930-
}
931-
932932
function close() {
933933
if (debug) {
934934
console.log("close")
@@ -1090,6 +1090,18 @@
10901090
}
10911091
selectedItem = newSelection
10921092
}
1093+
1094+
function setScrollAwareListPosition() {
1095+
const { height: viewPortHeight } = window.visualViewport
1096+
const { bottom: inputButtom, height: inputHeight } = input.getBoundingClientRect()
1097+
const { height: listHeight } = list.getBoundingClientRect()
1098+
1099+
if (inputButtom + listHeight > viewPortHeight) {
1100+
list.style.top = `-${inputHeight + listHeight}px`
1101+
} else {
1102+
list.style.top = "0px"
1103+
}
1104+
}
10931105
</script>
10941106

10951107
<div

0 commit comments

Comments
 (0)