How to correctly align via HTML the Submit button and Cancel button so that when I press the "Tab" in the keyboard, it will go from "Submit" then to "Cancel"? This is the ChildModalComponent:
<div class="w-100">
<button type="button" (click)="remove()" class="btn btn-primary mr-3 float-left" >Remove</button>
<button type="button" class="btn btn-info float-right" (click)="closeModal()" >Cancel</button>
<button type="submit" ngbAutofocus class="btn btn-primary mr-3 float-right" >Submit/button>
</div>
With this current code, on initial opening of modal, the "Tab" is highlighted on the Submit first since it has the ngbAutofocus. But when I click on the "Tab", the expected behavior should be to go to Cancel but it goes to the added "Close" button on my modal.
I am using import { NgbModal } from ‘@ng-bootstrap/ng-bootstrap’ for my modal,
import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
openModal = () => {
const modalRef = this.modalService.open(ParentModalComponent, {size: "xl", backdrop: "static"});
}
This is the ParentModalComponent:
<div class="modal-header">
<h3 class="modal-title w-100 text-center">Modal Header Here</h3>
<button type="button" class="close" aria-label="Close" (click)="activeModal.dismiss('Cross click')">
<span aria-hidden="true">×</span>
</button>
</div>
<app-child-modal></app-child-modal>
I’ve tried using "[tabIndex]=’value’, but when I continuously press "Tab", the focus goes outside the modal and I have to implement Focus Trapping but this is a lot of work.
2
Answers
This approach solved my problem. Without the need to add the tabIndex property, tabbing works in order as how HTML is laid out.
In this code, I’ve assigned tabindex values to each button in the desired tab order: Submit (1), Cancel (2), and Remove (3). This way, when you press the "Tab" key, the focus will move from the Submit button to the Cancel button and then to the Remove button. You won’t need to worry about focus trapping because the tab order within the modal is controlled explicitly.
Make sure to adjust the tabindex values according to your specific layout and needs.
Let me know if this works for you!
Happy to help!