Skip to content

Commit

Permalink
fix: prevent the contextmenu event of Notification (#670)
Browse files Browse the repository at this point in the history
  • Loading branch information
kiwiwong authored Feb 28, 2022
1 parent 0d42c0f commit 1aa9f7f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/controller/notification.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export interface INotificationController extends Partial<Controller> {
* Toggle the Notifications visibility
*/
toggleNotifications(): void;
onContextMenu?: (e: MouseEvent) => void;
}

@singleton()
Expand Down
19 changes: 19 additions & 0 deletions src/workbench/notification/__tests__/notificationPane.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,23 @@ describe('Test NotificationPane Component', () => {
fireEvent.click(testDom!);
});
});

test('Listen to the contextmenu event', () => {
expectFnCalled((fn) => {
const { getByText } = render(
<NotificationPane
id="test"
onContextMenu={fn}
data={[
{
id: 'test',
value: 'Test Notification',
},
]}
/>
);
const testDom = getByText('Test Notification');
fireEvent.contextMenu(testDom);
});
});
});
20 changes: 19 additions & 1 deletion src/workbench/notification/notificationPane/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { memo } from 'react';
import React, { memo, useRef, useEffect } from 'react';
import { INotification } from 'mo/model/notification';
import { INotificationController } from 'mo/controller/notification';
import {
Expand Down Expand Up @@ -35,19 +35,37 @@ export function NotificationPane(
showNotifications,
onActionBarClick,
onCloseNotification,
onContextMenu,
} = props;
const hasNotifications = data.length > 0;
const title = hasNotifications
? localize('notification.title', 'notifications')
: localize('notification.title.no', 'no new notifications');
const display = showNotifications ? 'block' : 'none';
const wrapper = useRef<HTMLDivElement>(null);

// Prevent the contextmenu event of the upper element from being triggered by mistake.
useEffect(() => {
const handleRightClick = function (e: MouseEvent) {
e.stopPropagation();
onContextMenu?.(e);
};
wrapper.current?.addEventListener('contextmenu', handleRightClick);
return () => {
wrapper.current?.removeEventListener(
'contextmenu',
handleRightClick
);
};
}, [onContextMenu]);

return (
<div
className={classNames(
defaultNotificationClassName,
shadowClassName
)}
ref={wrapper}
style={{ display }}
>
<header className={notificationHeaderClassName}>
Expand Down

0 comments on commit 1aa9f7f

Please sign in to comment.