Skip to main content

FallbackImageController

Purpose

If an image fails to load from the source, provide a fallback image, or hide it from the DOM altogether to prevent the unsightly little broken-image icon.

Actions

ActionPurpose
--

Targets

TargetPurposeDefault
---

Classes

ClassPurposeDefault
success (Optional)The class that will be applied to the image if the image loads successfully-
fail (Optional)The class that will be applied to the image if the fallback is triggered-

Values

ValueTypeDescriptionDefault
placeholder (Optional)StringThe URL of a fallback image to be displayed if the <img>'s src cannot be loaded.The <img>'s src

Events

EventWhenDispatched onevent.detail
fallback-image:placeholderWhen the image fails to load and falls back to the placeholderThe controller root element-
fallback-image:failWhen the image and the placeholder (if any) fails to loadThe controller root element-

Side Effects

  • Sets an onError handler for the mounted <img> tag.
  • Sets the style of the image to be display: none if the image fails to load.

How to Use

If an image fails to load, the controller will either try to set the src to a placeholder image you provide, or hide the image using display: none.

If the placeholder URL you provide also fails to load, the controller will fall back to the default behaviour of hiding the element.