Ribbon contextual tabs
3 Feb 202616 minutes to read
Ribbon Contextual Tabs are special tabs that appear only when a specific object or context is selected, such as a table, image, or chart. They provide users with a set of tools relevant only to the selected item. These tabs can host all built-in and custom Ribbon items to perform specific actions.
Visible tabs
You can control the initial visibility of a contextual tab by setting the visible property to true or false.
Adding contextual tabs
You can utilize the contextualTabs property to add contextual tabs in the Ribbon. This property accepts an array of Ribbon tabs, where you can add multiple tabs based on your needs.
import { Ribbon, RibbonTabModel, RibbonItemType, RibbonItemSize, RibbonContextualTab, RibbonContextualTabSettingsModel } from "@syncfusion/ej2-ribbon";
Ribbon.Inject(RibbonContextualTab);
let ribbonTabs: RibbonTabModel[] = [{
header: "Home",
groups: [{
showLauncherIcon: true,
header: "Clipboard",
groupIconCss: "e-icons e-paste",
id: "clipboardGroup",
collections: [{
items: [
{ type: RibbonItemType.Button, buttonSettings: { iconCss: "e-icons e-cut", content: "Cut", } },
{ type: RibbonItemType.Button, buttonSettings: { iconCss: "e-icons e-copy", content: "Copy" } },
{ type: RibbonItemType.Button, buttonSettings: { iconCss: "e-icons e-format-painter", content: "Format Painter" } }
]
}]
}]
}];
let contextualTabs: RibbonContextualTabSettingsModel = {
visible: true,
tabs: [{
id: "ShapeFormat",
header: "Shape Format",
groups: [{
showLauncherIcon: true,
header: "Text decoration",
collections: [{
items: [
{ type: RibbonItemType.Button, buttonSettings: { iconCss: "e-icons e-text-header", content: "Text Header" } },
{ type: RibbonItemType.Button, buttonSettings: { iconCss: "e-icons e-text-wrap", content: "Text Wrap" } },
{ type: RibbonItemType.Button, buttonSettings: { iconCss: "e-icons e-text-annotation", content: "Text Annotation" } },
]
}]
},{
header: "Accessibility",
collections: [{
items: [
{ type: RibbonItemType.Button, allowedSizes: RibbonItemSize.Large, buttonSettings: { iconCss: "e-icons e-text-alternative", content: "Alt Text" } },
]
}]
},{
showLauncherIcon: true,
header: "Arrange",
collections: [{
items: [
{ type: RibbonItemType.Button, buttonSettings: { iconCss: "e-icons e-bring-forward", content: "Bring Forward" } },
{ type: RibbonItemType.Button, buttonSettings: { iconCss: "e-icons e-send-backward", content: "Send Backward" } },
{ type: RibbonItemType.Button, buttonSettings: { iconCss: "e-icons e-show-hide-panel", content: "Selection Pane" } },
]
}]
}]
}]
};
let ribbon: Ribbon = new Ribbon({
tabs: ribbonTabs,
contextualTabs: [contextualTabs]
});
ribbon.appendTo("#ribbon");<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js"></script>
<title>Essential JS 2 - Ribbon</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
<meta name="description" content="Essential JS 2" />
<meta name="author" content="Syncfusion" />
<link href="https://cdn.syncfusion.com/ej2/32.2.3/ej2-base/styles/tailwind3.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/32.2.3/ej2-buttons/styles/tailwind3.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/32.2.3/ej2-popups/styles/tailwind3.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/32.2.3/ej2-splitbuttons/styles/tailwind3.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/32.2.3/ej2-inputs/styles/tailwind3.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/32.2.3/ej2-lists/styles/tailwind3.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/32.2.3/ej2-dropdowns/styles/tailwind3.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/32.2.3/ej2-navigations/styles/tailwind3.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/32.2.3/ej2-ribbon/styles/tailwind3.css" rel="stylesheet" />
<!--system js reference and configuration-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<script src="systemjs.config.js"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id='loader'>LOADING....</div>
<div id='container'>
<div id="ribbon"></div>
</div>
</body>
</html>Selected tabs
The isSelectedproperty determines which contextual tab is active upon initialization. Setting this property to true makes the corresponding tab the currently selected one.
import { Ribbon, RibbonTabModel, RibbonItemType, RibbonContextualTab, RibbonContextualTabSettingsModel } from "@syncfusion/ej2-ribbon";
Ribbon.Inject(RibbonContextualTab);
let ribbonTabs: RibbonTabModel[] = [{
header: "Home",
groups: [{
showLauncherIcon: true,
header: "Clipboard",
groupIconCss: "e-icons e-paste",
id: "clipboardGroup",
collections: [{
items: [
{ type: RibbonItemType.Button, buttonSettings: { iconCss: "e-icons e-cut", content: "Cut", } },
{ type: RibbonItemType.Button, buttonSettings: { iconCss: "e-icons e-copy", content: "Copy" } },
{ type: RibbonItemType.Button, buttonSettings: { iconCss: "e-icons e-format-painter", content: "Format Painter" } }
]
}]
}]
}];
let contextualTabs: RibbonContextualTabSettingsModel = {
visible: true,
isSelected: true,
tabs: [{
header: "Styles",
groups: [{
showLauncherIcon: true,
header: "Style",
collections: [{
items: [
{ type: RibbonItemType.Button, buttonSettings: { iconCss: "e-icons e-style", content: "Style" } },
{ type: RibbonItemType.Button, buttonSettings: { iconCss: "e-icons e-font-name", content: "Text Box" } },
{ type: RibbonItemType.Button, buttonSettings: { iconCss: "e-icons e-paint-bucket", content: "Paint" } },
]
}]
}]
}]
};
let ribbon: Ribbon = new Ribbon({
tabs: ribbonTabs,
contextualTabs: [contextualTabs]
});
ribbon.appendTo("#ribbon");<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js"></script>
<title>Essential JS 2 - Ribbon</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
<meta name="description" content="Essential JS 2" />
<meta name="author" content="Syncfusion" />
<link href="https://cdn.syncfusion.com/ej2/32.2.3/ej2-base/styles/tailwind3.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/32.2.3/ej2-buttons/styles/tailwind3.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/32.2.3/ej2-popups/styles/tailwind3.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/32.2.3/ej2-splitbuttons/styles/tailwind3.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/32.2.3/ej2-inputs/styles/tailwind3.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/32.2.3/ej2-lists/styles/tailwind3.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/32.2.3/ej2-dropdowns/styles/tailwind3.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/32.2.3/ej2-navigations/styles/tailwind3.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/32.2.3/ej2-ribbon/styles/tailwind3.css" rel="stylesheet" />
<!--system js reference and configuration-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<script src="systemjs.config.js"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id='loader'>LOADING....</div>
<div id='container'>
<div id="ribbon"></div>
</div>
</body>
</html>Methods
The Ribbon provides methods to dynamically manage the visibility of contextual tabs after the component has been initialized.
Show Tab
The showTab method makes a specific contextual tab visible in the Ribbon.
Hide Tab
The hideTab method hides a specific contextual tab in the Ribbon.
import { Ribbon, RibbonTabModel, RibbonItemType, RibbonContextualTab, RibbonContextualTabSettingsModel } from "@syncfusion/ej2-ribbon";
Ribbon.Inject(RibbonContextualTab);
let ribbonTabs: RibbonTabModel[] = [{
header: "Home",
groups: [{
showLauncherIcon: true,
header: "Clipboard",
groupIconCss: "e-icons e-paste",
id: "clipboardGroup",
collections: [{
items: [
{ type: RibbonItemType.Button, buttonSettings: { iconCss: "e-icons e-cut", content: "Cut", } },
{ type: RibbonItemType.Button, buttonSettings: { iconCss: "e-icons e-copy", content: "Copy" } },
{ type: RibbonItemType.Button, buttonSettings: { iconCss: "e-icons e-format-painter", content: "Format Painter" } }
]
}]
}]
}];
let contextualTabs: RibbonContextualTabSettingsModel = {
tabs: [{
id: "ArrangeView",
header: "Arrange & View",
groups: [{
showLauncherIcon: true,
header: "Arrange",
collections: [{
items: [
{ type: RibbonItemType.Button, buttonSettings: { iconCss: "e-icons e-bring-forward", content: "Bring Forward" } },
{ type: RibbonItemType.Button, buttonSettings: { iconCss: "e-icons e-send-backward", content: "Send Backward" } },
{ type: RibbonItemType.Button, buttonSettings: { iconCss: "e-icons e-show-hide-panel", content: "Selection Pane" } },
]
}]
}]
}]
};
let ribbon: Ribbon = new Ribbon({
tabs: ribbonTabs,
contextualTabs: [contextualTabs]
});
ribbon.appendTo("#ribbon");
(document.getElementById('show-contextual') as HTMLElement).onclick = () => {
ribbon.showTab('ArrangeView', true);
}
(document.getElementById('hide-contextual') as HTMLElement).onclick = () => {
ribbon.hideTab('ArrangeView', true);
}<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js"></script>
<title>Essential JS 2 - Ribbon</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
<meta name="description" content="Essential JS 2" />
<meta name="author" content="Syncfusion" />
<link href="https://cdn.syncfusion.com/ej2/32.2.3/ej2-base/styles/tailwind3.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/32.2.3/ej2-buttons/styles/tailwind3.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/32.2.3/ej2-popups/styles/tailwind3.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/32.2.3/ej2-splitbuttons/styles/tailwind3.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/32.2.3/ej2-inputs/styles/tailwind3.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/32.2.3/ej2-lists/styles/tailwind3.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/32.2.3/ej2-dropdowns/styles/tailwind3.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/32.2.3/ej2-navigations/styles/tailwind3.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/32.2.3/ej2-ribbon/styles/tailwind3.css" rel="stylesheet" />
<!--system js reference and configuration-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<script src="systemjs.config.js"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id='loader'>LOADING....</div>
<div id='container'>
<button class="e-btn" id="show-contextual"> Show tab </button>
<button class="e-btn" id="hide-contextual"> Hide tab </button>
<!--Element to render the Timeline control-->
<div id="ribbon" style="margin-top: 20px;"></div>
</div>
</body>
</html>