Security News
GitHub Removes Malicious Pull Requests Targeting Open Source Repositories
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
react-aspect-ratio
Advanced tools
React Component to maintain a consistent width-to-height ratio (aspect ratio), preventing cumulative layout shift.
Tiny React library to addresses the issue of browser reflow by providing an aspect ratio placeholder. It aims to prevent layout shifts, particularly in cases where components are being downloaded and rendered by the browser.
The concept of preventing Cumulative Layout Shift (CLS) is discussed in the article linked: Cumulative Layout Shift. It explains the importance of avoiding layout shifts to improve user experience.
You can see a live demonstration of the React aspect ratio placeholder implementation at this link: Demo.
The implementation was inspired by Thierry Koblentz, a recognized expert in web development and design.
The original idea for this implementation came from Sérgio Gomes.
For more in-depth information on aspect ratio boxes and related techniques, you can refer to the detailed post by Chris Coyier, a prominent figure in the web development community. The post can be found here: Post. Chris Coyier's Twitter profile is also linked for further reference: Chris Coyier.
The most common use case for image loading is when you include an image tag without specifying its dimensions. In such cases, the browser will assume a square size for the image before it is fully loaded, which can result in a layout shift or reflow once the image is loaded.
To prevent layout shifts, it is recommended to define the dimensions of the image explicitly. However, it's important to note that if you hardcode fixed dimensions, it may not adapt well to responsive designs where the layout needs to adjust dynamically based on different screen sizes and orientations.
This library employs a clever technique to create space based on the aspect ratio of an element. It utilizes a pseudo-element to achieve this effect, taking advantage of the aspect-ratio
property available in browsers like Chromium 88, Firefox 87, and Safari Technology Preview 118.
For browsers that do not support the aspect-ratio
property, the library employs a technique commonly known as the "Padding trick." It creates a wrapper HTML tag with a height of zero and applies a percentage value to the padding-bottom property to preserve space. The padding-bottom value is calculated as a percentage of the component's width.
To ensure compatibility and maintain a clean codebase, the library also utilizes CSS variable, available in modern browsers, and the CSS calc
API. These features help minimize the amount of styling required for different padding values, resulting in a more efficient implementation.
We replies on CSS custom property and CSS calc
function.
via yarn
$ yarn add react-aspect-ratio
or via npm
$ npm install react-aspect-ratio
Props | Type | Default | Description |
---|---|---|---|
ratio | string/number | 1 | Aspect ratio of your component, could be number or string like width/height |
other props | Object | {style: {--aspect-ratio: ${ratio}} } | Any props to your React component, the library will add --aspect-ratio to your style object |
children | React Element | Single DOM element |
You will need to import 'react-aspect-ratio/aspect-ratio.css'
import { AspectRatio } from 'react-aspect-ratio'; // Recommended: if you are using React > 15.6
import AspectRatio from 'react-aspect-ratio'; // Deprecated: if you are using React <= 15.6
import { AspectRatio } from 'react-aspect-ratio';
const RatioImage = () => (
<AspectRatio ratio="3/4" style={{ maxWidth: '400px' }}>
<img src="https://c1.staticflickr.com/4/3896/14550191836_cc0675d906.jpg" />
</AspectRatio>
);
import { AspectRatio } from 'react-aspect-ratio';
const RatioIframe = () => (
<AspectRatio ratio="560/315" style={{ maxWidth: '560px' }}>
<iframe src="https://www.youtube.com/embed/Bku71V5f66g" frameBorder="0" allowFullScreen />
</AspectRatio>
);
Can also use for background image
import { AspectRatio } from 'react-aspect-ratio';
<AspectRatio
ratio={0.75}
style={{
maxWidth: '300px',
backgroundImage: 'url(https://c1.staticflickr.com/4/3896/14550191836_cc0675d906.jpg)',
backgroundSize: 'cover'
}}
/>;
[style*="--aspect-ratio"] > :first-child {
width: 100%;
}
[style*="--aspect-ratio"] > img {
height: auto;
}
[style*="--aspect-ratio"] {
position: relative;
}
[style*="--aspect-ratio"] > :first-child {
position: absolute;
top: 0;
left: 0;
height: 100%;
}
[style*="--aspect-ratio"]::before {
content: "";
display: block;
}
@supports not (aspect-ratio: 1/1) {
[style*="--aspect-ratio"]::before {
height: 0;
padding-bottom: calc(100% / (var(--aspect-ratio)));
}
}
@supports (aspect-ratio: 1/1) {
[style*="--aspect-ratio"]::before {
aspect-ratio: calc(var(--aspect-ratio));
}
}
[style*="--aspect-ratio"]
as a hook to target the appropriate boxesaspect-ratio
property if browser supportedaspect-ratio
calc()
and var()
to calculate padding based on the value of the custom propertyFAQs
React Component to maintain a consistent width-to-height ratio (aspect ratio), preventing cumulative layout shift.
We found that react-aspect-ratio demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.