{"id":13725394,"url":"https://github.com/roderickhsiao/react-aspect-ratio","last_synced_at":"2025-04-06T22:09:10.541Z","repository":{"id":18635280,"uuid":"84791928","full_name":"roderickhsiao/react-aspect-ratio","owner":"roderickhsiao","description":"Preserve space for your element to prevent browser reflow (layout shift)","archived":false,"fork":false,"pushed_at":"2024-01-10T12:36:06.000Z","size":4480,"stargazers_count":101,"open_issues_count":0,"forks_count":7,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-01-29T23:52:16.953Z","etag":null,"topics":["aspect-ratio","browser-reflow","css","layout","layout-shift","react","responsive","ui-components"],"latest_commit_sha":null,"homepage":"https://roderickhsiao.github.io/react-aspect-ratio/","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/roderickhsiao.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2017-03-13T06:33:28.000Z","updated_at":"2024-02-04T10:58:01.911Z","dependencies_parsed_at":"2024-02-04T10:57:46.773Z","dependency_job_id":"e81b9889-4991-4174-9efe-e5a2ccaee474","html_url":"https://github.com/roderickhsiao/react-aspect-ratio","commit_stats":{"total_commits":155,"total_committers":8,"mean_commits":19.375,"dds":0.4580645161290322,"last_synced_commit":"a52f99bc8923c75199c351aea3e6804cc0ba5955"},"previous_names":[],"tags_count":16,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/roderickhsiao%2Freact-aspect-ratio","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/roderickhsiao%2Freact-aspect-ratio/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/roderickhsiao%2Freact-aspect-ratio/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/roderickhsiao%2Freact-aspect-ratio/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/roderickhsiao","download_url":"https://codeload.github.com/roderickhsiao/react-aspect-ratio/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247557767,"owners_count":20958047,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["aspect-ratio","browser-reflow","css","layout","layout-shift","react","responsive","ui-components"],"created_at":"2024-08-03T01:02:21.656Z","updated_at":"2025-04-06T22:09:10.520Z","avatar_url":"https://github.com/roderickhsiao.png","language":"TypeScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\n    \u003ch1\u003eReact Aspect Ratio\u003c/h1\u003e\n  \u003cimg src=\"https://cloud.githubusercontent.com/assets/3906130/23882532/7e0cd586-081e-11e7-995f-005196385335.jpg\" width=\"400\" alt=\"reac aspect ratio\"\u003e\n  \u003cbr\u003e\n  \u003ca href=\"https://www.npmjs.org/package/react-aspect-ratio\"\u003e\u003cimg src=\"https://img.shields.io/npm/v/react-aspect-ratio.svg?style=flat\" alt=\"npm\"\u003e\u003c/a\u003e\n  \u003ca href=\"https://unpkg.com/react-aspect-ratio\"\u003e\u003cimg src=\"https://img.badgesize.io/https://unpkg.com/react-aspect-ratio/dist/es/index.js?compression=gzip\" alt=\"gzip size\"\u003e\u003c/a\u003e\n  \u003ca href=\"https://www.npmjs.com/package/react-aspect-ratio\"\u003e\u003cimg src=\"https://img.shields.io/npm/dt/react-aspect-ratio.svg\" alt=\"downloads\" \u003e\u003c/a\u003e\n\u003c/p\u003e\n\n\u003chr\u003e\n\nTiny 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.\n\n\nThe concept of preventing [Cumulative Layout Shift (CLS)](https://web.dev/cls/) is discussed in the article linked: Cumulative Layout Shift. It explains the importance of avoiding layout shifts to improve user experience.\n\nYou can see a live demonstration of the React aspect ratio placeholder implementation at this link: [Demo](https://roderickhsiao.github.io/react-aspect-ratio/).\n\nThe implementation was inspired by [Thierry Koblentz](https://www.linkedin.com/in/thierryk/), a recognized expert in web development and design.\n\nThe original idea for this implementation came from [Sérgio Gomes](https://twitter.com/sergiomdgomes).\n\nFor 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](https://css-tricks.com/aspect-ratio-boxes/). Chris Coyier's Twitter profile is also linked for further reference: [Chris Coyier](https://twitter.com/chriscoyier).\n\n## Why\n\nThe 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.\n\nTo 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.\n\n## How\n\nThis 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`](https://developer.mozilla.org/en-US/docs/Web/CSS/aspect-ratio) property available in browsers like Chromium 88, Firefox 87, and Safari Technology Preview 118.\n\nFor 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.\n\nTo ensure compatibility and maintain a clean codebase, the library also utilizes [CSS variable](https://developer.mozilla.org/en-US/docs/Web/CSS/Using_CSS_variables), available in modern browsers, and the CSS `calc` [API](https://developer.mozilla.org/en-US/docs/Web/CSS/calc). These features help minimize the amount of styling required for different padding values, resulting in a more efficient implementation.\n\n## Browser Support\n\nWe replies on [CSS custom property](https://caniuse.com/css-variables) and CSS `calc` [function](https://caniuse.com/?search=calc).\n\n## Installation\n\nvia [yarn](https://yarnpkg.com/en/docs)\n\n```\n$ yarn add react-aspect-ratio\n```\n\nor via [npm](https://docs.npmjs.com/)\n\n```\n$ npm install react-aspect-ratio\n```\n\n## Usage\n\n### Props\n\n| Props       | Type          | Default                                     | Description                                                                                   |\n|-------------|---------------|---------------------------------------------|-----------------------------------------------------------------------------------------------|\n| ratio       | string/number | 1                                           | Aspect ratio of your component, could be number or string like width/height                   |\n| other props | Object        | ```{style: {--aspect-ratio: ${ratio}} }``` | Any props to your React component, the library will add `--aspect-ratio` to your style object |\n| children    | React Element |                                             | Single DOM element                                                                            |\n\n**You will need to `import 'react-aspect-ratio/aspect-ratio.css'`**\n\n* Note\n```js\nimport { AspectRatio } from 'react-aspect-ratio'; // Recommended: if you are using React \u003e 15.6\n\nimport AspectRatio from 'react-aspect-ratio'; // Deprecated: if you are using React \u003c= 15.6\n```\n\n```js\nimport { AspectRatio } from 'react-aspect-ratio';\n\nconst RatioImage = () =\u003e (\n  \u003cAspectRatio ratio=\"3/4\" style={{ maxWidth: '400px' }}\u003e\n    \u003cimg src=\"https://c1.staticflickr.com/4/3896/14550191836_cc0675d906.jpg\" /\u003e\n  \u003c/AspectRatio\u003e\n);\n```\n\n```js\nimport { AspectRatio } from 'react-aspect-ratio';\n\nconst RatioIframe = () =\u003e (\n  \u003cAspectRatio ratio=\"560/315\" style={{ maxWidth: '560px' }}\u003e\n    \u003ciframe src=\"https://www.youtube.com/embed/Bku71V5f66g\" frameBorder=\"0\" allowFullScreen /\u003e\n  \u003c/AspectRatio\u003e\n);\n```\n\nCan also use for background image\n\n```js\nimport { AspectRatio } from 'react-aspect-ratio';\n\n\u003cAspectRatio\n  ratio={0.75}\n  style={{\n    maxWidth: '300px',\n    backgroundImage: 'url(https://c1.staticflickr.com/4/3896/14550191836_cc0675d906.jpg)',\n    backgroundSize: 'cover'\n  }}\n/\u003e;\n```\n\n### CSS (Inspired by Thierry)\n\n```css\n[style*=\"--aspect-ratio\"] \u003e :first-child {\n  width: 100%;\n}\n\n[style*=\"--aspect-ratio\"] \u003e img {\n  height: auto;\n}\n\n[style*=\"--aspect-ratio\"] {\n  position: relative;\n}\n\n[style*=\"--aspect-ratio\"] \u003e :first-child {\n  position: absolute;\n  top: 0;\n  left: 0;\n  height: 100%;\n}\n\n[style*=\"--aspect-ratio\"]::before {\n  content: \"\";\n  display: block;\n}\n\n@supports not (aspect-ratio: 1/1) {\n  [style*=\"--aspect-ratio\"]::before {\n    height: 0;\n    padding-bottom: calc(100% / (var(--aspect-ratio)));\n  }\n}\n@supports (aspect-ratio: 1/1) {\n  [style*=\"--aspect-ratio\"]::before {\n    aspect-ratio: calc(var(--aspect-ratio));\n  }\n}\n```\n\n- We use `[style*=\"--aspect-ratio\"]` as a hook to target the appropriate boxes\n- We stretch the inner box regardless of support for custom property\n- We make sure the height of images comes from their intrinsic ratio rather than their height attribute\n- We style the container as a containing block (so the inner box references that ancestor for its positioning)\n- We create a pseudo-element to be used with\n  - native `aspect-ratio` property if browser supported\n  - the “padding hack” (it is that element that creates the aspect ratio) for browser not supporting `aspect-ratio`\n- We use `calc()` and `var()` to calculate padding based on the value of the custom property\n- We style the inner box so it matches the dimensions of its containing block\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Froderickhsiao%2Freact-aspect-ratio","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Froderickhsiao%2Freact-aspect-ratio","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Froderickhsiao%2Freact-aspect-ratio/lists"}