Skip to content

Commit

Permalink
* modal: support for setting class to body of iframe.
Browse files Browse the repository at this point in the history
  • Loading branch information
catouse committed Jun 20, 2024
1 parent b929568 commit 6ca85d9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
1 change: 1 addition & 0 deletions lib/modal/src/component/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './modal-dialog';
export * from './modal-iframe-content';
32 changes: 26 additions & 6 deletions lib/modal/src/component/modal-iframe-content.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {Component, createRef} from 'preact';
export type ModalIframeContentProps = {
url: string;
watchHeight?: boolean;
iframeBodyClass?: string;
};

export type ModalIframeContentState = {
Expand All @@ -20,6 +21,10 @@ export class ModalIframeContent extends Component<ModalIframeContentProps> {

state: ModalIframeContentState = {};

get iframeDoc() {
return this._ref.current?.contentWindow?.document;
}

componentDidMount() {
if (this.props.watchHeight) {
this._watchIframeHeight();
Expand All @@ -30,12 +35,11 @@ export class ModalIframeContent extends Component<ModalIframeContentProps> {
this._rob?.disconnect();
}

_watchIframeHeight = () => {
const iframeDoc = this._ref.current?.contentWindow?.document;
_watchIframeHeight() {
const iframeDoc = this.iframeDoc;
if (!iframeDoc) {
return;
}

let rob = this._rob;
rob?.disconnect();
rob = new ResizeObserver(() => {
Expand All @@ -47,17 +51,33 @@ export class ModalIframeContent extends Component<ModalIframeContentProps> {
rob.observe(iframeDoc.body);
rob.observe(iframeDoc.documentElement);
this._rob = rob;
}

_handleIframeLoad = () => {
const iframeDoc = this.iframeDoc;
if (!iframeDoc) {
return;
}

const {iframeBodyClass, watchHeight} = this.props;

if (watchHeight) {
this._watchIframeHeight();
}

if (iframeBodyClass) {
iframeDoc.body.classList.add(iframeBodyClass);
}
};

render() {
const {url, watchHeight} = this.props;
return (
<iframe
className="modal-iframe"
style={this.state}
src={url}
src={this.props.url}
ref={this._ref}
onLoad={watchHeight ? this._watchIframeHeight : undefined}
onLoad={this._handleIframeLoad}
/>
);
}
Expand Down

0 comments on commit 6ca85d9

Please sign in to comment.