Working with Resources

Resources in Elements are typically images or files that users add to components. This guide covers how to work with image resources in hooks.

Resource Object Structure

When a user adds an image via a resource control, the resource object contains:

{
    image: "/path/to/image.jpg",  // Path to the image file
    width: 1920,                   // Original width in pixels
    height: 1080,                  // Original height in pixels
    aspect: "16/9",               // Aspect ratio string
    alt: "Image description",      // Alt text
    format: "jpg"                  // File format
}

Accessing Resources

const transformHook = (rw) => {
    const { image, backgroundImage } = rw.props;
    
    // Check if resource exists and has an image
    const hasImage = image && image.image;
    
    if (hasImage) {
        console.log(image.image);  // "/resources/my-image.jpg"
        console.log(image.width);  // 1920
        console.log(image.height); // 1080
    }
    
    rw.setProps({
        hasImage,
        imageSrc: hasImage ? image.image : null,
        imageAlt: hasImage ? image.alt : ''
    });
};

exports.transformHook = transformHook;

Resizing Images

Use rw.resizeResource() to create optimized versions of images:

Retina/HiDPI Support

Always double the intended display width for retina displays:

Responsive Images

Generate sources for a <picture> element:

Template:

Process multiple images from a resource gallery:

Placeholder Images

Show placeholders in edit mode when no image is set:

Background Images

Generate CSS background image classes:

Last updated

Was this helpful?