1
+ /*
2
+ Create an immortal DOM node. This is a way to render HTML that stays stable
3
+ irregardless of it being unmounted/remounted.
4
+ This supports virtualization, window splitting, etc., without loss of state.
5
+ */
6
+
1
7
import { useCallback , useEffect , useRef } from "react" ;
2
8
import $ from "jquery" ;
3
9
4
- // This is just an initial default height; the actual height of the iframe should
10
+ // This is just an initial default height; the actual height of the should
5
11
// resize to the content.
6
- const HEIGHT = "70vh " ;
12
+ const HEIGHT = "50vh " ;
7
13
8
14
interface Props {
9
- key : string ;
15
+ globalKey : string ;
10
16
html : string ;
17
+ zIndex ?: number ;
11
18
}
12
19
13
- const immortals : { [ key : string ] : any } = { } ;
20
+ const immortals : { [ globalKey : string ] : any } = { } ;
21
+
22
+ const Z_INDEX = 1 ;
14
23
15
- export default function Immortal ( { key, html } : Props ) {
24
+ export default function ImmortalDomNode ( {
25
+ globalKey,
26
+ html,
27
+ zIndex = Z_INDEX , // todo: support changing?
28
+ } : Props ) {
16
29
const divRef = useRef < any > ( null ) ;
17
30
const eltRef = useRef < any > ( null ) ;
18
31
const intervalRef = useRef < any > ( null ) ;
@@ -45,17 +58,18 @@ export default function Immortal({ key, html }: Props) {
45
58
return ;
46
59
}
47
60
let elt ;
48
- if ( immortals [ key ] == null ) {
49
- elt = immortals [ key ] = $ (
50
- `<div style="border:0;overflow:hidden;width:100%;height:${ HEIGHT } ;position:absolute;left:130px"/>${ html } </div>` ,
61
+ if ( immortals [ globalKey ] == null ) {
62
+ elt = immortals [ globalKey ] = $ (
63
+ `<div id=" ${ globalKey } " style="border:0;overflow:hidden;width:100%;height:${ HEIGHT } ;position:absolute;left:130px;z-index: ${ zIndex } "/>${ html } </div>` ,
51
64
) ;
52
65
$ ( "body" ) . append ( elt ) ;
53
66
} else {
54
- elt = immortals [ key ] ;
67
+ elt = immortals [ globalKey ] ;
55
68
elt . show ( ) ;
56
69
}
57
70
eltRef . current = elt [ 0 ] ;
58
- intervalRef . current = setInterval ( position , 500 ) ;
71
+ intervalRef . current = setInterval ( position , 1000 ) ;
72
+ position ( ) ;
59
73
60
74
return ( ) => {
61
75
// unmounting so hide
@@ -66,5 +80,10 @@ export default function Immortal({ key, html }: Props) {
66
80
} ;
67
81
} , [ ] ) ;
68
82
69
- return < div ref = { divRef } /> ;
83
+ return (
84
+ < div
85
+ ref = { divRef }
86
+ style = { { border : "1px solid black" , height : HEIGHT } }
87
+ > </ div >
88
+ ) ;
70
89
}
0 commit comments