Command palette

@yasinsensoy/web

Web · Components

Image

Image that manages its own loading, error and placeholder states.

3 exports

Live example

Preview

Cappadocia

Code

import { Image } from "@yasinsensoy/web";

<Image src="/kapak.jpg" alt="Cappadocia" width={1200} height={800}
  className="rounded-xl"
/>

// In Next.js, when optimizeImages is passed to the @yasinsensoy/web/next provider
// it renders with next/image (resizing, WebP/AVIF).

Events

  1. Events appear here when you interact with the preview.

An image filling the box in a card (fill) and a custom error view

Aegean cove

Aegean cove

Profile

Custom error view

Import

import { Image } from "@yasinsensoy/web";

Image

componentClient component

Image that manages its own loading, error and placeholder states. Can be used anywhere; Gallery thumbnails are rendered with it too.

  • While loading: a shimmering skeleton, the image's dominant color or a small blurred image (placeholder); appears smoothly once loaded.
  • No layout shift: with width + height, ratio or fill the box reserves space before the image loads.
  • Error: an icon and "Try again"; it retries automatically when the connection comes back.
  • Loads lazily (loading="lazy"); priority for an image on the first screen.
  • Next.js: The component doesn't depend on next/image. When optimizeImages is enabled in the @yasinsensoy/web/next provider, all Images render with next/image (resizing, WebP/AVIF). For another CDN component use the image prop of UIProvider.

It is a client component; it can be used with props from a server component.

Image({ src, alt, width, height, ratio, fill, fit, placeholder, appear, fallback, priority, unoptimized, onLoad, onError, imageRef, className, classNames, style, sizes, loading, effect, ...props }: ImageProps): JSX.Element

Examples

<Image src="/cover.jpg" alt="Cover" width={1200} height={630} className="rounded-xl" />

// An image covering the box in a card, with the dominant color until it loads
<div className="relative aspect-video">
  <Image src={product.image} alt={product.name} fill placeholder="#d6c7b0" />
</div>

// Your own error view
<Image src={avatar} alt="" ratio={1} fallback={<span>YS</span>} />

Classes added to the parts of Image.

NameTypeDescription
image?stringThe <img> element itself.
placeholder?stringPlaceholder (color, blurred thumbnail or shimmer).
fallback?stringError view.

ImageProps

interface

Props of the Image component.

Extends: Omit<ComponentProps<"img">, "src" | "alt" | "width" | "height" | "placeholder" | "onLoad" | "onError" | "ref">

NameTypeDefaultDescription
srcstringImage URL.
altstringAlternative text. Pass "" for a decorative image.
width?numberOriginal width (px). Given together with height, the box reserves space at the right ratio before the image loads (the page doesn't shift); it shrinks if it doesn't fit the width.
height?numberOriginal height (px). See width.
ratio?numberBox ratio (width / height, e.g. 16 / 9). If given, the box becomes full width and reserves space at this ratio.
fill?booleanfalseFill the positioned (relative) container entirely. For cards, backgrounds and galleries.
fit?"cover" | "contain" | "fill" | "none" | "scale-down""cover"How the image fits the box (object-fit).
placeholder?string"shimmer"Shown until it loads: a color ("#8a9bb0", the image's dominant color), a small blurred image (data:image/... or a URL), "shimmer" a shimmering skeleton, or "none".
appear?"fade" | "scale" | "none""fade"Appearance on load: "fade", "scale" (growing slightly) or "none".
fallback?ReactNode | ((retry: () => void) => ReactNode)Content shown if it fails to load. If omitted, an icon, "Image failed to load" and a "Try again" button appear. With a function it receives the retry function. null shows nothing.
priority?booleanfalseThe page's first visible image (e.g. a cover, product image): doesn't load lazily, loads with priority. Keep it to one or two images per page.
unoptimized?booleanfalseSkip the image component given to UIProvider (e.g. next/image) and use a plain <img>. For blob: previews and external SVGs.
onLoad?(image: HTMLImageElement) => voidOn load; with the image itself (for measurements like naturalWidth).
onError?() => voidOn failure.
imageRef?Ref<HTMLImageElement>Access to the <img> element.
className?stringClasses added to the box (e.g. rounded-lg).
classNames?ImageClassNamesClasses added to the parts. See ImageClassNames.
style?CSSPropertiesStyle of the box.
effect?EffectPropEffect or effects of the image box (e.g. "shine", ["tilt", "glow"]). If omitted, UIProvider appearance.defaults.image.effect. See BuiltinEffect.