Custom context in EJ2 TypeScript Sidebar control

2 May 20235 minutes to read

Sidebar has a flexible option to make it initialize, target to any HTML element alongside of the main content of a web page.

By default, Sidebar initializes target to the body element. Using the target property, set target element to initialize the Sidebar inside any HTML element apart from the body element.

If required , zIndex can be set when sidebar act as overlay type.

In the following sample, sidebar is rendered context to a div container element which has the CSS class e-main-content.

import { Sidebar } from '@syncfusion/ej2-navigations';
import { Button } from '@syncfusion/ej2-buttons';
import { enableRipple } from '@syncfusion/ej2-base';

let defaultSidebar: Sidebar = new Sidebar({
    width: "280px",
    type: "Push",
    target: '.maincontent'
//end of Sidebar initialization

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

// Close the Sidebar
document.getElementById('close').onclick = (): void => {
    togglebtn.content = 'Open'

//Click Event.
document.getElementById('toggle').onclick = (): void => {
        if (document.getElementById('toggle').classList.contains('e-active')) {
            togglebtn.content = 'Close';
        } else {
            togglebtn.content = 'Open';
<!DOCTYPE html>
<html lang="en">

    <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="" rel="stylesheet" />
    <link href="" rel="stylesheet" />
    <link href="" rel="stylesheet" />
    <link href="" rel="stylesheet" />
    <link href="styles.css" rel="stylesheet" />
    <script src=""></script>
    <script src="systemjs.config.js"></script>
<script src="" type ="text/javascript"></script>

    <div id='loader'>LOADING....</div>
    <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 class="center-align">
                <button id="close" class="e-btn close-btn">Close Sidebar</button>
        <!-- end of sidebar element -->
        <!-- main content declaration -->

        <div id="head">
            <button id="toggle" class="e-btn e-info"></button>
        <div class="maincontent" style="height:325px;border:1px solid gray">
                <div class="title"> Main content</div>
                <div class="sub-title"> content goes here.</div>
        <!--end of main content -->