My element's display property is being changed to none by Angular 6

UPDATE: Even after removing all references to TS functions from the HTML template, the issue persists. The problem remains even if I clear out the contents of the SCSS classes side-nav-submenu and submenu-open. However, the problem disappears when I remove the side-nav-submenu class from the ul.

I'm working on developing a mobile side navigation menu that consists of an unordered list with buttons as list items. Each button is followed by another unordered list which acts as a submenu. The submenu has height:auto; and max-height:0;, with transition effects set on max-height for expanding and contracting when toggled.

I've encountered a very peculiar and specific bug. If I open a submenu, then click on one of its buttons (which currently have no functionality), and then click anywhere else, the submenu mysteriously receives style="display: none;" in real-time according to the dev tools.

I'm puzzled because there is nothing in my code that explicitly sets display: none; on these submenus. My only guess is that Angular might be responsible, but I can't comprehend why. Here's a snippet of my code.

side-nav-mobile.component.html

<ul [class.nav-active]="navOpen" class="side-nav-menu-mobile">
  <div (click)="toggleSubNav($event)">
    <li id="inv" class="side-nav-button">Inventory <span class="menu-arrow" [class.arrow-down]="subNavOpen['inv']">&#8250;</span></li>

    <ul class="side-nav-submenu">
      <li>Add An Item</li>
      <li>Inventory Search</li>
    </ul>

    <li id="cust" class="side-nav-button">Customers <span class="menu-arrow" [class.arrow-down]="subNavOpen['cust']">&#8250;</span></li>

    <ul class="side-nav-submenu">
      <li>Add New Customer</li>
      <li>Awaiting Action</li>
    </ul>

    <li id="trans" class="side-nav-button">Transactions <span class="menu-arrow" [class.arrow-down]="subNavOpen['trans']">&#8250;</span></li>

    <ul class="side-nav-submenu">
      <li>Bill Of Sale</li>
    </ul>

  </div>
</ul>

side-nav-mobile.component.scss

@import "../../global-vars.scss";

.side-nav-menu-mobile {
  display: inline-block;
  background-color: $page-background-color;
  left: -100%;
  overflow: hidden;
  height: 100vh;
  width: 50%;
  position: absolute;
  transition: 0.25s ease-in-out;
  z-index: 10;
  margin: 0;
  padding: 0;
}
.side-nav-menu-mobile > div {
  margin-top: 3em;
}
.side-nav-menu-mobile > ul {
  padding: 0;
  margin: 0;
}
.side-nav-menu-mobile li {
  list-style: none;
  padding: 1em;
}
.side-nav-menu-mobile div > li {
  width: calc(100% - 1em);
  border-top: 1px solid $border-color;
  border-bottom: 1px solid $border-color;
}
.side-nav-submenu {
  height: auto;
  max-height: 0;
  overflow: hidden;
  transition: max-height 0.15s linear;
  padding-left: 1em;

  -webkit-touch-callout: none;
  -webkit-user-select: none;
  -khtml-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}
.submenu-open {
  // This is a temporary solution.
  // Implement a better approach if available.
  max-height: 8em;
}
.nav-active {
  left: 0;
}
.menu-arrow {
  display: inline-block;
  transform: rotate(0);
  transition: 0.15s ease-in;
}
.arrow-down {
  transform: rotate(90deg);
}
@media screen and (min-width: 1200px) {
  .side-nav-menu-mobile {
    display: none;
  }
}

side-nav-mobile.component.ts

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-side-nav-mobile',
  templateUrl: './side-nav-mobile.component.html',
  styleUrls: ['./side-nav-mobile.component.scss']
})
export class SideNavMobileComponent implements OnInit {
  navOpen: boolean = false;
  subNavOpen: Object = {
    inv: false,
    cust: false,
    trans: false
  };

  toggleSlide(): void {
    this.navOpen = !this.navOpen;
  }

  toggleSubNav(event): void {
    if(event.target.className.indexOf("side-nav-button") == -1) {
      return;
    }
    if(event.target.nextElementSibling.className.indexOf("submenu-open") == -1) {
      event.target.nextElementSibling.classList.add("submenu-open");
      this.subNavOpen[event.target.id] = true;
    } else {
      event.target.nextElementSibling.classList.remove("submenu-open");
      this.subNavOpen[event.target.id] = false;
    }
  }

  constructor() { }

  ngOnInit() {
  }

}

Answer №1

element, it became apparent that the "side-nav-submenu" class was being employed in both the desktop side menu and causing a conflict. This issue arose because the desktop submenus were hidden with the CSS property display: none due to the mobile view. The problem was resolved by simply renaming the class to "side-nav-mobile-submenu."

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

CSS problem: The vertical line does not extend far enough following the heading

I am encountering a small problem with my CSS code. I am trying to add a Vertical line after my heading using bootstrap utilities, but I am running into an issue. Here is a real-time example of the code I am working on: <!DOCTYPE html> <html l ...

The list style image is failing to display properly due to an issue with the external

Below is the HTML code that I have written: <!DOCTYPE html> <html> <head> <style> ul{ list-style-image:url('img/grey.png'); } </style> <body> <ul> <li>Coffee</li> <li>Tea</li&g ...

Is there a way to import TypeScript modules from node_modules using browserify?

After successfully running tsc, I am facing difficulty understanding how to import TypeScript modules from node modules. The crucial section of my gulp file is as follows: gulp.task('compile-ts', ['clean'], function(){ var sourceTsF ...

Activate the front camera on an Android device using HTML5 or JavaScript

I'm currently working on a web app that needs to access the front camera of Android phones running version 4.0 or higher. After taking a picture, the app should then upload the captured image to my own server. Is there a way to launch the front camer ...

styled components are having issues with background gradients and opacity not functioning properly

Hello, I am currently working with styled components and have the following global style code: const GlobalStyle = createGlobalStyle` html{ font-family: roboto; background: linear-gradient(45deg,rgba(137,255,255,0.5),rgba(161,252,143, 0 ...

Turning jQuery Mobile XML into a Table

Recently, I switched from using the jQuery Mobile framework's grid layout to a table layout. However, I encountered an issue where only the first line of data from my XML parse was being displayed. There are actually 8 pieces of data that should be sh ...

Having trouble incorporating the Nativescript Localstorage plugin into a shared code project

Recently, I've been experimenting with Nativescript code sharing between web and mobile using Angular. After installing the latest stable version and attempting to use npm modules such as "nativescript-localstorage," I encountered a frustrating issue. ...

Guide to centering Embed horizontally with Bootstrap 5

My goal is to create a centralized input with an image positioned above it. I have been following the guidelines provided in the position documentation, but the image still does not align horizontally: https://i.sstatic.net/awWxL.png Here's the code ...

Issues with CSS3 keyframe animations failing to function in Firefox and IE

My CSS code is functioning properly in Chrome and Safari, but not in Firefox, IE, and Opera when I create the @keyframes rotate {}. It seems that these 4 lines might be causing the issue: animation-duration: 4s; animation-timing-functio ...

HTML/CSS flowchart illustration

Is there a user-friendly method to create a flow or swimline diagram without relying on scripting or tables? Specifically, I'm looking for a way to display different hosts sending packets (with hosts along the X axis, time along the Y axis, and arrows ...

Creating a Bootstrap column that is the height of two rows

Trying to create a calculator and have achieved perfect alignment for most elements, but struggling to fit in one last button. I need this button to be the height of two rows, but adjusting its height directly causes issues with rows below. https://i.ssta ...

Enhancing page accessibility through the implementation of shortcuts

Greetings to all members of the community! I am currently in the process of improving a website by implementing better accessibility practices. I have some concerns regarding a page that displays a large number of repeated result blocks, each containing ...

Guide to align elements (cards) side by side in a row within a container element (div)

I have a question that bears resemblance to another one found on this site. I decided to create my own version of the Material UI Card example from https://material-ui.com/demos/cards/. You can view and edit my version on the sandbox editor by clicking he ...

What could be causing the Custom CSS file to stop functioning properly?

Currently, I am in the process of developing an eCommerce website utilizing PHP, Bootstrap, and JQuery. In order to structure the header file for this project, I have followed the following format: <!DOCTYPE html> <html> <head> ...

Using an SVG as your cursor: A step-by-step guide

Is there a way to use an SVG image as the mouse cursor when hovering over a specific div? I've been trying to make it work by adding this line of code: cursor: url(http://elusivethemes.com/assets/down.svg), auto; Unfortunately, it doesn't seem t ...

The error message encountered states that the property 'status' is not found within the object type 'Error'

While working with Angular 2, I encountered an issue when passing an Error object from an HTTP request into the logAndPassOn method. The error message "Property 'status' does not exist on type 'Error'" was displayed. Although the typeof ...

Challenges encountered while using Selenium WebDriver to upload images

I'm having trouble adding an image as a normal attachment to an email (not as an inline image). When inspecting the HTML DOM with Firebug, I noticed there are two elements with xpath //input@type='file', which is due to the presence of two b ...

The querySelector method appears to have trouble locating elements within an ng-container

I'm currently in the process of building a web application using Angular Dart. My goal is to locate a 'div' element within the component by using document.querySelector() and then add some content to its body. Unfortunately, I'm encou ...

What is the best way to align a div to the bottom of its container div in Bootstrap 5?

Within the Bootstrap 5 code snippet provided, there is a nested div with the ID "div-in-right-div" inside another div with the ID "right-div". Currently, "div-in-right-div" is aligned with the top of "right-div". The question arises: How can you adjust the ...

Can you identify the selected item on the menu?

My goal is to highlight the menu item for the current page by adding a "current" class. I've tried several code snippets from this website, but most of them didn't work as expected. The code I'm currently using almost works, but it has a sma ...