Performance: Alert and notification patterns
This affects how quickly alerts and notifications appear and update on the page, impacting user interaction speed and visual stability.
<div class="alert-container"> <div class="alert bg-red-500 text-white p-4 mb-4" aria-live="polite" role="alert">Error: Something went wrong!</div> </div> <script> setInterval(() => { const alert = document.querySelector('.alert'); alert.textContent = 'Error updated at ' + new Date().toLocaleTimeString(); }, 1000); </script>
<div class="alert-container"> <div class="alert bg-red-500 text-white p-4 mb-4">Error: Something went wrong!</div> </div> <script> setInterval(() => { const alert = document.querySelector('.alert'); alert.textContent = 'Error updated at ' + new Date().toLocaleTimeString(); alert.style.padding = '2rem'; // changing padding triggers reflow }, 1000); </script>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Changing layout styles on alert update | 1 text update + style change | 1 reflow per update | High paint cost due to layout shift | [X] Bad |
| Updating only text content with ARIA live | 1 text update | 0 reflows | Low paint cost, stable layout | [OK] Good |
bg-green-500 sets green background, text-white sets white text, p-4 adds padding, and rounded rounds corners.<div class="bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded relative" role="alert"> <strong class="font-bold">Error!</strong> <span class="block sm:inline"> Something went wrong.</span> </div>
bg-red-100, border-red-400, and text-red-700 create a red-themed alert indicating an error or danger.<div class="bg-yellow-200 text-yellow-800 p-3 rounded"> <strong>Warning!</strong> Please check your input. </div>
role="alert" to notify assistive technologies about important messages.bg-yellow-200 and text-yellow-800 are correct for warning alerts; p-3 is a valid padding class.role="alert" attribute for accessibility -> Option Arole="alert" and aria-live="polite" so screen readers announce the alert politely.tabindex and aria-label).p-4 sm:p-6 to ensure good spacing on all screen sizes.role="alert", aria-live="polite", a close button with keyboard support, and responsive padding classes -> Option Crole="alert", aria-live="polite", a close button with keyboard support, and responsive padding classes [OK]Pass the test and this pattern moves unknown