Open In App

Angular PrimeNG Focus Trap Button with tabindex -1

Last Updated : 12 Aug, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Angular PrimeNG is an open-source framework with a rich set of native Angular UI components that are used for great styling and this framework is used to make responsive websites with very much ease. This article will show us how to use the Focus Trap Button with tabindex -1 in Angular PrimeNG. We will also learn about the various properties and their syntaxes used in the code example.

Angular PrimeNG Focus Trap is used to specify whether elements can only focus on elements inside the dialog. It is of the boolean datatype, the default value is true.

Angular PrimeNG Focus Trap Button with tabindex -1 properties:

  • tabindex: It describes an element’s tab order while navigating with the “tab” button.

Syntax:

<button pButton 
  type="button" 
  icon="..." 
  label="..."
  tabindex ="-1"
</button>

Creating Angular application & module installation:

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: Install PrimeNG in your given directory.

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

Project Structure: It will look like the following:

 

To run the above file run the below command:

ng serve --save

Example 1: Below is the example that illustrates the use of the Angular PrimeNG Focus Trap Button with tabindex -1.

app.components.html




<div pFocusTrap class="card">
  <h2 style="color: green">GeeksforGeeks</h2>
  <h5>Angular PrimeNG Button with tabindex -1</h5>
  
  <button
    pButton
    type="button"
    icon="pi pi-check"
    tabindex="1"
    label="tabindex 1"
    class="p-button-success p-button-outlined">
  </button>
  
  <button
    pButton
    type="button"
    icon="pi pi-check"
    tabindex="2"
    label="tabindex 2"
    class="p-button-info p-button-outlined">
  </button>
  
  <button
    pButton
    type="button"
    icon="pi pi-check"
    tabindex="-1"
    class="p-button-danger p-button-outlined"
    label="tabindex -1 (skipped)">
  </button>
  
  <button
    pButton
    type="button"
    icon="pi pi-check"
    tabindex="3"
    class="p-button-warning p-button-outlined"
    label="tabindex 3">
  </button>
</div>


app.components.ts




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


app.module.ts




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


Output:

 

Example 2: Below is another example that illustrates the use of the Angular PrimeNG Focus Trap Button with tabindex -1.

app.components.html




<div pFocusTrap class="card">
  <h2 style="color: green">GeeksforGeeks</h2>
  <h5>Angular PrimeNG Button with tabindex -1</h5>
  
  <div class="row">
    <div class="col">
      <button
        pButton
        type="button"
        icon="pi pi-check"
        tabindex="1"
        label="tabindex 1"
        class="p-button-success">
      </button>
    </div>
  </div>
  
  <div class="row">
    <div class="col">
      <button
        pButton
        type="button"
        icon="pi pi-times"
        tabindex="2"
        label="tabindex 2"
        class="p-button-info">
      </button>
    </div>
  </div>
  
  <div class="row">
    <div class="col">
      <button
        pButton
        type="button"
        icon="pi pi-code"
        tabindex="-1"
        class="p-button-danger"
        label="tabindex -1 (skipped)">
      </button>
    </div>
  </div>
  
  <div class="row">
    <div class="col">
      <button
        pButton
        type="button"
        icon="pi pi-hashtag"
        tabindex="3"
        class="p-button-warning"
        label="tabindex 3">
      </button>
    </div>
  </div>
</div>


app.components.ts




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


app.module.ts




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


Output:

 

Reference: https://primefaces.org/primeng/focustrap



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads