use aria-react for message hover & focus hooks (#1456)

This commit is contained in:
Ajay Bura 2023-10-19 17:42:35 +11:00 committed by GitHub
parent b92b281050
commit b4e1ced3ed
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 1616 additions and 10 deletions

1606
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -56,6 +56,7 @@
"prismjs": "1.29.0",
"prop-types": "15.8.1",
"react": "17.0.2",
"react-aria": "3.29.1",
"react-autosize-textarea": "7.1.0",
"react-blurhash": "0.2.0",
"react-dnd": "15.1.2",

View file

@ -32,6 +32,7 @@ import React, {
useState,
} from 'react';
import FocusTrap from 'focus-trap-react';
import { useHover, useFocusWithin } from 'react-aria';
import { MatrixEvent, Room } from 'matrix-js-sdk';
import { Relations } from 'matrix-js-sdk/lib/models/relations';
import classNames from 'classnames';
@ -607,6 +608,8 @@ export const Message = as<'div', MessageProps>(
const mx = useMatrixClient();
const senderId = mEvent.getSender() ?? '';
const [hover, setHover] = useState(false);
const { hoverProps } = useHover({ onHoverChange: setHover });
const { focusWithinProps } = useFocusWithin({ onFocusWithinChange: setHover });
const [menu, setMenu] = useState(false);
const [emojiBoard, setEmojiBoard] = useState(false);
@ -692,9 +695,6 @@ export const Message = as<'div', MessageProps>(
</Box>
);
const showOptions = () => setHover(true);
const hideOptions = () => setHover(false);
const handleContextMenu: MouseEventHandler<HTMLDivElement> = (evt) => {
if (evt.altKey || !window.getSelection()?.isCollapsed) return;
const tag = (evt.target as any).tagName;
@ -716,8 +716,8 @@ export const Message = as<'div', MessageProps>(
highlight={highlight}
selected={menu || emojiBoard}
{...props}
onMouseEnter={showOptions}
onMouseLeave={hideOptions}
{...hoverProps}
{...focusWithinProps}
ref={ref}
>
{!edit && (hover || menu || emojiBoard) && (
@ -958,12 +958,11 @@ export const Event = as<'div', EventProps>(
({ className, room, mEvent, highlight, canDelete, messageSpacing, children, ...props }, ref) => {
const mx = useMatrixClient();
const [hover, setHover] = useState(false);
const { hoverProps } = useHover({ onHoverChange: setHover });
const { focusWithinProps } = useFocusWithin({ onFocusWithinChange: setHover });
const [menu, setMenu] = useState(false);
const stateEvent = typeof mEvent.getStateKey() === 'string';
const showOptions = () => setHover(true);
const hideOptions = () => setHover(false);
const handleContextMenu: MouseEventHandler<HTMLDivElement> = (evt) => {
if (evt.altKey || !window.getSelection()?.isCollapsed) return;
const tag = (evt.target as any).tagName;
@ -985,8 +984,8 @@ export const Event = as<'div', EventProps>(
highlight={highlight}
selected={menu}
{...props}
onMouseEnter={showOptions}
onMouseLeave={hideOptions}
{...hoverProps}
{...focusWithinProps}
ref={ref}
>
{(hover || menu) && (