Avoid constantly reinitializing focus (#265)

This commit is contained in:
Sung Won Cho 2019-10-10 10:25:40 +08:00 committed by GitHub
commit 9f6d4dbbaa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 3 additions and 20 deletions

View file

@ -26,7 +26,6 @@ import { focusTextarea } from 'web/libs/dom';
import { getEditorSessionkey } from 'web/libs/editor';
import operations from 'web/libs/operations';
import { getNotePath, notePathDef } from 'web/libs/paths';
import { useFocus } from 'web/libs/hooks/dom';
import Editor from '../Common/Editor';
import Flash from '../Common/Flash';
import { useDispatch, useSelector } from '../../store';
@ -39,22 +38,6 @@ import styles from './New.scss';
interface Props extends RouteComponentProps {}
// useInitFocus initializes the focus on HTML elements depending on the current
// state of the editor.
function useInitFocus({ bookLabel, content, textareaRef, setTriggerFocus }) {
useEffect(() => {
if (!bookLabel && !content) {
setTriggerFocus();
} else {
const textareaEl = textareaRef.current;
if (textareaEl) {
focusTextarea(textareaEl);
}
}
}, [setTriggerFocus, bookLabel, textareaRef]);
}
const New: React.SFC<Props> = ({ history }) => {
const sessionKey = getEditorSessionkey(null);
const { editor } = useSelector(state => {

View file

@ -16,7 +16,7 @@
* along with Dnote. If not, see <https://www.gnu.org/licenses/>.
*/
import { useEffect, useRef } from 'react';
import { useEffect, useCallback, useRef } from 'react';
import {
KEYCODE_DOWN,
@ -163,13 +163,13 @@ export function useSearchMenuKeydown<T = Option>({
export function useFocus() {
const elRef = useRef<HTMLElement>();
const setFocus = () => {
const setFocus = useCallback(() => {
const currentEl = elRef.current;
if (currentEl) {
currentEl.focus();
}
};
}, [elRef.current]);
return [setFocus, elRef] as const;
}