@unlazy/solid
The Solid integration provides an UnLazyImage component for your Solid 1.9+ application.
Installation
Install the @unlazy/solid package using your favorite package manager:
pnpm add -D @unlazy/solidyarn add -D @unlazy/solidnpm install -D @unlazy/solidImport the UnLazyImage component in your component file:
import { UnLazyImage } from '@unlazy/solid'
export default function MyComponent() {
return (
<UnLazyImage
blurhash="LKO2:N%2Tw=w]~RBVZRi};RPxuwH"
srcSet="image-320w.jpg 320w, image-640w.jpg 640w"
autoSizes
/>
)
}UnLazyImage Component
The UnLazyImage component allows you to easily implement unlazy in your Solid application, providing a smoother image loading experience.
The component supports automatic calculation of the sizes attribute with the autoSizes prop. It also enables you to specify a blurhash or thumbhash for the blurry placeholder image.
Props
The UnLazyImage component accepts the following props:
| Prop | Type | Default | Description |
|---|---|---|---|
src | String | - | Image source URL to be lazy-loaded. |
srcSet | String | - | Image source set to be lazy-loaded. |
sources | Array | - | Array of UnLazySource objects to render a <picture> element. |
autoSizes | Boolean | false | Whether the sizes attribute should be automatically calculated. |
blurhash | String | - | A BlurHash string representing the blurry placeholder image. |
thumbhash | String | - | A ThumbHash string representing the blurry placeholder image. If both are provided, thumbhash takes precedence. |
placeholderSrc | String | - | Optional image source URL for a custom placeholder image. Ignored if a hash is provided. |
placeholderSize | Number | 32 | The size of the longer edge for BlurHash decoding. Ignored for ThumbHash. |
preload | Boolean | false | Whether the image should be preloaded immediately, bypassing lazy loading. |
loading | String | 'lazy' | Loading strategy for the image ('lazy' or 'eager'). |
onImageLoad | Function | - | Callback function invoked when the image has been successfully loaded. Receives the HTMLImageElement as an argument. |
onImageError | Function | - | Callback function invoked when an error occurs during image loading. Receives the HTMLImageElement and Event as arguments. |
The component also accepts all standard <img> HTML attributes.
Solid.js Reactivity
This component uses Solid.js's fine-grained reactivity primitives:
createSignal(): Creates a reactive signal for storing the img element referencecreateEffect(): Runs when tracked signals change, automatically setting up lazy loadingonCleanup(): Registers cleanup function to remove event listeners on unmountsplitProps(): Separates component-specific props from HTML attributes to be forwarded
Examples
return (
<UnLazyImage
width={800}
height={600}
blurhash="LKO2:N%2Tw=w]~RBVZRi};RPxuwH"
srcSet="image-320w.jpg 320w, image-640w.jpg 640w"
autoSizes
/>
)return (
<UnLazyImage
width={800}
height={600}
thumbhash="1QcSHQRnh493V4dIh4eXh1h4kJUI"
srcSet="image-320w.jpg 320w, image-640w.jpg 640w"
autoSizes
/>
)return (
<UnLazyImage
width={800}
height={600}
placeholderSrc="data:image/svg+xml, ..."
srcSet="image-320w.jpg 320w, image-640w.jpg 640w"
autoSizes
/>
)INFO
When using BlurHash, set explicit width and height props. See Hash-Based Placeholders for why.
TIP
In each example, the sizes attribute is automatically calculated given the auto-sizes prop.
Multiple Image Sources
Pass sources to render a <picture> element with format negotiation and art direction. Each entry becomes a <source> child:
import type { UnLazySource } from 'unlazy'
const sources: UnLazySource[] = [
{ type: 'image/avif', srcSet: 'hero.avif 1x, hero@2x.avif 2x' },
{
media: '(max-width: 600px)',
srcSet: 'hero-mobile.jpg',
width: 480,
height: 640,
},
]
return (
<UnLazyImage
src="/hero.jpg"
sources={sources}
width={640}
height={427}
/>
)TIP
Set width and height per source to prevent layout shift when the breakpoint changes. Modern browsers use the matched <source>'s dimensions for the rendered <img>'s aspect ratio.
Preload Image
Useful if the UnLazyImage is part of e.g. a slider, and you want to preload the next image.
return (
<UnLazyImage
blurhash="LKO2:N%2Tw=w]~RBVZRi};RPxuwH"
srcSet="image-320w.jpg 320w, image-640w.jpg 640w"
autoSizes
preload
/>
)