]> cat aescling's git repositories - mastodon.git/commitdiff
Dynamically calculate card height for embeds instead of padding (#5265)
authorEugen Rochko <eugen@zeonfederated.com>
Sun, 8 Oct 2017 00:34:49 +0000 (02:34 +0200)
committerGitHub <noreply@github.com>
Sun, 8 Oct 2017 00:34:49 +0000 (02:34 +0200)
The padding trick was hard-coded to a 16:9 ratio, but we can use
width and height provided from OEmbed information and width
of the card itself to calculate a new height

app/javascript/mastodon/features/status/components/card.js
app/javascript/styles/components.scss

index 41c4300d32163965fe9f536e05945ce8ffbcca05..bb83374b9b36d8d9a29219b4847e607f0bbe77a0 100644 (file)
@@ -30,6 +30,10 @@ export default class Card extends React.PureComponent {
     maxDescription: 50,
   };
 
+  state = {
+    width: 0,
+  };
+
   renderLink () {
     const { card, maxDescription } = this.props;
 
@@ -75,14 +79,25 @@ export default class Card extends React.PureComponent {
     );
   }
 
+  setRef = c => {
+    if (c) {
+      this.setState({ width: c.offsetWidth });
+    }
+  }
+
   renderVideo () {
-    const { card } = this.props;
-    const content  = { __html: card.get('html') };
+    const { card }  = this.props;
+    const content   = { __html: card.get('html') };
+    const { width } = this.state;
+    const ratio     = card.get('width') / card.get('height');
+    const height    = card.get('width') > card.get('height') ? (width / ratio) : (width * ratio);
 
     return (
       <div
+        ref={this.setRef}
         className='status-card-video'
         dangerouslySetInnerHTML={content}
+        style={{ height }}
       />
     );
   }
index d578e99307ea7ec949c3c2bb9cae61bb247c1735..b6da70c913153eb4684c3fae41ab9d0c1b944417 100644 (file)
@@ -2155,22 +2155,9 @@ button.icon-button.active i.fa-retweet {
 }
 
 .status-card-video {
-  position: relative;
-  width: 100%;
-  height: auto;
-  padding-top: 56.25%;
-
   iframe {
-    position: absolute;
-    top: 0;
-    left: 0;
-    bottom: 0;
-    right: 0;
-    width: 1px;
-    min-width: 100%;
-    height: 1px;
-    min-height: 100%;
-    margin: auto;
+    width: 100%;
+    height: 100%;
   }
 }