Image RS

Edit Component

Y Image RS Yew Usage

Adding Image RS to your project is simple:

  1. Make sure your project is set up with Yew. Follow their Getting Started Guide for setup instructions.

  2. Add the Image RS component to your dependencies by including it in your Cargo.toml file:

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

🛠️ Usage

Incorporating Image RS into your Yew application is easy. Follow these steps:

  1. Import the Image component into your Yew project:

    use yew::prelude::*;
    use image_rs::yew::Image;
    use image_rs::Layout;
  2. Use the Image component within your Yew application:

    use yew::prelude::*;
    use image_rs::yew::Image;
    use image_rs::Layout;
    
    #[function_component(App)]
    pub fn app() -> Html {
        html! {
            <Image
                src="/images/photo.jpg"
                alt="A beautiful view"
                width="800"
                height="600"
                layout={Layout::Responsive}
            />
        }
    }

🔧 Props

🖼️ Main Props

PropertyTypeDescriptionDefault
src&'static strThe image source URL.""
alt&'static strAlt text for accessibility."Image"
fallback_src&'static strImage shown if the primary source fails.""
width&'static strWidth in pixels.""
height&'static strHeight in pixels.""
layoutLayoutLayout type: Responsive, Fixed, etc.Responsive
placeholder&'static strPlaceholder image while loading."empty"
loadingLoadingLazy or Eager loading strategy.Lazy
prioritybool(Deprecated) Use loading = Eager instead.false

🎨 Styling Props

+-----------------------------------------------------------+
|                  [Image Container]                        |  <-- `class` & `style`
|                                                           |
|   +-----------------------------------------------+       |  <-- `layout`
|   |              [Loaded Image]                   |       |  <-- `src`
|   +-----------------------------------------------+       |
|                                                           |
+-----------------------------------------------------------+
PropertyTypeDescriptionDefault
style&'static strInline CSS styles.""
class&'static strCSS class name(s).""
object_fitObjectFitHow the image fits: Contain, Cover, Fill, etc.Contain
object_positionPositionImage position inside container (e.g., TopRight).Center
sizes&'static strDefines image sizes for responsive rendering.""
quality&'static strImage quality hint ("low", "high").""
blur_data_url&'static strLow-quality blur-up image while loading.""

⚙️ Behavioral Props

PropertyTypeDescriptionDefault
on_loadCallback<()>Triggered when image finishes loading.No-op
on_errorCallback<String>Triggered if the image fails to load.No-op
decodingDecodingControls image decode strategy: Auto, Sync, AsyncAuto

🌐 Network & Source Props

PropertyTypeDescriptionDefault
srcset&'static strComma-separated list of responsive image sources.""
crossoriginCrossOriginCORS policy (Anonymous, UseCredentials).CrossOrigin::None
referrerpolicyReferrerPolicyReferrer policy for requests.NoReferrer
usemap&'static strAssociates the image with a <map> by ID.""
ismapboolEnables server-side image maps (inside <a href>).false

⚡ Performance Props

PropertyTypeDescriptionDefault
fetchpriorityFetchPriorityNetwork priority (High, Low, Auto).Auto
elementtiming&'static strMarks image with ID for PerformanceElementTiming.""
attributionsrc&'static strURL for Attribution Reporting (experimental).""
lazy_boundary&'static strDistance from viewport to trigger lazy load (e.g. 200px)."100px"
unoptimizedboolDisables built-in image optimization.false

🧠 Accessibility Props (ARIA)

PropertyTypeDescriptionDefault
aria_current&'static strIndicates the current step/page ("page", "step", etc).""
aria_describedby&'static strID of an element describing the image.""
aria_expanded&'static str"true" or "false" for expanded/collapsed state.""
aria_hidden&'static strHides image from assistive tech ("true" or "false")."false"
aria_liveAriaLiveDynamic update priority (Off, Polite, Assertive).Off
aria_pressedAriaPressedIndicates toggle state (True, False, Mixed, or Undefined).False
aria_controls&'static strID of the element that the image controls.""
aria_labelledby&'static strID of the label element for the image.""

🧱 Utility

PropertyTypeDescriptionDefault
node_refNodeRefReference to the DOM image element for JS interop or tracking.Default

💡 Notes

  • The src and alt attributes are required for basic functionality.
  • The width and height are essential for Responsive, Intrinsic, and Fixed layouts.
  • Use the placeholder and blur_data_url properties for a smoother loading experience.
  • Customize the appearance and behavior using class, style, and other props like layout and object_fit.
  • Callbacks like on_load and on_error allow you to handle the image loading process effectively.
  • The Layout::Fill value ignores the width and height props and stretches to fill the container.
  • Accessibility attributes like aria-label and aria-hidden can be used directly on the image element.
  • Priority images (priority = true) are loaded eagerly rather than lazily.
  • If both src and fallback_src fail, the on_error callback is triggered with an error message.
  • blur_data_url is used for rendering a low-quality blurred image while the full image loads.
  • IntersectionObserver: This is used for intelligent lazy loading of images as they enter the viewport.
  • Async/Await: Fetch operations use non-blocking async/await for smoother fallback handling.

📈 Benchmark

  1. Open browser DevTools (Press F12) > Lighthouse tab.
  2. Record page load by clicking "Analyze Page Load".
  3. Observe:
    • First Contentful Paint (FCP)
    • Largest Contentful Paint (LCP)
    • Time to Interactive (TTI)
    • Total network transfer size
    • Memory usage

🚀 Summary

FeatureYew Image RSNext.js Image
Native Rust+Wasm
Built-in Image Optimization
SSR/SEO Friendly
Fine-grained DOM Control
Smaller JS Payload

📊 Performance Results

When loading 10 images, Yew Image RS and Next.js Image are on par:

MetricYew (Wasm)Next.js
Performance Score (Lighthouse)100100
Memory Usage (Heap)~8 MB~8 MB

However, when scaling up to 10,000 images loaded simultaneously:

MetricYew (Wasm)Next.js
Performance Score (Lighthouse)64❌ (Lighthouse fails)
Memory Usage (Heap)~78 MB~83 MB
Scrolling SmoothnessVery SmoothLaggy

Key observations:

  • Wasm (Yew) handles large DOM updates much better than JavaScript.
  • Memory usage is slightly lower with Wasm.
  • Next.js site failed Lighthouse audit at 10,000 images (due to TTI timeout).
  • Smoothness is significantly better with Yew under heavy load.

🛠️ Future Improvements

  • Image RS is working on automatic image optimization.
  • Progressive loading and lazy hydration strategies are being researched for even better large-scale performance.

Usage

CLI Usage

You can import Image RS directly into your project to customize and modify it using the opensass cli:

os add image-rs yew