Skip to content

Commit

Permalink
Merge pull request jupyterlab#7877 from saulshanabrook/right-click-op…
Browse files Browse the repository at this point in the history
…tion

Add support for disabling JupyterLab's context menu
  • Loading branch information
vidartf committed Mar 18, 2020
2 parents 0627c3b + b99a10e commit d87de68
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
16 changes: 16 additions & 0 deletions docs/source/developer/extension_points.rst
Expand Up @@ -125,6 +125,22 @@ right-clicks on a DOM element matching ``.jp-Notebook`` (that is to say, a noteb
The selector can be any valid CSS selector, and may target your own UI elements, or existing ones.
A list of CSS selectors currently used by context menu commands is given in :ref:`css-selectors`.
If you don't want JupyterLab's custom context menu to appear for your element, because you have
your own right click behavior that you want to trigger, you can add the `data-jp-suppress-context-menu` data attribute
to any node to have it and its children not trigger it.
For example, if you are building a custom React element, it would look like this:
.. code::
function MyElement(props: {}) {
return (
<div data-jp-suppress-context-menu>
<p>Hi</p>
<p onContextMenu={() => {console.log("right clicked")}}>There</p>
</div>
)
}
.. _copy_shareable_link:
Expand Down
12 changes: 11 additions & 1 deletion packages/application/src/frontend.ts
Expand Up @@ -156,7 +156,10 @@ export abstract class JupyterFrontEnd<
*/
protected evtContextMenu(event: MouseEvent): void {
this._contextMenuEvent = event;
if (event.shiftKey) {
if (
event.shiftKey ||
Private.suppressContextMenu(event.target as HTMLElement)
) {
return;
}
const opened = this.contextMenu.open(event);
Expand Down Expand Up @@ -355,4 +358,11 @@ namespace Private {
* ersatz command.
*/
export const CONTEXT_MENU_INFO = '__internal:context-menu-info';

/**
* Returns whether the element is itself, or a child of, an element with the `jp-suppress-context-menu` data attribute.
*/
export function suppressContextMenu(element: HTMLElement): boolean {
return element.closest('[data-jp-suppress-context-menu]') !== null;
}
}

0 comments on commit d87de68

Please sign in to comment.