Performance: Focus management in modals
Focus management affects user interaction responsiveness and visual stability during modal usage.
const modal = document.querySelector('#modal'); modal.addEventListener('shown.bs.modal', () => { modal.focus(); // Use Bootstrap's built-in focus trap or implement a focus trap to keep focus inside modal });
const modal = document.querySelector('#modal'); modal.addEventListener('shown.bs.modal', () => { // No focus trap implemented });
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| No focus trap in modal | Focus moves outside modal causing DOM focus changes | Multiple reflows as focus shifts | Increased paint cost due to layout shifts | [X] Bad |
| Focus trap implemented in modal | Focus constrained within modal DOM subtree | Single reflow on modal open | Minimal paint cost, stable layout | [OK] Good |
aria-modal='true' to indicate a modal dialog to screen readers.aria-modal='true' to trap focus and signal modal behavior.<button id='openModal'>Open</button>
<div class='modal' tabindex='-1' role='dialog' aria-modal='true'>
<div class='modal-dialog'>
<div class='modal-content'>
<button id='closeModal'>Close</button>
</div>
</div>
</div>tabindex='-1' on the modal container to manage focus properly.tabindex='-1', focus trapping fails and keyboard can move outside modal.tabindex='-1' attribute -> Option APass the test and this pattern moves unknown