Alert

Alerts are inline notification banners used to communicate status messages, warnings, errors, or confirmations directly within the page layout. FrontAlign provides two complementary Alert systems: a Core Engine for managing existing HTML alerts declaratively, and a Dynamic Alert class for creating and injecting alerts programmatically.

Quick reference

FrontAlign supports two modes:

  • Core (Static): Alert already exists in HTML — handles dismissal, animations, persistence, and custom events automatically.
  • Dynamic (Class): Create and inject a new alert from JavaScript at runtime.

Five status types are supported across both modes: Default · Success · Danger · Warning · Info


HTML Structure

An alert consists of a root .alert element with a status modifier class, an .alert-content span for the message, and optionally an .alert-close button for dismissal.

<div class="alert"fa-component="alert">
  <span class="alert-content">This is a default alert.</span>
  <button class="button alert-close" aria-label="Dismiss alert"></button>
</div>

<div class="alert is-success"fa-component="alert">
  <span class="alert-content">Your profile has been updated.</span>
  <button class="button alert-close" aria-label="Dismiss alert"></button>
</div>

<div class="alert is-warning"fa-component="alert">
  <span class="alert-content">Your session will expire soon.</span>
  <button class="button alert-close" aria-label="Dismiss alert"></button>
</div>

<div class="alert is-danger"fa-component="alert">
  <span class="alert-content">Something went wrong. Please try again.</span>
  <button class="button alert-close" aria-label="Dismiss alert"></button>
</div>
This is a default alert.
Your profile has been updated.
Your session will expire soon.
Something went wrong. Please try again.

Structure Breakdown

ElementSelectorDescription
Root container.alertThe alert wrapper. Accepts status modifier classes and behavior attributes.
Message.alert-contentDisplays the alert text content.
Close button.alert-closeTriggers dismissal. Requires aria-label for accessibility.

<div class="alert">
  <span class="alert-content">This is a default alert.</span>
  <button class="button alert-close" aria-label="Dismiss alert"></button>
</div>

<div class="alert is-success">
  <span class="alert-content">Action completed successfully.</span>
  <button class="button alert-close" aria-label="Dismiss alert"></button>
</div>

<div class="alert is-danger">
  <span class="alert-content">An error occurred.</span>
  <button class="button alert-close" aria-label="Dismiss alert"></button>
</div>

<div class="alert is-warning">
  <span class="alert-content">Proceed with caution.</span>
  <button class="button alert-close" aria-label="Dismiss alert"></button>
</div>

<div class="alert is-info">
  <span class="alert-content">Here is some useful information.</span>
  <button class="button alert-close" aria-label="Dismiss alert"></button>
</div>

Attributes

AttributeApplied ToValueDefaultDescription
data-close-animationRoot container"slide"fadeExit animation. slide collapses height; default is fade (opacity).
data-persistentRoot container"true"falseSaves dismissed state to localStorage so the alert never reappears for that user.
data-alertRoot containerstringUnique identifier required for persistent alerts. Used as the localStorage key.

data-close-animation

By default, alerts exit with a fade animation (opacity transition). Set data-animation="slide" to collapse the alert's height instead — useful when you want the surrounding content to close the gap smoothly after dismissal.

<!-- Default: fade transition -->
<div class="alert is-success"fa-component="alert">
  <span class="alert-content">This alert fades out on dismiss.</span>
  <button class="button alert-close" aria-label="Dismiss alert"></button>
</div>

<!-- Slide transition -->
<div class="alert is-info" data-close-animation="slide">
  <span class="alert-content">This alert slides up on dismiss.</span>
  <button class="button alert-close" aria-label="Dismiss alert"></button>
</div>
This alert fades out on dismiss (default).
This alert slides up on dismiss.

data-persistent

By default, dismissed alerts reappear on the next page load. Set data-persistent="true" along with a unique alert-id to permanently remember the dismissed state in localStorage — ideal for promotional banners or one-time notices.

<!-- Persistent alert — dismissed state stored in localStorage -->
<div class="alert is-info" fa-component="alert" data-persistent="true" data-alert="promo-banner-2025">
  <span class="alert-content">Check out our new features!</span>
  <button class="button alert-close" aria-label="Dismiss alert"></button>
</div>
Check out our new features! (Dismissing this remembers your choice.)

Icon Variant

Add has-icon to the root .alert element to display a status icon alongside the message.

<div class="alert is-success has-icon">
  <span class="alert-content">Your profile has been updated.</span>
  <button class="button alert-close" aria-label="Dismiss alert"></button>
</div>

<div class="alert is-danger has-icon">
  <span class="alert-content">Something went wrong. Please try again.</span>
  <button class="button alert-close" aria-label="Dismiss alert"></button>
</div>
Your profile has been updated.
Something went wrong. Please try again.

Event System

The Core engine dispatches two native CustomEvents during the dismiss lifecycle. The close event is cancelable. Both events carry an alert reference in event.detail.

EventDispatched OnCancelableDescription
fa.alert.closeThe alert element✅ YesFires when the close button is clicked, before removal. preventDefault() cancels the dismissal.
fa.alert.closeddocument❌ NoFires after the alert has been fully removed from the DOM.

event.detail

Both events carry one reference:

PropertyTypeDescription
alertHTMLElementThe .alert element that was dismissed.

Cancel dismiss based on a condition

document.querySelector('#my-alert').addEventListener('fa.alert.close', (e) => {
  if (hasUnsavedChanges) {
    e.preventDefault();
    showUnsavedWarning();
  }
});

React Usage

'use client';

import { useAlertDismiss } from 'frontalign/react';

export function Example() {
  useAlertDismiss();

  return (
    <div className="alert">
      <span className="alert-content">Your profile has been updated.</span>
      <button className="button alert-close" aria-label="Dismiss alert"></button>
    </div>
  );
}

Dynamic Alert

The Alert class lets you create and inject fully configured alerts from JavaScript — no HTML required. Ideal for responding to async operations, API responses, or user actions at runtime.

Basic Usage

// Use when you want to bypass the Core engine
import { Alert } from 'frontalign';

const alert = Alert.create('#reference-element', {
  message: 'Your file has been uploaded successfully.',
  status: 'success',
  animation: 'slide',
  autoClean: true
});

Configuration Options

OptionTypeDefaultDescription
messagestring"This is an alert message"Text content displayed inside the alert.
statusstring"default"Visual variant: success, danger, warning, info, or default.
dismissiblebooleantrueWhether to render a close button and enable click-to-dismiss.
positionstring"before""after" inserts after the reference element; "before" inserts before it.
animationstring"fade"Exit animation: fade (opacity) or slide (height collapse).
hasIconbooleanfalseAdds has-icon CSS class for a status icon.
animatedbooleantruefalse adds animation-none class to disable entrance animation.
borderedbooleanfalsefalse adds border-0 class to remove the alert border.
autoCleanbooleanfalseAutomatically calls .dispose() after dismissal.

Instance Methods

.dispose()

Removes the click event listener and nullifies all internal references. Call manually for cleanup, or set autoClean: true to have it called automatically.

alert.dispose();

Pro Tips

After an API call:

async function submitForm() {
  try {
    await postData('/api/submit', formData);
    Alert.create('#submit-btn', {
      message: 'Form submitted successfully!',
      status: 'success',
      animation: 'slide',
      autoClean: true
    });
  } catch {
    Alert.create('#submit-btn', {
      message: 'Something went wrong. Please try again.',
      status: 'danger',
      autoClean: true
    });
  }
}

React Usage — useAlert

The useAlert hook provides status-based shorthand methods for injecting dynamic alerts from any React component. It wraps the Alert class internally and is fully compatible with SSR ('use client' required).

Import

'use client';

import { useAlert } from 'frontalign/react';

Shorthand Methods

The hook returns five methods: show, success, danger, warning, and info. Each accepts a CSS selector and an options object.

MethodEquivalent StatusDescription
alert.show(selector, options)defaultGeneric alert — status must be passed via options.status.
alert.success(selector, options)successInjects a green success alert.
alert.danger(selector, options)dangerInjects a red danger alert.
alert.warning(selector, options)warningInjects a yellow warning alert.
alert.info(selector, options)infoInjects a blue info alert.

Basic Example

'use client';

import { useAlert } from 'frontalign/react';

export function AlertExample() {
  const alert = useAlert();

  return (
    <>
      <div id="form-message"></div>

      <button
        className="button is-primary"
        onClick={() => alert.success('#form-message', {
          message: 'Profile saved.',
          position: 'after',
          animation: 'slide'
        })}
      >
        Show Alert
      </button>
    </>
  );
}

All Statuses

'use client';

import { useAlert } from 'frontalign/react';

export function StatusExamples() {
  const alert = useAlert();

  return (
    <>
      <div id="alert-target"></div>

      <button className="button" onClick={() => alert.success('#alert-target', { message: 'Operation completed.' })}>
        Success
      </button>
      <button className="button" onClick={() => alert.danger('#alert-target', { message: 'An error occurred.' })}>
        Danger
      </button>
      <button className="button" onClick={() => alert.warning('#alert-target', { message: 'Proceed with caution.' })}>
        Warning
      </button>
      <button className="button" onClick={() => alert.info('#alert-target', { message: 'Here is some info.' })}>
        Info
      </button>
      <button className="button" onClick={() => alert.show('#alert-target', { message: 'A default alert.', status: 'default' })}>
        Default
      </button>
    </>
  );
}

After an Async Action

Use useAlert to respond to form submissions, API responses, or any async event:

'use client';

import { useState } from 'react';
import { useAlert } from 'frontalign/react';

export function SubmitForm() {
  const alert = useAlert();
  const [loading, setLoading] = useState(false);

  async function handleSubmit() {
    setLoading(true);
    try {
      await postData('/api/submit', formData);
      alert.success('#submit-btn', {
        message: 'Form submitted successfully!',
        animation: 'slide',
        autoClean: true
      });
    } catch {
      alert.danger('#submit-btn', {
        message: 'Something went wrong. Please try again.',
        autoClean: true
      });
    } finally {
      setLoading(false);
    }
  }

  return (
    <>
      <div id="submit-btn"></div>
      <button className="button is-primary" onClick={handleSubmit} disabled={loading}>
        {loading ? 'Submitting…' : 'Submit'}
      </button>
    </>
  );
}

With Icons and Custom Position

'use client';

import { useAlert } from 'frontalign/react';

export function IconAlerts() {
  const alert = useAlert();

  return (
    <>
      <div id="icon-target"></div>

      <button
        className="button"
        onClick={() => alert.danger('#icon-target', {
          message: 'Access denied. Please contact your administrator.',
          hasIcon: true,
          position: 'after',
          animation: 'fade',
          dismissible: true
        })}
      >
        Show Icon Alert
      </button>
    </>
  );
}

Cancel dismiss with confirmation:

document.querySelector('#important-alert').addEventListener('fa.alert.close', (e) => {
  if (hasUnsavedChanges) {
    e.preventDefault();
    showConfirmationModal();
  }
});

Notes

  • Two Complementary Systems: Use Core for static HTML alerts and the Alert class for dynamic ones, or combine both in the same project.
  • Persistent Alerts: Core alerts can be permanently dismissed per-user via localStorage, so they never reappear after being closed.
  • Dual Transition Modes: Choose between fade (opacity) or slide (height collapse) exit animation on dismissal.
  • Custom Events: Core alerts dispatch fa.alert.close and fa.alert.closed events, allowing you to hook into the dismiss lifecycle.
  • Flexible Injection: Dynamic alerts can be inserted either before or after the reference element.
  • Auto Clean: Dynamic alerts can automatically dispose their instance from memory after dismissal.
  • Accessibility: Close buttons include aria-label attributes for screen reader support.

FrontAlign