From 3bdbbfb49dd3ef4ceb57a18ac89befc2f686c087 Mon Sep 17 00:00:00 2001 From: Pavel Mineev Date: Fri, 2 Dec 2022 12:30:04 -0500 Subject: [PATCH] fix: update iframe size on textarea size change --- .../__field/comment-form__field.css | 1 + .../components/comment-form/comment-form.tsx | 2 + .../remark42/app/components/root/root.tsx | 6 +-- .../app/components/textarea-autosize.tsx | 49 +++++++++++-------- .../apps/remark42/app/utils/post-message.ts | 7 +++ 5 files changed, 39 insertions(+), 26 deletions(-) diff --git a/frontend/apps/remark42/app/components/comment-form/__field/comment-form__field.css b/frontend/apps/remark42/app/components/comment-form/__field/comment-form__field.css index 2934b67448..c6dab8f14a 100644 --- a/frontend/apps/remark42/app/components/comment-form/__field/comment-form__field.css +++ b/frontend/apps/remark42/app/components/comment-form/__field/comment-form__field.css @@ -13,6 +13,7 @@ line-height: 1.4; border: 0; resize: none; + overflow: hidden; /* prevent scrollbar from appearing */ backface-visibility: hidden; /* let's try to fix blinking in Safari */ transform: translateZ(0); /* let's try to fix blinking in Safari, again */ diff --git a/frontend/apps/remark42/app/components/comment-form/comment-form.tsx b/frontend/apps/remark42/app/components/comment-form/comment-form.tsx index bdc8f01103..26724553d7 100644 --- a/frontend/apps/remark42/app/components/comment-form/comment-form.tsx +++ b/frontend/apps/remark42/app/components/comment-form/comment-form.tsx @@ -19,6 +19,7 @@ import { SubscribeByRSS } from './__subscribe-by-rss'; import { MarkdownToolbar } from './markdown-toolbar'; import { TextExpander } from './text-expander'; import { updatePersistedComments, getPersistedComment, removePersistedComment } from './comment-form.persist'; +import { updateIframeHeight } from 'utils/post-message'; export type Props = { id: string; @@ -452,6 +453,7 @@ export class CommentForm extends Component { disabled={isDisabled} autofocus={!!autofocus} spellcheck={true} + onResize={updateIframeHeight} /> {charactersLeft < 100 && {charactersLeft}} diff --git a/frontend/apps/remark42/app/components/root/root.tsx b/frontend/apps/remark42/app/components/root/root.tsx index 839dd1d6c2..9c9b978bd3 100644 --- a/frontend/apps/remark42/app/components/root/root.tsx +++ b/frontend/apps/remark42/app/components/root/root.tsx @@ -36,7 +36,7 @@ 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'; @@ -287,10 +287,6 @@ export class Root extends Component { } } -function updateIframeHeight() { - postMessageToParent({ height: document.body.offsetHeight }); -} - interface CommentsProps { isLoading: boolean; topComments: string[]; diff --git a/frontend/apps/remark42/app/components/textarea-autosize.tsx b/frontend/apps/remark42/app/components/textarea-autosize.tsx index 530914fbc8..ca9ea6c8a8 100644 --- a/frontend/apps/remark42/app/components/textarea-autosize.tsx +++ b/frontend/apps/remark42/app/components/textarea-autosize.tsx @@ -2,36 +2,43 @@ import { h, JSX } from 'preact'; import { forwardRef } from 'preact/compat'; import { useEffect, useRef } from 'preact/hooks'; -function autoResize(textarea: HTMLTextAreaElement) { +function autoResize(textarea: HTMLTextAreaElement, onResize?: () => void) { textarea.style.height = ''; textarea.style.height = `${textarea.scrollHeight}px`; + // Call on rezie callback after textarea height is changed + if (onResize) { + window.requestAnimationFrame(onResize); + } } type Props = Omit, 'onInput'> & { - onInput?: (evt: JSX.TargetedEvent) => void; + onInput?(evt: JSX.TargetedEvent): void; + onResize?(): void; }; -export const TextareaAutosize = forwardRef(({ onInput, value, ...props }, externalRef) => { - const localRef = useRef(null); - const ref = externalRef || localRef; +export const TextareaAutosize = forwardRef( + ({ onInput, value, onResize, ...props }, externalRef) => { + const localRef = useRef(null); + const ref = externalRef || localRef; - const handleInput: JSX.GenericEventHandler = (evt) => { - if (!ref.current) { - return; - } + const handleInput: JSX.GenericEventHandler = (evt) => { + if (!ref.current) { + return; + } - if (onInput) { - return onInput(evt); - } + if (onInput) { + return onInput(evt); + } - autoResize(ref.current); - }; + autoResize(ref.current, onResize); + }; - useEffect(() => { - if (ref.current) { - autoResize(ref.current); - } - }, [value, ref]); + useEffect(() => { + if (ref.current) { + autoResize(ref.current, onResize); + } + }, [onResize, value, ref]); - return