Skip to content

Commit f61b1b5

Browse files
committed
feat: re-add Portal + index.ts
1 parent 963f351 commit f61b1b5

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

src/Portal.tsx

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import * as React from 'react';
2+
import * as ReactDOM from 'react-dom';
3+
import { ThemeConsumer } from 'styled-components';
4+
5+
export interface IPortalProps {
6+
children: any;
7+
className?: string;
8+
}
9+
10+
export class Portal extends React.PureComponent<IPortalProps> {
11+
private readonly el?: HTMLDivElement;
12+
private readonly root = typeof document === 'object' ? document.body : null;
13+
14+
constructor(props: IPortalProps) {
15+
super(props);
16+
17+
if (typeof document === 'undefined' || typeof document.createElement !== 'function') return;
18+
19+
this.el = document.createElement('div');
20+
21+
if (props.className !== undefined) {
22+
this.el.className = props.className;
23+
}
24+
}
25+
26+
public componentDidMount() {
27+
if (this.el !== undefined && this.root !== null) {
28+
this.root.appendChild(this.el);
29+
}
30+
}
31+
32+
public componentWillUnmount() {
33+
if (this.el !== undefined && this.root !== null) {
34+
this.root.removeChild(this.el);
35+
}
36+
}
37+
38+
private renderChildren = () => {
39+
const { children } = this.props;
40+
41+
if (typeof children === 'function') {
42+
return children();
43+
}
44+
45+
return React.Children.map(this.props.children, child => React.cloneElement(child));
46+
};
47+
48+
public render() {
49+
if (this.el === undefined) {
50+
return null;
51+
}
52+
53+
return ReactDOM.createPortal(<ThemeConsumer>{this.renderChildren}</ThemeConsumer>, this.el);
54+
}
55+
}

src/index.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* NOTE: any components with meaningful external dependencies should NOT be exported here.
3+
* Library users will need to `import { Select } from '@stoplight/ui-kit/select` them specifically.
4+
*/
5+
6+
// export * from './BlockQuote';
7+
export * from './Break';
8+
export * from './Button';
9+
// export * from './ContextMenu';
10+
export * from './Flex';
11+
export * from './Heading';
12+
export * from './Icon';
13+
export * from './Image';
14+
// export * from './List';
15+
export * from './Link';
16+
// export * from './Popup';
17+
export * from './Portal';
18+
// export * from './Mark';
19+
// export * from './Menu';
20+
// export * from './Table';
21+
export * from './Text';
22+
// export * from './Toggle';
23+
export * from './theme/types';

0 commit comments

Comments
 (0)