AlertBanner component displays important announcements in a modal dialog overlay. It includes automatic expiration, session-based dismissal tracking, and support for external links.
Features
- Modal Dialog: Full-screen overlay with centered content
- Auto-expiration: Automatically hides after a specified date/time
- Dismissal Tracking: Remembers dismissal via sessionStorage
- External Links: Optional redirect button instead of dismiss
- Keyboard Accessible: Escape key closes the dialog
- Click Outside: Backdrop click dismisses the alert
Installation
Basic Usage
Props
The main message content to display in the alert.
The heading/title text displayed at the top of the alert.
Text shown on the dismiss/action button.
Unique identifier for dismissal tracking. If not provided, a random ID is generated. Use a meaningful ID to track specific messages across sessions.
ISO 8601 date string indicating when the alert should no longer be shown. Format:
YYYY-MM-DDTHH:MM:SSZOptional URL to redirect users when they click the button. When provided, the button becomes a link instead of a dismiss action.
Examples
Simple Alert
With Expiration
With External Link
Development Testing
Fromsrc/layouts/Layout.astro:260:
Behavior
Dismissal Logic
- User clicks “Dismiss” button or backdrop
- Component stores
alert-{messageId}insessionStorage - On page refresh, checks sessionStorage before rendering
- Alert won’t show again during the current browser session
Expiration Logic
- Component compares current date to
expiresAt - If current date > expiration date, component doesn’t render
- Works on both server (Astro) and client (JavaScript) side
External Link Behavior
WhenexternalLink is provided:
- Button becomes an
<a>tag instead of<button> - Opens in new tab (
target="_blank") - Includes
rel="noopener noreferrer"for security - Still records dismissal before redirecting
Styling
The component uses utility classes and inline styles:Custom Fade-in Animation
Z-Index Layering
The alert usesz-[150] to appear above most content:
- Standard content:
z-0toz-50 - Navigation/Header:
z-100 - Cookie Banner:
z-9999 - AlertBanner:
z-150 - Accessibility Menu:
z-140
Session vs. Persistent Storage
The AlertBanner uses sessionStorage instead of localStorage or cookies. This means:
- Dismissal persists only during the current browser tab/window session
- Alert will reappear in new tabs or after browser restart
- No privacy concerns since data is temporary
Accessibility
- Uses
role="alertdialog"for screen readers - Includes
aria-modal="true"to indicate modal behavior - Escape key support for keyboard users
- Focus automatically on modal when it appears
Use Cases
- Important Announcements: Service time changes, building closures
- Event Promotions: Upcoming special events or registration deadlines
- Emergency Notices: Weather cancellations (see WeatherBanner for specialized version)
- Feature Announcements: Website updates or new resources
- Call-to-Action: Directing users to registration forms or external resources
Advanced Customization
Custom Styling
To change appearance, modify the inline styles or add custom classes:Dynamic Message ID
Generate unique IDs based on content:Related Components
- WeatherBanner: Specialized alert for weather cancellations
- SeasonalBanner: Non-modal banner for seasonal messages