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

[MOM-1902] ⚡ Fix mentions UI flickering issue, and introduce a custom click outside backdrop for mentions UI. #48

Merged
merged 2 commits into from
May 7, 2024
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 @@ -31,5 +31,9 @@ export const AutocompleteMenu = style([

export const AutocompleteMenuHeader = style([
DefaultReset,
{ padding: `0 ${config.space.S300}`, flexShrink: 0 },
{
padding: `0 ${config.space.S300}`,
flexShrink: 0,
justifyContent: 'space-between',
},
]);
20 changes: 16 additions & 4 deletions src/app/components/editor/autocomplete/AutocompleteMenu.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */
import React, { ReactNode } from 'react';
import FocusTrap from 'focus-trap-react';
import { isKeyHotkey } from 'is-hotkey';
import { Header, Menu, Scroll, config } from 'folds';
import { Header, Menu, Scroll, config, Icons, Icon, IconButton } from 'folds';

import * as css from './AutocompleteMenu.css';
import { preventScrollWithArrowKey } from '../../../utils/keyboard';
Expand All @@ -13,12 +14,20 @@ type AutocompleteMenuProps = {
};
export function AutocompleteMenu({ headerContent, requestClose, children }: AutocompleteMenuProps) {
return (
<div className={css.AutocompleteMenuBase}>
<div className={css.AutocompleteMenuContainer}>
<div
className={css.AutocompleteMenuBase}
data-testid="user-mention-autocomplete_outer-div-focus-trap"
>
<div
className={css.AutocompleteMenuContainer}
data-testid="user-mention-autocomplete_inner-div-focus-trap"
>
<FocusTrap
active
focusTrapOptions={{
initialFocus: false,
onDeactivate: () => requestClose(),
// don't use 'onDeactivate' this when in federation mode
// since Momentify is running on React 18
returnFocusOnDeactivate: false,
clickOutsideDeactivates: true,
allowOutsideClick: true,
Expand All @@ -29,6 +38,9 @@ export function AutocompleteMenu({ headerContent, requestClose, children }: Auto
<Menu className={css.AutocompleteMenu}>
<Header className={css.AutocompleteMenuHeader} size="400">
{headerContent}
<IconButton onClick={requestClose}>
<Icon src={Icons.Cross} />
</IconButton>
</Header>
<Scroll style={{ flexGrow: 1 }} onKeyDown={preventScrollWithArrowKey}>
<div style={{ padding: config.space.S200 }}>{children}</div>
Expand Down
50 changes: 42 additions & 8 deletions src/app/organisms/room/RoomInput.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */
/* eslint-disable no-unsafe-optional-chaining */
import React, {
KeyboardEventHandler,
RefObject,
Expand All @@ -7,6 +9,7 @@ import React, {
useMemo,
useRef,
useState,
useLayoutEffect,
} from 'react';
import { useAtom } from 'jotai';
import { isKeyHotkey } from 'is-hotkey';
Expand Down Expand Up @@ -120,6 +123,8 @@ export const RoomInput = forwardRef<HTMLDivElement, RoomInputProps>(
const [isMarkdown] = useSetting(settingsAtom, 'isMarkdown');
const commands = useCommands(mx, room);

const [clickOutsideHeight, setClickOutsideHeight] = useState<number>(0);

const [msgDraft, setMsgDraft] = useAtom(roomIdToMsgDraftAtomFamily(roomId));
const [replyDraft, setReplyDraft] = useAtom(roomIdToReplyDraftAtomFamily(roomId));
const [uploadBoard, setUploadBoard] = useState(true);
Expand Down Expand Up @@ -327,6 +332,11 @@ export const RoomInput = forwardRef<HTMLDivElement, RoomInputProps>(
customEditorRef,
]);

const handleCloseAutocomplete = useCallback(() => {
setAutocompleteQuery(undefined);
ReactEditor.focus(editor);
}, [editor]);

const handleKeyDown: KeyboardEventHandler = useCallback(
(evt) => {
if (isKeyHotkey('mod+enter', evt) || (!enterForNewline && isKeyHotkey('enter', evt))) {
Expand All @@ -344,6 +354,7 @@ export const RoomInput = forwardRef<HTMLDivElement, RoomInputProps>(
const handleKeyUp: KeyboardEventHandler = useCallback(
(evt) => {
if (isKeyHotkey('escape', evt)) {
handleCloseAutocomplete();
evt.preventDefault();
return;
}
Expand All @@ -356,14 +367,8 @@ export const RoomInput = forwardRef<HTMLDivElement, RoomInputProps>(
: undefined;
setAutocompleteQuery(query);
},
[editor, sendTypingStatus]
[editor, handleCloseAutocomplete, sendTypingStatus]
);

const handleCloseAutocomplete = useCallback(() => {
setAutocompleteQuery(undefined);
ReactEditor.focus(editor);
}, [editor]);

const handleEmoticonSelect = (key: string, shortcode: string) => {
editor.insertNode(createEmoticonElement(key, shortcode));
moveCursor(editor);
Expand All @@ -385,8 +390,37 @@ export const RoomInput = forwardRef<HTMLDivElement, RoomInputProps>(
});
};

useLayoutEffect(() => {
// @ts-ignore
if (ref?.current && autocompleteQuery) {
const elementHeights =
document.querySelector(
'div[data-testid="user-mention-autocomplete_inner-div-focus-trap"]'
// @ts-ignore
)?.offsetHeight +
// @ts-ignore
document.querySelector('div[data-testid="room-view-editor"]')?.offsetHeight || 0;
setClickOutsideHeight(elementHeights + 20);
}
}, [ref, autocompleteQuery]);

return (
<div ref={ref} className='app_roomInput'>
<div ref={ref} className="app_roomInput">
{autocompleteQuery && (
<div
data-testid="custom-click-outside-backdrop"
style={{
background: 'transparent',
position: 'absolute',
left: 0,
width: '100vw',
height: `calc(100vh - ${clickOutsideHeight}px)`,
bottom: `calc(100vh - (100vh - ${clickOutsideHeight}px))`,
}}
aria-hidden="true"
onClick={handleCloseAutocomplete}
/>
)}
{selectedFiles.length > 0 && (
<UploadBoard
header={
Expand Down
2 changes: 1 addition & 1 deletion src/app/organisms/room/RoomView.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ function RoomView({ room, eventId }) {
<RoomViewTyping room={room} />
</div>
<div className="room-view__sticky">
<div className="room-view__editor">
<div className="room-view__editor" data-testid="room-view-editor">
{tombstoneEvent ? (
<RoomTombstone
roomId={roomId}
Expand Down
24 changes: 17 additions & 7 deletions src/app/organisms/room/message/Message.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -664,9 +664,7 @@ export const Message = as<'div', MessageProps>(
// onClick={onUserClick}
>
{senderAvatarMxc ? (
<AvatarImage
src={senderAvatarMxc}
/>
<AvatarImage src={senderAvatarMxc} />
) : (
<AvatarFallback
style={{
Expand Down Expand Up @@ -786,7 +784,7 @@ export const Message = as<'div', MessageProps>(
variant="SurfaceVariant"
size="300"
radii="300"
className='room_message_edit_btn'
className="room_message_edit_btn"
>
<Icon src={Icons.Pencil} size="100" />
</IconButton>
Expand Down Expand Up @@ -936,18 +934,30 @@ export const Message = as<'div', MessageProps>(
</div>
)}
{messageLayout === 1 && (
<CompactLayout before={headerJSX} onContextMenu={handleContextMenu} className={'room_message_block_compact'}>
<CompactLayout
before={headerJSX}
onContextMenu={handleContextMenu}
className="room_message_block_compact"
>
{msgContentJSX}
</CompactLayout>
)}
{messageLayout === 2 && (
<BubbleLayout before={avatarJSX} onContextMenu={handleContextMenu} className={'room_message_block_bubble'}>
<BubbleLayout
before={avatarJSX}
onContextMenu={handleContextMenu}
className="room_message_block_bubble"
>
{headerJSX}
{msgContentJSX}
</BubbleLayout>
)}
{messageLayout !== 1 && messageLayout !== 2 && (
<ModernLayout before={avatarJSX} onContextMenu={handleContextMenu} className={'room_message_block_modern'}>
<ModernLayout
before={avatarJSX}
onContextMenu={handleContextMenu}
className="room_message_block_modern"
>
{headerJSX}
{msgContentJSX}
</ModernLayout>
Expand Down