-
Notifications
You must be signed in to change notification settings - Fork 701
/
Copy pathLayoutIframeTab.jsx
77 lines (66 loc) · 2.62 KB
/
LayoutIframeTab.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/////////////////////////////////////////////////////////////
//
// pgAdmin 4 - PostgreSQL Tools
//
// Copyright (C) 2013 - 2025, The pgAdmin Development Team
// This software is released under the PostgreSQL Licence
//
//////////////////////////////////////////////////////////////
import { Portal } from '@mui/material';
import React, { useEffect, useRef, useState } from 'react';
import Frame from 'react-frame-component';
import PropTypes from 'prop-types';
import CustomPropTypes from '../../custom_prop_types';
export default function LayoutIframeTab({target, src, children}) {
const selfRef = useRef();
const [iframeTarget, setIframeTarget] = useState();
useEffect(()=>{
if (!selfRef.current || !iframeTarget) return;
let lastKnownPosition = null;
const updatePositionAndSize = () => {
if (!selfRef.current) return;
const rect = selfRef.current.getBoundingClientRect();
rect.visibility = selfRef.current.closest('#'+target).style.visibility;
// Only update the iframe's position if the position has actually changed
if (
!lastKnownPosition ||
rect.top !== lastKnownPosition.top ||
rect.left !== lastKnownPosition.left ||
rect.width !== lastKnownPosition.width ||
rect.height !== lastKnownPosition.height ||
rect.visibility !== lastKnownPosition.visibility
) {
iframeTarget.style.position = 'fixed'; // You can adjust this if needed
iframeTarget.style.top = `${rect.top}px`;
iframeTarget.style.left = `${rect.left}px`;
iframeTarget.style.width = `${rect.width}px`;
iframeTarget.style.height = `${rect.height}px`;
iframeTarget.style.display = rect.visibility == 'hidden' ? 'none' : '';
lastKnownPosition = rect;
}
requestAnimationFrame(updatePositionAndSize); // Schedule the next check
};
updatePositionAndSize(); // initial update
return () => {
cancelAnimationFrame(updatePositionAndSize);
};
}, [iframeTarget]);
return (
<div ref={selfRef} data-target={target} style={{width: '100%', height: '100%'}}>
<Portal ref={(r)=>{
if(r) setIframeTarget(r.querySelector('#'+target));
}} container={document.querySelector('#layout-portal')}>
{src ?
<iframe src={src} title=" " id={target} style={{position: 'fixed', border: 0, zIndex: 1}} />:
<Frame src={src} id={target} style={{position: 'fixed', border: 0, zIndex: 1}}>
{children}
</Frame>
}
</Portal>
</div>);
}
LayoutIframeTab.propTypes = {
target: PropTypes.string,
src: PropTypes.string,
children: CustomPropTypes.children,
};