How to underline a character in a React Context Menu item
31 Aug 20264 minutes to read
To underline a specific character in a menu item’s text, use the beforeItemRender event. This technique is commonly used to visually indicate keyboard shortcut characters within menu items.
The underlining is achieved by adding <u> tags around the target character and assigning the modified string to the list item’s innerHTML during rendering. The handler typically checks args.item.text to scope the modification to a specific item.
Example
import { enableRipple } from '@syncfusion/ej2-base';
import { ContextMenuComponent } from '@syncfusion/ej2-react-navigations';
import * as React from 'react';
import * as ReactDom from 'react-dom';
enableRipple(true);
function App() {
let menuItems = [
{
text: 'Cut'
},
{
text: 'Copy'
},
{
text: 'Paste'
}
];
function itemBeforeEvent(args) {
if (args.item.text === 'Copy') {
args.element.innerHTML = '<u>C</u>opy';
}
}
return (<div className="container">
<div id='target'>Right click / Touch hold to open the ContextMenu</div>
<ContextMenuComponent id='contextmenu' target='#target' items={menuItems} beforeItemRender={itemBeforeEvent}/>
</div>);
}
export default App;
ReactDom.render(<App />, document.getElementById('element'));import { enableRipple } from '@syncfusion/ej2-base';
import { ContextMenuComponent, MenuEventArgs, MenuItemModel } from '@syncfusion/ej2-react-navigations';
import * as React from 'react';
import * as ReactDom from 'react-dom';
enableRipple(true);
function App() {
let menuItems: MenuItemModel[] = [
{
text: 'Cut'
},
{
text: 'Copy'
},
{
text: 'Paste'
}];
function itemBeforeEvent(args: MenuEventArgs) {
if (args.item.text === 'Copy') {
args.element.innerHTML = '<u>C</u>opy';
}
}
return (
<div className="container">
<div id='target'>Right click / Touch hold to open the ContextMenu</div>
<ContextMenuComponent id='contextmenu' target='#target' items={menuItems} beforeItemRender={itemBeforeEvent}/>
</div>
);
}
export default App;
ReactDom.render(<App />,document.getElementById('element'));