Skip to main content

Alert

The Alert component is used to provide visual feedback to the user after performing an action and can also serve as an attention-grabber to display important information on the page.

Import

// with @dhl-official/react-library:
import { DhlAlert } from "@dhl-official/react-library"
// with @dhl-official/ui-libraries/react-library:
import { DhlAlert } from "@dhl-official/ui-libraries/react-library"

Code

<DhlAlert bodyText="Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur."></DhlAlert>

Interactive Demo

Alert with a linkHref and linkText supplied

To apply correct stylings, ensure that both linkHref and linkText props are supplied.

<DhlAlert
linkHref="#"
linkText="Link text"
bodyText="Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur."
></DhlAlert>

Positioned Alert with Auto-dismiss

Alerts can be positioned at screen corners and automatically dismiss after a specified duration.

<DhlAlert
position="top-right"
type="success"
titleText="Success!"
bodyText="Your changes have been saved."
duration={5}
animationSpeed="normal"
dismissDirection="origin"
></DhlAlert>

New Props:

  • position: Position the alert at screen corners (top-right, top-left, bottom-right, bottom-left, center-top, center-bottom)
  • duration: Auto-dismiss after specified seconds (e.g., 5 for 5 seconds)
  • persistent: Keep alert visible indefinitely (overrides duration)
  • animationSpeed: Control animation speed (fast, normal, slow)
  • dismissDirection: Direction alert dismisses to (up, down, origin - slides back to where it came from)

Readme

Properties

PropertyAttributeDescriptionTypeDefault
animationSpeedanimation-speedAn optional prop to set the animation speed when dismissing the alert."fast" | "normal" | "slow"DHL_ALERT.ANIMATION_SPEED.NORMAL
bodyTextbody-textAn optional prop defining the text of the body.stringundefined
closeButtonAriaLabelclose-button-aria-labelAn optional prop defining the text read by the screen reader to represent the close Button.string`${this.type} Alert Close Button`
dataClassNamedata-class-nameAn optional class name prop for the component.stringundefined
dataIddata-idAn optional prop. Gives a valid HTML ID attribute value for the component.string`dhl-alert-${getRandomString()}`
dataTestiddata-testidAn optional prop. The test id attached to the component as a data-testid attribute.stringundefined
dataTrackingdata-trackingAn optional data tracking prop for the component.stringundefined
dismissDirectiondismiss-directionAn optional prop to set the direction the alert dismisses to."down" | "origin" | "up"DHL_ALERT.DISMISS_DIRECTION.ORIGIN
durationdurationAn optional prop to set how many seconds the alert should stay visible before auto-dismissing. If not set, the alert will not auto-dismiss.numberundefined
linkHreflink-hrefAn optional prop defining the href of the link.stringundefined
linkTargetlink-targetAn optional prop defining the target of the link.stringundefined
linkTextlink-textAn optional prop defining the text of the link.stringundefined
persistentpersistentAn optional boolean prop to make the alert stay visible indefinitely without auto-dismissing.booleanfalse
positionpositionAn optional prop to set the position of the alert on the screen."bottom-left" | "bottom-right" | "center-bottom" | "center-top" | "top-left" | "top-right"undefined
showCloseButtonshow-close-buttonAn optional boolean prop used to toggle the visibility of the close button.booleantrue
titleTexttitle-textAn optional prop defining the text of the title.stringundefined
typetypeAn optional prop to select the type of the alert. The type of the alert will determine the icon and color of the alert.Variants.error | Variants.info | Variants.neutral | Variants.success | Variants.warningDHL_ALERT.TYPE.NEUTRAL
variantvariantAn optional prop to select a component design variant."inline" | "toast"DHL_ALERT.VARIANT.TOAST

Events

EventDescriptionType
dhlAutoDismissEvent emitted when the alert auto-dismisses.CustomEvent<void>
dhlClickEvent emitted when the close button is clicked on.CustomEvent<MouseEvent>

CSS Custom Properties

NameDescription
--dhl-alert-background-colorUsed to set the background color of the component.
--dhl-alert-border-colorUsed to set the border color of the component.
--dhl-alert-foreground-colorUsed to set the foreground color of the component.

Dependencies

Depends on

Graph


Built by DHL User Interface Library Team!