Skip to content

Commit

Permalink
fix: update iframe size on textarea size change
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavel Mineev authored and umputun committed Dec 3, 2022
1 parent 6fe373d commit 68b8468
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -452,6 +453,7 @@ export class CommentForm extends Component<Props, State> {
disabled={isDisabled}
autofocus={!!autofocus}
spellcheck={true}
onResize={updateIframeHeight}
/>
</TextExpander>
{charactersLeft < 100 && <span className="comment-form__counter">{charactersLeft}</span>}
Expand Down
6 changes: 1 addition & 5 deletions frontend/apps/remark42/app/components/root/root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -287,10 +287,6 @@ export class Root extends Component<Props, State> {
}
}

function updateIframeHeight() {
postMessageToParent({ height: document.body.offsetHeight });
}

interface CommentsProps {
isLoading: boolean;
topComments: string[];
Expand Down
49 changes: 28 additions & 21 deletions frontend/apps/remark42/app/components/textarea-autosize.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<JSX.HTMLAttributes<HTMLTextAreaElement>, 'onInput'> & {
onInput?: (evt: JSX.TargetedEvent<HTMLTextAreaElement, Event>) => void;
onInput?(evt: JSX.TargetedEvent<HTMLTextAreaElement, Event>): void;
onResize?(): void;
};

export const TextareaAutosize = forwardRef<HTMLTextAreaElement, Props>(({ onInput, value, ...props }, externalRef) => {
const localRef = useRef<HTMLTextAreaElement>(null);
const ref = externalRef || localRef;
export const TextareaAutosize = forwardRef<HTMLTextAreaElement, Props>(
({ onInput, value, onResize, ...props }, externalRef) => {
const localRef = useRef<HTMLTextAreaElement>(null);
const ref = externalRef || localRef;

const handleInput: JSX.GenericEventHandler<HTMLTextAreaElement> = (evt) => {
if (!ref.current) {
return;
}
const handleInput: JSX.GenericEventHandler<HTMLTextAreaElement> = (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 <textarea {...props} data-testid={props.id} onInput={handleInput} value={value} ref={ref} />;
});
return <textarea {...props} data-testid={props.id} onInput={handleInput} value={value} ref={ref} />;
}
);
7 changes: 7 additions & 0 deletions frontend/apps/remark42/app/utils/post-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,10 @@ export function parseMessage({ data }: MessageEvent): AllMessages {

return data as AllMessages;
}

/**
* Sends size of the iframe to parent window
*/
export function updateIframeHeight() {
postMessageToParent({ height: document.body.offsetHeight });
}

0 comments on commit 68b8468

Please sign in to comment.