Liking cljdoc? Tell your friends :D

x-alert

A themeable, accessible alert banner that supports four semantic variants, an optional dismiss button, auto-dismiss via timeout, enter/exit animations, and full keyboard interaction.


Tag

<x-alert></x-alert>

Attributes

AttributeTypeDefaultDescription
typeenum"info"Semantic variant: info success warning error
textstring""Alert message text
iconstring | "none"Custom icon glyph. Absent = default glyph; "none" = hidden
dismissibleboolean†trueWhether the dismiss button is shown
disabledbooleanfalseDims the alert and disables the dismiss button
timeout-msnumberAuto-dismiss delay in milliseconds (positive int)

dismissible uses default-true semantics: absent or empty = true; dismissible="false" = false.


Properties

PropertyTypeReflects attribute
typestringtype
textstringtext
iconstring | nullicon
dismissiblebooleandismissible
disabledbooleandisabled
timeoutMsnumber | nulltimeout-ms

Events

EventBubblesComposedCancelableDetail
x-alert-dismissyesyesyes{ type, reason, text }

detail.reason is "button" (user click or Escape key) or "timeout" (auto-dismiss).

Calling event.preventDefault() cancels the removal — the alert stays in the DOM.


Slots

SlotDescription
iconCustom icon element (overrides default glyph and attribute icon)
<x-alert type="success" text="Saved">
  <svg slot="icon" aria-hidden="true">...</svg>
</x-alert>

Parts

PartDescription
containerOuter flex wrapper
iconIcon area <span>
default-iconFallback glyph <span>
textMessage text <span>
dismissDismiss <button>

CSS Custom Properties

Layout & typography

VariableDefaultDescription
--x-alert-radius10pxBorder radius
--x-alert-padding-y10pxVertical padding
--x-alert-padding-x12pxHorizontal padding
--x-alert-gap10pxGap between icon, text, button
--x-alert-font-size0.875remText size

Motion

VariableDefaultDescription
--x-alert-enter-duration140msEnter animation duration
--x-alert-exit-duration160msExit animation duration
--x-alert-motion-fast120msTransition speed
--x-alert-motion-easecubic-bezierEasing function
--x-alert-press-scale0.98Dismiss button press scale

Semantic colour tokens (light)

VariableDefault (light)
--x-alert-info-bgrgba(0,102,204,0.08)
--x-alert-info-borderrgba(0,102,204,0.35)
--x-alert-info-colorrgba(0,60,120,0.95)
--x-alert-success-bgrgba(16,140,72,0.10)
--x-alert-success-borderrgba(16,140,72,0.35)
--x-alert-success-colorrgba(10,90,46,0.95)
--x-alert-warning-bgrgba(204,120,0,0.12)
--x-alert-warning-borderrgba(204,120,0,0.45)
--x-alert-warning-colorrgba(120,70,0,0.95)
--x-alert-error-bgrgba(190,20,40,0.10)
--x-alert-error-borderrgba(190,20,40,0.45)
--x-alert-error-colorrgba(120,10,20,0.95)

Dark-mode variants are set automatically via @media (prefers-color-scheme: dark).


Accessibility

  • [part=container] carries role="status" (info/success) or role="alert" (warning/error).
  • The host element receives tabindex="0" and aria-keyshortcuts="Escape" when dismissible and not disabled.
  • The dismiss button carries aria-label="Dismiss alert".
  • When disabled, the host receives aria-disabled="true" and tabindex="-1".
  • Animations respect @media (prefers-reduced-motion: reduce).

Keyboard

KeyConditionAction
EscapeFocused, dismissible, not disabledDismisses the alert

Icon behaviour

icon attributeslot[name=icon] contentResult
absentabsentDefault glyph for type
absentpresentSlotted element shown
"none"absentIcon area hidden
"none"presentSlotted element shown (slot wins)
custom stringabsentCustom glyph shown
custom stringpresentSlotted element shown

Auto-dismiss

Set timeout-ms to a positive integer. The alert dispatches x-alert-dismiss with reason: "timeout" after the delay. The event is cancelable — call preventDefault() to keep the alert visible.

<x-alert type="success" text="Saved successfully" timeout-ms="4000"></x-alert>

The timer starts on connectedCallback and is cleared on disconnectedCallback. Changing timeout-ms while connected restarts the timer.


Examples

Basic variants

<x-alert type="info"    text="Your session will expire in 5 minutes."></x-alert>
<x-alert type="success" text="Changes saved."></x-alert>
<x-alert type="warning" text="Low disk space."></x-alert>
<x-alert type="error"   text="Failed to save. Please try again."></x-alert>

Non-dismissible

<x-alert type="warning" text="Read-only mode." dismissible="false"></x-alert>

Auto-dismiss

<x-alert type="success" text="Upload complete." timeout-ms="3000"></x-alert>

Custom icon via slot

<x-alert type="info" text="New features are available.">
  <svg slot="icon" width="16" height="16" aria-hidden="true">...</svg>
</x-alert>

Prevent dismiss programmatically

document.querySelector('x-alert').addEventListener('x-alert-dismiss', e => {
  if (!confirmed) e.preventDefault();
});

ClojureScript (hiccup renderer)

[:x-alert {:type "success" :text "Profile updated."}]

[:x-alert {:type    "error"
           :text    "Payment failed."
           :on-x-alert-dismiss (fn [e]
                                 (swap! state/app assoc :show-alert false))}]

[:x-alert {:type       "info"
           :text       "Session expiring soon."
           :timeout-ms "5000"}]

Can you improve this documentation?Edit on GitHub

cljdoc builds & hosts documentation for Clojure/Script libraries

Keyboard shortcuts
Ctrl+kJump to recent docs
Move to previous article
Move to next article
Ctrl+/Jump to the search field
× close