Skip to content

Commit

Permalink
feat: re-add Portal + index.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
P0lip committed Dec 6, 2018
1 parent 963f351 commit f61b1b5
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
55 changes: 55 additions & 0 deletions src/Portal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { ThemeConsumer } from 'styled-components';

export interface IPortalProps {
children: any;
className?: string;
}

export class Portal extends React.PureComponent<IPortalProps> {
private readonly el?: HTMLDivElement;
private readonly root = typeof document === 'object' ? document.body : null;

constructor(props: IPortalProps) {
super(props);

if (typeof document === 'undefined' || typeof document.createElement !== 'function') return;

this.el = document.createElement('div');

if (props.className !== undefined) {
this.el.className = props.className;
}
}

public componentDidMount() {
if (this.el !== undefined && this.root !== null) {
this.root.appendChild(this.el);
}
}

public componentWillUnmount() {
if (this.el !== undefined && this.root !== null) {
this.root.removeChild(this.el);
}
}

private renderChildren = () => {
const { children } = this.props;

if (typeof children === 'function') {
return children();
}

return React.Children.map(this.props.children, child => React.cloneElement(child));
};

public render() {
if (this.el === undefined) {
return null;
}

return ReactDOM.createPortal(<ThemeConsumer>{this.renderChildren}</ThemeConsumer>, this.el);
}
}
23 changes: 23 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* NOTE: any components with meaningful external dependencies should NOT be exported here.
* Library users will need to `import { Select } from '@stoplight/ui-kit/select` them specifically.
*/

// export * from './BlockQuote';
export * from './Break';
export * from './Button';
// export * from './ContextMenu';
export * from './Flex';
export * from './Heading';
export * from './Icon';
export * from './Image';
// export * from './List';
export * from './Link';
// export * from './Popup';
export * from './Portal';
// export * from './Mark';
// export * from './Menu';
// export * from './Table';
export * from './Text';
// export * from './Toggle';
export * from './theme/types';

0 comments on commit f61b1b5

Please sign in to comment.