Skip to content

Sidebar

The Sidebar component provides a collapsible side panel for navigation, settings, or other content. It can be positioned on either side of the screen and supports both static and dynamic modes.

  • Metro UI Core
  • Metro UI DOM utilities
<!-- Basic sidebar with header and menu -->
<div data-role="sidebar">
<div class="sidebar-header">
<div class="avatar">
<img src="path/to/avatar.jpg">
</div>
<span class="title">John Doe</span>
<span class="subtitle">Web Developer</span>
</div>
<ul class="sidebar-menu">
<li class="active"><a href="#"><span class="icon mif-home"></span>Home</a></li>
<li><a href="#"><span class="icon mif-user"></span>Profile</a></li>
<li><a href="#"><span class="icon mif-cog"></span>Settings</a></li>
<li class="divider"></li>
<li><a href="#"><span class="icon mif-exit"></span>Logout</a></li>
</ul>
</div>
<!-- Sidebar with toggle button -->
<button class="button" id="sidebar-toggle">Toggle Sidebar</button>
<div data-role="sidebar" data-toggle="#sidebar-toggle">
<!-- Sidebar content -->
</div>
<div data-role="sidebar">
<ul class="sidebar-menu">
<li class="group-title">Main Navigation</li>
<li><a href="#"><span class="icon mif-home"></span>Home</a></li>
<li><a href="#"><span class="icon mif-user"></span>Profile</a></li>
<li class="group-title">Settings</li>
<li><a href="#"><span class="icon mif-cog"></span>General</a></li>
<li><a href="#"><span class="icon mif-bell"></span>Notifications</a></li>
</ul>
</div>
<div data-role="sidebar">
<ul class="sidebar-menu">
<li><a href="#"><span class="icon mif-home"></span>Home<span class="hotkey">Alt+H</span></a></li>
<li><a href="#"><span class="icon mif-user"></span>Profile<span class="hotkey">Alt+P</span></a></li>
</ul>
</div>
ParameterDefaultDescription
menuScrollbarfalseWhether to show scrollbars in the menu
sidebarDeferred0Delay before initialization (in milliseconds)
position”left”Position of the sidebar (“left” or “right”)
shiftnullSelector for elements to be shifted when the sidebar opens
staticShiftnullSelector for elements to be shifted when the sidebar is in static mode
togglenullSelector for the element that toggles the sidebar
duration300Duration of the animation when opening/closing (in milliseconds)
staticnullBreakpoint at which the sidebar becomes static (e.g., “lg”, “md”)
closeOutsidetrueWhether to close the sidebar when clicking outside
onOpenMetro.noopCallback function when the sidebar opens
onCloseMetro.noopCallback function when the sidebar closes
onToggleMetro.noopCallback function when the sidebar toggles
onStaticSetMetro.noopCallback function when the sidebar enters static mode
onStaticLossMetro.noopCallback function when the sidebar exits static mode
onSidebarCreateMetro.noopCallback function when the sidebar is created

Configure global default options for all sidebar components.

Metro.sidebarSetup({
position: "right",
closeOutside: false
});

These methods can be called on a sidebar instance:

// Get sidebar instance
var sidebar = Metro.getPlugin('#mySidebar', 'sidebar');
// Call methods
sidebar.open();
sidebar.close();
sidebar.toggle();
var isOpen = sidebar.isOpen();

These methods can be called directly on the Metro.sidebar object:

// Open sidebar
Metro.sidebar.open('#mySidebar');
// Close sidebar
Metro.sidebar.close('#mySidebar');
// Toggle sidebar
Metro.sidebar.toggle('#mySidebar');
// Check if sidebar is open
var isOpen = Metro.sidebar.isOpen('#mySidebar');
<div data-role="sidebar">
<!-- Optional header -->
<div class="sidebar-header">
<div class="avatar"><!-- Avatar content --></div>
<span class="title"><!-- Title --></span>
<span class="subtitle"><!-- Subtitle --></span>
<span class="action"><!-- Action button --></span>
</div>
<!-- Menu -->
<ul class="sidebar-menu">
<li class="group-title"><!-- Group title --></li>
<li class="active">
<a href="#">
<span class="icon"><!-- Icon --></span>
<!-- Menu item text -->
<span class="hotkey"><!-- Hotkey --></span>
</a>
</li>
<li class="divider"></li>
<!-- More menu items -->
</ul>
<!-- Optional content -->
<div class="sidebar-content">
<!-- Additional content -->
</div>
</div>

The Sidebar component can be styled using the following CSS variables:

VariableLight ModeDark ModeDescription
—sidebar-width240px240pxWidth of the sidebar
—sidebar-background#ffffff#2b2d30Background color of the sidebar
—sidebar-color#191919#dbdfe7Text color of the sidebar
—sidebar-header-background#f8f8f8#1e1f22Background color of the header
—sidebar-header-color#191919#dbdfe7Text color of the header
—sidebar-border-color#e8e8e8#343538Color of the sidebar borders
—sidebar-avatar-background#ffffff#2b2d30Background color of the avatar
—sidebar-avatar-color#191919#ffffffText color of the avatar
—sidebar-avatar-border-color#fff#fffBorder color of the avatar
—sidebar-menu-background#ffffff#2b2d30Background color of the menu
—sidebar-menu-color#191919#dbdfe7Text color of the menu
—sidebar-item-backgroundinheritinheritBackground color of menu items
—sidebar-item-color#191919#dbdfe7Text color of menu items
—sidebar-item-background-hover#cecece#1e1f22Background color of menu items on hover
—sidebar-item-color-hover#0a0a0a#ffffffText color of menu items on hover
—sidebar-item-marker-color#468cff#468cffColor of the active item marker
—sidebar-item-color-disabled#ccc#43454aText color of disabled menu items
/* Custom sidebar styling */
:root {
--sidebar-width: 280px;
--sidebar-background: #f0f7ff;
--sidebar-color: #0d47a1;
--sidebar-header-background: #1976d2;
--sidebar-header-color: #ffffff;
--sidebar-item-background-hover: #bbdefb;
--sidebar-item-color-hover: #0d47a1;
--sidebar-item-marker-color: #ff4081;
}
  • .sidebar - Main sidebar container
  • .sidebar-header - Header section of the sidebar
  • .sidebar-menu - Menu container
  • .sidebar-content - Additional content container
  • .on-left - Positions the sidebar on the left side (default)
  • .on-right - Positions the sidebar on the right side
  • .open - Applied when the sidebar is open
  • .static - Applied when the sidebar is in static mode
  • .active - Indicates the active menu item
  • .disabled - Disables a menu item
  • .divider - Creates a divider line between menu items
  • .group-title - Creates a group title for categorizing menu items
  • .avatar - Container for user avatar in the header
  • .title - Title text in the header
  • .subtitle - Subtitle text in the header
  • .action - Action button in the header
  • .icon - Icon element in menu items
  • .hotkey - Hotkey indicator in menu items

The Sidebar component automatically adjusts its colors when the .dark-side class is applied to a parent element, providing appropriate styling for dark mode interfaces.

The sidebar menu should use proper semantic HTML for navigation:

<nav data-role="sidebar">
<ul class="sidebar-menu" role="menu">
<li role="menuitem"><a href="#">Home</a></li>
<!-- More menu items -->
</ul>
</nav>
  1. Use the sidebar for primary navigation or secondary controls
  2. Keep the sidebar content focused and organized
  3. Use group titles to categorize menu items
  4. Consider using static mode for larger screens and collapsible mode for mobile
  5. Provide a clear toggle button when the sidebar is hidden
  6. Use consistent icons and labels for better usability
  7. Ensure the sidebar doesn’t obscure important content when open
  8. Use the dark mode support to ensure the sidebar looks appropriate in both light and dark interfaces