React ContextMenu - Open and Close the Context Menu

User Interaction

By default, the ContextMenu appears when a user right-clicks the target element. This action corresponds to the dxcontextmenu event. If you need the ContextMenu to appear when another event is raised, assign the event's name to the showEvent property. This property can accept several events at once as well as an object.

jQuery
JavaScript
var contextMenuItems = [
    { text: 'Zoom In', icon: 'plus' },
    { text: 'Share', icon: 'message' },
    { text: 'Download', icon: 'download' }
];

$(function() {
    $("#contextMenuContainer").dxContextMenu({
        items: contextMenuItems,
        target: "#someElement",
        showEvent: "dxcontextmenu dblclick"
    });
});
Angular
HTML
TypeScript
<dx-context-menu
    [items]="contextMenuItems"
    target="#someElement"
    showEvent="dxcontextmenu dblclick">
</dx-context-menu>
import { DxContextMenuModule } from 'devextreme-angular';
// ...
export class AppComponent {
    contextMenuItems = [
        { text: 'Zoom In', icon: 'plus' },
        { text: 'Share', icon: 'message' },
        { text: 'Download', icon: 'download' }
    ];
}
@NgModule({
    imports: [
        // ...
        DxContextMenuModule
    ],
    // ...
})
Vue
App.vue
<template>
    <DxContextMenu
        :items="contextMenuItems"
        target="#someElement"
        showEvent="dxcontextmenu dblclick"
    />
</template>
<script>
import 'devextreme/dist/css/dx.light.css';

import DxContextMenu from 'devextreme-vue/context-menu';

export default {
    components: {
        DxContextMenu
    },
    data() {
        return {
            contextMenuItems: [
                { text: 'Zoom In', icon: 'plus' },
                { text: 'Share', icon: 'message' },
                { text: 'Download', icon: 'download' }
            ]
        };
    }
};
</script>
React
App.js
import React from 'react';
import 'devextreme/dist/css/dx.light.css';

import { ContextMenu } from 'devextreme-react/context-menu';

const contextMenuItems = [
    { text: 'Zoom In', icon: 'plus' },
    { text: 'Share', icon: 'message' },
    { text: 'Download', icon: 'download' }
];

class App extends React.Component {
    render() {
        return (
            <ContextMenu
                items={contextMenuItems}
                target="#someElement"
                showEvent="dxcontextmenu dblclick"
            />
        );
    }
}

export default App;

The ContextMenu is closed when a user clicks anywhere outside it. You can redefine this behavior or turn it off completely using the closeOnOutsideClick property. For example, the ContextMenu in the following code will not be closed until its own target element is clicked.

jQuery
JavaScript
var contextMenuItems = [
    { text: 'Zoom In', icon: 'plus' },
    { text: 'Share', icon: 'message' },
    { text: 'Download', icon: 'download' }
];

$(function() {
    $("#contextMenuContainer").dxContextMenu({
        items: contextMenuItems,
        target: "#someElement",
        closeOnOutsideClick: function(e){
            return e.target === $("#someElement").get()[0];
        }
    });
});
Angular
HTML
TypeScript
<dx-context-menu
    [items]="contextMenuItems"
    target="#someElement"
    (closeOnOutsideClick)="closeOnOutsideClick($event)">
</dx-context-menu>
import { DxContextMenuModule } from 'devextreme-angular';
// ...
export class AppComponent {
    contextMenuItems = [
        { text: 'Zoom In', icon: 'plus' },
        { text: 'Share', icon: 'message' },
        { text: 'Download', icon: 'download' }
    ];
    closeOnOutsideClick(e) {
        return e.target === document.getElementById('someElement');
    }
}
@NgModule({
    imports: [
        // ...
        DxContextMenuModule
    ],
    // ...
})
Vue
App.vue
<template>
    <DxContextMenu
        :items="contextMenuItems"
        target="#someElement"
        :close-on-outside-click="closeOnOutsideClick"
    />
</template>
<script>
import 'devextreme/dist/css/dx.light.css';

import DxContextMenu from 'devextreme-vue/context-menu';

export default {
    components: {
        DxContextMenu
    },
    data() {
        return {
            contextMenuItems: [
                { text: 'Zoom In', icon: 'plus' },
                { text: 'Share', icon: 'message' },
                { text: 'Download', icon: 'download' }
            ]
        };
    },
    methods: {
        closeOnOutsideClick(e) {
            return e.target === document.getElementById('someElement');
        }
    }
};
</script>
React
App.js
import React from 'react';
import 'devextreme/dist/css/dx.light.css';

import { ContextMenu } from 'devextreme-react/context-menu';

const contextMenuItems = [
    { text: 'Zoom In', icon: 'plus' },
    { text: 'Share', icon: 'message' },
    { text: 'Download', icon: 'download' }
];

class App extends React.Component {
    render() {
        return (
            <ContextMenu
                items={contextMenuItems}
                target="#someElement"
                closeOnOutsideClick={this.closeOnOutsideClick}
            />
        );
    }

    closeOnOutsideClick(e) {
        return e.target === document.getElementById('someElement');
    }
}

export default App;

API

The ContextMenu UI component is not supposed to be opened programmatically, but rare scenarios demand this. This article shows how to do this using the Button UI component. This choice is made for purely demonstrational purposes, and you can do the same with another UI component following the same guidelines.

See Also
jQuery

To open or close the ContextMenu from code, call the show() or hide() method. You can do the same thing using the toggle(showing) method. Pass true or false to this method to open or close the context menu, respectively:

JavaScript
var contextMenuItems = [
    { text: 'Zoom In', icon: 'plus' },
    { text: 'Share', icon: 'message' },
    { text: 'Download', icon: 'download' }
];

$(function() {
    $("#buttonContainer").dxButton({
        text: "Open the context menu",
        onClick: function() {
            $("#contextMenuContainer").dxContextMenu("show");
            // === or ===
            $("#contextMenuContainer").dxContextMenu("toggle", true);
        }
    });

    $("#contextMenuContainer").dxContextMenu({
        items: contextMenuItems,
        target: "#someElement"
    });
});
Angular

To open or close the ContextMenu from code, bind the visible property of the ContextMenu to a component property. After that, change this component property, and the context menu will be opened or closed:

HTML
TypeScript
<dx-button
    text="Open the context menu"
    (onClick)="isContextMenuVisible = true">
</dx-button>
<dx-context-menu
    [items]="contextMenuItems"
    target="#someElement"
    [(visible)]="isContextMenuVisible">
</dx-context-menu>
import { DxContextMenuModule, DxButtonModule } from 'devextreme-angular';
// ...
export class AppComponent {
    contextMenuItems = [
        { text: 'Zoom In', icon: 'plus' },
        { text: 'Share', icon: 'message' },
        { text: 'Download', icon: 'download' }
    ];
    isContextMenuVisible = false;
}
@NgModule({
     imports: [
         // ...
         DxContextMenuModule,
         DxButtonModule
     ],
     // ...
 })
Vue

To open or close the ContextMenu from code, bind the visible property of the ContextMenu to a component property. After that, change this component property, and the context menu will be opened or closed:

App.vue
<template>
    <div>
        <DxButton
            text="Open the context menu"
            @click="openContextMenu"
        />
        <DxContextMenu
            :items="contextMenuItems"
            target="#someElement"
            v-model:visible="isContextMenuVisible"
        />
    </div>
</template>
<script>
import 'devextreme/dist/css/dx.light.css';

import DxButton from 'devextreme-vue/button';
import DxContextMenu from 'devextreme-vue/context-menu';

export default {
    components: {
        DxContextMenu,
        DxButton
    },
    data() {
        return {
            contextMenuItems: [
                { text: 'Zoom In', icon: 'plus' },
                { text: 'Share', icon: 'message' },
                { text: 'Download', icon: 'download' }
            ],
            isContextMenuVisible: false
        };
    },
    methods: {
        openContextMenu() {
            this.isContextMenuVisible = true;
        }
    }
};
</script>
React

To open or close the ContextMenu from code, bind the visible property of the ContextMenu to a state property. After that, change this state property, and the context menu will be opened or closed:

App.js
import React from 'react';
import 'devextreme/dist/css/dx.light.css';

import { Button } from 'devextreme-react/button';
import { ContextMenu } from 'devextreme-react/context-menu';

const contextMenuItems = [
    { text: 'Zoom In', icon: 'plus' },
    { text: 'Share', icon: 'message' },
    { text: 'Download', icon: 'download' }
];

class App extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
           isContextMenuVisible: false 
        };
    }

    render() {
        return (
            <div>
                <Button
                    text="Open the context menu"
                    onClick={this.openContextMenu}
                />
                <ContextMenu
                    items={contextMenuItems}
                    target="#someElement"
                    visible={this.state.isContextMenuVisible}
                    onOptionChanged={this.handleOptionChange}
                />
            </div>
        );
    }

    openContextMenu = () => {
        this.setState({
          isContextMenuVisible: true;  
        })
    }

    handleOptionChange = (e) => {
        if(e.fullName === 'visible') {
            this.setState({
                isContextMenuVisible: e.value
            });
        }
    }
}

export default App;

When invoking the context menu from code, you may want to specify its position.

jQuery
JavaScript
$(function() {
    $("#contextMenuContainer").dxContextMenu({
        // ...
        target: "#someElement",
        position: {
            my: "top right",
            at: "top left"
        }
    });
});
Angular
HTML
TypeScript
<dx-context-menu ...
    target="#someElement"
    [position]="contextMenuPosition">
</dx-context-menu>
import { ..., ViewChild } from "@angular/core";
import { DxContextMenuModule, DxButtonModule } from "devextreme-angular";
// ...
export class AppComponent {
    contextMenuPosition = {
        my: 'top right', 
        at: 'top left' 
    };
}
@NgModule({
     imports: [
         // ...
         DxContextMenuModule,
         DxButtonModule
     ],
     // ...
 })
Vue
App.vue
<template>
    <DxContextMenu ...
        target="#someElement"
        :position="contextMenuPosition"
    />
</template>
<script>
import 'devextreme/dist/css/dx.light.css';

import DxContextMenu from 'devextreme-vue/context-menu';

export default {
    components: {
        DxContextMenu
    },
    data() {
        return {
            contextMenuPosition: {
                my: 'top right', 
                at: 'top left' 
            }
        }
    }
};
</script>
React
App.js
import React from 'react';
import 'devextreme/dist/css/dx.light.css';

import { ContextMenu } from 'devextreme-react/context-menu';

const contextMenuPostion = { 
    my: 'top right', 
    at: 'top left' 
};

class App extends React.Component {
    render() {
        return (
            <ContextMenu ...
                target="#someElement"
                position={contextMenuPosition}
            />
        );
    }
}

export default App;

This configuration of the position property reads as follows: "place my top right corner at the top left corner of the target element".

Events

To execute certain commands before or after the ContextMenu was opened/closed, handle the showing, shown, hiding or hidden event. If the event handling function is not going to be changed during the lifetime of the UI component, assign it to the corresponding onEventName property when you configure the UI component.

jQuery
JavaScript
$(function() {
    $("#contextMenuContainer").dxContextMenu({
        // ...
        onShowing: function(e) {
            // Handler of the 'showing' event
        },
        onShown: function(e) {
            // Handler of the 'shown' event
        },
        onHiding: function(e) {
            // Handler of the 'hiding' event
        },
        onHidden: function(e) {
            // Handler of the 'hidden' event
        }
    });
});
Angular
HTML
TypeScript
<dx-context-menu ...
    (onShowing)="handleShowingEvent($event)"
    (onShown)="handleShownEvent($event)"
    (onHiding)="handleHidingEvent($event)"
    (onHidden)="handleHiddenEvent($event)">
</dx-context-menu>
import { DxContextMenuModule } from "devextreme-angular";
// ...
export class AppComponent {
    // ...
    handleShowingEvent(e) {
        // Handler of the 'showing' event
    }
    handleShownEvent(e) {
        // Handler of the 'shown' event
    }
    handleHidingEvent(e) {
        // Handler of the 'hiding' event
    }
    handleHiddenEvent(e) {
        // Handler of the 'hidden' event
    }
}
@NgModule({
     imports: [
         // ...
         DxContextMenuModule
     ],
     // ...
 })
Vue
App.vue
<template>
    <DxContextMenu ...
        @showing="handleShowingEvent"
        @shown="handleShownEvent"
        @hiding="handleHidingEvent"
        @hidden="handleHiddenEvent"
    />
</template>
<script>
import 'devextreme/dist/css/dx.light.css';

import DxContextMenu from 'devextreme-vue/context-menu';

export default {
    components: {
        DxContextMenu
    },
    methods: {
        handleShowingEvent(e) {
            // Handler of the 'showing' event
        }
        handleShownEvent(e) {
            // Handler of the 'shown' event
        }
        handleHidingEvent(e) {
            // Handler of the 'hiding' event
        }
        handleHiddenEvent(e) {
            // Handler of the 'hidden' event
        }
    }
};
</script>
React
App.js
import React from 'react';
import 'devextreme/dist/css/dx.light.css';

import { ContextMenu } from 'devextreme-react/context-menu';

class App extends React.Component {
    render() {
        return (
            <ContextMenu
                onShowing={this.handleShowingEvent}
                onShown={this.handleShownEvent}
                onHiding={this.handleHidingEvent}
                onHidden={this.handleHiddenEvent}
            />
        );
    }

    handleShowingEvent(e) {
        // Handler of the 'showing' event
    }
    handleShownEvent(e) {
        // Handler of the 'shown' event
    }
    handleHidingEvent(e) {
        // Handler of the 'hiding' event
    }
    handleHiddenEvent(e) {
        // Handler of the 'hidden' event
    }
}

export default App;

If you are going to change event handlers at runtime, or if you need to attach several handlers to a single event, subscribe to the events using the on(eventName, eventHandler) method. This approach is more typical of jQuery.

JavaScript
var hiddenEventHandler1 = function(e) {
    // First handler of the "hidden" event
};

var hiddenEventHandler2 = function(e) {
    // Second handler of the "hidden" event
};

$("#contextMenuContainer").dxContextMenu("instance")
    .on("hidden", hiddenEventHandler1)
    .on("hidden", hiddenEventHandler2);
See Also