Skip to content

Aspect Ratio

Hiển thị nội dung trong tỷ lệ khung hình mong muốn.

Photo by Drew Beamer

Cài đặt

bash
npx @galaxy-stack/nebula-cli@latest add aspect-ratio
bash
pnpm dlx @galaxy-stack/nebula-cli@latest add aspect-ratio
bash
yarn dlx @galaxy-stack/nebula-cli@latest add aspect-ratio
bash
bunx @galaxy-stack/nebula-cli@latest add aspect-ratio

Sử dụng

vue
<script setup lang="ts">
import { AspectRatio } from '@/components/ui/aspect-ratio'
</script>

<template>
  <AspectRatio :ratio="16 / 9" class="bg-muted">
    <img
      src="https://images.unsplash.com/photo-1588345921523-c2dcdb7f1dcd?w=800&dpr=2&q=80"
      alt="Photo by Drew Beamer"
      class="rounded-md object-cover"
    />
  </AspectRatio>
</template>
tsx
import { AspectRatio } from "@/components/ui/aspect-ratio"

export default function App() {
  return (
    <AspectRatio ratio={16 / 9} className="bg-muted">
      <img
        src="https://images.unsplash.com/photo-1588345921523-c2dcdb7f1dcd?w=800&dpr=2&q=80"
        alt="Photo by Drew Beamer"
        className="rounded-md object-cover"
      />
    </AspectRatio>
  )
}
typescript
import { Component } from '@angular/core';
import { AspectRatioComponent } from '@/components/ui/aspect-ratio';

@Component({
  selector: 'app-root',
  standalone: true,
  imports: [AspectRatioComponent],
  template: `
    <ui-aspect-ratio [ratio]="16 / 9" class="bg-muted">
      <img
        src="https://images.unsplash.com/photo-1588345921523-c2dcdb7f1dcd?w=800&dpr=2&q=80"
        alt="Photo by Drew Beamer"
        class="rounded-md object-cover"
      />
    </ui-aspect-ratio>
  `
})
export class AppComponent {}

API Reference

Props

PropTypeDefaultMô tảFrameworks
rationumber1Tỷ lệ khung hình mong muốn (ví dụ: 16/9)All
asChildbooleanfalseRender như phần tử conReact, Vue, Angular
childrenReactNode-Nội dung bị giới hạn bởi tỷ lệReact, Vue, Angular

Ví dụ

Tỷ lệ 16:9

vue
<AspectRatio :ratio="16 / 9">
  <img src="..." alt="16:9 aspect ratio" />
</AspectRatio>
tsx
<AspectRatio ratio={16 / 9}>
  <img src="..." alt="16:9 aspect ratio" />
</AspectRatio>
typescript
<ui-aspect-ratio [ratio]="16 / 9">
  <img src="..." alt="16:9 aspect ratio" />
</ui-aspect-ratio>

Tỷ lệ 4:3

vue
<AspectRatio :ratio="4 / 3">
  <img src="..." alt="4:3 aspect ratio" />
</AspectRatio>
tsx
<AspectRatio ratio={4 / 3}>
  <img src="..." alt="4:3 aspect ratio" />
</AspectRatio>
typescript
<ui-aspect-ratio [ratio]="4 / 3">
  <img src="..." alt="4:3 aspect ratio" />
</ui-aspect-ratio>

Tỷ lệ 1:1 (Vuông)

vue
<AspectRatio :ratio="1">
  <img src="..." alt="Square aspect ratio" />
</AspectRatio>
tsx
<AspectRatio ratio={1}>
  <img src="..." alt="Square aspect ratio" />
</AspectRatio>
typescript
<ui-aspect-ratio [ratio]="1">
  <img src="..." alt="Square aspect ratio" />
</ui-aspect-ratio>

Khả năng truy cập

  • Điều hướng bàn phím: [TODO]
  • Đọc màn hình: [TODO]
  • Quản lý focus: [TODO]
  • Tuân thủ WCAG: Tuân thủ WCAG 2.1 cấp độ AA

Tác giả

Bùi Trọng Hiếu (kevinbui)

Giấy phép

MIT © 2025 Bùi Trọng Hiếu (kevinbui)

Phát hành theo Giấy phép MIT.