Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new Custom DialogTitle to @comet/admin #2609

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .changeset/curvy-bulldogs-boil.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
"@comet/admin": minor
---

Add new custom `Dialog`

- `title` props allows setting DialogTitle
- optional `onClose` prop adds Close Button to the Component

Example:

- `<Dialog title="Dialog Title" onClose={() => {...}} />`
106 changes: 106 additions & 0 deletions packages/admin/admin/src/common/Dialog.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import { Close } from "@comet/admin-icons";
import {
ComponentsOverrides,
css,
Dialog as MUIDialog,
DialogTitle as MUIDialogTitle,
IconButton as MUIIconButton,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use Mui instead of MUI to stay consistent with other import renames.

Theme,
useThemeProps,
} from "@mui/material";
import { ReactNode } from "react";

import { createComponentSlot } from "../helpers/createComponentSlot";
import { ThemedComponentBaseProps } from "../helpers/ThemedComponentBaseProps";

export type DialogClassKey = "root" | "iconButton" | "titleWrapper" | "dialogTitle";

export interface DialogProps
extends ThemedComponentBaseProps<{
iconButton: typeof MUIIconButton;
root: typeof MUIDialog;
dialogTitle: typeof MUIDialogTitle;
titleWrapper: "div";
}> {
children?: ReactNode;
title: ReactNode;
open?: boolean;
onClose?: () => void;
iconMapping?: {
closeIcon?: ReactNode;
};
}
export function Dialog(inProps: DialogProps) {
const {
slotProps,
open = false,
title,
iconMapping = {},
children,
onClose,
...restProps
} = useThemeProps({ props: inProps, name: "CometAdminDialog" });
const { closeIcon = <Close color="inherit" /> } = iconMapping;
return (
<Root open={open} {...slotProps?.root} {...restProps}>
<DialogTitle {...slotProps?.dialogTitle}>
<TitleWrapper {...slotProps?.titleWrapper}>{title}</TitleWrapper>
{onClose && (
<IconButton {...slotProps?.iconButton} onClick={onClose}>
{closeIcon}
</IconButton>
)}
</DialogTitle>
{children}
</Root>
);
}

const Root = createComponentSlot(MUIDialog)<DialogClassKey>({
componentName: "Dialog",
slotName: "root",
})(css``);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
})(css``);
})();


const DialogTitle = createComponentSlot(MUIDialogTitle)<DialogClassKey>({
componentName: "Dialog",
slotName: "dialogTitle",
})(css`
display: flex;
align-items: center;
`);

const IconButton = createComponentSlot(MUIIconButton)<DialogClassKey>({
componentName: "Dialog",
slotName: "iconButton",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IconButton is too generic imo, I'd prefer something like CloseButton

})(css`
color: inherit;
position: absolute;
right: 20px;
`);

const TitleWrapper = createComponentSlot("div")<DialogClassKey>({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically a div is not allowed inside h2.

Suggested change
const TitleWrapper = createComponentSlot("div")<DialogClassKey>({
const TitleWrapper = createComponentSlot("span")<DialogClassKey>({

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't the styles of TitleWrapper just be part of DialogTitle directly? 🤔

componentName: "Dialog",
slotName: "titleWrapper",
})(css`
color: inherit;
width: 100%;
Comment on lines +85 to +86
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should be the default when using a div.

Suggested change
color: inherit;
width: 100%;

padding-right: 40px;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This padding should only be added when there is a close-button, otherwise the spacings left and right of the text are inconsistent.

min-height: 20px;
`);

declare module "@mui/material/styles" {
interface ComponentsPropsList {
CometAdminDialog: DialogProps;
}

interface ComponentNameToClassKey {
CometAdminDialog: DialogClassKey;
}

interface Components {
CometAdminDialog?: {
defaultProps?: Partial<ComponentsPropsList["CometAdminDialog"]>;
styleOverrides?: ComponentsOverrides<Theme>["CometAdminDialog"];
};
}
}
1 change: 1 addition & 0 deletions packages/admin/admin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export { SplitButtonContext, SplitButtonContextOptions } from "./common/buttons/
export { useSplitButtonContext } from "./common/buttons/split/useSplitButtonContext";
export { ClearInputAdornment, ClearInputAdornmentProps } from "./common/ClearInputAdornment";
export { CometLogo } from "./common/CometLogo";
export { Dialog, DialogClassKey, DialogProps } from "./common/Dialog";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably add an import restriction to prevent using MUIs Dialog.
This is how it's done for Alert for example:

{
name: "@mui/material",
importNames: ["Alert"],
message: "Please use Alert from @comet/admin instead",
},

export { FieldSet, FieldSetClassKey, FieldSetProps } from "./common/FieldSet";
export { HoverActions, HoverActionsClassKey, HoverActionsProps } from "./common/HoverActions";
export { Loading, LoadingProps } from "./common/Loading";
Expand Down
30 changes: 30 additions & 0 deletions storybook/src/admin/Dialog.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Dialog } from "@comet/admin";
import { DialogContent, Typography } from "@mui/material";
import { storiesOf } from "@storybook/react";
import React from "react";

const textContent =
"Dialog content. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.";

storiesOf("@comet/admin/Dialog", module)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably be combined with the existing Dialog story: https://storybook.comet-dxp.com/?path=/story/comet-admin-mui--dialog

Maybe extend the existing one and add knobs for title and onClose, similar to Dialog size:

const selectedDialogSize = select("Dialog size", dialogSizeOptions, "sm");

.add("Dialog", () => (
<Dialog open title="Title">
<DialogContent>
<Typography>{textContent}</Typography>
</DialogContent>
</Dialog>
))
.add("Dialog with Close Button", () => (
<Dialog open title="Title" onClose={() => {}}>
<DialogContent>
<Typography>{textContent}</Typography>
</DialogContent>
</Dialog>
))
.add("Dialog with empty title", () => (
<Dialog open title="" onClose={() => {}}>
<DialogContent>
<Typography>{textContent}</Typography>
</DialogContent>
</Dialog>
));
Loading