ComposeForm: don't focus the input constantly, add usePrevious hook

environments/review-compose-fo-dvb72u/deployments/996
Alex Gleason 2 years ago
parent 1e6993975c
commit 6b57b62b38
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7

@ -18,7 +18,7 @@ import AutosuggestInput, { AutoSuggestion } from 'soapbox/components/autosuggest
import AutosuggestTextarea from 'soapbox/components/autosuggest_textarea';
import Icon from 'soapbox/components/icon';
import { Button, Stack } from 'soapbox/components/ui';
import { useAppDispatch, useAppSelector, useCompose, useFeatures } from 'soapbox/hooks';
import { useAppDispatch, useAppSelector, useCompose, useFeatures, usePrevious } from 'soapbox/hooks';
import { isMobile } from 'soapbox/is_mobile';
import EmojiPickerDropdown from '../components/emoji-picker/emoji-picker-dropdown';
@ -76,6 +76,7 @@ const ComposeForm = <ID extends string>({ id, shouldCondense, autoFocus, clickab
const features = useFeatures();
const { text, suggestions, spoiler, spoiler_text: spoilerText, privacy, focusDate, caretPosition, is_submitting: isSubmitting, is_changing_upload: isChangingUpload, is_uploading: isUploading, schedule: scheduledAt } = compose;
const prevSpoiler = usePrevious(spoiler);
const hasPoll = !!compose.poll;
const isEditing = compose.id !== null;
@ -206,9 +207,10 @@ const ComposeForm = <ID extends string>({ id, shouldCondense, autoFocus, clickab
}, []);
useEffect(() => {
switch (spoiler) {
case true: focusSpoilerInput(); break;
case false: focusTextarea(); break;
if (spoiler && !prevSpoiler) {
focusSpoilerInput();
} else if (!spoiler && prevSpoiler) {
focusTextarea();
}
}, [spoiler]);

@ -8,6 +8,7 @@ export { useFeatures } from './useFeatures';
export { useLocale } from './useLocale';
export { useOnScreen } from './useOnScreen';
export { useOwnAccount } from './useOwnAccount';
export { usePrevious } from './usePrevious';
export { useRefEventHandler } from './useRefEventHandler';
export { useSettings } from './useSettings';
export { useSoapboxConfig } from './useSoapboxConfig';

@ -0,0 +1,13 @@
import { useRef, useEffect } from 'react';
/** Get the last version of this value. */
// https://usehooks.com/usePrevious/
export const usePrevious = <T>(value: T): T | undefined => {
const ref = useRef<T>();
useEffect(() => {
ref.current = value;
}, [value]);
return ref.current;
};
Loading…
Cancel
Save