Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/components/Dialog/components/ContextMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,7 @@ export type ContextMenuItemProps = ComponentProps<'button'>;
export type ContextMenuItemComponent = ComponentType<ContextMenuItemProps>;

type ContextMenuContextValue = {
anchorReferenceElement?: HTMLElement | null;
closeMenu: () => void;
openSubmenu: (params: ContextMenuOpenSubmenuParams) => void;
returnToParentMenu: () => void;
Expand Down Expand Up @@ -464,6 +465,7 @@ type ContextMenuAnchorProps = Partial<
export type ContextMenuProps = ContextMenuBaseProps & ContextMenuAnchorProps;

export type ContextMenuContentProps = ContextMenuBaseProps & {
anchorReferenceElement?: HTMLElement | null;
transitionDirection?: 'forward' | 'backward';
};

Expand All @@ -475,6 +477,7 @@ export type ContextMenuContentProps = ContextMenuBaseProps & {
* handling from `ContextMenu`.
*/
export function ContextMenuContent({
anchorReferenceElement,
backLabel = 'Back',
children,
className,
Expand Down Expand Up @@ -547,7 +550,9 @@ export function ContextMenuContent({
}, [transitionDirection, menuStack.length]);

return (
<ContextMenuContext.Provider value={{ closeMenu, openSubmenu, returnToParentMenu }}>
<ContextMenuContext.Provider
value={{ anchorReferenceElement, closeMenu, openSubmenu, returnToParentMenu }}
>
<ContextMenuRoot
className={clsx(className, activeMenu.menuClassName)}
data-str-chat-enable-animations={enableAnimations}
Expand Down Expand Up @@ -705,6 +710,7 @@ export const ContextMenu = (props: ContextMenuProps) => {

const content = (
<ContextMenuContentComponent
anchorReferenceElement={isAnchored ? referenceElement : undefined}
{...menuProps}
key={`context-menu-content-${contentResetToken}`}
onMenuLevelChange={handleMenuLevelChange}
Expand Down
4 changes: 4 additions & 0 deletions src/components/Message/styling/Message.scss
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@
--str-chat-message-options-size: calc(3 * var(--str-chat__message-options-button-size));
padding-inline: var(--str-chat__message-composer-padding);

@media (max-width: 767px) {
--str-chat-message-options-size: var(--str-chat__message-options-button-size);
}

.str-chat__message-bubble {
width: min(100%, var(--str-chat__message-max-width));
max-width: var(--str-chat__message-max-width);
Expand Down
74 changes: 73 additions & 1 deletion src/components/MessageActions/MessageActions.defaults.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { useMessageComposerController } from '../MessageComposer/hooks/useMessag
import { savePreEditSnapshot } from '../MessageComposer/preEditSnapshot';
import { useNotificationApi } from '../Notifications';
import { useMessageReminder } from '../Message/hooks/useMessageReminder';
import { ReactionSelector as DefaultReactionSelector } from '../Reactions';
import { ReactionSelectorWithButton } from '../Reactions/ReactionSelectorWithButton';
import {
useChatContext,
Expand All @@ -40,6 +41,7 @@ import {
import { RemindMeSubmenu, RemindMeSubmenuHeader } from './RemindMeSubmenu';
import {
ContextMenuButton,
DialogAnchor,
useContextMenuContext,
useDialogIsOpen,
useDialogOnNearestManager,
Expand Down Expand Up @@ -67,6 +69,61 @@ const getNotificationError = (error: unknown): Error | undefined => {

const DefaultMessageActionComponents = {
dropdown: {
React() {
const { ReactionSelector = DefaultReactionSelector } = useComponentContext();
const { anchorReferenceElement } = useContextMenuContext();
const { isMyMessage, message, threadList } = useMessageContext();
const { t } = useTranslationContext();
const [referenceElement, setReferenceElement] = useState<HTMLElement | null>(null);
const dialogId = `${DefaultReactionSelector.getDialogId({
messageId: message.id,
threadList,
})}-dropdown`;
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={referenceElement}
trapFocus
updatePositionOnContentResize
>
<ReactionSelector dialogId={dialogId} />
</DialogAnchor>
<ContextMenuButton
aria-expanded={dialogIsOpen}
aria-label={t('aria/Open Reaction Selector')}
className={clsx(
msgActionsBoxButtonClassName,
'str-chat__message-actions-list-item-button--react',
)}
data-testid='dropdown-react-action'
Icon={IconEmoji}
onClick={(event) => {
if (dialogIsOpen) {
dialog.close();
return;
}
setReferenceElement(
anchorReferenceElement instanceof HTMLElement
? anchorReferenceElement
: event.currentTarget,
);
dialog.open();
}}
>
{t('Add reaction')}
</ContextMenuButton>
</>
);
},
ThreadReply() {
const { closeMenu } = useContextMenuContext();
const { handleOpenThread } = useMessageContext();
Expand Down Expand Up @@ -586,13 +643,20 @@ const DefaultMessageActionComponents = {
// eslint-disable-next-line react/display-name
DropdownToggle: forwardRef<HTMLButtonElement>((_, ref) => {
const { t } = useTranslationContext();
const { message } = useMessageContext();
const { message, threadList } = useMessageContext();
const dropdownDialogIsOpen = useDialogIsOpen(
MessageActions.getDialogId({ messageId: message.id }),
);
const { dialog } = useDialogOnNearestManager({
id: MessageActions.getDialogId({ messageId: message.id }),
});
const reactionSelectorDialogId = DefaultReactionSelector.getDialogId({
messageId: message.id,
threadList,
});
const { dialog: dropdownReactionSelectorDialog } = useDialogOnNearestManager({
id: `${reactionSelectorDialogId}-dropdown`,
});

return (
<QuickMessageActionsButton
Expand All @@ -602,6 +666,9 @@ const DefaultMessageActionComponents = {
className='str-chat__message-actions-box-button'
data-testid='message-actions-toggle-button'
onClick={() => {
// Close dropdown-anchored reaction selectors before toggling actions menu
// to avoid stale selector re-anchoring.
dropdownReactionSelectorDialog?.close();
dialog?.toggle();
}}
ref={ref}
Expand Down Expand Up @@ -646,6 +713,11 @@ export const defaultMessageActionSet: MessageActionSetItem[] = [
placement: 'quick',
type: 'react',
},
{
Component: DefaultMessageActionComponents.dropdown.React,
placement: 'dropdown',
type: 'react',
},
{
Component: DefaultMessageActionComponents.dropdown.ThreadReply,
placement: 'dropdown',
Expand Down
11 changes: 10 additions & 1 deletion src/components/MessageActions/MessageActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export const MessageActions: MessageActionsInterface = ({
messageId: message.id,
threadList,
});
const dropdownReactionSelectorDialogId = `${reactionSelectorDialogId}-dropdown`;
const { dialog, dialogManager } = useDialogOnNearestManager({
id: messageActionsDialogId,
});
Expand All @@ -92,6 +93,10 @@ export const MessageActions: MessageActionsInterface = ({
reactionSelectorDialogId,
dialogManager?.id,
);
const dropdownReactionSelectorDialogIsOpen = useDialogIsOpen(
dropdownReactionSelectorDialogId,
dialogManager?.id,
);

// do not render anything if total action count is zero
if (dropdownActionSet.length + quickActionSet.length === 0) {
Expand All @@ -102,7 +107,9 @@ export const MessageActions: MessageActionsInterface = ({
<div
className={clsx('str-chat__message-options', {
'str-chat__message-options--active':
messageActionsDialogIsOpen || reactionSelectorDialogIsOpen,
messageActionsDialogIsOpen ||
reactionSelectorDialogIsOpen ||
dropdownReactionSelectorDialogIsOpen,
})}
>
{quickDropdownToggleAction && dropdownActionSet.length > 0 && (
Expand All @@ -112,6 +119,8 @@ export const MessageActions: MessageActionsInterface = ({
<ContextMenuComponent
backLabel={t('Back')}
className={clsx('str-chat__message-actions-box', {
'str-chat__message-actions-box--hidden':
dropdownReactionSelectorDialogIsOpen,
'str-chat__message-actions-box--open': messageActionsDialogIsOpen,
})}
dialogManagerId={dialogManager?.id}
Expand Down
86 changes: 86 additions & 0 deletions src/components/MessageActions/__tests__/MessageActions.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ const MESSAGE_ACTIONS_HOST_TEST_ID = 'message-actions-host';
const dialogOverlayTestId = 'str-chat__dialog-overlay';
const threadActionTestId = 'thread-action';
const reactionActionTestId = 'message-reaction-action';
const dropdownReactionActionTestId = 'dropdown-react-action';
const reactionSelectorTestId = 'reaction-selector';

const defaultMessageContextValue = {
Expand Down Expand Up @@ -557,6 +558,22 @@ describe('<MessageActions />', () => {

expect(queryByTestId(reactionActionTestId)).not.toBeInTheDocument();
});

it('should display reaction action at the top of dropdown list', async () => {
const { container } = await renderMessageActions({
channelStateOpts: {
channelCapabilities: { 'send-reaction': true },
},
});
await toggleOpenMessageActions();

const contextMenuButtons = container.querySelectorAll(
'.str-chat__context-menu__button',
);

expect(contextMenuButtons[0]).toHaveTextContent('Add reaction');
expect(screen.getByTestId(dropdownReactionActionTestId)).toBeInTheDocument();
});
});

describe('Reaction selector', () => {
Expand Down Expand Up @@ -634,6 +651,75 @@ describe('<MessageActions />', () => {

expect(actionsHost).toHaveClass('str-chat__message-options--active');
});

it('should render ReactionSelector when dropdown reaction action is clicked', async () => {
await renderMessageActions({
channelStateOpts: {
channelCapabilities: { 'send-reaction': true },
},
});
await toggleOpenMessageActions();

await act(async () => {
await fireEvent.click(screen.getByTestId(dropdownReactionActionTestId));
});

const reactionSelector = screen.getByTestId(reactionSelectorTestId);
expect(reactionSelector).toBeInTheDocument();
expect(reactionSelector.closest('.str-chat__context-menu')).toBeNull();
const messageActionsMenu = document.querySelector('.str-chat__message-actions-box');
expect(messageActionsMenu).toBeInTheDocument();
expect(messageActionsMenu).toHaveClass('str-chat__message-actions-box--hidden');
});

it('should close ReactionSelector when dropdown reaction action is clicked while selector is open', async () => {
await renderMessageActions({
channelStateOpts: {
channelCapabilities: { 'send-reaction': true },
},
});
await toggleOpenMessageActions();

await act(async () => {
await fireEvent.click(screen.getByTestId(dropdownReactionActionTestId));
});

expect(screen.getByTestId(reactionSelectorTestId)).toBeInTheDocument();

await act(async () => {
await fireEvent.click(screen.getByTestId(dropdownReactionActionTestId));
});

await waitFor(() => {
expect(screen.queryByTestId(reactionSelectorTestId)).not.toBeInTheDocument();
});

const messageActionsMenu = document.querySelector('.str-chat__message-actions-box');
expect(messageActionsMenu).not.toHaveClass('str-chat__message-actions-box--hidden');
});

it('should close ReactionSelector when message actions toggle is clicked while dropdown selector is open', async () => {
await renderMessageActions({
channelStateOpts: {
channelCapabilities: { 'send-reaction': true },
},
});
await toggleOpenMessageActions();

await act(async () => {
await fireEvent.click(screen.getByTestId(dropdownReactionActionTestId));
});

expect(screen.getByTestId(reactionSelectorTestId)).toBeInTheDocument();

await act(async () => {
await fireEvent.click(screen.getByTestId(TOGGLE_ACTIONS_BUTTON_TEST_ID));
});

await waitFor(() => {
expect(screen.queryByTestId(reactionSelectorTestId)).not.toBeInTheDocument();
});
});
});

describe('Mark as unread action', () => {
Expand Down
24 changes: 24 additions & 0 deletions src/components/MessageActions/styling/MessageActions.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

.str-chat__message-actions-box {
min-width: 180px;

&.str-chat__message-actions-box--hidden {
visibility: hidden;
pointer-events: none;
}
}

.str-chat__message-options {
Expand Down Expand Up @@ -41,3 +46,22 @@
position: relative;
}
}

.str-chat
.str-chat__message-actions-list-item-button.str-chat__message-actions-list-item-button--react {
display: none;
}

@media (max-width: 767px) {
.str-chat .str-chat__message-options {
.str-chat__button.str-chat__message-reactions-button,
.str-chat__button.str-chat__message-reply-in-thread-button {
display: none;
}
}

.str-chat
.str-chat__message-actions-list-item-button.str-chat__message-actions-list-item-button--react {
display: flex;
}
}
Loading
Loading