Skip to content

Commit 1aa9f7f

Browse files
authored
fix: prevent the contextmenu event of Notification (#670)
1 parent 0d42c0f commit 1aa9f7f

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

src/controller/notification.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export interface INotificationController extends Partial<Controller> {
2727
* Toggle the Notifications visibility
2828
*/
2929
toggleNotifications(): void;
30+
onContextMenu?: (e: MouseEvent) => void;
3031
}
3132

3233
@singleton()

src/workbench/notification/__tests__/notificationPane.test.tsx

+19
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,23 @@ describe('Test NotificationPane Component', () => {
101101
fireEvent.click(testDom!);
102102
});
103103
});
104+
105+
test('Listen to the contextmenu event', () => {
106+
expectFnCalled((fn) => {
107+
const { getByText } = render(
108+
<NotificationPane
109+
id="test"
110+
onContextMenu={fn}
111+
data={[
112+
{
113+
id: 'test',
114+
value: 'Test Notification',
115+
},
116+
]}
117+
/>
118+
);
119+
const testDom = getByText('Test Notification');
120+
fireEvent.contextMenu(testDom);
121+
});
122+
});
104123
});

src/workbench/notification/notificationPane/index.tsx

+19-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { memo } from 'react';
1+
import React, { memo, useRef, useEffect } from 'react';
22
import { INotification } from 'mo/model/notification';
33
import { INotificationController } from 'mo/controller/notification';
44
import {
@@ -35,19 +35,37 @@ export function NotificationPane(
3535
showNotifications,
3636
onActionBarClick,
3737
onCloseNotification,
38+
onContextMenu,
3839
} = props;
3940
const hasNotifications = data.length > 0;
4041
const title = hasNotifications
4142
? localize('notification.title', 'notifications')
4243
: localize('notification.title.no', 'no new notifications');
4344
const display = showNotifications ? 'block' : 'none';
45+
const wrapper = useRef<HTMLDivElement>(null);
46+
47+
// Prevent the contextmenu event of the upper element from being triggered by mistake.
48+
useEffect(() => {
49+
const handleRightClick = function (e: MouseEvent) {
50+
e.stopPropagation();
51+
onContextMenu?.(e);
52+
};
53+
wrapper.current?.addEventListener('contextmenu', handleRightClick);
54+
return () => {
55+
wrapper.current?.removeEventListener(
56+
'contextmenu',
57+
handleRightClick
58+
);
59+
};
60+
}, [onContextMenu]);
4461

4562
return (
4663
<div
4764
className={classNames(
4865
defaultNotificationClassName,
4966
shadowClassName
5067
)}
68+
ref={wrapper}
5169
style={{ display }}
5270
>
5371
<header className={notificationHeaderClassName}>

0 commit comments

Comments
 (0)