auto focus thread input on type

This commit is contained in:
Ajay Bura 2026-08-16 11:33:04 +05:30
commit bad6cdcd8a
5 changed files with 124 additions and 57 deletions

View file

@ -1,4 +1,4 @@
import React, { useCallback } from 'react';
import React, { useCallback, useRef } from 'react';
import { Box, Line } from 'folds';
import { useParams } from 'react-router-dom';
import { isKeyHotkey } from 'is-hotkey';
@ -37,6 +37,7 @@ export function Room() {
const activeThreadId = useObservableValueOptionally(timeline?.activeThread);
const threads = useObservableValueOptionally(timeline?.threads);
const threadTimeline = activeThreadId ? threads?.get(activeThreadId) : undefined;
const threadFocusRef = useRef<boolean>(false);
const [isDrawer] = useSetting(settingsAtom, 'isPeopleDrawer');
const [hideActivity] = useSetting(settingsAtom, 'hideActivity');
@ -71,7 +72,12 @@ export function Room() {
<Box grow="Yes" direction="Column">
<RoomViewHeader />
<Box grow="Yes">
<RoomView key={roomId} timeline={timeline} eventId={eventId} />
<RoomView
key={roomId}
threadFocusRef={threadFocusRef}
timeline={timeline}
eventId={eventId}
/>
</Box>
</Box>
</TimelineViewProvider>
@ -81,6 +87,7 @@ export function Room() {
<Line variant="Surface" direction="Vertical" size="300" />
<ThreadView
threadTimeline={threadTimeline}
threadFocusRef={threadFocusRef}
requestClose={() => timeline.closeThread()}
/>
</TimelineViewProvider>

View file

@ -498,7 +498,6 @@ export function RoomTimeline({ eventId, roomInputRef, editor, timeline }: RoomTi
};
const readReceipt = useUserReadReceipt();
console.log(readReceipt, ct.roomToLatestEvent.read.get(roomId));
const { getDayDivider, getNewDivider, updateDivider, clearDayDivider, clearNewDivider } =
useEventDivider(readReceipt?.ts);
const { getCollapsed, updateCollapse } = useEventCollapse();

View file

@ -1,7 +1,5 @@
import React, { useCallback, useRef } from 'react';
import React, { MutableRefObject, useRef } from 'react';
import { Box, Text, config } from 'folds';
import { ReactEditor } from 'slate-react';
import { isKeyHotkey } from 'is-hotkey';
import { usePowerLevelsContext } from '../../hooks/usePowerLevels';
import { useEditor } from '../../components/editor';
import { RoomInputPlaceholder } from './RoomInputPlaceholder';
@ -10,8 +8,6 @@ import { RoomTombstone } from './RoomTombstone';
import { RoomInput } from './RoomInput';
import { RoomViewFollowing, RoomViewFollowingPlaceholder } from './RoomViewFollowing';
import { Page } from '../../components/page';
import { useKeyDown } from '../../hooks/useKeyDown';
import { editableActiveElement } from '../../utils/dom';
import { settingsAtom } from '../../state/settings';
import { useSetting } from '../../state/hooks/settings';
import { useRoomPermissions } from '../../hooks/useRoomPermissions';
@ -21,39 +17,14 @@ import { useObservableValue } from '../../hooks/useObservableValue';
import { useClient } from '../../hooks/useClient';
import { MessageEvent } from '../../../types/matrix/room';
import { Timeline } from '../../../client/room';
import { useEditorAutoFocus } from './hooks/useEditorAutoFocus';
const FN_KEYS_REGEX = /^F\d+$/;
const shouldFocusMessageField = (evt: KeyboardEvent): boolean => {
const { code } = evt;
if (evt.metaKey || evt.altKey || evt.ctrlKey) {
return false;
}
if (FN_KEYS_REGEX.test(code)) return false;
if (
code.startsWith('OS') ||
code.startsWith('Meta') ||
code.startsWith('Shift') ||
code.startsWith('Alt') ||
code.startsWith('Control') ||
code.startsWith('Arrow') ||
code.startsWith('Page') ||
code.startsWith('End') ||
code.startsWith('Home') ||
code === 'Tab' ||
code === 'Space' ||
code === 'Enter' ||
code === 'NumLock' ||
code === 'ScrollLock'
) {
return false;
}
return true;
type RoomViewProps = {
eventId?: string;
threadFocusRef: MutableRefObject<boolean>;
timeline: Timeline;
};
export function RoomView({ eventId, timeline }: { eventId?: string; timeline: Timeline }) {
export function RoomView({ eventId, timeline, threadFocusRef }: RoomViewProps) {
const roomInputRef = useRef<HTMLDivElement>(null);
const roomViewRef = useRef<HTMLDivElement>(null);
@ -72,22 +43,7 @@ export function RoomView({ eventId, timeline }: { eventId?: string; timeline: Ti
const permissions = useRoomPermissions(creators, powerLevels);
const canMessage = permissions.event(MessageEvent.RoomMessage, ct.session.userId);
useKeyDown(
window,
useCallback(
(evt) => {
if (editableActiveElement()) return;
const portalContainer = document.getElementById('portalContainer');
if (portalContainer && portalContainer.children.length > 0) {
return;
}
if (shouldFocusMessageField(evt) || isKeyHotkey('mod+v', evt)) {
ReactEditor.focus(editor);
}
},
[editor]
)
);
useEditorAutoFocus(editor, threadFocusRef, false);
return (
<Page ref={roomViewRef}>

View file

@ -0,0 +1,100 @@
import { MutableRefObject, RefObject, useCallback, useEffect } from 'react';
import { isKeyHotkey } from 'is-hotkey';
import { ReactEditor } from 'slate-react';
import { Editor } from 'slate';
import { useKeyDown } from '../../../hooks/useKeyDown';
import { editableActiveElement } from '../../../utils/dom';
const FN_KEYS_REGEX = /^F\d+$/;
const shouldFocusMessageField = (evt: KeyboardEvent): boolean => {
const { code } = evt;
if (evt.metaKey || evt.altKey || evt.ctrlKey) {
return false;
}
if (FN_KEYS_REGEX.test(code)) return false;
if (
code.startsWith('OS') ||
code.startsWith('Meta') ||
code.startsWith('Shift') ||
code.startsWith('Alt') ||
code.startsWith('Control') ||
code.startsWith('Arrow') ||
code.startsWith('Page') ||
code.startsWith('End') ||
code.startsWith('Home') ||
code === 'Tab' ||
code === 'Space' ||
code === 'Enter' ||
code === 'NumLock' ||
code === 'ScrollLock'
) {
return false;
}
return true;
};
export const useEditorAutoFocus = (
editor: Editor,
threadFocusRef: RefObject<boolean>,
thread: boolean
) => {
useKeyDown(
window,
useCallback(
(evt) => {
if (editableActiveElement()) return;
const portalContainer = document.getElementById('portalContainer');
if (portalContainer && portalContainer.children.length > 0) {
return;
}
if (!shouldFocusMessageField(evt) && !isKeyHotkey('mod+v', evt)) {
return;
}
if (threadFocusRef.current && !thread) {
return;
}
ReactEditor.focus(editor);
},
[editor, threadFocusRef, thread]
)
);
};
export const useThreadFocus = (
threadFocusRef: MutableRefObject<boolean>,
viewElementRef: RefObject<HTMLElement>
) => {
useEffect(() => {
const handlePointerDown = (evt: PointerEvent) => {
const viewElement = viewElementRef.current;
if (!viewElement) return;
const lastTarget = evt.target;
const clickedInsideView = lastTarget instanceof Node && viewElement.contains(lastTarget);
// eslint-disable-next-line no-param-reassign
threadFocusRef.current = clickedInsideView;
};
// eslint-disable-next-line no-param-reassign
threadFocusRef.current = true;
window.addEventListener('pointerdown', handlePointerDown);
return () => {
// eslint-disable-next-line no-param-reassign
threadFocusRef.current = true;
window.removeEventListener('pointerdown', handlePointerDown);
};
}, [threadFocusRef, viewElementRef]);
return threadFocusRef;
};

View file

@ -1,4 +1,4 @@
import React, { useRef } from 'react';
import React, { MutableRefObject, useRef } from 'react';
import classNames from 'classnames';
import { Box, config, Icon, IconButton, Icons, Text, Tooltip, TooltipProvider } from 'folds';
import FocusTrap from 'focus-trap-react';
@ -10,19 +10,24 @@ import { useEditor } from '../../../components/editor';
import { RoomInput } from '../RoomInput';
import { RoomViewFollowingPlaceholder } from '../RoomViewFollowing';
import { TimelineView } from './TimelineView';
import { useEditorAutoFocus, useThreadFocus } from '../hooks/useEditorAutoFocus';
type ThreadViewProps = {
threadTimeline: ThreadTimeline;
threadFocusRef: MutableRefObject<boolean>;
requestClose: () => void;
};
export function ThreadView({ threadTimeline, requestClose }: ThreadViewProps) {
export function ThreadView({ threadTimeline, threadFocusRef, requestClose }: ThreadViewProps) {
const screenSize = useScreenSizeContext();
const floating = screenSize !== ScreenSize.Desktop;
const editor = useEditor();
const threadInputRef = useRef<HTMLDivElement>(null);
const threadViewRef = useRef<HTMLDivElement>(null);
useThreadFocus(threadFocusRef, threadViewRef);
useEditorAutoFocus(editor, threadFocusRef, true);
return (
<FocusTrap
paused={!floating}