In addition to built-in options, you can equip your context menu with custom options.
To see the context menu, right-click on a cell:
import{ HotTable }from'@handsontable/react';import{ ContextMenu }from'handsontable/plugins/contextMenu';import{ registerAllModules }from'handsontable/registry';import'handsontable/dist/handsontable.full.min.css';// register Handsontable's modulesregisterAllModules();exportconstExampleComponent=()=>{return(<div>
<HotTable
id="hot"
data={[['A1','B1','C1','D1','E1'],['A2','B2','C2','D2','E2'],['A3','B3','C3','D3','E3'],['A4','B4','C4','D4','E4'],['A5','B5','C5','D5','E5'],]}
colHeaders={true}
height="auto"
contextMenu={{items:{'row_above':{name:'Insert row above this one (custom name)'},'row_below':{},'separator': ContextMenu.SEPARATOR,'clear_custom':{name:'Clear all cells (custom)',callback:function(){this.clear();}}}}}
licenseKey="non-commercial-and-evaluation"
/>
</div>)}
Context menu with a fully custom configuration
This example shows how to:
Add common callback for all options
Dynamically disable option
Set custom text for predefined option
Add own custom option
Add callback for specific option
To see the context menu, right-click on a cell:
import{ HotTable }from'@handsontable/react';import{ registerAllModules }from'handsontable/registry';import'handsontable/dist/handsontable.full.min.css';// register Handsontable's modulesregisterAllModules();exportconstExampleComponent=()=>{return(<HotTable
data={[['','Tesla','Nissan','Toyota','Honda','Mazda','Ford'],['2017',10,11,12,13,15,16],['2018',10,11,12,13,15,16],['2019',10,11,12,13,15,16],['2020',10,11,12,13,15,16],['2021',10,11,12,13,15,16]]}
rowHeaders={true}
colHeaders={true}
licenseKey="non-commercial-and-evaluation"
height="auto"
contextMenu={{callback(key, selection, clickEvent){// Common callback for all options
console.log(key, selection, clickEvent);},items:{row_above:{disabled(){// `disabled` can be a boolean or a function// Disable option when first row was clickedreturnthis.getSelectedLast()[0]===0;// `this` === hot}},// A separator line can also be added like this:// 'sp1': { name: '---------' }// and the key has to be uniquesp1:'---------',row_below:{name:'Click to add row below'// Set custom text for predefined option},about:{// Own custom optionname(){// `name` can be a string or a functionreturn'<b>Custom option</b>';// Name can contain HTML},hidden(){// `hidden` can be a boolean or a function// Hide the option when the first column was clickedreturnthis.getSelectedLast()[1]==0;// `this` === hot},callback(key, selection, clickEvent){// Callback for specific optionsetTimeout(()=>{alert('Hello world!');// Fire alert after menu close (with timeout)},0);}},colors:{// Own custom optionname:'Colors...',submenu:{// Custom option with submenu of itemsitems:[{// Key must be in the form 'parent_key:child_key'key:'colors:red',name:'Red',callback(key, selection, clickEvent){setTimeout(()=>{alert('You clicked red!');},0);}},{key:'colors:green',name:'Green'},{key:'colors:blue',name:'Blue'}]}},credits:{// Own custom property// Custom rendered element in the context menurenderer(hot, wrapper, row, col, prop, itemValue){const elem = document.createElement('marquee');
elem.style.cssText ='background: lightgray;';
elem.textContent ='Brought to you by...';return elem;},disableSelection:true,// Prevent mouseoever from highlighting the item for selectionisCommand:false// Prevent clicks from executing command and closing the menu}}}}/>);};
Related keyboard shortcuts
Windows
macOS
Action
Excel
Sheets
Arrow keys
Arrow keys
Move one available menu item up, down, left, or right
✓
✓
Page Up
Page Up
Move to the first visible item of the context menu or submenu
✓
✗
Page Down
Page Down
Move to the last visible item of the context menu or submenu