From 01695822bbf2fc27216b78d8e786b4da3932f72f Mon Sep 17 00:00:00 2001 From: Paul Mineev Date: Sun, 8 Jan 2023 13:46:00 -0500 Subject: [PATCH] fix: calculate correct size when no_footer=true --- .../app/components/auth/auth.hooks.ts | 13 +++--------- .../remark42/app/components/root/root.tsx | 11 ++-------- .../apps/remark42/app/utils/post-message.ts | 21 +++++++++++++++++++ 3 files changed, 26 insertions(+), 19 deletions(-) diff --git a/frontend/apps/remark42/app/components/auth/auth.hooks.ts b/frontend/apps/remark42/app/components/auth/auth.hooks.ts index f8ab27c3..db265cea 100644 --- a/frontend/apps/remark42/app/components/auth/auth.hooks.ts +++ b/frontend/apps/remark42/app/components/auth/auth.hooks.ts @@ -3,16 +3,9 @@ import { useIntl } from 'react-intl'; import { errorMessages, RequestError } from 'utils/errorUtils'; import { isObject } from 'utils/is-object'; -import { parseMessage, postMessageToParent } from 'utils/post-message'; +import { parseMessage, updateIframeHeight } from 'utils/post-message'; import { messages } from './auth.messsages'; -function handleChangeIframeSize(element: HTMLElement) { - const { top } = element.getBoundingClientRect(); - const height = Math.max(window.scrollY + Math.abs(top) + element.scrollHeight + 20, document.body.offsetHeight); - - postMessageToParent({ height }); -} - export function useDropdown(disableClosing?: boolean) { const rootRef = useRef(null); const clickInsideRef = useRef(false); @@ -74,10 +67,10 @@ export function useDropdown(disableClosing?: boolean) { return; } - handleChangeIframeSize(dropdownElement); + updateIframeHeight(dropdownElement); const observer = new ResizeObserver(() => { - handleChangeIframeSize(dropdownElement); + updateIframeHeight(dropdownElement); }); observer.observe(dropdownElement); diff --git a/frontend/apps/remark42/app/components/root/root.tsx b/frontend/apps/remark42/app/components/root/root.tsx index ef90878b..ed77175b 100644 --- a/frontend/apps/remark42/app/components/root/root.tsx +++ b/frontend/apps/remark42/app/components/root/root.tsx @@ -35,19 +35,12 @@ import { ConnectedComment as Comment } from 'components/comment/connected-commen import { uploadImage, getPreview } from 'common/api'; import { isUserAnonymous } from 'utils/isUserAnonymous'; import { bindActions } from 'utils/actionBinder'; -import { postMessageToParent, parseMessage } from 'utils/post-message'; +import { postMessageToParent, parseMessage, updateIframeHeight } from 'utils/post-message'; import { useActions } from 'hooks/useAction'; import { setCollapse } from 'store/thread/actions'; import styles from './root.module.css'; -/** - * Sends size of the iframe to parent window - */ -export function updateIframeHeight() { - postMessageToParent({ height: document.body.offsetHeight }); -} - const mapStateToProps = (state: StoreState) => ({ sort: state.comments.sort, isCommentsLoading: state.comments.isFetching, @@ -336,7 +329,7 @@ export function ConnectedRoot() { const actions = useActions(boundActions); useEffect(() => { - const observer = new ResizeObserver(updateIframeHeight); + const observer = new ResizeObserver(() => updateIframeHeight()); updateIframeHeight(); observer.observe(document.body); diff --git a/frontend/apps/remark42/app/utils/post-message.ts b/frontend/apps/remark42/app/utils/post-message.ts index cfd006b9..f14c0874 100644 --- a/frontend/apps/remark42/app/utils/post-message.ts +++ b/frontend/apps/remark42/app/utils/post-message.ts @@ -61,3 +61,24 @@ export function parseMessage({ data }: MessageEvent): AllMessages { return data as AllMessages; } + +/** + * Sends message to parent window with height of iframe + * + * @param dropdown provide dropdown element if present on screen + */ +export function updateIframeHeight(dropdown?: HTMLElement) { + let scrollHeight = 0; + + // If dropdown is present on screen, we need to calculate size according to it size since it's positioned absolutely + if (dropdown) { + const { top } = dropdown.getBoundingClientRect(); + // The size of shadow under the dropdown is 20px + scrollHeight = window.scrollY + Math.abs(top) + dropdown.scrollHeight + 20; + } + + // The size of vertical padding on body is 12px + const bodyHeight = document.body.offsetHeight + 12; + + postMessageToParent({ height: Math.max(scrollHeight, bodyHeight) }); +}