Skip to content

Commit c82b586

Browse files
committed
DBC22-4645: resize YouTube videos keeping aspect ratio
1 parent 834075b commit c82b586

2 files changed

Lines changed: 41 additions & 11 deletions

File tree

src/frontend/src/App.scss

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,23 @@ body,
6868
.responsive-object {
6969
position: static;
7070
padding-bottom: 2rem !important;
71-
}
7271

73-
.responsive-object iframe,
74-
.responsive-object img,
75-
.responsive-object object,
76-
.responsive-object embed {
77-
position: static;
78-
top: 0;
79-
left: 0;
80-
width: 100%;
81-
height: 100%;
82-
margin: 0;
72+
display: flex;
73+
align-items: center;
74+
justify-content: center;
75+
76+
iframe, img, object, embed {
77+
position: static;
78+
top: 0;
79+
left: 0;
80+
width: 100%;
81+
height: 100%;
82+
margin: 0;
83+
84+
@media screen and (min-width: 768px) {
85+
width: 60%;
86+
}
87+
}
8388
}
8489

8590
img {

src/frontend/src/Components/shared/renderWagtailBody.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,31 @@ function replace(domNode) {
1414
document.head.appendChild(script);
1515
}
1616

17+
/* DBC22-4645
18+
* Resize YouTube videos based on screen size while keeping aspect ratio
19+
*/
20+
if (domNode.name === 'iframe') {
21+
const width = parseInt(domNode.attribs.width, 10);
22+
const height = parseInt(domNode.attribs.height, 10);
23+
24+
if (width && height && !isNaN(width) && !isNaN(height)) {
25+
const aspectRatio = width / height;
26+
27+
// Copy all original attributes
28+
const props = {...domNode.attribs};
29+
30+
// Set the style object
31+
props.style = { aspectRatio: aspectRatio };
32+
33+
// Return a React iframe element with all props
34+
return (
35+
<iframe {...props}>
36+
{domToReact(domNode.children, { replace })}
37+
</iframe>
38+
);
39+
}
40+
}
41+
1742
/* DBC22-3141
1843
* Part of implementing subpages for advisories and bulletins: links coming
1944
* from cms need to be converted to NavLinks so they participate properly in

0 commit comments

Comments
 (0)