Open In App

Angular PrimeNG StyleClass Component

Angular PrimeNG is an open-source framework for Angular applications. It has a rich set of native UI components that can be used to make attractive and scalable web interfaces. In this article, we will see Angular PrimeNG StyleClass Component.

StyleClass is used to manage CSS classes during entering and leaving animations of a component or to toggle a class on an element. The properties of the StyleClass are listed below.



Angular PrimeNG StyleClass Component Properties:

 



Target Keywords for pStyleClass Property:

Syntax:

<button 
     pButton
     label="..." 
     pStyleClass="#gfg" 
     leaveClass="..."
     leaveActiveClass="..." 
     leaveToClass="...">
</button>
<div id="gfg"> ... </div>

Creating Angular Application and Installing the Module:

Step 1: Create an Angular application using the following command.

ng new appname

Step 2: After creating your project folder i.e. appname, move to it using the following command.

cd appname

Step 3: Finally, Install PrimeNG in your given directory.

npm install primeng --save
npm install primeicons --save

Project Structure: The project Structure will look like this after following the above steps:

Project Structure

ng serve --open

Example 1: In this example, we used the toggleClass property of StyleClass to enable and disable a text input by toggling the p-disabled class on that element.




<h2 style="color: green">
  GeeksforGeeks
</h2>
<h3>
  Angular PrimeNG StyleClass Component
</h3>
<h4>ToggleClass Demo</h4>
  
<button 
    pButton
    class="block mb-4" 
    label="Enable/Disable Input" 
    pStyleClass="#gfg" 
    toggleClass="p-disabled">
</button>
  
<input pInputText type="text" 
       id="gfg">




import { Component } from "@angular/core";
  
@Component({
    selector: "app-root",
    templateUrl: "./app.component.html",
    styles: []
})
export class AppComponent { }




import { NgModule } from "@angular/core";
import { BrowserModule }
    from "@angular/platform-browser";
import { BrowserAnimationsModule }
    from "@angular/platform-browser/animations";
import { AppComponent } from "./app.component";
import { ButtonModule } from "primeng/button";
import { StyleClassModule } from 'primeng/styleclass';
import { InputTextModule } from 'primeng/inputtext';
  
@NgModule({
    imports: [
        BrowserModule,
        BrowserAnimationsModule,
        ButtonModule,
        StyleClassModule,
        InputTextModule
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent],
})
  
export class AppModule { }

Output:

 

Example 2: This example shows how to add entering and leaving animations using StyleClass.




<h2 style="color: green">GeeksforGeeks</h2>
<h3>Angular PrimeNG StyleClass Component</h3>
<h4>Enter/Leave Animation Demo</h4>
  
<div class="mb-4">
    <button 
        pButton
        class="mr-4" 
        label="Hide Image" 
        pStyleClass="#gfg"
        leaveActiveClass="imageOutAnimClass" 
        leaveToClass="hidden">
    </button>
  
    <button 
        pButton 
        label="Show Image" 
        pStyleClass="#gfg"
        enterClass="hidden"
        enterActiveClass="imageInAnimClass">
    </button>
  
</div>
  
<img src=
     alt="gfg_logo"
     id="gfg"/>




/* ImageInAnim */
@keyframes imageInAnim {
    0%{
        opacity: 0;
    }
  
    100%{
        opacity: 1;
    }
}
  
/* ImageOutAnim */
@keyframes imageOutAnim {
    0% {
        opacity: 1;
    }
  
    100% {
        opacity: 0;
    }
}
  
.imageInAnimClass{
    animation: imageInAnim 2s linear;
}
  
.imageOutAnimClass{
    animation: imageOutAnim 2s linear;
}




import { Component } from "@angular/core";
  
@Component({
    selector: "app-root",
    templateUrl: "./app.component.html",
    styleUrls: ["./app.component.css"]
})
  
export class AppComponent { }




import { NgModule } from "@angular/core";
import { BrowserModule }
    from "@angular/platform-browser";
import { BrowserAnimationsModule }
    from "@angular/platform-browser/animations";
import { AppComponent } from "./app.component";
import { ButtonModule } from "primeng/button";
import { StyleClassModule } from 'primeng/styleclass';
  
@NgModule({
    imports: [
        BrowserModule,
        BrowserAnimationsModule,
        ButtonModule,
        StyleClassModule
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent],
})
export class AppModule { }

Output:

 

Reference: http://primefaces.org/primeng/styleclass


Article Tags :