-
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.
EuiOverlayMask
converted to TS (#1858)
* `EuiOverlayMask` converted to TS * `EuiOverlayMask` updated Changelog * `EuiOverlayMask` code review fixes
- Loading branch information
1 parent
3844645
commit 2d0ac3b
Showing
7 changed files
with
66 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 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, keysOf, Omit } from '../common'; | ||
|
||
export interface EuiOverlayMaskProps { | ||
onClick?: () => void; | ||
} | ||
|
||
export type Props = CommonProps & | ||
Omit< | ||
Partial<Record<keyof HTMLAttributes<HTMLDivElement>, string>>, | ||
keyof EuiOverlayMaskProps | ||
> & | ||
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); | ||
} | ||
keysOf(rest).forEach(key => { | ||
if (typeof rest[key] !== 'string') { | ||
throw new Error( | ||
`Unhandled property type. EuiOverlayMask property ${key} is not a string.` | ||
); | ||
} | ||
this.overlayMaskNode!.setAttribute(key, rest[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!); | ||
} | ||
} |