Skip to content

Image

Reactive load an image in the browser

๐ŸŽฌ Usage

<script>
    import { image } from "@sveu/browser"

    const { loading } = image({
        src: "https://i.ibb.co/JjL8tSt/final-fantasy-xiii-2-lightning-2560x1440.jpg",
    })
</script>

๐Ÿ‘ฉโ€๐Ÿ’ปAPI

๐Ÿ‘ป Arguments

Name Description Type Required
options Read options ImageOptions Yes
async_state_options Read options AsyncStateOptions No

๐Ÿ™ˆ Options

Name Description Type Default
src Image source string Required filed
srcset Image source set string undefined
sizes Image sizes string undefined

โ†ฉ๏ธ Returns

AsyncState Returns

๐Ÿงช Playground

Source Code ๐Ÿ‘€

Source Code
import { async_state } from "@sveu/shared"
import type { AsyncStateOptions } from "@sveu/shared"

import type { ImageOptions } from "../utils"

async function load_image(options: ImageOptions): Promise<HTMLImageElement> {
    return new Promise((resolve, reject) => {
        const img = new Image()

        const { src, srcset, sizes } = options

        img.src = src

        if (srcset) img.srcset = srcset

        if (sizes) img.sizes = sizes

        img.onload = () => resolve(img)

        img.onerror = reject
    })
}

/**
 * Reactive load an image in the browser, you can wait the result to display it or show a fallback.
 *
 * @param options - Options.
 * - `src` Address of the resource.
 * - `srcset` Images to use in different situations, e.g., high-resolution displays, small monitors, etc.
 * - `sizes` Image sizes for different page layouts.
 *
 * @param async_state_options - see [Async state options](https://svelte-u.netlify.app/shared/async_state/#options)
 *
 * @returns An async state. See [Async state](https://svelte-u.netlify.app/shared/async_state/#returns)
 */
export function image(
    options: ImageOptions,
    async_state_options?: AsyncStateOptions
) {
    const state = async_state<HTMLImageElement | undefined>(
        () => load_image(options),
        undefined,
        {
            reset_on_execute: true,
            ...async_state_options,
        }
    )
    state.execute(async_state_options ? async_state_options.delay : 0)

    return state
}

Last update: 2023-03-09