Aspect Ratio
npx @vyui/cli add aspect-ratioOverview
VyAspectRatio is the @vyui/core AspectRatio primitive re-exported by @vyui/kit. It renders one primitive element at width: 100% and applies the native CSS aspect-ratio property.
Usage
Import the component and its public type from the kit barrel.
import { VyAspectRatio, type AspectRatioProps } from '@vyui/kit'
The unprefixed AspectRatio primitive is still available from @vyui/core if you prefer the raw name.
Features and behavior
ratiois width divided by height: use16 / 9,4 / 3, or1for a square.- The component sets
width: 100%because Lynx needs one definite dimension before it can derive the other. - It renders a single element rather than a padding wrapper with absolutely positioned content.
- The default slot receives
aspect, calculated as(1 / ratio) * 100, for consumers that need the percentage form. asandasChildcome from the corePrimitiveAPI.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
ratio | number | 1 | Desired width-to-height ratio. |
as | AsTag | Component | 'view' | Element or component to render. |
asChild | boolean | false | Merges behavior and attributes into the single child through the primitive slot mechanism. |
Additional attributes, including class and style, are forwarded to the rendered primitive. The component's inline style supplies width: 100% and aspectRatio.
Emits
This component does not emit events.
Slots
| Slot | Props | Description |
|---|---|---|
default | { aspect: number } | Content constrained by the ratio; aspect is the percentage equivalent. |
Styling and theming
VyAspectRatio is not themeable through appConfig.ui and has no ui prop, slots, variants, or kit theme file. Style the component with forwarded class or style, and style its child so it fills the available box.
Accessibility
The ratio wrapper is structural and adds no role. Accessibility comes from its content: provide meaningful alternative text or native accessibility labeling for images and other media. Decorative media should be treated as decorative by the consuming application.
Platform notes
- Lynx Starlight supports the
aspect-ratioproperty directly. - If you want height to be the definite dimension, override the default width and provide an explicit height.
- Avoid
ratio={0}or negative ratios; the component does not validate the value. - The root exposes
data-vyui-aspect-ratiowith the current numeric ratio.
Related components
Avatarfor circular profile imagery.Skeletonfor fixed-ratio loading placeholders.Cardfor framed media and content.