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

71 lines
1.6 KiB
TypeScript
Raw Normal View History

2020-10-30 20:34:04 +00:00
// Copyright 2019-2020 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
2019-11-07 21:36:16 +00:00
import React from 'react';
export type PropsType = {
id: string;
conversationId: string;
isSelected: boolean;
selectMessage?: (messageId: string, conversationId: string) => unknown;
2019-11-07 21:36:16 +00:00
};
export class InlineNotificationWrapper extends React.Component<PropsType> {
public focusRef: React.RefObject<HTMLDivElement> = React.createRef();
2020-09-14 19:51:27 +00:00
public setFocus = (): void => {
const container = this.focusRef.current;
if (container && !container.contains(document.activeElement)) {
container.focus();
2019-11-07 21:36:16 +00:00
}
};
2020-09-14 19:51:27 +00:00
public handleFocus = (): void => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
if (window.getInteractionMode() === 'keyboard') {
this.setSelected();
}
};
2020-09-14 19:51:27 +00:00
public setSelected = (): void => {
2019-11-07 21:36:16 +00:00
const { id, conversationId, selectMessage } = this.props;
if (selectMessage) {
selectMessage(id, conversationId);
}
2019-11-07 21:36:16 +00:00
};
2020-09-14 19:51:27 +00:00
public componentDidMount(): void {
2019-11-07 21:36:16 +00:00
const { isSelected } = this.props;
if (isSelected) {
this.setFocus();
}
}
2020-09-14 19:51:27 +00:00
public componentDidUpdate(prevProps: PropsType): void {
const { isSelected } = this.props;
if (!prevProps.isSelected && isSelected) {
2019-11-07 21:36:16 +00:00
this.setFocus();
}
}
2020-09-14 19:51:27 +00:00
public render(): JSX.Element {
2019-11-07 21:36:16 +00:00
const { children } = this.props;
return (
<div
className="module-inline-notification-wrapper"
2020-09-14 19:51:27 +00:00
// eslint-disable-next-line jsx-a11y/no-noninteractive-tabindex
2019-11-07 21:36:16 +00:00
tabIndex={0}
ref={this.focusRef}
onFocus={this.handleFocus}
2019-11-07 21:36:16 +00:00
>
{children}
</div>
);
}
}