improve scroll on open

This commit is contained in:
Cory LaViska
2022-12-20 10:31:55 -05:00
parent 10cb26b81e
commit 9f79445292
2 changed files with 10 additions and 14 deletions

View File

@@ -472,6 +472,9 @@ export default class SlSelect extends ShoelaceElement implements ShoelaceFormCon
}
if (this.open) {
const selectedOption = this.getOptionByValue(this.value);
const currentOption = selectedOption || this.getFirstOption();
// Show
this.emit('sl-show');
this.addOpenListeners();
@@ -480,25 +483,20 @@ export default class SlSelect extends ShoelaceElement implements ShoelaceFormCon
this.listbox.hidden = false;
this.popup.active = true;
// Select the appropriate option based on value after the listbox opens
requestAnimationFrame(() => {
// Select the appropriate option based on value after the listbox opens
const selectedOption = this.getOptionByValue(this.value);
const currentOption = selectedOption || this.getFirstOption();
this.setSelectedOption(selectedOption);
this.setCurrentOption(currentOption);
// Scroll to the selected option
if (currentOption) {
//
// TODO - improve this logic so the selected option is centered in the listbox instead of at the top
//
this.listbox.scrollTop = currentOption.offsetTop;
}
});
const { keyframes, options } = getAnimation(this, 'select.show', { dir: this.localize.dir() });
await animateTo(this.popup.popup, keyframes, options);
// Make sure the current option is scrolled into view (required for Safari)
if (currentOption) {
scrollIntoView(currentOption, this.listbox, 'vertical', 'auto');
}
this.emit('sl-after-show');
} else {
// Hide

View File

@@ -22,9 +22,7 @@ export function unlockBodyScrolling(lockingEl: HTMLElement) {
}
}
/**
* Scrolls an element into view of its container. If the element is already in view, nothing will happen.
*/
/** Scrolls an element into view of its container. If the element is already in view, nothing will happen. */
export function scrollIntoView(
element: HTMLElement,
container: HTMLElement,