signal-desktop/ts/components/conversation/InlineNotificationWrapper.tsx

52 lines
1.2 KiB
TypeScript
Raw Normal View History

2023-01-03 19:55:46 +00:00
// Copyright 2019 Signal Messenger, LLC
2020-10-30 20:34:04 +00:00
// SPDX-License-Identifier: AGPL-3.0-only
import type { ReactNode } from 'react';
import React, { useCallback, useEffect, useRef } from 'react';
2023-03-14 15:55:31 +00:00
import { getInteractionMode } from '../../services/InteractionMode';
2019-11-07 21:36:16 +00:00
type PropsType = {
2019-11-07 21:36:16 +00:00
id: string;
conversationId: string;
2023-03-20 22:23:53 +00:00
isTargeted: boolean;
targetMessage: (messageId: string, conversationId: string) => unknown;
children: ReactNode;
2019-11-07 21:36:16 +00:00
};
export function InlineNotificationWrapper({
id,
conversationId,
isTargeted,
targetMessage,
children,
}: PropsType): JSX.Element {
const focusRef = useRef<HTMLDivElement>(null);
2019-11-07 21:36:16 +00:00
useEffect(() => {
if (isTargeted) {
const container = focusRef.current;
if (container && !container.contains(document.activeElement)) {
container.focus();
}
2019-11-07 21:36:16 +00:00
}
}, [isTargeted]);
2019-11-07 21:36:16 +00:00
const handleFocus = useCallback(() => {
2023-03-14 15:55:31 +00:00
if (getInteractionMode() === 'keyboard') {
2023-03-20 22:23:53 +00:00
targetMessage(id, conversationId);
}
}, [id, conversationId, targetMessage]);
return (
<div
className="module-inline-notification-wrapper"
// eslint-disable-next-line jsx-a11y/no-noninteractive-tabindex
tabIndex={0}
ref={focusRef}
onFocus={handleFocus}
>
{children}
</div>
);
2019-11-07 21:36:16 +00:00
}