-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.tsx
62 lines (52 loc) · 1.22 KB
/
index.tsx
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
import { HTMLProps, PureComponent } from 'react';
import { createPortal } from 'react-dom';
export default class Portal extends PureComponent<IProps> {
/**
* Children wrapper element
*/
protected element: HTMLElement;
/**
* @param {IProps} props
*/
public constructor(props: IProps) {
super(props);
this.element = this.props.element || document.createElement('span');
this.element.setAttribute('style', 'display:contents');
}
/**
* @inheritdoc
*/
public componentDidMount() {
this.props.to.appendChild(this.element);
}
/**
* @inheritdoc
*/
public componentWillUnmount() {
this.props.to.removeChild(this.element);
}
/**
* @inheritdoc
*/
public render() {
const that = this;
return createPortal(
that.props.children,
that.element,
);
}
/**
* Gets portal element
*
* @return HTMLElement
*/
public getElement() {
return this.element;
}
}
interface IProps extends HTMLProps<Portal> {
// parent element to append portal to
to: HTMLElement,
// portal contents wrapper element
element?: HTMLElement,
}