Performance: Modal and overlay patterns
MEDIUM IMPACT
This affects page load speed and interaction responsiveness by controlling how overlays and modals block or delay user input and rendering.
<div id="modal" class="fixed inset-0 bg-black bg-opacity-50 opacity-0 pointer-events-none transition-opacity duration-300"> <div class="absolute top-1/2 left-1/2 transform -translate-x-1/2 -translate-y-1/2 bg-white p-6"> Modal content </div> </div> <script> function openModal() { const modal = document.getElementById('modal'); modal.classList.remove('opacity-0', 'pointer-events-none'); } </script>
<div id="modal" class="hidden fixed inset-0 bg-black bg-opacity-50"> <div class="absolute top-1/2 left-1/2 transform -translate-x-1/2 -translate-y-1/2 bg-white p-6"> Modal content </div> </div> <script> function openModal() { const modal = document.getElementById('modal'); modal.style.display = 'block'; modal.style.opacity = 0; let opacity = 0; const fadeIn = setInterval(() => { opacity += 0.05; modal.style.opacity = opacity; if (opacity >= 1) clearInterval(fadeIn); }, 16); } </script>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| JS inline style animation for opacity | No DOM change | Multiple reflows per frame | High paint cost due to repeated style changes | [X] Bad |
| Tailwind CSS opacity + pointer-events with transition | No DOM change | No reflows | Low paint cost, GPU compositing only | [OK] Good |
| Removing modal from DOM on close | DOM removal | Full reflow | High paint cost | [X] Bad |
| Hiding modal with CSS classes (opacity + pointer-events) | No DOM change | No reflows | Low paint cost | [OK] Good |