aspect-ratio.tsx
Overview
The aspect-ratio.tsx file provides a simple wrapper component for maintaining a consistent aspect ratio in React applications. It leverages the @radix-ui/react-aspect-ratio package, a well-tested and accessible primitive for handling aspect ratio constraints on elements. This file re-exports the Root component from Radix UI's Aspect Ratio primitive under the name AspectRatio, making it easy to import and use within the broader application.
This component is primarily used to ensure that content (such as images, videos, or other media) scales responsively while preserving a specified width-to-height ratio, which is crucial for maintaining visual consistency and layout stability.
Detailed Explanation
Imported Modules
@radix-ui/react-aspect-ratio: This package provides composable React components which enforce an element's aspect ratio via CSS. The
Rootcomponent is the main building block that wraps content to constrain its aspect ratio.
Exported Component
AspectRatio
Type: React Functional Component (from Radix UI Primitive)
Source:
AspectRatioPrimitive.Rootfrom@radix-ui/react-aspect-ratio
Purpose
AspectRatio acts as a container that forces its children to maintain a given width-to-height ratio. This is particularly useful when embedding media or other content that should not distort regardless of screen size or container resizing.
Usage
import { AspectRatio } from './aspect-ratio';
function Example() {
return (
<AspectRatio ratio={16 / 9} style={{ width: 300 }}>
<img src="video-thumbnail.jpg" alt="Video Thumbnail" />
</AspectRatio>
);
}
Parameters / Props
Since AspectRatio is directly re-exported from AspectRatioPrimitive.Root, all props supported by the Radix UI primitive are supported:
ratio (
number): Required. The width-to-height ratio (e.g., 16/9, 4/3, 1).style (
React.CSSProperties): Optional. Inline styles (e.g., width) to define the size of the container.className (
string): Optional. CSS class names for styling.children (
React.ReactNode): The content that should maintain the aspect ratio.
Return Value
Returns a React element that constrains its children to the specified aspect ratio.
Implementation Details
The file uses the
'use client';directive indicating the code is intended to run in a client-side React environment, typically relevant in frameworks like Next.js with server and client components.The file imports everything from
@radix-ui/react-aspect-ratioasAspectRatioPrimitive, but only re-exports theRootcomponent under the nameAspectRatio.No additional logic or styling is applied here; it's a direct pass-through to promote standardized usage and simplify imports in other parts of the application.
Interactions with Other System Components
UI Layer / Components: This file is likely imported by other UI components that require consistent aspect ratios for media or content blocks.
Styling: While this component manages the aspect ratio, visual styling (colors, borders, shadows) should be applied externally, ensuring separation of concerns.
Radix UI Library: Depends on
@radix-ui/react-aspect-ratiofor core functionality, ensuring accessibility and responsiveness are handled robustly.
Mermaid Diagram: Component Interaction
componentDiagram
direction TB
AspectRatio <|-- AspectRatioPrimitive.Root : Re-export
AspectRatio o-- Children : Wraps and constrains
This diagram shows that the AspectRatio component is a direct re-export (alias) of AspectRatioPrimitive.Root and wraps its children to enforce the aspect ratio constraint.
Summary
aspect-ratio.tsxis a minimal wrapper for Radix UI's Aspect Ratio primitive.It exports
AspectRatio, a React component that constrains its children to a fixed width-to-height ratio.No additional logic or styles are added, promoting simplicity and reuse.
Enables responsive, accessible aspect ratio control in UI components.
Integrates seamlessly with other UI components needing consistent layout behavior.