postImage

How to create a Angular Bootstrap Sidebar

blogImage

By Amarachi Iheanacho

Technical Writer

A sidebar is a user interface element usually to the left or right of your website that contains pieces of additional information. Examples of this additional information are navigational links, supplementary information about the information conveyed on the main part of the website.

In this article we would be discussing building an angular bootstrap sidebar using Contrast.

Table Of Contents

  • What are we building?
  • Prerequisites
  • Understanding what Contrast is
  • Creating a new Angular Project
  • Adding Routing ability in our Angular project
  • Installing the Contrast Angular Bootstrap in your project
  • Creating our Angular Bootstrap Sidebar
  • Default Sidebar
  • Multilevel Sidebar
  • Resources

What we are building

In this article, we are going to walk through building an Angular Bootstrap sidebar with Contrast. Like the image we have below:

Angular Bootstrap 5 Sidebar

Prerequisites

To make the most of this article, it is important that you have the following:

  • A basic understanding of HTML.
  • A basic understanding of CSS.
  • Node and it’s package manager, npm​. Run the command node -v​ && npm -v​ to verify we have them installed, or install them from here.
  • Alternatively, we can use another package manager, Yarn.

Understanding what Contrast is

Contrast or Contrast Design Bootstrap is an elegant bootstrap UI kit featuring over 2000+ essential components. Contrast helps simplify the web development process and can be integrated with any project to build mobile-first, responsive, and elegant websites and web apps.

Creating a new Angular Project

To create an angular project, we first have to make sure we have the angular cli installed, to do this we go to our terminal and run this command.

1npm install -g @angular/cli

The -g flag indicates that we want it installed globally and not just in one file.

Then we go on to create the angular project. We do this by first changing the directory to the location we want our project to be installed and then running this command.

1cd <location path>
2ng new <name of our project>

We then cd into our newly created project and run ng serve to see our application on the browser.

1cd <name of our project>
2ng serve

We should see our application on the browser when we go to http://localhost:4200/

Angular Bootstrap Sidebar

Adding Routing ability in our project

Even though our application is single paged, we would still need the ability to route between different pages. To do this we need to import the RouterModule into the app.module.ts file of our project.

1import { RouterModule } from '@angular/router';

Next we add it into the imports array.

1imports: [
2 BrowserModule,
3 RouterModule.forRoot([])
4]

Installing the Contrast Angular Bootstrap in your project

1npm install --save ng-cdbangular
2
3# or
4
5
6yarn add ng-cdbangular

Next we add our bootstrap-css file, into angular.json

1"styles": [
2 "node_modules/bootstrap-css-only/css/bootstrap.min.css"
3]

We then import the module we need, in this case the SidebarModule and add it to the the imports array.

1import { SidebarModule } from 'ng-cdbangular';
2
3@NgModule({
4 imports: [
5 BrowserModule,
6 RouterModule.forRoot([]),
7 SidebarModule
8 ]
9})

Our app.module.ts file should look like this

1import { BrowserModule } from '@angular/platform-browser';
2import { RouterModule, Routes } from '@angular/router';
3import { SidebarModule } from 'ng-cdbangular';
4
5
6import { AppComponent } from './app.component';
7
8@NgModule({
9 declarations: [
10 AppComponent
11 ],
12 imports: [
13 BrowserModule,
14 RouterModule.forRoot([]),
15 SidebarModule
16 ],
17 providers: [],
18 bootstrap: [AppComponent]
19})
20export class AppModule { }

Creating our Augular Bootstrap Sidebar

With the Contrast Angular Bootstrap Sidebar,we get access to two types of sidebars.

  • Default Sidebar
  • Multilevel Sidebar

Default Sidebar

To create a default sidebar using Contrast, we use different components to create different parts of the sidebar. These components are :

  • CDBSidebarHeader: This is used to create the header, the top part of the sidebar. It also takes in a prefix prop which indicates what element you want before the text in your header.

  • CDBSidebarContent: This component is the body of the sidebar. It acts as a container that holds the sidebar menu and its items. CDBSidebarMenu: This component holds a group of items. This group of items is usually an unordered list of navigational links or text.

  • CDBSidebarMenuItem: This component refers to each item in the CDBSidebarMenu container. In the block of code below, this component takes in two props, the icon prop, which indicates what icon you want before the text in the menu item, and the link prop, which defines the path you want the item to lead you to once you click on it.

  • CDBSidebarFooter: This component is used to hold the footer of the sidebar. A sidebar's footer usually consists of a website's trademark etc.

1<div class="app" style="display: flex; height: 100vh; overflow:scroll initial">
2 <CDBSidebar textColor="#ffffff" backgroundColor="#333333">
3 <CDBSidebarHeader [prefix]="icon" >
4 <ng-template #icon>
5 <i class="fa fa-bars fa-large"></i>
6 </ng-template>
7
8 Contrast
9 </CDBSidebarHeader>
10
11 <CDBSidebarContent>
12 <CDBSidebarMenu class="sidebar-content">
13 <CDBSidebarMenuItem link='/' icon="columns" >
14 Dashboard
15 </CDBSidebarMenuItem>
16 <CDBSidebarMenuItem link='/tables' icon="table" >
17 Tables
18 </CDBSidebarMenuItem>
19 <CDBSidebarMenuItem link='/profile' icon="user" >
20 Profile
21 </CDBSidebarMenuItem>
22 <CDBSidebarMenuItem href='/404' icon="exclamation-circle" >
23 <span style="margin-left: 5px;">404 Page</span>
24 </CDBSidebarMenuItem>
25 </CDBSidebarMenu>
26 </CDBSidebarContent>
27 <div class="mt-auto">
28
29 <CDBSidebarFooter style="text-align: center; margin-top: auto;">
30 <div class="sidebar-btn-wrapper" style="padding: 20px 5px">
31 Sidebar Footer
32 </div>
33 </CDBSidebarFooter>
34 </div>
35 </CDBSidebar>
36</div>

With this, we end up with a sidebar that looks like the picture below

Angular Bootstrap Default Sidebar

Multilevel Sidebar

The multilevel sidebar is only available to Contrast PRO users, this sidebar gives you access to create submenus in your sidebar, that is a menubar in a menubar.

Installing CDBAngular PRO

To use Cdbangular PRO in your project it is important that you add the tgz file in the root of your project.

Next you go to your package.json file and to the dependencies object you add

1"cdbangular": "<file path of the tgz file in the root of your project>"

Finally we run npm install

Creating our Multilevel Sidebar

With the multilevel sidebar we gat access to the CDBSidebarSubMenu component, which allows us to create the submenus.

In creating the multilevel sidebar we used other Contrast Bootstrap Angular components like CDBIcon, and CDBBadge, so to use them in our project we have to import their modules in the app.module.ts file.

1import { SidebarModule, IconModule, BadgeModule } from 'ng-cdbangular';
2
3@NgModule({
4 imports: [
5 BrowserModule,
6 RouterModule.forRoot([]),
7 SidebarModule,
8 IconModule,
9 BadgeModule
10 ]
11})
1<CDBSidebar textColor="#f4f4f4" backgroundColor="#000000">
2 <CDBSidebarHeader [prefix]="icon">
3 <ng-template #icon>
4 <CDBIcon icon="bars" size="lg"></CDBIcon>
5 </ng-template>
6
7 Contrast
8 </CDBSidebarHeader>
9
10 <CDBSidebarContent>
11 <CDBSidebarMenu>
12 <CDBSidebarMenuItem icon="th-large" [suffix]="badge1">
13 <ng-template #badge1>
14 <CDBBadge color="danger" size="small" borderType="pill">new</CDBBadge>
15 </ng-template>
16
17 Dashboard
18 </CDBSidebarMenuItem>
19
20 <CDBSidebarMenuItem icon="sticky-note" [suffix]="badge2">
21 <ng-template #badge2>
22 <CDBBadge color="danger" size="small" borderType="pill">new</CDBBadge>
23 </ng-template>
24
25 Components
26 </CDBSidebarMenuItem>
27 </CDBSidebarMenu>
28
29 <CDBSidebarMenu>
30 <CDBSidebarSubMenu title="Sidemenu" icon="th" textColor="#f4f4f4" backgroundColor="#000000">
31 <CDBSidebarMenuItem>submenu 1</CDBSidebarMenuItem>
32 <CDBSidebarMenuItem>submenu 2</CDBSidebarMenuItem>
33 <CDBSidebarMenuItem>submenu 3</CDBSidebarMenuItem>
34 </CDBSidebarSubMenu>
35
36 <CDBSidebarSubMenu
37 title="Sidemenu2"
38 icon="book"
39 [suffix]="badge3"
40 textColor="#f4f4f4"
41 backgroundColor="#000000"
42 >
43 <ng-template #badge3>
44 <CDBBadge color="danger" size="small" borderType="pill">new</CDBBadge>
45 </ng-template>
46
47 <CDBSidebarMenuItem>submenu 1</CDBSidebarMenuItem>
48 <CDBSidebarMenuItem>submenu 2</CDBSidebarMenuItem>
49 <CDBSidebarMenuItem>submenu 3</CDBSidebarMenuItem>
50 </CDBSidebarSubMenu>
51
52 <CDBSidebarSubMenu
53 title="MultiLevel with Icon"
54 icon="table"
55 textColor="#f4f4f4"
56 backgroundColor="#000000"
57 >
58 <CDBSidebarMenuItem>submenu 1</CDBSidebarMenuItem>
59 <CDBSidebarMenuItem>submenu 2</CDBSidebarMenuItem>
60
61 <CDBSidebarSubMenu title="submenu 3" textColor="#f4f4f4" backgroundColor="#000000">
62 <CDBSidebarMenuItem>submenu 3.1</CDBSidebarMenuItem>
63 <CDBSidebarMenuItem>submenu 3.2</CDBSidebarMenuItem>
64
65 <CDBSidebarSubMenu title="subnt" textColor="#f4f4f4" backgroundColor="#000000">
66 <CDBSidebarMenuItem>submenu 3.3.1</CDBSidebarMenuItem>
67 <CDBSidebarMenuItem>submenu 3.3.2</CDBSidebarMenuItem>
68 <CDBSidebarMenuItem>submenu 3.3.3</CDBSidebarMenuItem>
69 </CDBSidebarSubMenu>
70 </CDBSidebarSubMenu>
71 </CDBSidebarSubMenu>
72 </CDBSidebarMenu>
73 </CDBSidebarContent>
74
75 <div class="mt-auto">
76 <CDBSidebarFooter style="text-align: center">
77 <div class="sidebar-btn-wrapper" style="padding: 20px 5px">
78 Sidebar Footer
79 </div>
80 </CDBSidebarFooter>
81 </div>
82</CDBSidebar>

Resources

You may also find the following resources useful

Create Stunning websites and web apps

Trying to build out all user interfaces and components for your website or web app from scratch can become a very tedious task. A huge reason why we created Contrast Bootstrap to help reduce the amount of time we spend doing that, so we can focus on building some other aspects of the project. Contrast Bootstrap PRO consists of a UI Kit featuring over 10000+ component variants. Together with a template of 5 admin dashboards and 23+ additional multipurpose pages template for building almost any type of website or web app. You can view a demo and learn more about Contrast by clicking here.Try contrast pro

ad-banner

Contrast Bootstrap PRO was built using the most popular CSS framework Bootstrap to help build your next landing, admin SAAS, prelaunch etc project with a clean, prebuilt and well documented template and UI components.Learn more about Contrast

Related Posts

Comments

...