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

33 lines
792 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> {
2021-09-05 01:09:30 +02:00
content: (props: T) => React.ReactElement;
id: string;
props: T;
removePopup: (id: string) => void;
}
2021-06-14 21:42:38 +02:00
export function Popup<T>(props: IProps<T>): React.ReactElement {
2021-09-05 01:09:30 +02:00
function keyDown(event: KeyboardEvent): void {
if (event.key === "Escape") props.removePopup(props.id);
}
2021-09-05 01:09:30 +02:00
useEffect(() => {
document.addEventListener("keydown", keyDown);
return () => {
document.removeEventListener("keydown", keyDown);
};
});
2021-09-05 01:09:30 +02:00
return (
<div className={"popup-box-content"} id={`${props.id}-content`}>
{React.createElement(props.content, props.props)}
</div>
);
}