Fix dropdown opening
* change iframe height on content change * change iframe size on dropdown open
This commit is contained in:
@@ -1,5 +1,16 @@
|
||||
import { useEffect, useRef, useState } from 'preact/hooks';
|
||||
|
||||
function handleChangeIframeSize(element: HTMLElement) {
|
||||
const { top } = element.getBoundingClientRect();
|
||||
const height = window.scrollY + Math.abs(top) + element.scrollHeight + 20;
|
||||
|
||||
if (window.innerHeight > height) {
|
||||
return;
|
||||
}
|
||||
|
||||
document.body.style.setProperty('min-height', `${height}px`);
|
||||
}
|
||||
|
||||
export function useDropdown(disableClosing?: boolean) {
|
||||
const rootRef = useRef<HTMLDivElement>(null);
|
||||
const [showDropdown, setShowDropdown] = useState(false);
|
||||
@@ -14,6 +25,8 @@ export function useDropdown(disableClosing?: boolean) {
|
||||
|
||||
const dropdownElement = rootRef.current;
|
||||
|
||||
handleChangeIframeSize(dropdownElement);
|
||||
|
||||
function handleMessageFromParent(evt: MessageEvent) {
|
||||
if (typeof evt.data !== 'string' || disableClosing) {
|
||||
return;
|
||||
@@ -54,21 +67,9 @@ export function useDropdown(disableClosing?: boolean) {
|
||||
return;
|
||||
}
|
||||
|
||||
let prevHeight: number | null = null;
|
||||
|
||||
function handleDropdownContentChange() {
|
||||
const { top } = dropdownElement.getBoundingClientRect();
|
||||
const height = window.scrollY + Math.abs(top) + dropdownElement.scrollHeight + 20;
|
||||
|
||||
if (prevHeight === null && window.innerHeight > height) {
|
||||
return;
|
||||
}
|
||||
|
||||
prevHeight = height;
|
||||
document.body.style.setProperty('min-height', `${height}px`);
|
||||
}
|
||||
|
||||
const observer = new MutationObserver(handleDropdownContentChange);
|
||||
const observer = new MutationObserver(() => {
|
||||
handleChangeIframeSize(dropdownElement);
|
||||
});
|
||||
|
||||
observer.observe(dropdownElement, { attributes: true, childList: true, subtree: true });
|
||||
|
||||
|
||||
Reference in New Issue
Block a user