Skip to content

Commit db6f2a3

Browse files
committed
fix: multiple message menu can be opened on mobile
1 parent 46ef749 commit db6f2a3

File tree

3 files changed

+22
-23
lines changed

3 files changed

+22
-23
lines changed

projects/stream-chat-angular/src/lib/message-actions.service.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,13 +186,18 @@ export class MessageActionsService<
186186
* By default the [`MessageComponent`](/chat/docs/sdk/angular/components/MessageComponent/) will display the [`MessageActionsBoxComponent`](/chat/docs/sdk/angular/components/MessageActionsBoxComponent/). You can override that behavior by providing your own event handler.
187187
*/
188188
customActionClickHandler?: (details: MessageActionsClickDetails<T>) => void;
189+
/**
190+
* @internal
191+
*/
192+
messageMenuOpenedFor$ = new BehaviorSubject<string | undefined>(undefined);
189193
private hasDisplayedClipboardWarning = false;
190194

191195
constructor(
192196
private chatClientService: ChatClientService,
193197
private notificationService: NotificationService,
194198
private channelService: ChannelService
195199
) {
200+
this.messageMenuOpenedFor$.subscribe(console.log);
196201
combineLatest([
197202
this.messageToEdit$,
198203
this.channelService.activeChannel$,

projects/stream-chat-angular/src/lib/message/message.component.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<div
2-
#container
32
data-testid="message-container"
43
class="str-chat__message-simple str-chat__message str-chat__message--{{
54
message?.type

projects/stream-chat-angular/src/lib/message/message.component.ts

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import {
2828
CustomMetadataContext,
2929
MessageTextContext,
3030
} from '../types';
31-
import { fromEvent, Observable, Subscription, take } from 'rxjs';
31+
import { Observable, Subscription, take } from 'rxjs';
3232
import { CustomTemplatesService } from '../custom-templates.service';
3333
import { listUsers } from '../list-users';
3434
import { DateParserService } from '../date-parser.service';
@@ -112,12 +112,10 @@ export class MessageComponent
112112
messageMenuFloat!: NgxFloatUiContentComponent;
113113
@ViewChild('messageTextElement') messageTextElement?: ElementRef<HTMLElement>;
114114
@ViewChild('messageBubble') messageBubble?: ElementRef<HTMLElement>;
115-
@ViewChild('container') messageContainer?: ElementRef<HTMLElement>;
116115
private showMessageMenuTimeout?: ReturnType<typeof setTimeout>;
117116
private shouldPreventMessageMenuClose = false;
118117
private _visibleMessageActionsCount = 0;
119118
private channelMemberCount?: number;
120-
private touchOutsideMessageSubscriptions: Subscription[] = [];
121119

122120
constructor(
123121
private chatClientService: ChatClientService,
@@ -292,18 +290,27 @@ export class MessageComponent
292290
this.registerMenuTriggerEventHandlers();
293291
});
294292
}
293+
this.subscriptions.push(
294+
this.messageActionsService.messageMenuOpenedFor$.subscribe((id) => {
295+
if (this.message && this.message.id === id) {
296+
if (!this.areMessageOptionsOpen) {
297+
this.messageMenuTrigger?.show();
298+
}
299+
} else if (
300+
(id === undefined || this.message?.id !== id) &&
301+
this.areMessageOptionsOpen
302+
) {
303+
this.messageMenuTrigger?.hide();
304+
}
305+
})
306+
);
295307
}
296308

297309
ngOnDestroy(): void {
298310
this.subscriptions.forEach((s) => s.unsubscribe());
299-
this.messageMenuHidden();
300-
}
301-
302-
messageMenuHidden() {
303311
if (this.showMessageMenuTimeout) {
304312
clearTimeout(this.showMessageMenuTimeout);
305313
}
306-
this.touchOutsideMessageSubscriptions.forEach((s) => s.unsubscribe());
307314
}
308315

309316
mousePushedDown(event: MouseEvent) {
@@ -551,20 +558,8 @@ export class MessageComponent
551558
)
552559
(document.activeElement as HTMLInputElement).blur();
553560
this.messageMenuTrigger.show();
554-
// click event is handled by ngx-float-ui SDK, but long tap won't trigger click handler
555-
this.touchOutsideMessageSubscriptions.push(
556-
fromEvent(document, 'touchstart').subscribe((event) => {
557-
if (
558-
event.target === this.messageContainer?.nativeElement ||
559-
this.messageContainer?.nativeElement.contains(
560-
event.target as Node
561-
)
562-
) {
563-
return;
564-
}
565-
this.messageMenuTrigger.hide();
566-
this.messageMenuHidden();
567-
})
561+
this.messageActionsService.messageMenuOpenedFor$.next(
562+
this.message?.id
568563
);
569564
}
570565
if (this.isViewInited) {

0 commit comments

Comments
 (0)