Skip to content

Commit

Permalink
EuiOverlayMask converted to TS
Browse files Browse the repository at this point in the history
  • Loading branch information
Theo committed Apr 19, 2019
1 parent c6fda89 commit 224c98a
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 90 deletions.
1 change: 0 additions & 1 deletion src/components/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
/// <reference path="./modal/index.d.ts" />
/// <reference path="./observer/mutation_observer/index.d.ts" />
/// <reference path="./observer/resize_observer/index.d.ts" />
/// <reference path="./overlay_mask/index.d.ts" />
/// <reference path="./page/index.d.ts" />
/// <reference path="./pagination/index.d.ts" />
/// <reference path="./panel/index.d.ts" />
Expand Down
20 changes: 0 additions & 20 deletions src/components/overlay_mask/index.d.ts

This file was deleted.

3 changes: 0 additions & 3 deletions src/components/overlay_mask/index.js

This file was deleted.

1 change: 1 addition & 0 deletions src/components/overlay_mask/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { EuiOverlayMask } from './overlay_mask';
66 changes: 0 additions & 66 deletions src/components/overlay_mask/overlay_mask.js

This file was deleted.

64 changes: 64 additions & 0 deletions src/components/overlay_mask/overlay_mask.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* NOTE: We can't test this component because Enzyme doesn't support rendering
* into portals.
*/

import { Component, HTMLAttributes } from 'react';
import { createPortal } from 'react-dom';
import classNames from 'classnames';
import { CommonProps } from '../common';

export interface EuiOverlayMaskProps {
onClick?: () => void;
}

export type Props = CommonProps &
HTMLAttributes<HTMLDivElement> &
EuiOverlayMaskProps;

export class EuiOverlayMask extends Component<Props> {
private overlayMaskNode?: HTMLDivElement;

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

const { className, children, onClick, ...rest } = this.props;

this.overlayMaskNode = document.createElement('div');
this.overlayMaskNode.className = classNames('euiOverlayMask', className);
if (onClick) {
this.overlayMaskNode.addEventListener('click', onClick);
}
interface RestType {
[key: string]: any;
}
Object.keys(rest).forEach((key: string) => {
if (typeof (rest as RestType)[key] !== 'string') {
throw new Error(
`Unhandled property type. EuiOverlayMask property ${key} is not a string.`
);
}
this.overlayMaskNode!.setAttribute(key, (rest as RestType)[key]);
});

document.body.appendChild(this.overlayMaskNode);
}

componentDidMount() {
document.body.classList.add('euiBody-hasOverlayMask');
}

componentWillUnmount() {
document.body.classList.remove('euiBody-hasOverlayMask');

if (this.props.onClick) {
this.overlayMaskNode!.removeEventListener('click', this.props.onClick);
}
document.body.removeChild(this.overlayMaskNode!);
this.overlayMaskNode = undefined;
}

render() {
return createPortal(this.props.children, this.overlayMaskNode!);
}
}

0 comments on commit 224c98a

Please sign in to comment.