-
Notifications
You must be signed in to change notification settings - Fork 298
Expand file tree
/
Copy pathReactionSelectorWithButton.tsx
More file actions
63 lines (59 loc) · 2.21 KB
/
ReactionSelectorWithButton.tsx
File metadata and controls
63 lines (59 loc) · 2.21 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
57
58
59
60
61
62
63
import type { ComponentRef } from 'react';
import React, { useRef } from 'react';
import { ReactionSelector as DefaultReactionSelector } from './ReactionSelector';
import { DialogAnchor, useDialogIsOpen, useDialogOnNearestManager } from '../Dialog';
import {
useComponentContext,
useMessageContext,
useTranslationContext,
} from '../../context';
import type { IconProps } from '../../types/types';
import { QuickMessageActionsButton } from '../MessageActions';
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<ComponentRef<'button'>>(null);
const dialogId = DefaultReactionSelector.getDialogId({
messageId: message.id,
threadList,
});
const { dialog, dialogManager } = useDialogOnNearestManager({ id: dialogId });
const dialogIsOpen = useDialogIsOpen(dialogId, dialogManager?.id);
return (
<>
<DialogAnchor
dialogManagerId={dialogManager?.id}
id={dialogId}
offset={8}
placement={isMyMessage() ? 'top-end' : 'top-start'}
referenceElement={buttonRef.current}
trapFocus
updatePositionOnContentResize
>
<ReactionSelector />
</DialogAnchor>
<QuickMessageActionsButton
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' />
</QuickMessageActionsButton>
</>
);
};