Bootstrap 4 Navbar doesnt collapse in Angular4 - css

I'm working with Bootstrap 4 and Ng-Bootstrap in an Angular 4 Project,
i have linked the que bootstrap.css in my angular-cli.json this way
"styles": [
"../node_modules/bootstrap/dist/css/bootstrap.css",
"styles.css"
],
and imported the NgbModule in my app.module that looks like this
import { BrowserModule } from '#angular/platform-browser';
import { NgModule } from '#angular/core';
import { NgbModule } from '#ng-bootstrap/ng-bootstrap';
import { AppComponent } from './app.component';
#NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
NgbModule.forRoot()
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
i made a Navbar copying the documentation from the bootstrap website and added the toggle component from ng-bootstrap (it works well) the code looks like this
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="#">Navbar</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-
target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-
expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="#">Home <span class="sr-only">(current)</span>
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li>
<div ngbDropdown class="d-inline-block">
<div class="nav-link" id="dropdownBasic1" ngbDropdownToggle>Toggle
dropdown</div>
<div ngbDropdownMenu aria-labelledby="dropdownBasic1">
<button class="dropdown-item">Action - 1</button>
<button class="dropdown-item">Another Action</button>
<button class="dropdown-item">Something else is here</button>
</div>
</div>
</li>
<li class="nav-item">
<a class="nav-link disabled" href="#">Disabled</a>
</li>
</ul>
<form class="form-inline my-2 my-lg-0">
<input class="form-control mr-sm-2" type="search" placeholder="Search"
aria-label="Search">
<button class="btn btn-outline-success my-2 my-sm-0"
type="submit">Search</button>
</form>
</div>
</nav>
the problem starts when i scale down the window and the toggle does its work, when i press the button for collapsing the navbar it doesn't work.
i tried installing and linking the js files of jquery and popper and the bootstrap one, but it still doesn't work, the actual angular-cli.json looks like this
"styles": [
"../node_modules/bootstrap/dist/css/bootstrap.css",
"styles.css"
],
"scripts": [
"../node_modules/popper.js/dist/umd/popper.min.js",
"../node_modules/jquery/jquery.min.js",
"../node_modules/bootstrap/dist/js/bootstrap.min.js"
],
what am i doing wrong?

To work with angular you need this fix
first define this in your class public navbarCollapsed = true;
then chnage your button
<button class="navbar-toggler" type="button" (click)="navbarCollapsed = !navbarCollapsed" [attr.aria-expanded]="!navbarCollapsed" aria-controls="navbarContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
also you need to add this to your collapsing div
<div class="collapse navbar-collapse" [ngbCollapse]="navbarCollapsed" id="navbarSupportedContent">
now you don't need jquery, popper or bootstrap.js
you can find more info about this issue here.
also check ng-bootstraps working demo page code link.

I had the same problem, I used normal bootstrap 4 rather then ng-bootstrap and this way I don't need to any module in app.module file as well. it works. Below is the script add into angular.json (angular v6) and bootstrap 4+:
"scripts": ["../node_modules/jquery/dist/jquery.js",
"../node_modules/popper.js/dist/umd/popper.js",
"../node_modules/bootstrap/dist/js/bootstrap.js"]
For CSS I prefer to use Scss version so it goes like this.
"styles": [
"src/styles.scss",
"../node_modules/font-awesome/scss/font-awesome.scss"
],
and i import bootstrap into style.scss file
#import "~bootstrap/scss/bootstrap.scss";
Cheers!
JK

Look for the compiled classes, maybe you get the wrong version. I had a similar issue with the class col-sm-offset-#, the problem was that this classes doesn't exist in that version. Maybe could be the same issue
This is the route ..\node_modules\bootstrap\dist\js here we have two file
bootstrap.js and bootstrap.min.js
find into bootstrap.js the nav classes. if you could not found it, look for another Bootstrap repository.
Good luck.

this works fine for me
I installed
npm i --save #ng-bootstrap/ng-bootstrap ng add #angular/localize
app.module.ts
import {NgbModule} from '#ng-bootstrap/ng-bootstrap';
imports:[NgbModule]
NgbModule.forRoot() not working
angular.json
"styles": [ "node_modules/bootstrap/dist/css/bootstrap.min.css", "src/styles.css" ], "scripts": ["node_modules/jquery/dist/jquery.min.js",
"node_modules/bootstrap/dist/js/bootstrap.js]`
Hope this helps, even just alittle bit

Related

Change the Main Body Background Colour dependant on current Razor Page

I am working on a basic Razor Pages web app which has three main pages. I want to be able to change the body's background colour depending on which Razor Page the user is currently viewing.
The body is contained in the _Layout.cshtml file where I am able to change the colour for the whole app:
<body style="background-color: lightcoral ">
However at this late hour in the day I cannot seem to figure out how I can change this colour depending on the page I am currently on. My initial thought is to look at using JQuery to get the body element and update the background style attribute through there, but I am still not certain how I tell which page I am currently on to decide the colour.
If you want to add different background colors to different areas,you can add body id with current area name,and add styles to different ids.Here is a demo:
_layout.cshtml:
#{
var routeUrl = this.Url.RouteUrl(ViewContext.RouteData.Values);
var area = routeUrl.Split("/")[1];
}
<body id="#area">
<header>
<nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">
<div class="container">
<a class="navbar-brand" asp-area="" asp-page="/Index">RazorPageWithAreas</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".navbar-collapse" aria-controls="navbarSupportedContent"
aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="navbar-collapse collapse d-sm-inline-flex justify-content-between">
<ul class="navbar-nav flex-grow-1">
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-page="/Index">Home</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-page="/Privacy">Privacy</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" asp-area="Entry" asp-page="/Create">Entry/Create</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" asp-area="Session" asp-page="/Create">Session/Create</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" asp-area="Trip" asp-page="/Create">Trip/Create</a>
</li>
</ul>
</div>
</div>
</nav>
</header>
...
<style>
#Session {
background-color: #ffefef;
}
#Trip {
background-color: #fff8d7;
}
#Entry {
background-color: #eafff3;
}
</style>
</body>
folder structure:
result:
So I have come up with this which works, however the background is still flickering white when I navigate between pages. I would be interested to hear anyone else's solutions that might be better, more performant... Even to include some transitions ;)
I have three areas each containing a Create, Edit, Details, Index, Delete page. The Areas are Season, Trip, Entry. So, for each page under Season I have added a hidden field:
<input type="hidden" name="hdnPageSelector" id="hdnSeasonPage" />
Each page under Trip I have added:
<input type="hidden" name="hdnPageSelector" id="hdnTripPage" />
and each page under Entry I have added:
<input type="hidden" name="hdnPageSelector" id="hdnEntryPage" />
Then I have a piece of Javascript/JQuery that executes on DOMCONTENTLOADED:
document.addEventListener("DOMContentLoaded", function () {
var pageId = $("[name ='hdnPageSelector']").attr('id');
if (pageId == "hdnSeasonPage") {
$("body,html").css("background-color", "#ffefef");
}
if (pageId == "hdnTripPage") {
$("body,html").css("background-color", "#fff8d7");
}
if (pageId == "hdnEntryPage") {
$("body,html").css("background-color", "#eafff3");
}
});
As I mentioned, this seems to work but would be interested in hearing some better solutions to this.
You could set a value in ViewData in your code and then just use that in the layout page
<body style="background-color: #ViewData["BackgroundColor"]">
Personally I would probably use a class to drive this
<body class=" #ViewData["BackgroundClass"]">

Navbar collapse button does not show items for bootstrap 4.5.0 and Nextjs 9.4.4

I am trying to create a very simple nextjs demo project using bootstrap for styling.
I am able to use the basic styling very easily by first installing bootstrap using npm and then including it in my app.js
import 'bootstrap/dist/css/bootstrap.min.css'
I created a navbar and it worked fine but when I tried to make it collapse when my screen size shrinks does not seem to work.
I tried to find my way through it and came to know that it requires popperjs and jquery in particular order, here, and I did the same. I included the 2 in my app.js file. My import is the below.
import 'jquery/dist/jquery.min.js'
import '#popperjs/core/dist/umd/popper.min.js'
import 'bootstrap/dist/css/bootstrap.min.css'
But still my toggle button does not pop the navbar items when clicked. I can see not errors on my browser console and the dev console from where I run my project.
Here is a section of dependencies from my package.json
"dependencies": {
"#popperjs/core": "^2.4.2",
"bootstrap": "^4.5.0",
"jquery": "^3.5.1",
"next": "9.4.4",
"react": "16.13.1",
"react-bootstrap": "^1.0.1",
"react-dom": "16.13.1"
}
I can see similar questions on SO but all of them are for older bootstrap version with different class names for bootstrap components.
Here is my component
/**
* className to create the navbar using bootstrap, TODO, intial version
*/
export default class NavBar extends Component {
render() {
return (
<div id="rootdiv">
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<button
class="navbar-toggler"
type="button"
data-toggle="collapse"
data-target="#navbarTogglerDemo01"
aria-controls="navbarTogglerDemo01"
aria-expanded="false"
aria-label="Toggle navigation"
>
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarTogglerDemo01">
<a class="navbar-brand" href="#">
Hidden brand
</a>
<ul class="navbar-nav mr-auto mt-2 mt-lg-0">
<li class="nav-item active">
<a class="nav-link" href="#">
Home <span class="sr-only">(current)</span>
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">
Link
</a>
</li>
<li class="nav-item">
<a class="nav-link disabled" href="#">
Disabled
</a>
</li>
</ul>
</div>
</nav>
</div>
);
}
}```
I don't know why it did not work with native bootstrap but I was able to get it to work after switching to react-bootstrap. I hope this will help some one in future.
Also there is no need to manually import popper and jquery in the app.js file.
I only imported the bootstrap min file.
import 'bootstrap/dist/css/bootstrap.min.css'
import Navbar from "react-bootstrap/Navbar";
import Nav from "react-bootstrap/Nav";
import React from "react";
const RBNavBar = () => {
return (
<Navbar bg="light" expand="lg" id="myNavbar">
<Navbar.Brand href="#home"><img src="/logo_lpb_small.png"></img></Navbar.Brand>
<Navbar.Toggle aria-controls="basic-navbar-nav" />
<Navbar.Collapse id="basic-navbar-nav">
<Nav className="ml-auto" id="myNavItem">
<Nav.Link href="/" id="myNavItem">Home</Nav.Link>
<Nav.Link href="contact" id= "myNavItem">Contact</Nav.Link>
<Nav.Link href="about" id= "myNavItem">About Us</Nav.Link>
</Nav>
</Navbar.Collapse>
</Navbar>
);
};
export default RBNavBar;

How to toggle dark mode in Angular

I am trying to toggle the dark mode when a user toggles the switch, which is in the header component. See image below:
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="#">Navbar</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNavAltMarkup">
<div class="navbar-nav">
<a class="nav-item nav-link active" href="/">Home <span class="sr-only">(current)</span></a>
<a class="nav-item nav-link" href="/about">About</a>
<a class="nav-item nav-link" href="/contact">Contact</a>
<a class="nav-item nav-link" href="/resume">Resume</a>
</div>
</div>
<div class="custom-control custom-switch">
<input (change)="onChangeToggle()" type="checkbox" class="custom-control-input" id="toggleTheme">
<label class="custom-control-label" for="toggleTheme">Dark Mode</label>
</div>
</nav>
My header component ts file holds a boolean value called setDark which is triggered in an event emitter through the Output decorator :
// header.component.ts
import { Component, OnInit , Output, EventEmitter} from '#angular/core';
#Component({
selector: 'app-header',
templateUrl: './header.component.html',
styleUrls: ['./header.component.css']
})
export class HeaderComponent implements OnInit {
#Output() mode = new EventEmitter<boolean>();
setDark = false;
constructor() { }
ngOnInit(): void {
}
onChangeToggle() {
this.setDark = !this.setDark;
this.mode.emit(this.setDark);
console.log(this.setDark);
}
}
Then in the app.comoonent.html receive boolean
<app-header (mode)="receiveMode($event)"></app-header>
<router-outlet></router-outlet>
// app.component.ts
import { Component } from '#angular/core';
#Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
setMode = false;
receiveMode($event) {
this.setMode = $event;
console.log("MODEEEE", this.setMode);
}
title = 'about-me';
}
I have tried to wrap the div around the app-header and router-outlet like so:
<app-header (mode)="receiveMode($event)"></app-header>
<div class="darkTheme">
<router-outlet></router-outlet>
and using ngClass:
<div [ngClass]="{
darkTheme: setMode
}">
</div>
But it doesn't work. I did check to see whether or not the boolean is being logged into the console when toggling the switch. It is.
Have a look at this stackblitz: https://stackblitz.com/edit/angular-rubaka?file=src/app/toolbar-multirow-example.html
it toggles the class "dark-theme" on the #container:ElementRef:
<div #container style="padding: 4rem;" class="mat-typography dark-theme">
The styles.scss defines two different theme's on line 28 (default theme)...
// Include the default theme styles.
#include angular-material-theme($candy-app-theme);
and on line 39 (dark theme)...
.dark-theme {
#include angular-material-theme($dark-theme)
};
Hope this helps.

Toggle button seems not have css

I need to create a dropdown button, so I installed bootstrap and ng-boostrap. This is my html:
<div ngbDropdown class="d-inline-block">
<button class="btn btn-outline-primary" id="dropdownBasic1" ngbDropdownToggle>Toggle dropdown</button>
<div ngbDropdownMenu aria-labelledby="dropdownBasic1">
<button class="dropdown-item">Action - 1</button>
<button class="dropdown-item">Another Action</button>
<button class="dropdown-item">Something else is here</button>
</div>
</div>
and in my app.module.ts I put:
imports:{
NgbModule.forRoot()}
I have just also put bootstrap css in my angular.json file.
The problem is that dropdown appear grey like it doesn't have css. I don't know what is the problem. Anyone can help me?
EDIT
My angular.json file have this:
"styles": [
"src/styles.css",
"node_modules/bootstrap/dist/css/bootstrap.min.css",
"node_modules/font-awesome/css/font-awesome.css"
],
Step 1) install #ng-bootstrap/ng-bootstrap
npm install --save #ng-bootstrap/ng-bootstrap
Step 2) Import the ng-bootstrap package in app.module.ts file
import {NgbModule} from '#ng-bootstrap/ng-bootstrap';
#NgModule({
...
imports: [NgbModule, ...],
...
})
export class AppModule {
}
Note: No need of doing NgbModule.forRoot()
Step 3) Use your html code in app.component.html file
<div ngbDropdown class="d-inline-block">
<button class="btn btn-outline-primary" id="dropdownBasic1" ngbDropdownToggle>Toggle dropdown</button>
<div ngbDropdownMenu aria-labelledby="dropdownBasic1">
<button class="dropdown-item">Action - 1</button>
<button class="dropdown-item">Another Action</button>
<button class="dropdown-item">Something else is here</button>
</div>
</div>
I have created the same example, which is forked from the ng-bootstrap.
View Demo
Hope this will help you.
I solved this problem by adding line:
#import '~bootstrap/dist/css/bootstrap.min.css'
to my /src/styles.css file.
(You need have bootstrap or bootstrap-css-only installed. To install run command npm install bootstrap-css-only --save)

Archives widget dropdown to be styled like Bootstrap dropdown

In WordPress, there is default Archives widget that displays a form select with the list of monthly archives and on change executes:
document.location.href=this.options[this.selectedIndex].value;
I need the same functionality but styled like a Bootstrap dropbdown
<div class="btn-group dropdown">
<button class="btn btn-link">Dropdown Button</button>
<button class="btn dropdown-toggle btn-large" data-toggle="dropdown"><span class="caret"></span></button>
<ul class="dropdown-menu">
<li>Link</li>
<li>Link</li>
<li>Link</li>
</ul>
</div>
How can I achieve this?
Is there any class that I could add to the select, so that I get the Bootstrap dropdown style along with the same functionality?
There's no way of modifying the output for that widget. You have to copy the whole WP_Widgets_Archives and create your own widget.
See the Widgets_API and this old, but still valid article: The complete guide to creating widgets in WordPress 2.8.
You'll have to place your code in this block:
if ( $d ) {
?>
<div class="btn-group dropdown">
<button class="btn btn-link">Dropdown Button</button>
<button class="btn dropdown-toggle btn-large" data-toggle="dropdown"><span class="caret"></span></button>
<ul class="dropdown-menu" role="menu">
<?php wp_get_archives( array('type' => 'monthly', 'format' => 'html', 'show_post_count' => $c)); ?>
</ul>
</div>
<?php
}
Note the format html for wp_get_archives, this produces a <li> list.
If you are using SASS
#import "~bootstrap/scss/bootstrap";
.widget_archive select {
#extend .form-control;
}

Resources