HextaUI

Aspect ratio: Slow load

The box holds its shape and shimmers until the image arrives, then the image fades in, so nothing below it moves. Press Reload to watch it again.

Sunset over mountains
"use client"

import * as React from "react"

import { AspectRatio } from "@/components/ui/aspect-ratio"
import { Button } from "@/components/ui/button"

export function AspectRatioSlowLoad() {
  const [src, setSrc] = React.useState<string | undefined>(undefined)
  const [attempt, setAttempt] = React.useState(0)

  React.useEffect(() => {
    const timer = setTimeout(
      () => setSrc(`/preview/landscape.svg?v=${attempt}`),
      1500
    )
    return () => clearTimeout(timer)
  }, [attempt])

  return (
    <div className="flex w-full max-w-md flex-col gap-3">
      <AspectRatio ratio={16 / 9} className="rounded-xl">
        <img src={src} alt="Sunset over mountains" />
      </AspectRatio>
      <Button
        variant="outline"
        size="sm"
        className="self-start"
        onClick={() => {
          setSrc(undefined)
          setAttempt(attempt + 1)
        }}
      >
        Reload
      </Button>
    </div>
  )
}

This adds the example as components/examples/aspect-ratio/slow-load.tsx, together with the Aspect ratio component and the HextaUI theme tokens it needs.

pnpm dlx shadcn@latest add https://hextaui.com/r/aspect-ratio-slow-load.json

To add only the component, follow the Aspect ratio installation.

Props in this example

The Aspect ratio props this example sets, from the API reference.

PropTypeDefault
ratio
number | `${number}/${number}` | `${number}:${number}`1

Every prop is in the Aspect ratio API reference.

See them all, with keyboard support and accessibility notes, on the Aspect ratio page.