postcss-aspect-ratio-polyfill icon indicating copy to clipboard operation
postcss-aspect-ratio-polyfill copied to clipboard

[enhancement] Wrap polyfill in `@supports not`

Open kerberjg opened this issue 3 years ago • 3 comments

This polyfill unnecessarily removes and replaces aspect-ratio even in browsers where it is supported.

I suggest that the original aspect-ratio property is left intact, and the polyfill-generated code is wrapped in a @supports not instruction.

https://developer.mozilla.org/en-US/docs/Web/CSS/@supports

Input:

.aspect-ratio-box {
  aspect-ratio: 16 / 9;
}

Expected output:

.card-wide {
    aspect-ratio: 16 / 9;
}

@supports not (aspect-ratio: 1/1) {
  .aspect-ratio-box::before {
    float: left;
    padding-top: 56.25%;
    content: '';
  }
  .aspect-ratio-box::after {
    display: block;
    content: '';
    clear: both;
  }
}

Note: 1/1 is used only as a test value to check whether aspect-ratio is supported at all, it does not affect the resulting layout.

kerberjg avatar Nov 02 '21 11:11 kerberjg

But the problem with this are the browsers that don't support "@supports". https://caniuse.com/css-featurequeries

alike03 avatar Jun 10 '22 12:06 alike03

@alike03 So browsers from 7+ years ago? A small price to pay, considering that all modern browsers then get to keep the native version.

scherii avatar Jun 11 '22 14:06 scherii

While I agree that's a pretty fringe use case (just IE?), it wouldn't be so hard to support those browsers as well. That means

.aspect-ratio-box {
  aspect-ratio: 16 / 9;
}

becomes

.aspect-ratio-box::before {
  float: left;
  padding-top: 56.25%;
  content: '';
}
.aspect-ratio-box::after {
  display: block;
  content: '';
  clear: both;
}

@supports (aspect-ratio: 1/1) {
  .aspect-ratio-box {
      aspect-ratio: 16 / 9;
  }
  .aspect-ratio-box::before, .aspect-ratio-box::after {
    content: none;
  }
}

Are there any downsides to doing it this way? All I can think of is that it's three more lines of code. Wondering if it's worth updating #8...

dawaltconley avatar Oct 11 '22 22:10 dawaltconley