Loading...
Loading...
Guide for implementing Syncfusion Angular Sidebar component for navigation, collapsible menus, and dynamic content. Use this when creating sidebars with multiple positioning options, animations, gestures, docking, responsive behavior, TreeView/ListView content, and backdrop overlays. This skill covers sidebar navigation, toggle menus, responsive navigation panels, collapsible layouts, and expanding/collapsing navigation elements.
npx skill4agent add syncfusion/angular-ui-components-skills syncfusion-angular-sidebarimport { SidebarModule } from '@syncfusion/ej2-angular-navigations';
import { Component, ViewChild } from '@angular/core';
import { SidebarComponent } from '@syncfusion/ej2-angular-navigations';
@Component({
imports: [SidebarModule],
standalone: true,
selector: 'app-root',
template: `<ejs-sidebar #sidebar id="default-sidebar">
<div class="title">Sidebar Content</div>
</ejs-sidebar>
<div>
<div class="title">Main Content</div>
<button (click)="toggleSidebar()">Toggle Sidebar</button>
</div>`,
styles: [`
.title { padding: 20px; font-weight: bold; }
`]
})
export class AppComponent {
@ViewChild('sidebar') sidebar?: SidebarComponent;
toggleSidebar() {
this.sidebar?.toggle();
}
}targettarget<div><ejs-sidebar id="sidebar" type="Push"></ejs-sidebar>
<div> <!-- Automatically becomes target - no wrapper needed -->
<div class="content">Main Content</div>
</div>[target]="'#selector'"<ejs-sidebar [target]="'#content-area'" type="Push"></ejs-sidebar>
<div id="content-area"> <!-- Explicit target -->
<div> <!-- ⚠️ REQUIRED: Inner wrapper -->
<div class="content">Main Content</div>
</div>
</div>target<div>| Mode | When to Use | Complexity | Wrapper Needed |
|---|---|---|---|
| Implicit (No target) | Default for most layouts | Simple | ❌ No |
| Explicit (With target) | Complex layouts, selective targeting | Advanced | ✅ Yes (inner) |
booleantrue// Example: Disable animations for instant toggle
<ejs-sidebar [animate]="false"></ejs-sidebar>
// Example: Enable animations (default)
<ejs-sidebar [animate]="true"></ejs-sidebar>booleanfalse// Example: Auto-close sidebar on document click
<ejs-sidebar [closeOnDocumentClick]="true"></ejs-sidebar>
// Example: In component
this.closeOnClick = true;string | number'auto'// Example: Set dock size to 72 pixels
<ejs-sidebar [dockSize]="'72px'" [enableDock]="true"></ejs-sidebar>
// Example: Set as number
<ejs-sidebar [dockSize]="72" [enableDock]="true"></ejs-sidebar>
// Example: In component
public dockSize: string = '72px';booleanfalse// Example: Enable docking for icon-only sidebar
<ejs-sidebar [enableDock]="true" [dockSize]="'72px'">
<div class="sidebar-item" title="Home">
<i class="e-icons e-home"></i>
</div>
<div class="sidebar-item" title="Settings">
<i class="e-icons e-settings"></i>
</div>
</ejs-sidebar>
// Example: In component
public enableDock = true;
public dockSize = '72px';booleantrue// Example: Enable gesture support (default)
<ejs-sidebar [enableGestures]="true"></ejs-sidebar>
// Example: Disable gestures for specific use cases
<ejs-sidebar [enableGestures]="false"></ejs-sidebar>booleanfalse// Example: Enable persistence to remember sidebar state
<ejs-sidebar [enablePersistence]="true"></ejs-sidebar>
// Persisted states:
// 1. Position (Left/Right)
// 2. Type (Push/Slide/Over/Auto)
// 3. Open/Closed statebooleanfalse// Example: Enable RTL layout
<ejs-sidebar [enableRtl]="true"></ejs-sidebar>
// Example: Dynamically set based on language
public isRtl = document.documentElement.lang === 'ar';
<ejs-sidebar [enableRtl]="isRtl"></ejs-sidebar>booleanfalse// Example: Initially open sidebar
<ejs-sidebar [isOpen]="true"></ejs-sidebar>
// Example: Initially closed (default)
<ejs-sidebar [isOpen]="false"></ejs-sidebar>
// Example: Toggle state from component
@ViewChild('sidebar') sidebar?: SidebarComponent;
toggleOpen() {
this.sidebar!.isOpen = !this.sidebar!.isOpen;
}
// Note: When sidebar type is 'Auto', this property is ignored on mobile devicesstring | MediaQueryListnull// Example: Open sidebar on screens wider than 600px
<ejs-sidebar [mediaQuery]="'(min-width: 600px)'"></ejs-sidebar>
// Example: Using MediaQueryList object
public mediaQuery = window.matchMedia('(min-width: 768px)');
<ejs-sidebar [mediaQuery]="mediaQuery"></ejs-sidebar>
// Example: Common breakpoints
// Mobile: '(max-width: 600px)'
// Tablet: '(min-width: 601px) and (max-width: 1024px)'
// Desktop: '(min-width: 1025px)'SidebarPosition'Left''Left''Right'// Example: Position sidebar on the left (default)
<ejs-sidebar [position]="'Left'"></ejs-sidebar>
// Example: Position sidebar on the right
<ejs-sidebar [position]="'Right'"></ejs-sidebar>
// Example: Set position dynamically
public sidebarPosition: SidebarPosition = 'Left';
<ejs-sidebar [position]="sidebarPosition"></ejs-sidebar>'Left''Right'booleanfalse// Example: Show backdrop overlay
<ejs-sidebar [showBackdrop]="true"></ejs-sidebar>
// Example: Backdrop with auto-close on click
<ejs-sidebar [showBackdrop]="true" [closeOnDocumentClick]="true"></ejs-sidebar>
// Example: Combine with other properties
<ejs-sidebar
[showBackdrop]="true"
[closeOnDocumentClick]="true"
[type]="'Over'">
</ejs-sidebar>HTMLElement | stringnull<div>// ✅ Example: Explicit targeting with ID selector (CORRECT - with wrapper)
<ejs-sidebar [target]="'#content-area'" [type]="'Push'"></ejs-sidebar>
<div id="content-area">
<div> <!-- Required wrapper for transforms -->
<div>Content here</div>
</div>
</div>
// ✅ Example: Explicit targeting with CSS class (CORRECT - with wrapper)
<ejs-sidebar [target]="'.main-content'" [type]="'Push'"></ejs-sidebar>
<div class="main-content">
<div> <!-- Required wrapper for transforms -->
<div>Content here</div>
</div>
</div>
// ✅ Example: Pass HTMLElement directly
@ViewChild('targetDiv') targetElement?: ElementRef;
<ejs-sidebar [target]="targetElement?.nativeElement" [type]="'Push'"></ejs-sidebar>
<div #targetDiv>
<div> <!-- Required wrapper for transforms -->
<div>Content here</div>
</div>
</div>SidebarType'Auto''Push''Slide''Over''Auto'// Example: Push type - sidebar pushes content aside
<ejs-sidebar [type]="'Push'"></ejs-sidebar>
// Example: Slide type - sidebar slides over and translates content
<ejs-sidebar [type]="'Slide'"></ejs-sidebar>
// Example: Over type - sidebar floats over content
<ejs-sidebar [type]="'Over'"></ejs-sidebar>
// Example: Auto type - Over on mobile, Push on desktop
<ejs-sidebar [type]="'Auto'"></ejs-sidebar>'Push''Slide''Over''Auto'string | number'280px'// Example: Set width in pixels
<ejs-sidebar [width]="'300px'"></ejs-sidebar>
// Example: Set width as number (treated as pixels)
<ejs-sidebar [width]="300"></ejs-sidebar>
// Example: Set width in percentage
<ejs-sidebar [width]="'50%'"></ejs-sidebar>
// Example: Set width in em units
<ejs-sidebar [width]="'20em'"></ejs-sidebar>
// Example: Responsive width
public sidebarWidth = window.innerWidth < 768 ? '100%' : '300px';
<ejs-sidebar [width]="sidebarWidth"></ejs-sidebar>string | number1000'Over''Auto'// Example: Set z-index for layering
<ejs-sidebar [zIndex]="1000"></ejs-sidebar>
// Example: High z-index to appear above other modals
<ejs-sidebar [zIndex]="9999" [type]="'Over'"></ejs-sidebar>evoidimport { Component, ViewChild } from '@angular/core';
import { SidebarComponent } from '@syncfusion/ej2-angular-navigations';
@Component({
selector: 'app-root',
template: `
<ejs-sidebar #sidebar></ejs-sidebar>
<button (click)="openSidebar()">Open Sidebar</button>
`
})
export class AppComponent {
@ViewChild('sidebar') sidebar?: SidebarComponent;
// Method 1: Show without event
openSidebar() {
this.sidebar?.show();
}
// Method 2: Show with event
openSidebarWithEvent(event: MouseEvent) {
this.sidebar?.show(event);
}
}evoid@Component({
selector: 'app-root',
template: `
<ejs-sidebar #sidebar></ejs-sidebar>
<button (click)="closeSidebar()">Close Sidebar</button>
`
})
export class AppComponent {
@ViewChild('sidebar') sidebar?: SidebarComponent;
// Method 1: Hide without event
closeSidebar() {
this.sidebar?.hide();
}
// Method 2: Hide with click event
closeSidebarOnClick(event: MouseEvent) {
this.sidebar?.hide(event);
}
}void@Component({
selector: 'app-root',
template: `
<ejs-sidebar #sidebar id="sidebar" [isOpen]="false">
<button (click)="toggleMenu()">Close</button>
</ejs-sidebar>
<button (click)="toggleMenu()">☰ Menu</button>
`
})
export class AppComponent {
@ViewChild('sidebar') sidebar?: SidebarComponent;
toggleMenu() {
this.sidebar?.toggle();
}
}void@Component({
selector: 'app-root',
template: `
<ejs-sidebar #sidebar></ejs-sidebar>
<button (click)="destroySidebar()">Destroy Sidebar</button>
`
})
export class AppComponent {
@ViewChild('sidebar') sidebar?: SidebarComponent;
// Destroy and remove the sidebar
destroySidebar() {
this.sidebar?.destroy();
}
}void@Component({
selector: 'app-root',
template: `
<ejs-sidebar
#sidebar
[width]="sidebarWidth"
[type]="sidebarType">
</ejs-sidebar>
<button (click)="changeSidebarProperties()">Change Properties</button>
`
})
export class AppComponent {
@ViewChild('sidebar') sidebar?: SidebarComponent;
sidebarWidth = '280px';
sidebarType = 'Push';
// Change sidebar properties dynamically
changeSidebarProperties() {
// Modify properties
this.sidebarWidth = '350px';
this.sidebarType = 'Slide';
// Apply changes to the component
(this.sidebar as SidebarComponent).dataBind();
}
// Another example: Toggle width on docked state
updateDockSize() {
(this.sidebar as SidebarComponent).dockSize = '100px';
(this.sidebar as SidebarComponent).dataBind();
}
}EventArgscancelelementeventisInteractedmodel@Component({
selector: 'app-root',
template: `
<ejs-sidebar
#sidebar
(open)="onOpen($event)">
</ejs-sidebar>
`
})
export class AppComponent {
@ViewChild('sidebar') sidebar?: SidebarComponent;
onOpen(event: any) {
console.log('Sidebar opened');
console.log('Event:', event.event);
console.log('Element:', event.element);
console.log('Is Interacted:', event.isInteracted);
// Prevent opening if needed
// event.cancel = true;
}
}EventArgscancelelementeventisInteractedmodel@Component({
selector: 'app-root',
template: `
<ejs-sidebar
#sidebar
(close)="onClose($event)">
</ejs-sidebar>
`
})
export class AppComponent {
@ViewChild('sidebar') sidebar?: SidebarComponent;
onClose(event: any) {
console.log('Sidebar closed');
console.log('Is Interacted:', event.isInteracted);
// Prevent closing if needed
// event.cancel = true;
}
}ChangeEventArgselementname@Component({
selector: 'app-root',
template: `
<ejs-sidebar
#sidebar
(change)="onChange($event)">
</ejs-sidebar>
`
})
export class AppComponent {
@ViewChild('sidebar') sidebar?: SidebarComponent;
onChange(event: any) {
console.log('Sidebar state changed');
console.log('Event name:', event.name);
console.log('Element:', event.element);
}
}Object@Component({
selector: 'app-root',
template: `
<ejs-sidebar
#sidebar
(created)="onCreated($event)"
style="visibility: hidden">
</ejs-sidebar>
`
})
export class AppComponent {
@ViewChild('sidebar') sidebar?: SidebarComponent;
onCreated(event: any) {
console.log('Sidebar created and initialized');
// Make sidebar visible after creation
(this.sidebar as SidebarComponent).element.style.visibility = '';
}
}Object@Component({
selector: 'app-root',
template: `
<ejs-sidebar
#sidebar
(destroyed)="onDestroyed($event)">
</ejs-sidebar>
`
})
export class AppComponent {
@ViewChild('sidebar') sidebar?: SidebarComponent;
onDestroyed(event: any) {
console.log('Sidebar destroyed');
// Cleanup any additional resources
}
}// In component.ts
toggleSidebar() {
this.sidebar?.toggle();
}
// In template
<button (click)="toggleSidebar()">Toggle</button>public mediaQuery = window.matchMedia('(max-width: 600px)');
// In template
<ejs-sidebar [mediaQuery]="mediaQuery" [isOpen]="true">public enableDock = true;
public dockSize = '72px';
// In template
<ejs-sidebar [enableDock]="enableDock" [dockSize]="dockSize">// In template
<ejs-sidebar [showBackdrop]="true" [closeOnDocumentClick]="true">| Property | Type | Default | Purpose |
|---|---|---|---|
| Push | Slide | Over | Auto | Auto | Expand behavior and content interaction |
| Left | Right | Left | Sidebar direction and positioning |
| string | number | 280px | Sidebar width in expanded state |
| string | number | auto | Width when docked/collapsed |
| boolean | false | Enable compact docked state |
| boolean | false | Initial open/closed state |
| boolean | true | Enable expand/collapse animations |
| boolean | false | Display overlay on main content |
| boolean | false | Close when clicking outside |
| boolean | true | Enable touch swipe gestures |
| string | MediaQueryList | null | Auto-close on screen size match |
| boolean | false | Right-to-left layout support |
| boolean | false | Persist state between page reloads |