Performance: Modal structure and triggers
MEDIUM IMPACT
This affects page load speed and interaction responsiveness by controlling how modals are added to and removed from the DOM and how triggers activate them.
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#exampleModal">Open Modal</button> <div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <h5 class="modal-title" id="exampleModalLabel">Modal title</h5> <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button> </div> <div class="modal-body">Modal body content</div> <div class="modal-footer"> <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button> </div> </div> </div> </div>
<button onclick="document.getElementById('modal').style.display='block'">Open Modal</button> <div id="modal" style="display:none; position:fixed; top:0; left:0; width:100vw; height:100vh; background:rgba(0,0,0,0.5);"> <div style="background:#fff; margin:10vh auto; padding:1rem; width:300px;">Modal Content<button onclick="document.getElementById('modal').style.display='none'">Close</button></div> </div>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Manual style toggle for modal visibility | Modal always in DOM | Multiple reflows on open/close | High initial parse cost due to overlay and content always present | [X] Bad |
| Bootstrap modal with data attributes and JS | Modal efficiently shown/hidden | Single reflow per open/close | Paint only when modal visible | [OK] Good |