Skip to content

Instantly share code, notes, and snippets.

@alexdiliberto
Created April 3, 2020 17:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexdiliberto/d3f9a41e0f25aaa5199741be4705ce3b to your computer and use it in GitHub Desktop.
Save alexdiliberto/d3f9a41e0f25aaa5199741be4705ce3b to your computer and use it in GitHub Desktop.
image aspect ratio component
<div class="relative" style={{this.style}}>
<div class="absolute object-cover w-full h-full overflow-hidden">
{{yield}}
</div>
</div>
// References:
// https://tailwindcss.com/course/locking-images-to-a-fixed-aspect-ratio/#app
// https://github.com/embermap/emberconf2020-tailwind-css-best-practices
import Component from "@glimmer/component";
import { htmlSafe } from "@ember/string";
export default class AspectRatioComponent extends Component {
get style() {
let paddingBottom = this.args.ratio
.split(":")
.map(str => parseInt(str, 10))
.reduce((prev, curr) => curr / prev);
return htmlSafe(`padding-bottom: ${paddingBottom * 100}%`);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment