Module leptos

Source
Available on crate feature lep only.
Expand description

§🌱 Leptos Image RS Usage

Adding Image RS to your Leptos project is simple:

  1. Make sure your project is set up with Leptos. Refer to their Getting Started Guide for setup instructions.

  2. Add image-rs to your dependencies:

    cargo add image-rs --features=lep
  3. Import the Image component into your Leptos component and start using it in your app.

§🛠️ Usage

Incorporating the Image component into your Leptos application is easy. Here’s a basic example:

use leptos::{*, prelude::*};
use image_rs::leptos::Image;
use image_rs::Layout;
use leptos::logging::log;

#[component]
pub fn App() -> impl IntoView {
    view! {
        <Image
            src="https://example.com/image.jpg"
            alt="An example image"
            width="600"
            height="400"
            layout=Layout::Responsive
            class="my-image"
            style="border-radius: 8px;"
            placeholder="blur"
            blur_data_url="data:image/png;base64,..."
            fallback_src="https://example.com/fallback.jpg"
            on_load=Callback::new(|_| log!("Image loaded successfully"))
            on_error=Callback::new(|err| log!("Image failed: {err}"))
        />
    }
}

§🔧 Props

§Image Props

§Main Props
PropertyTypeDescriptionDefault
src&'static strThe URL of the image to be displayed.""
alt&'static strAlt text for accessibility and SEO."Image"
fallback_src&'static strURL for the fallback image in case of error.""
width&'static strThe width of the image.""
height&'static strThe height of the image.""
layoutLayoutImage layout: Fill, Responsive, or Intrinsic.Responsive
§Loading & Placeholder Props
PropertyTypeDescriptionDefault
loadingLoadingImage loading behavior: Eager or Lazy.Lazy
placeholder&'static strPlaceholder type: use "blur" for blurred placeholder."empty"
blur_data_url&'static strBase64-encoded data URL used when placeholder="blur".""
§Styling Props
PropertyTypeDescriptionDefault
class&'static strCSS class for the <img> element.""
style&'static strAdditional inline styles.""
object_fitObjectFitHow the image should fit within its box.Fill
object_positionPositionImage alignment inside the container.Center
sizes&'static strSizes attribute for responsive images.""
srcset&'static strSrcset for responsive image variants.""
§Event Callbacks
PropertyTypeDescription
on_loadOption<Callback<()>>Triggered when the image successfully loads.
on_errorOption<Callback<String>>Triggered when the image fails to load.
§Advanced Browser Attributes
PropertyTypeDescriptionDefault
decodingDecodingHow the browser should decode the image.Auto
crossoriginCrossOriginCORS behavior for the image.None
referrerpolicyReferrerPolicyReferrer policy for the request.NoReferrer
fetchpriorityFetchPriorityPriority for fetching the image.Auto
§Accessibility Props
PropertyTypeDescriptionDefault
aria_current&'static strARIA current attribute""
aria_describedby&'static strARIA description""
aria_expanded&'static strARIA expanded attribute""
aria_hidden&'static strWhether the image is hidden from assistive technologies.""
aria_liveAriaLiveARIA live region setting.Off
aria_pressedAriaPressedARIA pressed state.Undefined
aria_controls&'static strID of the element that this controls.""
aria_labelledby&'static strID of the element that labels this image.""
§Other Props
PropertyTypeDescriptionDefault
node_refNodeRef<Img>Reference to the <img> DOM element.None
usemap&'static strHTML usemap attribute value.""
ismapboolIndicates if the image is part of a server-side map.false
elementtiming&'static strUsed for performance reporting (e.g., LCP).""
attributionsrc&'static strAttribution source for content licensing.""

§💡 Notes

  • The Image component is flexible for layout use cases: use Fill to cover parent, or Responsive to maintain aspect ratio.
  • Use the placeholder="blur" and blur_data_url to provide a low-res preview while loading.
  • Fallback logic automatically switches to fallback_src if the main image fails to load.
  • All ARIA attributes and semantic accessibility features are built-in and customizable.
  • The component supports lazy loading by default with loading=Loading::Lazy.

Structs§

ImageProps
Props for the Image component.

Functions§

Image
Optional Props