Adjust the message background color once it has been viewed

One of the buttons attached to a mat-menu has a red background when clicked.

If you want to see it in action, check out this Stackblitz.

.list-item.error {
  background-color:#FCE8FF;
}

However, if the button is clicked more than two times, the color should change to:

list-item.seen {
  background-color: lightgray;
  opacity: .5;
}

The functionality can be achieved using these classes:

[class.seen]="!seenMe"                      
[class.error]="true"

Here's the HTML code:

<button mat-icon-button
(click)="openMe()"
        [matMenuTriggerFor]="notify">
  <mat-icon>
    warning
  </mat-icon> Click Me
</button>
<mat-menu #notify="matMenu" class="mat-menu-notify">
  <mat-dialog-content (click)="stayOpen($event);">
    <mat-list >
      <div >
        <mat-list-item [class.seen]="!seenMe"                      
                       [class.error]="true"
                       class="list-item">
            <span class="message">
              I was here
            </span>
        </mat-list-item>
      </div>
    </mat-list>
  </mat-dialog-content>
</mat-menu>

And the corresponding TypeScript code:

notSeenYet = true;

  get seenMe(): boolean|undefined {
    return this.notSeenYet;
    console.log(this.notSeenYet);
  }

  openMe(): boolean{
    return this.notSeenYet = false;
  }

Answer №1

My interpretation is as follows:

Utilize the directive ngClass

HTML:

[ngClass]="notViewedYet ? 'failure' : 'viewed'" 

CSS:

.failure{
  // Additional CSS styles
  background-color: red;
}

.viewed{
  // Additional CSS styles
  background-color: gray;
}

Therefore, if the variable notViewedYet is true, it will assign the CSS class failure, otherwise it will use the class viewed.

Click here for a demonstration example.

Answer №2

It seems like there is some confusion about your desired behavior, but based on my understanding, here is what you may be looking for:

<mat-list-item [class.seen]="seenMe" [class.error]="presses>2" class="list-item">
   <span class="message">
      I was here
   </span>
</mat-list-item>

TypeScript Code:

openMe(): boolean {
    this.presses++;
    return this.notSeenYet = false;
}

Access Stackblitz Example

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

Syntax error is not caught - why are there invalid regular expression flags being used?

I'm attempting to dynamically create rows that, when clicked, load a view for the associated row. Check out my javascript and jquery code below: var newRow = $('<tr />'); var url = '@Url.Action("Get", "myController", new ...

Error Continues - "Exceeding Header Size Error"

In my current Next.js project, I have integrated a login system using NextAuth. Initially, everything was running smoothly. However, I started encountering an error whenever I attempted to retrieve the session. The Error: https://pastebin.com/Mh624N3c Du ...

Navigating with Anchors, Styling and jQuery

Firstly: Apologies in advance for any language errors as English is not my native tongue. :) The Scenario Here's the deal: I'm attempting to create a single button that, when clicked by the user, automatically scrolls down to the next DIV. Each ...

A guide on incorporating router links within a list component in a React project

In one of my projects, I've implemented a navbar with a profile icon that expands to show four different options: "Log in", "Register", "Edit", and "Admin". However, I'm facing an issue where clicking on these links doesn't always redirect m ...

Unable to generate a navigation panel using HTML/CSS and jQuery

I recently completed the basic courses in HTML/CSS, JavaScript, jQuery, and PHP on Codecademy. I'm currently working on creating a website using HTML/CSS and jQuery in Codecademy's codebits. However, I'm facing some issues with my navigation ...

Is it possible for you to pinpoint the mistake in the code below?

I've been trying to locate an error in the function but have been unable to do so. As a beginner in this area, I would appreciate your patience. <html> <head><title>print names</title> <script langua ...

Ensure the presence of a value in an array using Angular

I need to check if any of the "cuesData" have values or lengths greater than 0. However, in my current code, I can only evaluate the first array and not the rest. https://i.sstatic.net/bMpvg.jpg TS checkValues(values) { const result = Object.values ...

Centered Tailwind CSS logo design

Here is my current fiddle: https://jsfiddle.net/w5c36epd/ Upon close inspection, you may notice that the logo is not properly centered. I have experiment with various flexbox options such as: <div class="flex-shrink-0 justify-center"> ...

Analyzing a Typescript project using SonarQube Scanner on a Mac OS slave in Jenkins: A step-by-step guide

My current task involves examining a TypeScript project on Jenkins using the SonarQube Scanner plugin on a Mac OS slave. Software: Jenkins (version 2.32.1) SonarQube Scanner for Jenkins plug-in (version 2.5) SonarQube Scanner (version 2.8) SSH slave plu ...

The Filereader seems to be having trouble reading a specific file

In my Next.js application, I am attempting to allow users to upload an image from their system using an input field of type "file" and then read the content of the file using FileReader(). const OnChange = (e) => { const file = e.target.files[0]; ...

Ways to toggle the visibility of components in React for creating a step-by-step form functionality

I am a React beginner and I need help with showing and hiding parts of my code simultaneously. In the code below, I want only the "step_one" class to be visible on load, while the "step_two" class should remain hidden. When I click on SUBMIT, I want the "s ...

Changing the font family for Bootstrap Glyphicons on a button

I have recently started using Bootstraps Glyphicons and encountered an issue while trying to insert text inside a button with a glyphicon icon: <button type="button" class="btn btn-default pull-left" data-toggle="collapse" data-bind="attr:{'href&a ...

What is the best way to identify and retrieve the objects that do not match when comparing two arrays of objects

obj1 represents the original object while obj2 symbolizes the modified object. The goal is to extract the key-value pairs and types for all changed objects within the obje2 array of objects. Therefore, I am looking for a solution where if the values for " ...

Tips for integrating the Bootstrap 5 JS file (bootstrap.bundle.js) into Laravel 8

I have successfully integrated Bootstrap v5.1.0 into my Laravel 8 project using npm. Now, I am facing an issue with including bootstrap.bundle.js in my public/app.js. After installing popperjs, I added the following code to my resources/js/bootstrap.js: t ...

I want to use flexbox to center three divs on my webpage

I am trying to align 3 divs inside a flex container in the center of the page with equal distance between them and also from the edge of the page. .container { display : flex; width : 60%; } .para { width : 33%; padding : 1em; borde ...

Tips for validating user input in AngularJS without using a form tag

Within a popup, I am displaying HTML content that has been copied from another div and shown in the popup. I need to validate this input field to ensure it is required, and display an error message below the input box. <!-- HTML code for changing zip c ...

Issues with the Bootstrap Datetimepicker span breaking animation

I'm experiencing issues with the Bootstrap 3 Datepicker v4 as something seems to be breaking and I can't seem to fix it. My guess is that the input is causing a CSS issue. <div class="container"> <div class="row"> <div ...

Is it possible to utilize React hooks within a conditional statement?

My current situation involves an object serving as a state, complete with various properties. Now, I am looking to update this state within a specific condition using a hook. However, this update seems to trigger an infinite loop. The question at hand is ...

What is the best way to extract the delta from audio.currentTime?

I'm looking to synchronize my Three.js skeletal animation with an audio track. Typically, for animating the model, I would use the following code: var clock = new THREE.Clock(); function animate(){ var delta = clock.getDelta(); THREE.Animati ...

Tips for updating the value of the most recently created div in an ng-repeat loop

Within my HTML document, the following code is present: <div ng-repeat="selection in selections" style="margin-left:{{marginLeft}}%;width:{{progress}}%;background-color:{{selection}}"> </div> In my controller, I have implemented function ...