Hide div if I click on the class in Angular - html

I am making a Hamburger menu for my Angular practice (version 13).
When I click on the hamburger icon it hides and opens the menu (toggle), but I also want to hide the menu after I clicked on one of the elements (Home,About me,My work).
My planned solution is to hide the nav class if I click on one of the nav__item classes.
My Header Component:
Html:
<header>
<div class="logo">
<img src="assets/img/norberticon.png" alt="">
</div>
<button class="nav-toggle" aria-label="toggle navigation" (click)="toggleShow()">
<span class="hamburger"></span>
</button>
<nav class="nav" *ngIf="isShown">
<ul class="nav__list" >
<li class="nav__item"><a routerLink="/" class="nav__link">Home</a></li>
<li class="nav__item"><a routerLink="about" class="nav__link">About me</a></li>
<li class="nav__item"><a routerLink="/mywork" class="nav__link">My Work</a></li>
</ul>
</nav>
</header>
.Ts code that toggles the Menu:
isShown: boolean = false ; // hidden by default
toggleShow() {
this.isShown = ! this.isShown;
}
The Menu:

Adding toggleShow() function to each li element does not work?
<li class="nav__item"><a routerLink="/" class="nav__link" (click) = "toggleShow()">Home</a></li>
<li class="nav__item"><a routerLink="about" class="nav__link" (click) = "toggleShow()">About me</a></li>
<li class="nav__item"><a routerLink="/mywork" class="nav__link" (click) = "toggleShow()">My Work</a></li>

CSS3:
Create one hide and one show property as -
.hidden{
display:none;
}
.show{
display:block; //or inline etc
}
JavaScript:
const elementToBeHidden = document.
querySelector('.classselector');
const ToggleShow =()=>{
elementToBeHidden.classList.toggle('hidden');
}
This will effectively toggle between hiding and showing any element you want to as many times you want.
Note that this is just psuedocode to clear up the logic
Hope this helps you.....PEACE OuT 👍

Related

Can I nest block or element inside modifier in BEM CSS?

I'd like to write something like that:
.block__element--modifier .blok {
/*styles*/
}
The thing is I don't know if above way is right in BEM methodology which I use. In practice I'm creating a navigation for website which is going to be opened by hamburger-button. When user click the button (and the navigation is closed and invisible) then menu is going to be show (and in main navigation block will be added the other navigation--opened class). Something like the following:
<nav class="navigation navigation--opened">
<ul class="navigation__list">
<li class="navigation__item">
<a class="navigation__link" href="#">Link 1</a>
</li>
<li class="navigation__item">
<a class="navigation__link" href="#">Link 2</a>
</li>
<li class="navigation__item">
<a class="navigation__link" href="#">Link 3</a>
</li>
<li class="navigation__item">
<a class="navigation__link" href="#">Link 4</a>
</li>
</ul>
<button type="button" class="hamburger">
<img src="obrazek.png" alt="Button to open menu" class="hamburger__icon hamburger__icon--opened">
<img src="obrazek.png" alt="Button to close menu" class="hamburger__icon hamburger__icon--closed">
</button>
</nav>
When the navigation--opened class exists that means the menu has been opened. So now I want to style opened menu. To make it I'm writing the follow:
.navigation--opened .navigation__list {
/*styles*/
}
.navigation--opened .navigation__item {
/*styles*/
}
.navigation--opened .navigation__link {
/*styles*/
}
I make it to style opened menu and its components.
Is .block__element--modifier .blok {/*styles*/} way proper in BEM? Thank you in advance for answer.
It's fine to style block's child elements by modifier of the parent block. So
.navigation--opened .navigation__list {
/*styles*/
}
.navigation--opened .navigation__item {
/*styles*/
}
.navigation--opened .navigation__link {
/*styles*/
}
is fine.
But it's better to avoid styling other blocks using nested selectors.
See https://en.bem.info/methodology/css/#nested-selectors for more info.

How do I retrieve the user selected option from my dropdown list?

I have created a nav-bar which consists of a dropdown list. How do I retrieve the user selected option and display it in my Home component? The list items displayed are directly from a JSON file and I am not using the "select-option" tags for it. Is there anyway to do this with my code?
Header.component.html
<nav class="navbar navbar-expand-sm">
<div class="container-fluid">
<ul class="nav navbar-nav">
<li class="dropdown">
<a class="navbar-brand" href="#room" class="dropdown-toggle" data-toggle="dropdown" role="button"> Rooms </a>
<ul class="dropdown-menu" >
<li *ngFor="let room of room_list.rooms">
<a class="navbar-brand" href="#room" [routerLink]="['./room']" routerLinkActive="router-link-active">{{room.name}}</a>
</li>
</ul>
</li>
<li>
<a class="navbar-brand" href="#home" [routerLink]="['/']" routerLinkActive="router-link-active" >Home</a>
</li>
</ul>
</div>
</nav>
Header.component.ts
export class HeaderComponent implements OnInit {
room_list: any;
constructor() {
}
ngOnInit() {
//this.room_list = roomlist.default;
this.room_list =JSON.stringify(roomlist.default);
this.room_list=JSON.parse(this.room_list);
console.log(roomlist);
}
For your issue related to #freddy 's answer #V_stack
<li .. (click)="selectedRoom(room)" (blur)= "DropdownBlur = true " >
(blur) will lose focus of drop-down after the condition written after it will be true.
TS file, initialize DropdownBlur with false and set it to true when you want to close the drop-down
DropdownBlur = false;
...
selectedRoom(room):void {
console.log(room);
this.DropdownBlur = true;
}
On the li you can make a call when the user clicks
<li .. (click)="selectedRoom(room)" >
Header.component.ts
selectedRoom(room):void {
console.log(room);
}

Style not rendering in innerHTML in Angular2

I am developing a page in Angular2/4 which has a left navigation bar. I put this left menu in a separate component and nesting this in the main component. Reason being I need to reuse this left menu in multiple pages/ components, however, different page will have different menu items. So, I am trying to pass the menu items from the main component through #Input() binding:
sidebar.component.ts:
#Component({
selector:'sidebar',
templateUrl: './sidebar.component.html',
styleUrls: ['./sidebar.component.css']
})
export class SidebarComponent{
#Input() innerContent:string;
....
sidebar.component.html:
<div id='mySidebar' class='sidebar-primary sidebar-animate' [class.sideBarShow]='isSideBarShow'
[class.sideBarHide]='!isSideBarShow' >
<ul class='goo-collapsible' style="margin-bottom:0px" >
<li class='header'><!--Common header in left menu-->
<div class='glyphicon glyphicon-arrow-left' style='float: left; width: 33%;padding-left:5px;cursor:pointer;' (click)='editTheme()'></div>
<div class='glyphicon glyphicon-search' style='float: left; width: 34%;cursor:pointer;'></div>
<div class='glyphicon glyphicon-remove' style='float: left; width: 33%;cursor:pointer;' (click)='sideBarClose()'></div>
</li>
</ul>
<ul class="goo-collapsible" style="margin-bottom:0px" [innerHTML] = "innerContent"><!--This should hold menu items depending upon main page-->
</ul>
</div>
mainpage.component.html:
-------------------------
sidebar [isSideBarShow]="isSideBarShowShared" [innerContent] =
"viewLayout" (notify) ="onNotify($event)"></sidebar>
mainpage.component.ts
-----------------------
....
ngOnInit() {
//this.nav.hide();
this.viewLayout = `<!li class='dropdown'><a (click)='changeHeaderTextAlign()'><span class='icon-table'></span> Top Navigation Bar</a>
<ul>
<li ><a href='#'>Content</a></li>
<li ><a href='#'>Comments</a></li>
<li ><a href='#'>Tags</a></li>
</ul>
</li>
<li><a href='#'><span class='icon-folder-open'></span><input type='color' value='#999999' (input)='headerColorChange($event)'> Header Image with Text</a></li>
<li class='dropdown'><a><span class='icon-user'></span><input type='range' min='0.1' max='1' step='0.1' (change)='setHeaderOpacity($event)'> Page Section</a>
<ul>
<li ><a href='#'>Group</a></li>
<li ><a href='#'>User</a></li>
</ul>
</li>
<li><a (click) = 'addDynamicComponent()'><span class='icon-cogs'></span> Footer and Social Media</a>
</li>`;
}
What I am seeing is that the style is being dropped from the innerHTML. I followed following instructions, but no luck:
Angular2 innerHtml binding remove style attribute
I learned that accessing DOM directly is not recommended in angular2 but any better/ recommended approach for this scenario? Any help would be very much appreciated!!
regards
The thing you are trying to achieve by innerHTML can also be done using ng-content which will enclose the specific component html in your sidemenu html.
Your sidemenu.html should look like this
<div id='mySidebar' class='sidebar-primary sidebar-animate' [class.sideBarShow]='isSideBarShow'
[class.sideBarHide]='!isSideBarShow' >
<ul class='goo-collapsible' style="margin-bottom:0px" >
<li class='header'><!--Common header in left menu-->
<div class='glyphicon glyphicon-arrow-left' style='float: left; width: 33%;padding-left:5px;cursor:pointer;' (click)='editTheme()'></div>
<div class='glyphicon glyphicon-search' style='float: left; width: 34%;cursor:pointer;'></div>
<div class='glyphicon glyphicon-remove' style='float: left; width: 33%;cursor:pointer;' (click)='sideBarClose()'></div>
</li>
</ul>
<ul class="goo-collapsible" style="margin-bottom:0px">
<!--This should hold menu items depending upon main page-->
<ng-content></ng-content>
</ul>
</div>
You application-main.component.html or any file where you want to include sidemenu comeponent should be like this :-
<div>
..... Blah Blah whatever page component html
<sidebar [isSideBarShow]="isSideBarShowShared" (notify)="onNotify($event)">
<!---- Whatever goes inside the component tag , is part of ng-content ---->
<!li class='dropdown'><a (click)='changeHeaderTextAlign()'><span class='icon-table'></span> Top Navigation Bar</a>
<ul>
<li ><a href='#'>Content</a></li>
<li ><a href='#'>Comments</a></li>
<li ><a href='#'>Tags</a></li>
</ul>
</li>
<li><a href='#'><span class='icon-folder-open'></span><input type='color' value='#999999' (input)='headerColorChange($event)'> Header Image with Text</a></li>
<li class='dropdown'><a><span class='icon-user'></span><input type='range' min='0.1' max='1' step='0.1' (change)='setHeaderOpacity($event)'> Page Section</a>
<ul>
<li ><a href='#'>Group</a></li>
<li ><a href='#'>User</a></li>
</ul>
</li>
<li><a (click) = 'addDynamicComponent()'><span class='icon-cogs'></span> Footer and Social Media</a>
</li>
</sidebar>
</div>
Also you can put multiple ng-content in a component using [select] attribute.
<ng-content select="[header]"></ng-content>
And in the other component file using it, just on the parent html tag give the name assigned in select as an attribute to the tag like this..
<div header> Blah blah..... </div>

How can I make the submenu in the MaterializeCSS dropdown?

I am trying to have a submenu dropdown inside a dropdown, using MaterializeCSS framework. I tried with the following code, but it didn't work.
<!-- this the main dropdown -->
<ul id="MainDropDown" class="dropdown-content">
<li>Dropdown1<span class="right caret">►</span></li>
<li>Dropdown2<span class="right caret">►</span></li>
<li>Dropdown3<span class="right scaret">►</span></li>
</ul>
<ul id="drop1" class="dropdown-content">
<li>Create</li>
</ul>
<ul id="drop2" class="dropdown-content">
<li>Create</li>
<li>Update</li>
</ul>
<ul id="drop3" class="dropdown-content">
<li>Create</li>
</ul>
I was having the same issue myself.
Turns out it's as simple as nesting another dropdown link,
setting an appropriate gutter, and making sure the overflow of
dropdown-content is set to visible.
Here is a link to the modified jsfiddle linked in Nested dropdowns in materialize
https://jsfiddle.net/fb0c6b5b/
$('.dropdown-button2').dropdown({
inDuration: 300,
outDuration: 225,
constrain_width: false, // Does not change width of dropdown to that of the activator
hover: true, // Activate on hover
gutter: ($('.dropdown-content').width()*3)/2.5 + 5, // Spacing from edge
belowOrigin: false, // Displays dropdown below the button
alignment: 'left' // Displays dropdown with edge aligned to the left of button
}
);
.dropdown-content{
overflow: visible !important;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.6/css/materialize.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a class='dropdown-button btn' href='#' data-activates='dropdown1' data-beloworigin="true">Drop Me!</a>
<ul id='dropdown1' class='dropdown-content'>
<li>two</li>
<li class="divider"></li>
<li>three</li>
<li><a class='dropdown-button2 d' href='#' data-activates='dropdown2' data-hover="hover" data-alignment="left">Drop Me!</a></li>
</ul>
<ul id='dropdown2' class='dropdown-content'>
<li>one</li>
<li>two</li>
<li class="divider"></li>
<li>three</li>
</ul>
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.6/js/materialize.min.js"></script>
I tried it here but my was aligned on the same line from the dropdown, then did it and it worked:
.dropdown-content {
overflow-y: visible;
}
.dropdown-content .dropdown-content {
margin-left: 100%;
}
Based on the answer from #LibanH and updated for version v1.0.0, you need some change.
constrain_width must be replaced by constrainWidth and gutter is no longer an option.
In order to display the nested dropdown at the good position, we need to get the height of the selected item in first dropdown and set position with CSS
$('.dropdown-button').dropdown({
onOpenStart: function() {
var selectedItem = $(this).find('.selected');
$('#dropdown2').css('top', selectedItem.outerHeight() + "px");
}
});
$('.dropdown-button2').dropdown({
constrainWidth: false //change
hover: true,
alignment: 'left'
}
);
and add some CSS
#dropdown2 {
position: absolute;
left: 100%;
}

dynamically highlighting a tab using css

I have the following code and I want to highlight the currently selected tab using css.
<div id="Maintabs">
<ul class"tablist">
<li><a href="AshukuWeb.jsp?VIEW=Summary" target=_top>Summary</a></li>
<li><a href="AshukuWeb.jsp?VIEW=Advanced" target=_top>Advanced</a></li>
<li><a href="AshukuWeb.jsp?VIEW=Expert" target=_top>Expert</a></li>
</ul>
</div>
is there any way I can do this? I know css hover gives the element on which mouse is hovere, is there something similar for selected
thanks guys,
yes I do need dynamic handling, so I did the way you told. I capture the click event on that tab and the class. in css I apply the required styles to that class but it doesn't work.
here is my code:
in javaScript:
$('#summary').click(function(){
$(this).addClass("selected");
alert(" summary");
});
HTML code:
<div id="Maintabs">
<ul>
<li style="width: 100px;"><a id="summary" href="AshukuWeb.jsp?VIEW=Summary" target=_top>Summary</a></li>
<li style="width: 100px;"><a id="advanced" href="AshukuWeb.jsp?VIEW=Advanced" target=_top>Advanced</a></li>
<li style="width: 100px;"><a id="expert" href="AshukuWeb.jsp?VIEW=Expert" target=_top>Expert</a></li>
</ul>
</div>
CSS code:
.selected{
background-color:#FEE0C6;
}
what do you think I am doing wrong??
I'm not sure how you are using pure CSS to produce a tab effect. You would normally need Javascript or jQuery to dynamically change what the current tab is.
However, if you are using Javascript or jQuery for the tab effect, you could simply add a class to highlight the selected tab.
For example, this could be your jQuery:
$("#tab1").addClass("selected-tab");
And this your CSS:
.selected-tab
{
/*Some style to highlight it and show it's the selected tab*/
}
You're going to want to make an active class. By giving your li the defined class active. Then you can use css to make the .active a different color, size, shape, etc
Here's an example (first li):
HTML
<div id="Maintabs">
<ul class"tablist">
<li class="active"><a href="AshukuWeb.jsp?VIEW=Summary" target=_top>Summary</a></li>
<li><a href="AshukuWeb.jsp?VIEW=Advanced" target=_top>Advanced</a></li>
<li><a href="AshukuWeb.jsp?VIEW=Expert" target=_top>Expert</a></li>
</ul>
</div>
Here's an example (second li):
HTML
<div id="Maintabs">
<ul class"tablist">
<li><a href="AshukuWeb.jsp?VIEW=Summary" target=_top>Summary</a></li>
<li class="active"><a href="AshukuWeb.jsp?VIEW=Advanced" target=_top>Advanced</a></li>
<li><a href="AshukuWeb.jsp?VIEW=Expert" target=_top>Expert</a></li>
</ul>
</div>
Here's an example (third li):
HTML
<div id="Maintabs"> <ul class"tablist">
<li><a href="AshukuWeb.jsp?VIEW=Summary" target=_top>Summary</a></li>
<li><a href="AshukuWeb.jsp?VIEW=Advanced" target=_top>Advanced</a></li>
<li class="active"><a href="AshukuWeb.jsp?VIEW=Expert" target=_top>Expert</a></li>
</ul>
</div>
CSS
#maintabs.active {background-color: #000;}
#maintabs {background-color: #ccc;}
The result will be the active tab being black (#000), and the inactive tabs being light grey (#ccc)