roderickhsiao / react-aspect-ratio

Preserve space for your element to prevent browser reflow (layout shift)
https://roderickhsiao.github.io/react-aspect-ratio/
MIT License
103 stars 7 forks source link

Not working at all? #67

Closed ignacio-dev closed 2 years ago

ignacio-dev commented 2 years ago

The following code won't work:

import { AspectRatio } from 'react-aspect-ratio';

<AspectRatio
    ratio="4/5"
    style={{
        maxWidth: '600px',
        backgroundImage: 'url(https://c1.staticflickr.com/4/3896/14550191836_cc0675d906.jpg)',
        backgroundSize: 'cover'
    }}
>...</AspectRatio>

This is the resulting output:

<div
    class="react-aspect-ratio-placeholder"
    style="max-width: 600px; background-image: url("https://c1.staticflickr.com/4/3896/14550191836_cc0675d906.jpg";);background-size: cover; --aspect-ratio:(0.8);"
>
   ...
</div>

But it just gets render like a regular div...

None of the examples provided work either! The package is properly installed. What am I missing?

roderickhsiao commented 2 years ago

did you install the CSS file? https://github.com/roderickhsiao/react-aspect-ratio#css-inspired-by-thierry

https://github.com/roderickhsiao/react-aspect-ratio/blob/main/aspect-ratio.css

roderickhsiao commented 2 years ago

for background image, AspectRatio should be your container of the background image div

https://roderickhsiao.github.io/react-aspect-ratio/?path=/story/aspect-ratio--background-image

import { AspectRatio } from 'react-aspect-ratio';

<AspectRatio
    ratio="4/5"
    style={{
        maxWidth: '600px',
    }}
>
  <div 
    style={{ 
      backgroundImage: 'url(https://c1.staticflickr.com/4/3896/14550191836_cc0675d906.jpg)',
      backgroundSize: 'cover'
    }}
  />
</AspectRatio>
ignacio-dev commented 2 years ago

Importing the CSS file solved the issue:

import 'react-aspect-ratio/aspect-ratio.css';

Thank you!

roderickhsiao commented 2 years ago

Awesome!