All Projects → roderickhsiao → react-aspect-ratio

roderickhsiao / react-aspect-ratio

Licence: MIT license
Preserve space for your element to prevent browser reflow (layout shift)

Programming Languages

typescript
32286 projects
CSS
56736 projects
javascript
184084 projects - #8 most used programming language
shell
77523 projects
HTML
75241 projects

Projects that are alternatives of or similar to react-aspect-ratio

bootstrap-grid-ms
Missing grid range in Bootstrap 3, micro-small from 480-767px.
Stars: ✭ 34 (-63.83%)
Mutual labels:  responsive, layout
Styled System
⬢ Style props for rapid UI development
Stars: ✭ 7,126 (+7480.85%)
Mutual labels:  responsive, layout
Re Flex
Resizable Flex layout container components for advanced React web applications
Stars: ✭ 349 (+271.28%)
Mutual labels:  responsive, layout
Greedo Layout For Android
Full aspect ratio grid LayoutManager for Android's RecyclerView
Stars: ✭ 1,588 (+1589.36%)
Mutual labels:  layout, aspect-ratio
Grid
This package has moved and renamed
Stars: ✭ 2,079 (+2111.7%)
Mutual labels:  responsive, layout
React Native Responsive Grid
Bringing the Web's Responsive Design to React Native
Stars: ✭ 369 (+292.55%)
Mutual labels:  responsive, layout
Flex Layout
Provides HTML UI layout for Angular applications; using Flexbox and a Responsive API
Stars: ✭ 5,705 (+5969.15%)
Mutual labels:  responsive, layout
Easygrid
EasyGrid - VanillaJS Responsive Grid
Stars: ✭ 77 (-18.09%)
Mutual labels:  responsive, layout
Shuffle
Categorize, sort, and filter a responsive grid of items
Stars: ✭ 2,170 (+2208.51%)
Mutual labels:  responsive, layout
React Native Flexbox Grid
Responsive Grid for React Native
Stars: ✭ 95 (+1.06%)
Mutual labels:  responsive, layout
griding
🧱 lean grid & responsive for react
Stars: ✭ 18 (-80.85%)
Mutual labels:  responsive, layout
react-super-styled
Responsive JSX layouts with Styled Components
Stars: ✭ 77 (-18.09%)
Mutual labels:  responsive, layout
react-native-bootstrap-styles
Bootstrap style library for React Native
Stars: ✭ 95 (+1.06%)
Mutual labels:  responsive, layout
mpv-scripts
dynamic-crop.lua script for mpv player/lib.
Stars: ✭ 43 (-54.26%)
Mutual labels:  aspect-ratio
GlowButton
Beautify your layouts with glowing buttons. Support with a ⭐️ Contributions are welcome! 🙌
Stars: ✭ 54 (-42.55%)
Mutual labels:  layout
react-matchmedia-connect
Higher order components for matchMedia
Stars: ✭ 49 (-47.87%)
Mutual labels:  responsive
dockview
Zero dependency layout manager and builder with ReactJS support
Stars: ✭ 45 (-52.13%)
Mutual labels:  layout
editorjs-layout
Layout block tool for Editor.js.
Stars: ✭ 45 (-52.13%)
Mutual labels:  layout
FillProgressLayout
A simple and flexible Fillable Progress Layout written in Kotlin
Stars: ✭ 77 (-18.09%)
Mutual labels:  layout
vue-responsive-text
↔ Vue component that scales its child node in relation to its parent node's width
Stars: ✭ 23 (-75.53%)
Mutual labels:  responsive

React Aspect Ratio

reac aspect ratio


npm gzip size downloads


This is a React implementation for aspect ratio placeholder preventing browser reflow before browser downloads and renders your component.

Cumulative Layout Shift

Demo

Inspired by Thierry Koblentz

Original idea from Sérgio Gomes

You can also read a detail post by Chris Coyier

Why

Most common use case is image loading. If you are not define dimensions for your image tag, browser will assume its a square size of image before image loaded. Hence you will see browser reflow your layout (layout shift) after image loaded.

If you define a hard dimensions, it might not fit a responsive design.

How

This library using a pseudo element to create space based on the aspect ratio. For browser supporting aspect-ratio property (Chromium 88, Firefox 87, and Safari Technology Preview 118), the style will be adopted to the pseudo element.

Other browsers will be using what people call "Padding trick" - creating a wrapper html tag with zero height and a percentage of padding-bottom to perserve space. (padding-bottom will be percentage of your component width).

This library also utilizes CSS variable for modern browser as well as CSS calc API to minimized the style needed for different padding value.

Browser Support

We replies on CSS custom property and CSS calc function.

Installation

via yarn

$ yarn add react-aspect-ratio

or via npm

$ npm install react-aspect-ratio

Usage

Props

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'

  • Note
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'
  }}
/>;

CSS (Inspired by Thierry)

[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));
  }
}
  • We use [style*="--aspect-ratio"] as a hook to target the appropriate boxes
  • We stretch the inner box regardless of support for custom property
  • We make sure the height of images comes from their intrinsic ratio rather than their height attribute
  • We style the container as a containing block (so the inner box references that ancestor for its positioning)
  • We create a pseudo-element to be used with
    • native aspect-ratio property if browser supported
    • the “padding hack” (it is that element that creates the aspect ratio) for browser not supporting aspect-ratio
  • We use calc() and var() to calculate padding based on the value of the custom property
  • We style the inner box so it matches the dimensions of its containing block
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].