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

70 lines
1.6 KiB
TypeScript
Raw Normal View History

2023-01-03 11:55:46 -08:00
// Copyright 2019 Signal Messenger, LLC
2020-10-30 15:34:04 -05:00
// SPDX-License-Identifier: AGPL-3.0-only
2019-11-07 13:36:16 -08:00
import React from 'react';
2023-03-14 11:55:31 -04:00
import { getInteractionMode } from '../../services/InteractionMode';
2019-11-07 13:36:16 -08:00
export type PropsType = {
id: string;
conversationId: string;
2023-03-20 15:23:53 -07:00
isTargeted: boolean;
targetMessage?: (messageId: string, conversationId: string) => unknown;
2019-11-07 13:36:16 -08:00
};
export class InlineNotificationWrapper extends React.Component<PropsType> {
public focusRef: React.RefObject<HTMLDivElement> = React.createRef();
2020-09-14 12:51:27 -07:00
public setFocus = (): void => {
const container = this.focusRef.current;
if (container && !container.contains(document.activeElement)) {
container.focus();
2019-11-07 13:36:16 -08:00
}
};
2020-09-14 12:51:27 -07:00
public handleFocus = (): void => {
2023-03-14 11:55:31 -04:00
if (getInteractionMode() === 'keyboard') {
2023-03-20 15:23:53 -07:00
this.setTargeted();
}
};
2023-03-20 15:23:53 -07:00
public setTargeted = (): void => {
const { id, conversationId, targetMessage } = this.props;
2019-11-07 13:36:16 -08:00
2023-03-20 15:23:53 -07:00
if (targetMessage) {
targetMessage(id, conversationId);
}
2019-11-07 13:36:16 -08:00
};
public override componentDidMount(): void {
2023-03-20 15:23:53 -07:00
const { isTargeted } = this.props;
if (isTargeted) {
2019-11-07 13:36:16 -08:00
this.setFocus();
}
}
public override componentDidUpdate(prevProps: PropsType): void {
2023-03-20 15:23:53 -07:00
const { isTargeted } = this.props;
2020-09-14 12:51:27 -07:00
2023-03-20 15:23:53 -07:00
if (!prevProps.isTargeted && isTargeted) {
2019-11-07 13:36:16 -08:00
this.setFocus();
}
}
public override render(): JSX.Element {
2019-11-07 13:36:16 -08:00
const { children } = this.props;
return (
<div
className="module-inline-notification-wrapper"
2020-09-14 12:51:27 -07:00
// eslint-disable-next-line jsx-a11y/no-noninteractive-tabindex
2019-11-07 13:36:16 -08:00
tabIndex={0}
ref={this.focusRef}
onFocus={this.handleFocus}
2019-11-07 13:36:16 -08:00
>
{children}
</div>
);
}
}