Sidebar

Create a hidden sidebar with a special component.

About

New in 4.2.7

Often you can see the component that hides on the side of the content and contains a menu and is displayed on demand. Such a component is called a sidebar. You can create it with Metro 4 sidebar component.

What is Lorem Ipsum?

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.

Create sidebar

To create sidebar you must write valid html structure and to behavior with sidebar, add role data-role="sidebar". The sidebar contains two main blocks: header and menu.


                    <aside class="sidebar" data-role="sidebar">
                        <div class="sidebar-header">
                            ...
                        </div>
                        <ul class="sidebar-menu">
                            ...
                        </ul>
                    </aside>
                

Sidebar header

Sidebar header can contains next elements: .sub-action, .avatar, .title and .subtitle.


                    <div class="sidebar-header">
                        <a href="/" class="fg-white sub-action"
                           onclick="Metro.sidebar.close('#sb1'); return false;">
                            <span class="mif-arrow-left mif-2x"></span>
                        </a>
                        <div class="avatar">
                            <img data-role="gravatar" data-email="sergey@pimenov.com.ua">
                        </div>
                        <span class="title">Metro 4 Components Library</span>
                        <span class="subtitle"> 2018 © Sergey Pimenov</span>
                    </div>
                
Sidebar header background

By default, sidebar header have a white background color. To change it you must use color classes, add custom style or to, set image background, use special attribute data-image="...".


                    <div class="sidebar-header bg-red">
                        ...
                    </div>

                    ...or...

                    <div class="sidebar-header" style="background-color: red;">
                        ...
                    </div>

                    ...or...

                    <div class="sidebar-header" data-image="images/sb-bg-1.jpg">
                        ...
                    </div>
                

Sidebar menu

Sidebar menu contains links to site pages or other any targets. The menu is a list with links.


                    <ul class="sidebar-menu">
                        <li><a><span class="mif-home icon"></span>Home</a></li>
                        <li><a><span class="mif-books icon"></span>Guide</a></li>
                        <li><a><span class="mif-files-empty icon"></span>Examples</a></li>
                        <li class="divider"></li>
                        <li><a><span class="mif-images icon"></span>Icons</a></li>
                    </ul>
                
Sidebar menu - grouping items

You can grouping menu items. To mark group, use <li> with classes .group-title and .divider


                    <ul class="sidebar-menu">
                        <li class="group-title">Group one</li>
                        <li><a>Item 1</a></li>
                        <li><a>Item 2</a></li>
                        <li><a>Item 3</a></li>
                        <li class="divider"></li>
                        <li><a>Item 4</a></li>
                        <li><a>Item 5</a></li>

                        <li class="group-title">Group two</li>
                        <li><a>Item 6</a></li>
                        <li><a>Item 7</a></li>
                    </ul>
                

Sidebar toggle

You can set sidebar toggle with special attribute data-toggle="...". Value for this attribute must be a valid element selector id or class. For sidebar toggle Metro 4 bind click event, who toggle sidebar state.


                    <aside class="sidebar pos-absolute z-2" data-role="sidebar" data-toggle="#sidebar-toggle-2">
                        <div class="sidebar-header" data-image="images/sb-bg-1.jpg">
                            <div class="avatar">
                                <img data-role="gravatar" data-email="sergey@pimenov.com.ua">
                            </div>
                            <span class="title fg-white">Metro 4 Components Library</span>
                            <span class="subtitle fg-white"> 2018 © Sergey Pimenov</span>
                        </div>
                        <ul class="sidebar-menu">
                            <li><a><span class="mif-home icon"></span>Home</a></li>
                            <li><a><span class="mif-books icon"></span>Guide</a></li>
                            <li><a><span class="mif-files-empty icon"></span>Examples</a></li>
                            <li class="divider"></li>
                            <li><a><span class="mif-images icon"></span>Icons</a></li>
                        </ul>
                    </aside>
                    <div class="h-100 p-ab">
                        <button class="button square pos-absolute pos-top-right"
                                id="sidebar-toggle-2">
                            <span class="mif-menu"></span>
                        </button>
                    </div>
                

Shifting content

You can use attribute data-shift="..." to create shifting content effect.

What is Lorem Ipsum?

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.


                    <aside class="sidebar pos-absolute z-2"
                           data-role="sidebar"
                           data-toggle="#sidebar-toggle-3"
                           id="sb3"
                           data-shift=".shifted-content">
                        <div class="sidebar-header" data-image="images/sb-bg-1.jpg">
                            <div class="avatar">
                                <img data-role="gravatar" data-email="sergey@pimenov.com.ua">
                            </div>
                            <span class="title fg-white">Metro 4 Components Library</span>
                            <span class="subtitle fg-white"> 2018 © Sergey Pimenov</span>
                        </div>
                        <ul class="sidebar-menu">
                            <li><a><span class="mif-home icon"></span>Home</a></li>
                            <li><a><span class="mif-books icon"></span>Guide</a></li>
                            <li><a><span class="mif-files-empty icon"></span>Examples</a></li>
                            <li class="divider"></li>
                            <li><a><span class="mif-images icon"></span>Icons</a></li>
                        </ul>
                    </aside>
                    <div class="shifted-content h-100 p-ab">
                        <div class="app-bar pos-absolute bg-red z-1" data-role="appbar">
                            <button class="app-bar-item c-pointer" id="sidebar-toggle-3">
                                <span class="mif-menu fg-white"></span>
                            </button>
                        </div>

                        <div class="h-100 p-4">
                            <p class="h1">What is Lorem Ipsum?</p>
                            <p>
                                Lorem Ipsum is simply dummy text of the printing and typesetting industry.
                                Lorem Ipsum has been the industry's standard dummy text ever since the 1500s
                                ...
                            </p>
                        </div>
                    </div>
                

Static sidebar

You can set attribute data-static="..." with one of media points md, lg, xl, xxl to set rule when sidebar will be opened always. Also you can define elements, who must shifted when sidebar receive static state with attribute data-static-shift="...".

What is Lorem Ipsum?

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s.


                    <aside class="sidebar pos-absolute z-2"
                           data-role="sidebar"
                           data-toggle="#sidebar-toggle-4"
                           id="sb4"
                           data-shift=".shifted-content-2"
                           data-static-shift=".shifted-content-2"
                           data-static="md">
                        <div class="sidebar-header" data-image="images/sb-bg-1.jpg">
                            <div class="avatar">
                                <img data-role="gravatar" data-email="sergey@pimenov.com.ua">
                            </div>
                            <span class="title fg-white">Metro 4 Components Library</span>
                            <span class="subtitle fg-white"> 2018 © Sergey Pimenov</span>
                        </div>
                        <ul class="sidebar-menu">
                            <li><a><span class="mif-home icon"></span>Home</a></li>
                            <li><a><span class="mif-books icon"></span>Guide</a></li>
                            <li><a><span class="mif-files-empty icon"></span>Examples</a></li>
                            <li class="divider"></li>
                            <li><a><span class="mif-images icon"></span>Icons</a></li>
                        </ul>
                    </aside>
                    <div class="shifted-content-2 h-100 p-ab">
                        <div class="appbar pos-absolute bg-red z-1" data-role="appbar">
                            <button class="app-bar-item c-pointer" id="sidebar-toggle-4">
                                <span class="mif-menu fg-white"></span>
                            </button>
                        </div>

                        <div class="h-100 p-4">
                            <p class="h1">What is Lorem Ipsum?</p>
                            <p>
                                Lorem Ipsum is simply dummy text of the printing and typesetting industry.
                                Lorem Ipsum has been the industry's standard dummy text ever since the 1500s...
                            </p>
                        </div>
                    </div>
                

Options

You can set any options for sidebar:

Options Data-* Default Description
shift data-shift Set elements selectors who will be shifted
staticShift data-static-shift Set elements selectors who will be shifted when sidebar in static mode
toggle data-toggle Set sidebar toggle element
static data-static Set media point for static mode
menuItemClick data-menu-item-click true If true, sidebar will be closed when user click on sidebar menu item

Events

When sidebar works, his generate events. You can use a callback for these events to interact with a stepper.

Event Data-* Desc
onOpen(sb) data-on-open Fired when sidebar open
onClose(sb) data-on-close Fired when sidebar close
onToggle(sb) data-on-toggle Fired when sidebar toggle state
onStaticSet(sb) data-on-static-set Fired when sidebar receive static state
onStaticLoss(sb) data-on-static-loss Fired when sidebar loss static state
onSidebarCreate(element) data-on-sidebar-create Fired when sidebar created

Methods

To interact with component you can use methods:

Method Desc
open() Open sidebar
close() Close sidebar
toggle() Toggle sidebar state
isOpen() Use this method to check sidebar state

Sidebar object

Metro 4 contains special object Metro.sidebar to manipulate Sidebars. This object contains next methods:

  • open(el) - open Sidebar
  • close(el) - close Sidebar
  • toggle(el) - toggle Sidebar state
  • isSidebar(el) - check if element is Sidebar instance
  • isOpen(el) - check if sidebar is open