-
Notifications
You must be signed in to change notification settings - Fork 298
Expand file tree
/
Copy pathReactionSelectorWithButton.tsx
More file actions
56 lines (53 loc) · 1.97 KB
/
ReactionSelectorWithButton.tsx
File metadata and controls
56 lines (53 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import type { ElementRef } from 'react';
import React, { useRef } from 'react';
import { ReactionSelector as DefaultReactionSelector } from './ReactionSelector';
import { DialogAnchor, useDialog, useDialogIsOpen } from '../Dialog';
import {
useComponentContext,
useMessageContext,
useTranslationContext,
} from '../../context';
import type { IconProps } from '../../types/types';
type ReactionSelectorWithButtonProps = {
/* Custom component rendering the icon used in a button invoking reactions selector for a given message. */
ReactionIcon: React.ComponentType<IconProps>;
};
/**
* Internal convenience component - not to be exported. It just groups the button and the dialog anchor and thus prevents
* cluttering the parent component.
*/
export const ReactionSelectorWithButton = ({
ReactionIcon,
}: ReactionSelectorWithButtonProps) => {
const { t } = useTranslationContext('ReactionSelectorWithButton');
const { isMyMessage, message, threadList } = useMessageContext('MessageOptions');
const { ReactionSelector = DefaultReactionSelector } =
useComponentContext('MessageOptions');
const buttonRef = useRef<ElementRef<'button'>>(null);
const dialogIdNamespace = threadList ? '-thread-' : '';
const dialogId = `reaction-selector${dialogIdNamespace}--${message.id}`;
const dialog = useDialog({ id: dialogId });
const dialogIsOpen = useDialogIsOpen(dialogId);
return (
<>
<DialogAnchor
id={dialogId}
placement={isMyMessage() ? 'top-end' : 'top-start'}
referenceElement={buttonRef.current}
trapFocus
>
<ReactionSelector />
</DialogAnchor>
<button
aria-expanded={dialogIsOpen}
aria-label={t('aria/Open Reaction Selector')}
className='str-chat__message-reactions-button'
data-testid='message-reaction-action'
onClick={() => dialog?.toggle()}
ref={buttonRef}
>
<ReactionIcon className='str-chat__message-action-icon' />
</button>
</>
);
};