bitburner-src/src/ui/React/Popup.tsx

33 lines
845 B
TypeScript
Raw Normal View History

/**
* React component for a popup content container
*
* Takes in a prop for rendering the content inside the popup
*/
import React, { useEffect } from "react";
2021-06-14 21:42:38 +02:00
interface IProps<T> {
content: (props: T) => React.ReactElement;
id: string;
2021-06-14 21:42:38 +02:00
props: T;
removePopup: (id: string) => void;
}
2021-06-14 21:42:38 +02:00
export function Popup<T>(props: IProps<T>): React.ReactElement {
function keyDown(event: KeyboardEvent): void {
if(event.key === 'Escape') props.removePopup(props.id);
}
useEffect(() => {
document.addEventListener('keydown', keyDown);
return () => {
document.removeEventListener('keydown', keyDown);
}
});
return (
<div className={"popup-box-content"} id={`${props.id}-content`}>
{React.createElement(props.content, props.props)}
</div>
)
}