Search for a command to run...
Displays content within a desired ratio.
npx shadcn@latest add @zaidan/aspect-ratiopnpx shadcn add @zaidan/aspect-ratioyarn dlx shadcn@latest add @zaidan/aspect-ratiobunx shadcn@latest add @zaidan/aspect-ratioCopy and paste the following code into your project.
1import type { ComponentProps, JSX } from "solid-js";2import { splitProps } from "solid-js";3
4import { cn } from "~/lib/utils";5
6type AspectRatioProps = ComponentProps<"div"> & {7 ratio: number;8 class?: string | undefined;9};10
11const AspectRatio = (props: AspectRatioProps) => {12 const [local, others] = splitProps(props, ["class", "ratio"]);13
14 return (15 <div16 data-slot="aspect-ratio"17 style={18 {19 "--ratio": local.ratio,20 } as JSX.CSSProperties21 }22 class={cn("relative aspect-(--ratio) overflow-hidden", local.class)}23 {...others}24 />25 );26};27
28export { AspectRatio };29export type { AspectRatioProps };Here are the source code of all the examples from the preview page:
import { AspectRatio } from "~/components/ui/aspect-ratio";function AspectRatio16x9() { return ( <Example title="16:9" class="items-center justify-center"> <AspectRatio ratio={16 / 9} class="w-full rounded-lg bg-muted"> <img src="https://avatar.vercel.sh/shadcn1" alt="Photo" class="h-full w-full rounded-lg object-cover grayscale dark:brightness-20" /> </AspectRatio> </Example> );}import { AspectRatio } from "~/components/ui/aspect-ratio";function AspectRatio1x1() { return ( <Example title="1:1" class="items-start"> <AspectRatio ratio={1 / 1} class="w-full rounded-lg bg-muted"> <img src="https://avatar.vercel.sh/shadcn1" alt="Photo" class="h-full w-full rounded-lg object-cover grayscale dark:brightness-20" /> </AspectRatio> </Example> );}import { AspectRatio } from "~/components/ui/aspect-ratio";function AspectRatio9x16() { return ( <Example title="9:16" class="items-center justify-center"> <AspectRatio ratio={9 / 16} class="w-full rounded-lg bg-muted"> <img src="https://avatar.vercel.sh/shadcn1" alt="Photo" class="h-full w-full rounded-lg object-cover grayscale dark:brightness-20" /> </AspectRatio> </Example> );}import { AspectRatio } from "~/components/ui/aspect-ratio";function AspectRatio21x9() { return ( <Example title="21:9" class="items-center justify-center"> <AspectRatio ratio={21 / 9} class="w-full rounded-lg bg-muted"> <img src="https://avatar.vercel.sh/shadcn1" alt="Photo" class="h-full w-full rounded-lg object-cover grayscale dark:brightness-20" /> </AspectRatio> </Example> );}