-
Notifications
You must be signed in to change notification settings - Fork 842
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Theo
committed
Apr 19, 2019
1 parent
c6fda89
commit 224c98a
Showing
6 changed files
with
65 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { EuiOverlayMask } from './overlay_mask'; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!); | ||
} | ||
} |