Skip to content

Instantly share code, notes, and snippets.

@ondrasak
Created March 29, 2020 12:56
Show Gist options
  • Select an option

  • Save ondrasak/05b338b34b2848bbbfbb9fbafa0d180c to your computer and use it in GitHub Desktop.

Select an option

Save ondrasak/05b338b34b2848bbbfbb9fbafa0d180c to your computer and use it in GitHub Desktop.
import {fullscreenChangeEvents} from 'shared/utils/fullscreen';
type IframeResizerOptions = {
iframeElement: HTMLElement,
containerElement: HTMLElement
};
export class IframeResizer {
private originalWidth: number;
private originalHeight: number;
private originalRatio: number;
constructor(private options: IframeResizerOptions) {
// TODO: if height/width is not defined by element attr -> pick real measurement directly from DOM
this.originalWidth = Number(options.iframeElement.getAttribute('width'));
this.originalHeight = Number(options.iframeElement.getAttribute('height'));
this.originalRatio = this.originalWidth / this.originalHeight;
this.resize();
// Register listeners
window.addEventListener('orientationchange', this.onScreenChange);
window.addEventListener('resize', this.onScreenChange);
$(document).on(fullscreenChangeEvents.join(' '), this.onScreenChange);
}
onScreenChange = () => {
this.resize();
}
resize() {
const containerWidth = this.options.containerElement.clientWidth;
this.options.iframeElement.setAttribute('width', `${containerWidth}px`);
this.options.iframeElement.setAttribute('height', `${containerWidth / this.originalRatio}px`);
}
destroy() {
// Unregister listeners
window.removeEventListener('orientationchange', this.onScreenChange);
window.removeEventListener('resize', this.onScreenChange);
$(document).off(fullscreenChangeEvents.join(' '), this.onScreenChange);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment