|
1 | 1 | <script> |
2 | 2 | import { flip } from "svelte/animate" |
3 | 3 | import { fade } from "svelte/transition" |
| 4 | + import {afterUpdate} from 'svelte' |
4 | 5 |
|
5 | 6 | // the list of items the user can select from |
6 | 7 | export let items = [] |
|
203 | 204 | // other state |
204 | 205 | let inputDelayTimeout |
205 | 206 |
|
| 207 | + let setPositionOnNextUpdate = false; |
| 208 | +
|
| 209 | + // --- Lifecycle events --- |
| 210 | +
|
| 211 | + afterUpdate(() => { |
| 212 | + if(setPositionOnNextUpdate) { |
| 213 | + setScrollAwareListPosition() |
| 214 | + } |
| 215 | + setPositionOnNextUpdate = false |
| 216 | + }) |
| 217 | +
|
206 | 218 | // --- Functions --- |
207 | 219 |
|
208 | 220 | function safeFunction(theFunction, argument) { |
|
912 | 924 | return |
913 | 925 | } |
914 | 926 |
|
915 | | - setScrollAwareListPosition() |
| 927 | + setPositionOnNextUpdate = true |
916 | 928 |
|
917 | 929 | opened = true |
918 | 930 | } |
919 | 931 |
|
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 | | -
|
932 | 932 | function close() { |
933 | 933 | if (debug) { |
934 | 934 | console.log("close") |
|
1090 | 1090 | } |
1091 | 1091 | selectedItem = newSelection |
1092 | 1092 | } |
| 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 | + } |
1093 | 1105 | </script> |
1094 | 1106 |
|
1095 | 1107 | <div |
|
0 commit comments