Getting started in EJ2 TypeScript Sidebar control

17 Dec 202424 minutes to read

This section briefly explains how to create a simple Sidebar component, and configure it in TypeScript using Essential JS 2 quickstart seed repository.

This application is integrated with the webpack.config.js configuration and uses the latest version of the webpack-cli. It requires node v14.15.0 or higher. For more information about webpack and its features, refer to the webpack documentation.

Dependencies

The following list of dependencies are required to use the Sidebar component in your application.

|-- @syncfusion/ej2-navigations
  |-- @syncfusion/ej2-base
  |-- @syncfusion/ej2-build
  |-- @syncfusion/ej2-lists
  |-- @syncfusion/ej2-data
  |-- @syncfusion/ej2-inputs
    |-- @syncfusion/ej2-splitbuttons
  |-- @syncfusion/ej2-popups
    |-- @syncfusion/ej2-buttons

Set up development environment

Open the command prompt from the required directory, and run the following command to clone the Syncfusion JavaScript (Essential JS 2) quickstart project from GitHub.

git clone https://github.com/SyncfusionExamples/ej2-quickstart-webpack- ej2-quickstart

After cloning the application in the ej2-quickstart folder, run the following command line to navigate to the ej2-quickstart folder.

cd ej2-quickstart

Add Syncfusion JavaScript packages

Syncfusion JavaScript (Essential JS 2) packages are available on the npmjs.com public registry. You can install all Syncfusion JavaScript (Essential JS 2) controls in a single @syncfusion/ej2 package or individual packages for each control.

The quickstart application is preconfigured with the dependent @syncfusion/ej2 package in the ~/package.json file. Use the following command to install the dependent npm packages from the command prompt.

npm install

Import the Syncfusion CSS styles

To render the Sidebar component, need to import sidebar and its dependent component’s styles as given below in the ~/src/styles/styles.css file, as shown below:

@import "../../node_modules/@syncfusion/ej2-base/styles/material.css";
@import "../../node_modules/@syncfusion/ej2-navigations/styles/material.css";

Note: If you want to refer the combined component styles, please make use of our CRG (Custom Resource Generator) in your application.

Initialize Sidebar using HTML elements

A Sidebar can be initialized using any HTML element. Most probably the <aside> tag is used to render Sidebar as it contains secondary content aside from the main content. The immediate sibling element of the Sidebar will be considered as the main content.

To render the Sidebar, refer to the following structure of the HTML elements:

[src/index.html]

<!DOCTYPE html>
<html lang="en">

<head>
    <title>Essential JS 2 Sidebar</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 rel="shortcut icon" href="resources/favicon.ico" />
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
</head>

<body>
    <div id='container'>
        <aside id="default">
            <div class="title"> Sidebar </div>
        </aside>
        <!-- end of sidebar element -->
        <!-- main content declaration -->
        <div>
            <div class="title">Main content</div>
            <div class="sub-title"> Content goes here</div>
        </div>
    </div>
</body>

</html>
  • Import the Sidebar component to your app.ts file, and initialize it to the #default as follows:

[src/app/app.ts]

import { Sidebar } from '@syncfusion/ej2-navigations';
import { enableRipple } from '@syncfusion/ej2-base';
enableRipple(true);

let defaultSidebar: Sidebar = new Sidebar();
defaultSidebar.appendTo('#default');
    //end of sidebar initialization

Run the application

The Essential JS 2 quickstart application project is configured to compile and run the application in browser.
Use the following command to run the application.

npm start
import { Sidebar } from '@syncfusion/ej2-navigations';
import { enableRipple } from '@syncfusion/ej2-base';
enableRipple(true);

let defaultSidebar: Sidebar = new Sidebar({
});
defaultSidebar.appendTo('#default');
    //end of sidebar initialization
<!DOCTYPE html>
<html lang="en">

<head>
    <title>Essential JS 2 Sidebar</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name="description" content="Typescript UI Controls" />
    <meta name="author" content="Syncfusion" />
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/css/bootstrap.min.css" rel="stylesheet" />
    <link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-base/styles/material.css" rel="stylesheet" />
    <link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-buttons/styles/material.css" rel="stylesheet" />
    <link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-navigations/styles/material.css" rel="stylesheet" />
    <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='container'>
        <!-- sidebar element declaration -->
        <aside id="default">
            <div class="title"> Sidebar </div>
        </aside>
        <!-- end of sidebar element -->
        <!-- main content declaration -->
        <div>
            <div class="title">Main content</div>
            <div class="sub-title"> Content goes here</div>
        </div>
        <!--end of main content -->
    </div>

    <style>
        .header {
            width: 100%;
            height: 40px;
            font-size: 20px;
            line-height: 40px;
            font-weight: 500;
            background: #eee;
            display: inline-block;
        }
    
        #close,
        #close:hover,
        #close:active,
        #close:focus {
            /* csslint allow: adjoining-classes*/
            background: #fafafa;
            color: black;
        }
    
        .center-align {
            text-align: center;
            padding: 20px;
        }
    
        .title {
            text-align: center;
            font-size: 20px;
            padding: 15px;
        }
    
        .sub-title {
            text-align: center;
            font-size: 16px;
            padding: 10px;
        }
    
        .center {
            text-align: center;
            display: none;
            font-size: 13px;
            font-weight: 400;
            margin-top: 20px;
        }
    
        #wrapper {
            display: block;
        }
    
        .sb-content-tab .center {
            /* csslint allow: adjoining-classes*/
            display: block;
        }
    
        .sb-content-tab #wrapper {
            /* csslint allow: adjoining-classes*/
            display: none;
        }
    
        .right {
            float: right;
        }
    
        body {
            margin: 0;
        }
    
        #default {
            background-color: rgb(25, 118, 210);
            color: #ffffff;
        }
    
        .close-btn:hover {
            color: #fafafa;
        }
    
        .content-section {
            padding: 30px 10px 10px 20px;
        }
    
        .book .ej2-sample-frame {
            /* csslint allow: adjoining-classes */
            padding: 0;
        }
    </style>
</body>

</html>

Sidebar will, by default, fit the size of its content. There is also an option to set a certain width using the width property.

Enable backdrop

Enabling the showBackdrop in the Sidebar component will prevent the main content from user interactions, when it is in expanded state. Here, the DOM elements will not get changed. It only closes the main content by covering with a black backdrop overlay and focuses the Sidebar in the screen. Sidebar can be rendered with specific width by setting width property.

NOTE

To achieve a proper backdrop, we suggest that you create a wrapper parent container for the div block in which you intend to enable the backdrop. Set the class name of this parent container as the target for the Sidebar. Alternatively, you can place an empty div container after the target container.

The following example shows a Sidebar component with enabled backdrop.

import { Sidebar } from '@syncfusion/ej2-navigations';
import { enableRipple } from '@syncfusion/ej2-base';
enableRipple(true);

let defaultSidebar: Sidebar = new Sidebar({
    showBackdrop: true, type: "Push",width: '280px'
});
defaultSidebar.appendTo('#default-sidebar');
//end of Sidebar initialization

// Toggle(Open/Close) the Sidebar
const toggleButton = document.getElementById('toggle');
if (toggleButton) {
    toggleButton.onclick = (): void => {
        defaultSidebar.toggle();
    };
}

// Close the Sidebar
const closeButton = document.getElementById('close');
if (closeButton) {
    closeButton.onclick = (): void => {
        defaultSidebar.hide();
    };
}
<!DOCTYPE html>
<html lang="en">

<head>
    <title>Essential JS 2 Sidebar</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name="description" content="Typescript UI Controls" />
    <meta name="author" content="Syncfusion" />
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/css/bootstrap.min.css" rel="stylesheet" />
    <link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-base/styles/material.css" rel="stylesheet" />
    <link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-buttons/styles/material.css" rel="stylesheet" />
    <link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-lists/styles/material.css" rel="stylesheet" />
    <link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-navigations/styles/material.css" rel="stylesheet" />
    <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='container'>
        <!-- sidebar element declaration-->
        <aside id="default-sidebar">
            <div class="title"> Sidebar content</div>
            <div class="sub-title">
                Click the button to close the Sidebar.
            </div>
            <div class="center-align">
                <button id="close" class="e-btn close-btn">Close Sidebar</button>
            </div>
        </aside>
        <!-- end of sidebar element -->
        <!-- main content declaration -->
        <div>
            <div class="title">Main content</div>
            <div class="sub-title"> Click the button to open/close the Sidebar.</div>
            <div style="padding:20px" class="center-align">
                <button id="toggle" class="e-btn e-info">Toggle Sidebar</button>
            </div>
        </div>
        <!--end of main content -->
    </div>

    <style>
        .header {
            width: 100%;
            height: 40px;
            font-size: 20px;
            line-height: 40px;
            font-weight: 500;
            background: #eee;
            display: inline-block;
        }
    
        .center-align {
            text-align: center;
            padding: 20px;
        }
    
        #close,
        #close:hover,
        #close:active,
        #close:focus {
            /* csslint allow: adjoining-classes*/
            background: #fafafa;
            color: black
        }
    
        .title {
            text-align: center;
            font-size: 20px;
            padding: 15px;
        }
    
        .sub-title {
            text-align: center;
            font-size: 16px;
            padding: 10px;
        }
    
        .center {
            text-align: center;
            display: none;
            font-size: 13px;
            font-weight: 400;
            margin-top: 20px;
        }
    
        #wrapper {
            display: block;
        }
    
        .sb-content-tab .center {
            /* csslint allow: adjoining-classes*/
            display: block;
        }
    
        .sb-content-tab #wrapper {
            /* csslint allow: adjoining-classes*/
            display: none;
        }
    
        .right {
            float: right;
        }
    
        body {
            margin: 0;
        }
    
        #default-sidebar {
            background-color: rgb(25, 118, 210);
            color: #ffffff;
        }
    
        .book .ej2-sample-frame {
            /* csslint allow: adjoining-classes */
            padding: 0;
        }
    </style>
</body>

</html>

Position

Positioning the Sidebar to the right or left of the main content can be achieved by using the position property. If the position is not set, the Sidebar will expand from the left to the body element. enablePersistence will persist the component’s state between page reloads. change event will be triggered when the state(expand/collapse) of the component is changed.

In the following sample, the position of the Sidebar can be changed using the radio buttons in the main content.

import { Sidebar } from '@syncfusion/ej2-navigations';
import { Button,RadioButton,ChangeArgs } from '@syncfusion/ej2-buttons';
import { enableRipple } from '@syncfusion/ej2-base';
enableRipple(true);
//Sidebar initialization
 let defaultSidebar: Sidebar = new Sidebar({
    width: "280px",
    type: "Push",
    enablePersistence: true,
    target:<HTMLElement> document.querySelector('.maincontent')
    });
    defaultSidebar.appendTo('#default-sidebar');
    //end of Sidebar initialization

//toggle button initialization
let togglebtn: Button = new Button({iconCss: 'e-icons burg-icon', isToggle: true, content:'Open'}, '#toggle');

//Click Event.
const toggleButton = document.getElementById('toggle');
if(toggleButton){
    toggleButton.onclick = (): void => {
        if (toggleButton.classList.contains('e-active')) {
            togglebtn.content = 'Close';
            defaultSidebar.show();
        } else {
            togglebtn.content = 'Open';
            defaultSidebar.hide();
        }
    };
}

// Close the Sidebar
const closeButton = document.getElementById('close');
if (closeButton) {
    closeButton.onclick = (): void => {
        defaultSidebar.hide();

        const toggleButton = document.getElementById('toggle');
        if (toggleButton) {
            toggleButton.classList.remove('e-active');
        }
    };
}

let positionLeft: RadioButton = new RadioButton({ label: 'Left', name: 'state', checked: true,change:Change });
positionLeft.appendTo('#left');

//unchecked state.
let positionRight: RadioButton = new RadioButton({ label: 'Right', name: 'state',change:Change });
positionRight.appendTo('#right');

function Change(args: ChangeArgs) {
  defaultSidebar.position = ((<HTMLInputElement>args.event?.target).id == "left") ? "Left" : "Right";
}
<!DOCTYPE html>
<html lang="en">

<head>
    <title>Essential JS 2 Sidebar</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name="description" content="Typescript UI Controls" />
    <meta name="author" content="Syncfusion" />
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/css/bootstrap.min.css" rel="stylesheet" />
    <link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-base/styles/material.css" rel="stylesheet" />
    <link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-buttons/styles/material.css" rel="stylesheet" />
    <link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-navigations/styles/material.css" rel="stylesheet" />
    <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='container'>
        <!-- sidebar element declaration -->
        <aside id="default-sidebar">
            <div class="title"> Sidebar content</div>
            <div id="list"></div>
            <div class="sub-title">
                Click the button to close the Sidebar.
            </div>
            <div class="center-align">
                <button id="close" class="e-btn close-btn">Close Sidebar</button>
            </div>
        </aside>
        <!-- end of sidebar element -->
        <!-- main content declaration -->
        <div id="head">
            <button id="toggle" class="e-btn e-info"></button>
        </div>
        <div class="maincontent" style="height:335px;border:1px solid gray">
            <div>
                <div class="title">Main content</div>
                <div class="sub-title">
                    <div class="radiobutton">
                        <input type="radio" id="left" />
                    </div>
                    <div class="radiobutton">
                        <input type="radio" id="right" />
                    </div>
                </div>
            </div>
        </div>
        <!-- end of main content declaration -->
    </div>   
    
    <style>
        .header {
            width: 100%;
            height: 40px;
            font-size: 20px;
            line-height: 40px;
            font-weight: 500;
            background: #eee;
            display: inline-block;
        }
    
        .center-align {
            text-align: center;
            padding: 20px;
        }
    
        .burg-icon:before {
            content: '\e10d';
            font-size: 16px;
        }
    
        .title {
            text-align: center;
            font-size: 20px;
            padding: 15px;
        }
    
        #head {
            border: 1px solid #424242;
            border-bottom-color: transparent;
            background: #00897B;
        }
    
        #container .e-btn.e-info,
        #container .e-btn.e-info:hover,
        #container .e-btn.e-info:focus {
            /* csslint allow: adjoining-classes*/
            background: #00695C;
            box-shadow: none;
            border-radius: 0;
            height: 39px;
            width: 100px;
        }
    
        #close,
        #close:hover,
        #close:active,
        #close:focus {
            /* csslint allow: adjoining-classes*/
            background: #fafafa;
            color: black
        }
    
        .sub-title {
            text-align: center;
            font-size: 16px;
            padding: 10px;
        }
    
        .radiobutton {
            display: inline-block;
            padding: 10px;
        }
    
        .center {
            text-align: center;
            display: none;
            font-size: 13px;
            font-weight: 400;
            margin-top: 20px;
        }
    
        #wrapper {
            display: block;
        }
    
        .sb-content-tab .center {
            /* csslint allow: adjoining-classes*/
            display: block;
        }
    
        .sb-content-tab #wrapper {
            /* csslint allow: adjoining-classes*/
            display: none;
        }
    
        .right {
            float: right;
        }
    
        body {
            margin: 0;
        }
    
        #default-sidebar {
            background-color: #26A69A;
            color: #ffffff;
        }
    
        .close-btn:hover {
            color: #fafafa;
        }
    
        .content-section {
            /* csslint allow: adjoining-classes */
            padding: 30px 10px 10px 20px;
        }
    
        .book .ej2-sample-frame {
            /* csslint allow: adjoining-classes */
            padding: 0;
        }
    
        @font-face {
            font-family: 'e-icons';
            src: url(data:application/x-font-ttf;charset=utf-8;base64,AAEAAAAKAIAAAwAgT1MvMjciQ6oAAAEoAAAAVmNtYXBH1Ec8AAABsAAAAHJnbHlmKcXfOQAAAkAAAAg4aGVhZBLt+DYAAADQAAAANmhoZWEHogNsAAAArAAAACRobXR4LvgAAAAAAYAAAAAwbG9jYQukCgIAAAIkAAAAGm1heHABGQEOAAABCAAAACBuYW1lR4040wAACngAAAJtcG9zdEFgIbwAAAzoAAAArAABAAADUv9qAFoEAAAA//UD8wABAAAAAAAAAAAAAAAAAAAADAABAAAAAQAAlbrm7l8PPPUACwPoAAAAANfuWa8AAAAA1+5ZrwAAAAAD8wPzAAAACAACAAAAAAAAAAEAAAAMAQIAAwAAAAAAAgAAAAoACgAAAP8AAAAAAAAAAQPqAZAABQAAAnoCvAAAAIwCegK8AAAB4AAxAQIAAAIABQMAAAAAAAAAAAAAAAAAAAAAAAAAAAAAUGZFZABA4QLhkANS/2oAWgPzAJYAAAABAAAAAAAABAAAAAPoAAAD6AAAA+gAAAPoAAAD6AAAA+gAAAPoAAAD6AAAA+gAAAPoAAAD6AAAAAAAAgAAAAMAAAAUAAMAAQAAABQABABeAAAADgAIAAIABuEC4QnhD+ES4RvhkP//AADhAuEJ4QvhEuEa4ZD//wAAAAAAAAAAAAAAAAABAA4ADgAOABYAFgAYAAAAAQACAAYABAADAAgABwAKAAkABQALAAAAAAAAAB4AQABaAQYB5gJkAnoCjgKwA8oEHAAAAAIAAAAAA+oDlQAEAAoAAAEFESERCQEVCQE1AgcBZv0mAXQB5P4c/g4Cw/D+lwFpAcP+s24BT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HUAbABhAHIAZQAtAGkAYwBvAG4AcwAtAG0AZQB0AHIAbwBlAC0AaQBjAG8AbgBzAC0AbQBlAHQAcgBvAFYAZQByAHMAaQBvAG4AIAAxAC4AMABlAC0AaQBjAG8AbgBzAC0AbQBlAHQAcgBvAEYAbwBuAHQAIABnAGUAbgBlAHIAYQB0AGUAZAAgAHUAcwBpAG4AZwAgAFMAeQBuAGMAZgB1AHMAaQBvAG4AIABNAGUAdAByAG8AIABTAHQAdQBkAGkAbwB3AHcAdwAuAHMAeQBuAGMAZgB1AHMAaQBvAG4ALgBjAG8AbQAAAAACAAAAAAAAAAoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAwBAgEDAQQBBQEGAQcBCAEJAQoBCwEMAQ0AB2hvbWUtMDELQ2xvc2UtaWNvbnMHbWVudS0wMQR1c2VyB0JUX2luZm8PU2V0dGluZ19BbmRyb2lkDWNoZXZyb24tcmlnaHQMY2hldnJvbi1sZWZ0CE1UX0NsZWFyDE1UX0p1bmttYWlscwRzdG9wAAA=) format('truetype');
            font-weight: normal;
            font-style: normal;
        }
    </style>
</body>

</html>

Animate

Animation transitions can be set while expanding or collapsing the Sidebar using the animate property. By default , animate property is set to true.

import { Sidebar } from '@syncfusion/ej2-navigations';
import { enableRipple } from '@syncfusion/ej2-base';
enableRipple(true);

let defaultSidebar: Sidebar = new Sidebar({
    type: "Push",width: '280px',animate: false
});
defaultSidebar.appendTo('#default-sidebar');
//end of Sidebar initialization

// Toggle(Open/Close) the Sidebar
const toggleButton = document.getElementById('toggle');
if (toggleButton) {
    toggleButton.onclick = (): void => {
        defaultSidebar.toggle();
    };
}

const closeButton = document.getElementById('close');
if (closeButton) {
    closeButton.onclick = (): void => {
        defaultSidebar.hide();
    };
}
<!DOCTYPE html>
<html lang="en">

<head>
    <title>Essential JS 2 Sidebar</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name="description" content="Typescript UI Controls" />
    <meta name="author" content="Syncfusion" />
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/css/bootstrap.min.css" rel="stylesheet" />
    <link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-base/styles/material.css" rel="stylesheet" />
    <link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-buttons/styles/material.css" rel="stylesheet" />
    <link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-navigations/styles/material.css" rel="stylesheet" />
    <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='container'>
        <aside id="default-sidebar">
        <div class="title"> Sidebar content</div>
        <div class="sub-title">
            Click the button to close the Sidebar
        </div>
        <div class="center-align">
            <button id="close" class="e-btn close-btn">Close Sidebar</button>
        </div>
        </aside>
        <!-- end of sidebar element -->
        <!-- main content declaration -->
        <div>
            <div class="title">Main content</div>
            <div class="sub-title"> Click the button to open/close the Sidebar.</div>
            <div style="padding:20px" class="center-align">
            <button id="toggle" class="e-btn e-info">Toggle Sidebar</button>
            </div>
        </div>
    </div>

    <style>
        .header {
            width: 100%;
            height: 40px;
            font-size: 20px;
            line-height: 40px;
            font-weight: 500;
            background: #eee;
            display: inline-block;
        }
    
        .center-align {
            text-align: center;
            padding: 20px;
        }
    
        #close,
        #close:hover,
        #close:active,
        #close:focus {
            /* csslint allow: adjoining-classes*/
            background: #fafafa;
            color: black
        }
    
        .title {
            text-align: center;
            font-size: 20px;
            padding: 15px;
        }
    
        .sub-title {
            text-align: center;
            font-size: 16px;
            padding: 10px;
        }
    
        .center {
            text-align: center;
            display: none;
            font-size: 13px;
            font-weight: 400;
            margin-top: 20px;
        }
    
        #wrapper {
            display: block;
        }
    
        .sb-content-tab .center {
            /* csslint allow: adjoining-classes*/
            display: block;
        }
    
        .sb-content-tab #wrapper {
            /* csslint allow: adjoining-classes*/
            display: none;
        }
    
        .right {
            float: right;
        }
    
        body {
            margin: 0;
        }
    
        #default-sidebar {
            background-color: rgb(25, 118, 210);
            color: #ffffff;
        }
    
        .book .ej2-sample-frame {
            /* csslint allow: adjoining-classes */
            padding: 0;
        }
    </style>
</body>

</html>

Close on document click

Sidebar can be closed on document click by setting closeOnDocumentClick to true. If this property is not set, the Sidebar will not close on document click since its default value is false. Sidebar can be kept opened during rendering using isOpen property.

import { Sidebar } from '@syncfusion/ej2-navigations';
import { enableRipple } from '@syncfusion/ej2-base';
enableRipple(true);

let defaultSidebar: Sidebar = new Sidebar({
    type: "Push",width: '280px',closeOnDocumentClick: true,isOpen: true
});
defaultSidebar.appendTo('#default-sidebar');
//end of Sidebar initialization

// Toggle(Open/Close) the Sidebar
const toggleButton = document.getElementById('toggle');
if (toggleButton) {
    toggleButton.onclick = (): void => {
        defaultSidebar.show();
    };
}
<!DOCTYPE html>
<html lang="en">

<head>
    <title>Essential JS 2 Sidebar</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name="description" content="Typescript UI Controls" />
    <meta name="author" content="Syncfusion" />
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/css/bootstrap.min.css" rel="stylesheet" />
    <link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-base/styles/material.css" rel="stylesheet" />
    <link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-buttons/styles/material.css" rel="stylesheet" />
    <link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-lists/styles/material.css" rel="stylesheet" />
    <link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-navigations/styles/material.css" rel="stylesheet" />
    <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='container'>
            <aside id="default-sidebar">
            <div class="title"> Sidebar content</div>
          </aside>
            <!-- end of sidebar element -->
            <!-- main content declaration -->
            <div>
               <div class="title">Main content</div>
               <div class="sub-title"> Click the button to open the Sidebar.</div>
               <div style="padding:20px" class="center-align">
                <button id="toggle" class="e-btn e-info">Open Sidebar</button>
            </div>
        </div>
    </div>

    <style>
        .header {
            width: 100%;
            height: 40px;
            font-size: 20px;
            line-height: 40px;
            font-weight: 500;
            background: #eee;
            display: inline-block;
        }
    
        .center-align {
            text-align: center;
            padding: 20px;
        }
    
        #close,
        #close:hover,
        #close:active,
        #close:focus {
            /* csslint allow: adjoining-classes*/
            background: #fafafa;
            color: black
        }
    
        .title {
            text-align: center;
            font-size: 20px;
            padding: 15px;
        }
    
        .sub-title {
            text-align: center;
            font-size: 16px;
            padding: 10px;
        }
    
        .center {
            text-align: center;
            display: none;
            font-size: 13px;
            font-weight: 400;
            margin-top: 20px;
        }
    
        #wrapper {
            display: block;
        }
    
        .right {
            float: right;
        }
    
        body {
            margin: 0;
        }
    
        #default-sidebar {
            background-color: rgb(25, 118, 210);
            color: #ffffff;
        }
    </style>
</body>

</html>

Enable gestures

Expand or collapse the Sidebar while swiping in touch devices using enableGestures property. By default, enableGestures is set to true.

import { Sidebar } from '@syncfusion/ej2-navigations';
import { enableRipple } from '@syncfusion/ej2-base';
enableRipple(true);

let defaultSidebar: Sidebar = new Sidebar({
    type: "Push",width: '280px',enableGestures:false
});
defaultSidebar.appendTo('#default-sidebar');
//end of Sidebar initialization

// Toggle(Open/Close) the Sidebar
const toggleButton = document.getElementById('toggle');
if (toggleButton) {
    toggleButton.onclick = (): void => {
        defaultSidebar.toggle();
    };
}

// Close the Sidebar
const closeButton = document.getElementById('close');
if (closeButton) {
    closeButton.onclick = (): void => {
        defaultSidebar.hide();
    };
}
<!DOCTYPE html>
<html lang="en">

<head>
    <title>Essential JS 2 Sidebar</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name="description" content="Typescript UI Controls" />
    <meta name="author" content="Syncfusion" />
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/css/bootstrap.min.css" rel="stylesheet" />
    <link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-base/styles/material.css" rel="stylesheet" />
    <link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-buttons/styles/material.css" rel="stylesheet" />
    <link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-navigations/styles/material.css" rel="stylesheet" />
    <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='container'>
            <aside id="default-sidebar">
            <div class="title"> Sidebar content</div>
            <div class="sub-title">
                Click the button to close the Sidebar
            </div>
            <div class="center-align">
                <button id="close" class="e-btn close-btn">Close Sidebar</button>
            </div>
           </aside>
            <!-- end of sidebar element -->
            <!-- main content declaration -->
             <div>
            <div class="title">Main content</div>
            <div class="sub-title"> Click the button to open/close the Sidebar.</div>
            <div style="padding:20px" class="center-align">
                <button id="toggle" class="e-btn e-info">Toggle Sidebar</button>
            </div>
        </div>
    </div>

    <style>
        .header {
            width: 100%;
            height: 40px;
            font-size: 20px;
            line-height: 40px;
            font-weight: 500;
            background: #eee;
            display: inline-block;
        }
    
        .center-align {
            text-align: center;
            padding: 20px;
        }
    
        #close,
        #close:hover,
        #close:active,
        #close:focus {
            /* csslint allow: adjoining-classes*/
            background: #fafafa;
            color: black
        }
    
        .title {
            text-align: center;
            font-size: 20px;
            padding: 15px;
        }
    
        .sub-title {
            text-align: center;
            font-size: 16px;
            padding: 10px;
        }
    
        .center {
            text-align: center;
            display: none;
            font-size: 13px;
            font-weight: 400;
            margin-top: 20px;
        }
    
        #wrapper {
            display: block;
        }
    
        body {
            margin: 0;
        }
    
        #default-sidebar {
            background-color: rgb(25, 118, 210);
            color: #ffffff;
        }
    </style>
</body>

</html>

See Also