The panel header is clickable and overlaps with the header buttons

My panel component includes a header with a title and buttons positioned in the right corner. Currently, the downward arrow (chevron) is used to toggle the expansion/minimization of the panel's contents.

When I attempt to make the header clickable to control the expanding/minimizing of the panels contents, it also affects the "+" symbol button's click event, causing the panel to expand/minimize which is not desired.

(click)="toggleShowHide()" shouldn't impact the "+" button.

How can I ensure that the "+" symbol remains unaffected by the clickable event on the whole header? Keep in mind that other buttons may be added beside the "+"" symbol in the future.

https://i.stack.imgur.com/S10nE.jpg

dashboard.html

<div style="width: 600px">
  <custom-panel heading="TABLE" enableShowHide="true">
    <span class="panel-tools" >
      <div class="input-group">
        <span  title="New content" (click)="addContent()">
          <i class="tools-plus fas fa-plus" aria-hidden="true"></i>
        </span>
      </div>
    </span>
    //content
  </custom-panel>
</div>

panel.html

<div class="panel panel-custom">
  <div class="panel-heading" *ngIf="heading"  (click)="toggleShowHide()">
    {{heading}}

    <span *ngIf="enableShowHide" class="float-right panel-chevron" (click)="toggleShowHide()" style="cursor: pointer;">
      <i [hidden]="!showPanel" title="Hide" class="tools-plus fas fa-chevron-down" aria-hidden="true"></i>
      <i [hidden]="showPanel" title="Show" class="tools-plus fas fa-chevron-right" aria-hidden="true;"></i>
    </span>

    <span class="float-right">
      <ng-content select=".panel-tools"></ng-content>
    </span>
  </div>

  <div class="panel-body" [ngClass]="{'panel-scrolling': panelScrolling }" [hidden]="shouldHidePanel()">
    <div class="mb-3">
    </div>

    <ng-content></ng-content>
  </div>
</div>

panel.ts

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

@Component({
  selector: 'panel',
  templateUrl: './panel.component.html',
  styleUrls: ['./panel.component.scss']
})
export class PanelComponent {
  @Input() heading: string;
  @Input() size: string;
  @Input() panelScrolling = false;
  @Input() enableShowHide = false;
  @Input() hideFirst = false;
  showPanel = true;

  toggleShowHide(): void {
    this.showPanel = !this.showPanel;
  }

  shouldHidePanel(): boolean {
    if (this.enableShowHide) {
      if (this.hideFirst) {
        this.showPanel = false;
        this.hideFirst = false;
      }

      return !this.showPanel;
    }

    return false;
  }
}

panel.scss

.panel-body {
  padding: 12px;
}

.panel-custom {
  margin: 10px 15px 10px 0;
  border: none;
  border-radius: 0;
  box-shadow: 0 4px 4px 0 rgba(0, 0, 0, 0.15);
  background-color: white;

  .panel-heading {
    color: black;
    font-size: 16px;
    border-bottom: 1px solid black;
    padding: 8px 12px;
    @media (max-width: 1500px) {
      font-size: 14px;
    }
  }

Answer №1

I suggest delving into the concept of "event bubbling" by checking out this resource. By adding an event listener to a parent element, you can automatically apply it to all child elements (unless they have their own event handlers that prevent propagation of parent events, such as links).

In your event handler method, you can use the "stopPropagation" method on the event object as an argument.

Feel free to leave a comment if you need further assistance - I'm here to help!

Answer №2

Preventing the event from spreading any further is a wise decision.

<div class="panel-heading" *ngIf="heading" (click)="toggleShowHide($event)">
<span title="New content" (click)="addContent($event)">
  toggleShowHide(event : any): void {
    event.stopPropagation();
    this.showPanel = !this.showPanel;
  }

  addContent(event : any): void {
    event.stopPropagation();
  }

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

I possess legacy Angular 1 code in which I implemented div styles. How can I replicate the same functionality in Angular 11?

Recently, I have been revamping an older project and encountered a challenge in setting the style of an array where each point needs to have a unique position. The Divs are all identified by id = obj.id, and I must assign specific left and top values to ea ...

Are there any known browser compatibility issues with using "./" (dot slash) before HTML files?

Within the realm of shells, using ./ to indicate the current directory is quite common and generally not perceived as risky when specifying a path. On the other hand, in HTML coding, it's typical to omit ./ and instead directly specify the file name. ...

Interactive website built on Angular 16 offering advanced search and result display functionalities, along with options to edit and update data

Seeking guidance from experienced Angular developers as I am relatively new to the framework. Any tips or advice would be greatly appreciated. Project Overview: Front-end development using Angular, minimal focus on Back-end (C#) for now. Goal of the webs ...

What is the best way to make my page headers resize with a responsive layout?

Currently, I am working on a website that needs to be fully functional on both mobile devices and computers. I have successfully implemented Bootstrap-responsive to make many elements work, but now I am focusing on the hero-unit for the homepage. Specifica ...

steps to overlay an image over another in bootstrap:

I am trying to place an image over another image. Here is the code I have used: <!DOCTYPE html> <html lang="en"> <head> <title>Bootstrap Example</title> <meta charset="utf-8> <meta name="viewp ...

Arrange an element to appear hidden behind a text

I have a stylish horizontal navigation bar with links set up like this: <div class="blackbar"> <span class="blackbar-text"><a href="news.php">NEWS</a></span> <span class="blackbar-text"><a href="foo.php">F ...

Customize the default styles for Angular 2/4 Material's "md-menu" component

Seeking to customize default styles of md-menu in Angular Material. The challenge lies in the dynamic generation of elements by Angular Material, preventing direct access from HTML. Visual representation of DOM: https://i.sstatic.net/v8GE0.png Component ...

Switching up the Label Colors in Chart.JS

It's been a while since my last question, so please bear with me if I'm not following the rules. I've attached my current code and a reference image of the chart. I am completely new to working with ChartJS. The situation is a bit unique: t ...

The issue of Azure AD integration with Angular and ASP.net web api is causing problems with the Msal Angular HTTP interceptor. The interceptor fails to attach

I am currently utilizing Azure AD for Authentication in my application, which consists of a Single Page Application (SPA) using Angular and an ASP.net core web API. I have encountered no issues when reading user information or making any GET calls from Ang ...

What is the best way to make multiple network requests with varying parameters using rxjs in an Angular application?

I am facing a challenge with my mat-table and mat-paginator elements. The table is populated with data from an API, which usually returns 10 elements per request. However, I need to display 25 elements in the table, so I have to make 3 separate API calls t ...

Bootstrap 5 - Achieve automatic column width within a row

In my Bootstrap 5 setup, I have a variety of pages that have different column layouts - sometimes 2 columns, sometimes 3. The template system I'm using dynamically adjusts the layout to accommodate the need for a third column when necessary. <div ...

concealing the date selection feature on the data picker

$('.year').datepicker({ changeMonth: true, changeYear: true, showButtonPanel: true, dateFormat: 'MM yy', onOpen: function(dateText, inst) { $("table.ui-datepicker-calendar").addClass('hide') }, onClos ...

Navigating to specific rows in a primeng virtualscroll table using the scrollToIndex

Utilizing Primeng virtual scroll table in Angular to manage large datasets in an array. I am interested in using the scrollToIndex. Is there an equivalent of cdk-virtual-scroll-viewport in Primeng table? I need the functionality where, upon a user clicking ...

Choose a sibling element using CSS styling

As I'm developing an AngularJS component, I am on the quest to identify ANY sibling of a particular tag, irrespective of whether they are div, span, or p. I'm hoping for a CSS-native solution that resembles something along the lines of div:siblin ...

A superior method for implementing CSS keyframe transitions

Currently, I am utilizing the Rico St.Cruz brilliant query.transit library for my work. However, I need to make some changes involving classes instead of CSS transitions. Although I am not very confident in CSS transitions, I attempted to replace: JS: $ ...

Emphasizing buttons that are in a "pressed" or "inactive" state

I've implemented three push buttons in my code using input with type="button", which act as toggle buttons (on/off). In the past, I used images to indicate when a button was pressed or reset, but now I want to achieve this effect using CSS. However, ...

Is the transcluded content visible to the class as a whole?

Angular2 makes it simple to create a component like this: @Component({ selector: 'some', properties: ['header'] }) @View({ template: ` <div> <h2>{{ getFormattedHeader() }}</h2> <p><conte ...

The 'Subscription' type does not contain the properties _isScalar, source, operator, lift, and several others that are found in the 'Observable<any>' type

Looking to retrieve data from two APIs in Angular 8. I have created a resolver like this: export class AccessLevelResolve implements Resolve<any>{ constructor(private accessLevel: AccessLevelService) { } resolve(route: ActivatedRouteSnapshot, sta ...

Reactive Forms are being loaded dynamically without waiting for the completion of the http call

I've been working on loading dynamic field forms based on input from an HTTP service. The service provides metadata about which fields to load, including the name and type of each field. Therefore, I need to dynamically build the formGroup in ngOnInit ...

Updating the class of a div based on a checkbox being checked or unchecked

I am attempting to apply the "isChecked" class to the parent div when a checkbox is checked, using the ngClass directive. Here is the HTML: <div ng-class="{isChecked: (isChecked_1 == 'true')}"> <input type="checkbox" id="check_ ...