Performance: Modal structure and triggers
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 |
data-bs-toggle="modal" on the trigger element to activate modal functionality.data-bs-target with a CSS selector to specify which modal to open.#, so #modalExample is correct.<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#myModal">Open</button>
<div class="modal fade" id="myModal" tabindex="-1" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Title</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">Content here</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>data-bs-toggle="modal" and data-bs-target="#myModal", so it triggers the modal with ID myModal.<button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="myModal">Open</button>
href="#", data-bs-toggle="modal", and data-bs-target="#myModal" correctly triggers the modal.tabindex="-1" for focus management and data-bs-backdrop="true" (default) to close on outside click and Escape key.Pass the test and this pattern moves unknown