Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update iframe size on textarea size change #1541

Merged
merged 1 commit into from
Dec 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 });
}