mirror of
https://github.com/fleetdm/fleet
synced 2026-05-17 14:08:25 +00:00
> Related issue: #9956 # Checklist for submitter If some of the following don't apply, delete the relevant line. <!-- Note that API documentation changes are now addressed by the product design team. --> - [x] Changes file added for user-visible changes in `changes/`, `orbit/changes/` or `ee/fleetd-chrome/changes`. See [Changes files](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/Committing-Changes.md#changes-files) for more information. - [x] Input data is properly validated, `SELECT *` is avoided, SQL injection is prevented (using placeholders for values in statements) - [x] Added/updated tests - [x] If paths of existing endpoints are modified without backwards compatibility, checked the frontend/CLI for any necessary changes - [x] If database migrations are included, checked table schema to confirm autoupdate - For database migrations: - [x] Checked schema for all modified table for columns that will auto-update timestamps during migration. - [x] Confirmed that updating the timestamps is acceptable, and will not cause unwanted side effects. - [x] Ensured the correct collation is explicitly set for character columns (`COLLATE utf8mb4_unicode_ci`). - [x] Manual QA for all new/changed functionality --------- Co-authored-by: Martin Angers <martin.n.angers@gmail.com> Co-authored-by: Gabriel Hernandez <ghernandez345@gmail.com> Co-authored-by: Roberto Dip <rroperzh@gmail.com> Co-authored-by: Sarah Gillespie <73313222+gillespi314@users.noreply.github.com> Co-authored-by: Dante Catalfamo <43040593+dantecatalfamo@users.noreply.github.com> Co-authored-by: Roberto Dip <dip.jesusr@gmail.com>
122 lines
3.3 KiB
TypeScript
122 lines
3.3 KiB
TypeScript
import React, { useEffect } from "react";
|
|
import classnames from "classnames";
|
|
import Button from "components/buttons/Button/Button";
|
|
import Icon from "components/Icon/Icon";
|
|
|
|
const baseClass = "modal";
|
|
|
|
type ModalWidth = "medium" | "large" | "xlarge" | "auto";
|
|
|
|
export interface IModalProps {
|
|
title: string | JSX.Element;
|
|
children: JSX.Element;
|
|
onExit: () => void;
|
|
onEnter?: () => void;
|
|
/** medium 650px, large 800px, xlarge 850px, auto auto-width
|
|
* @default "medium"
|
|
*/
|
|
width?: ModalWidth;
|
|
/** isHidden can be set true to hide the modal when opening another modal
|
|
* @default false
|
|
*/
|
|
isHidden?: boolean;
|
|
/** isLoading can be set true to enable targeting elements by loading state
|
|
* @default false
|
|
*/
|
|
isLoading?: boolean;
|
|
/** `isContentDisabled` can be set to true to display the modal content as disabled.
|
|
* At the moment this will place an overlay over the modal content and make it
|
|
* unclickable. The top right will not be disabled and will still be clickable.
|
|
*
|
|
* @default false
|
|
*/
|
|
isContentDisabled?: boolean;
|
|
className?: string;
|
|
}
|
|
|
|
const Modal = ({
|
|
title,
|
|
children,
|
|
onExit,
|
|
onEnter,
|
|
width = "medium",
|
|
isHidden = false,
|
|
isLoading = false,
|
|
isContentDisabled = false,
|
|
className,
|
|
}: IModalProps): JSX.Element => {
|
|
useEffect(() => {
|
|
const closeWithEscapeKey = (e: KeyboardEvent) => {
|
|
if (e.key === "Escape") {
|
|
onExit();
|
|
}
|
|
};
|
|
|
|
document.addEventListener("keydown", closeWithEscapeKey);
|
|
|
|
return () => {
|
|
document.removeEventListener("keydown", closeWithEscapeKey);
|
|
};
|
|
}, []);
|
|
|
|
useEffect(() => {
|
|
if (onEnter) {
|
|
const closeOrSaveWithEnterKey = (event: KeyboardEvent) => {
|
|
if (event.code === "Enter" || event.code === "NumpadEnter") {
|
|
event.preventDefault();
|
|
onEnter();
|
|
}
|
|
};
|
|
|
|
document.addEventListener("keydown", closeOrSaveWithEnterKey);
|
|
return () => {
|
|
document.removeEventListener("keydown", closeOrSaveWithEnterKey);
|
|
};
|
|
}
|
|
}, [onEnter]);
|
|
|
|
const backgroundClasses = classnames(`${baseClass}__background`, {
|
|
[`${baseClass}__hidden`]: isHidden,
|
|
});
|
|
|
|
const modalContainerClasses = classnames(
|
|
className,
|
|
`${baseClass}__modal_container`,
|
|
`${baseClass}__modal_container__${width}`,
|
|
{
|
|
[`${className}__loading`]: isLoading,
|
|
}
|
|
);
|
|
|
|
const contentWrapperClasses = classnames(`${baseClass}__content-wrapper`, {
|
|
[`${baseClass}__content-wrapper-disabled`]: isContentDisabled,
|
|
});
|
|
|
|
const contentClasses = classnames(`${baseClass}__content`, {
|
|
[`${baseClass}__content-disabled`]: isContentDisabled,
|
|
});
|
|
|
|
return (
|
|
<div className={backgroundClasses}>
|
|
<div className={modalContainerClasses}>
|
|
<div className={`${baseClass}__header`}>
|
|
<span>{title}</span>
|
|
<div className={`${baseClass}__ex`}>
|
|
<Button className="button button--unstyled" onClick={onExit}>
|
|
<Icon name="close" color="core-fleet-black" size="medium" />
|
|
</Button>
|
|
</div>
|
|
</div>
|
|
|
|
<div className={contentWrapperClasses}>
|
|
{isContentDisabled && (
|
|
<div className={`${baseClass}__disabled-overlay`} />
|
|
)}
|
|
<div className={contentClasses}>{children}</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export default Modal;
|