status

Feedback and loading states: alerts, toasts, progress bars, spinners and skeletons

These helpers return feedback and loading markup without running the underlying operation. You control the application state and decide when to add, update, or remove each component.

from fasthtml.common import Form
from answerui.demo import demo
from fastcore.test import *

Notice

_notice builds the shared inner structure for Alert and Toast: a Stack of content beside an optional quiet icon close button marked dismiss. Shared CSS styles the flex row, surface, and expanding content column.

Alert

Alert renders a message and optional supporting content on a tinted accent surface ([alert]). Pass ui_tone ('warning', 'danger', 'success', or 'info') to choose a severity hue. With dismissible=True, clicking the close button removes the alert through the delegated click listener in browser.js. Because that listener is attached to document, alerts inserted later by htmx are dismissible without extra wiring. Plain links inside [alert] inherit --v2 contrast, so a top-of-form error summary can link directly to #field-id inputs.


Alert

def Alert(
    *c, dismissible:bool=False, **attrs
):

An in-page message with optional dismissal

Severity colour and screen-reader announcements are separate choices. Alert adds no role by default because a persistent warning need not interrupt a screen-reader user. For polite feedback, place an empty role="status" region in the page first, then insert or update the message inside it. Reserve role="alert" for urgent messages. Screen readers do not reliably announce a message when a single swap creates the live region with the message already inside it. See ARIA live regions.

Alert.demo()

Alerts have no auto-dismiss timer because important information must stay visible until the user reads or closes it.

Toast

Place an empty Toaster() in the page before delivering messages to it. Toaster renders a polite live region (role="status", aria-live="polite") with popover="manual". When a [toaster] mounts, browser.js calls showPopover(). Because it is a manual popover, the browser draws it above ordinary page content and leaves it open when the user clicks outside or presses Escape. Pass toaster_position ('top-start', 'bottom-start', or 'bottom-end') to move it from the default top-end corner. A modal dialog opened later sits above the toaster. Put feedback needed during a modal interaction inside that dialog.

By default, Toast sets --toast-duration: 5000ms (controlled by life) for the ui-toast-lifetime CSS animation. While the pointer hovers over a toast or keyboard focus is inside it, CSS pauses that animation (animation-play-state: paused). When animationend fires for ui-toast-lifetime, browser.js removes the toast. Passing life=None omits --toast-duration to keep the toast visible until the user dismisses it. Under prefers-reduced-motion: reduce, --motion drops to 0ms while --toast-duration stays unchanged.


Toaster

def Toaster(
    *c, **attrs
):

The page's polite live region for toast deliveries


Toast

def Toast(
    *c, life:int=5000, dismissible:bool=True, partial:bool=False, **attrs
):

A brief message for the toast region

Deliver a toast into the page's [toaster] from a fragment route with Append('[toaster]', Toast('Saved')) or Toast('Saved', partial=True). Both wrap the toast in an <hx-partial hx-target="[toaster]" hx-swap="beforeend">, appending the [toast] element intact without replacing the triggering element.

The example below stores the partial in a template element whose HTML the button's hx-on:click handler passes to htmx.swap. This demonstrates delivery without a server or a registered behavior. In an app, return the partial directly from a route with no inline script. Click the button to deliver both messages, then try hovering or focusing the timed toast.

Toast.demo(height='14rem')

Queued toasts

A redirect response contains no HTML for a toast. An htmx route returns a partial fragment rather than a full page with a Toaster. In both cases, call add_toast(sess, msg, life=5000, **attrs) to queue the message in the session under toasts_key ('aui_toasts'), passing severity as an attribute such as ui_tone='success'.

On a full-page route, render queued toasts inside Toaster(*pop_toasts(sess)). On a fragment route, include toasts_partial(sess) in the response for htmx to swap into the existing [toaster]. Call setup_toasts(app) to register toast_after on app.after and inject toasts_partial(sess) automatically on every markup or empty response. toast_after does nothing when the queue is empty, as happens after a full-page route calls pop_toasts(sess).


toasts_partial

def toasts_partial(
    sess
):

The toasts queued in session sess, wrapped in an Append('[toaster]', ...) partial, clearing the queue; None when nothing is queued


pop_toasts

def pop_toasts(
    sess
):

The toasts queued in session sess, as Toasts, clearing the queue


add_toast

def add_toast(
    sess, msg, life:int=5000, **attrs
):

Queue msg in session sess for the next page or fragment, with its severity in attrs

toast_after appends toasts_partial(sess) to req.injects when a route returns markup (tuple, FT, or FtResponse) or an empty response (for routes that only queue a toast). After a redirect or file response, queued toasts stay in the session for the next page:


setup_toasts

def setup_toasts(
    app
):

Deliver queued toasts as an <hx-partial> with every markup or empty response of app


toast_after

def toast_after(
    resp, req, sess
):

FastHTML after hook that injects queued toasts as an <hx-partial> on a markup or empty response

Queue two messages as a route does and pop them with toasts_partial, clearing sess:

sess = {}
add_toast(sess, 'Your changes have been saved.', ui_tone='success')
add_toast(sess, 'The upload failed. Your original file has not changed.', life=None, ui_tone='danger')
p = toasts_partial(sess)
test_eq(sess, {})
test_eq(p.tag, 'hx-partial')
test_eq([t.attrs.get('ui-tone') for t in p.children], ['success', 'danger'])
p
Your changes have been saved.
The upload failed. Your original file has not changed.

Progress

Progress wraps the native <progress> element (progress[progress]). Pass value and max when the completed fraction is known, or omit value for an indeterminate bar. The [progress] CSS rule sets only inline-size: 100%, block-size: var(--space-2), and accent-color: var(--color-accent) without changing the browser's native rendering or animation. Wrap the bar in a Label describing the operation.


Progress

def Progress(
    *c, **attrs
):

Native progress; omit value when the amount completed is unknown

A labelled bar for a known amount of work, and an indeterminate one for an unknown amount:

Progress.demo()

Spinner

Spinner renders a Span ([spinner]) with a CSS ring (::before) beside visible status text (label, defaulting to 'Loading...'). The ring uses the ui-spin animation with duration calc(var(--motion) * 5). Under prefers-reduced-motion: reduce, the animation stops while the label remains visible. As with Alert, add a live-region role only when screen readers should announce the status.


Spinner

def Spinner(
    label:str='Loading...', **attrs
):

A small spinning ring with visible text explaining the wait

Spinner.demo()

Skeleton

Skeleton renders an unanimated block placeholder (Span with [skeleton] and aria-hidden="true") that reserves layout space before content arrives. By default it spans inline-size: 100% and block-size: 1em with var(--n-1) background and var(--radius-1) corners. Override those dimensions with CSS such as style. Because aria-hidden="true" hides the placeholder from the accessibility tree, provide a separate visible loading message for screen readers.


Skeleton

def Skeleton(
    **attrs
):

A decorative placeholder for content that has not loaded

Skeleton.demo()

The loading message is outside the busy region. When the content arrives, replace the placeholders and clear aria-busy, or replace the whole card. Skeleton does not make these changes itself.