Skip to content

Commit

Permalink
Added backdrop prop to Dialog (#210)
Browse files Browse the repository at this point in the history
* Added `backdrop` prop to `Dialog`.
  • Loading branch information
rubenthoms authored Feb 25, 2022
1 parent 20a0694 commit 8db7ed2
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 42 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- [#202](https://github.com/equinor/webviz-core-components/pull/202) - Adjusted `z-index` of suggestions of `SmartNodeSelector` to a hard-coded value of `1500`.
- [#201](https://github.com/equinor/webviz-core-components/pull/201) - Implemented wrapper around `MaterialUI's` draggable dialog. Makes a new `Dialog` component available in `Dash`.
- [#210](https://github.com/equinor/webviz-core-components/pull/210) - Added `backdrop` property to `Dialog`. This allows to disable the backdrop behind a dialog and makes all other elements remain clickable.

## [0.5.5] - 2022-02-09

Expand Down
21 changes: 9 additions & 12 deletions react/src/demo/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,12 @@ type MenuProps = {
};

const App: React.FC = () => {
const [
nodeSelectorState,
setNodeSelectorState,
] = React.useState<SmartNodeSelectorProps>({
selectedNodes: [],
selectedIds: [],
selectedTags: [],
});
const [nodeSelectorState, setNodeSelectorState] =
React.useState<SmartNodeSelectorProps>({
selectedNodes: [],
selectedIds: [],
selectedTags: [],
});

const [currentPage, setCurrentPage] = React.useState<MenuProps>({
url: "",
Expand Down Expand Up @@ -111,6 +109,7 @@ const App: React.FC = () => {
id="dialog"
max_width="xl"
open={dialogOpen}
backdrop={false}
draggable={true}
setProps={(newProps) => {
console.log(newProps);
Expand Down Expand Up @@ -140,13 +139,11 @@ const App: React.FC = () => {
deprecation_warnings={[
{
message: "Deprecated 1",
url:
"https://github.com/equinor/webviz-core-components",
url: "https://github.com/equinor/webviz-core-components",
},
{
message: "Deprecated 2",
url:
"https://github.com/equinor/webviz-core-components",
url: "https://github.com/equinor/webviz-core-components",
},
]}
/>
Expand Down
97 changes: 73 additions & 24 deletions react/src/lib/components/Dialog/Dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from "react";
import PropTypes, { InferProps } from "prop-types";

import {
withStyles,
Button,
Dialog as MuiDialog,
DialogActions,
Expand Down Expand Up @@ -54,6 +55,10 @@ const propTypes = {
PropTypes.arrayOf(PropTypes.node),
PropTypes.node,
]),
/**
* Set to false if you do not want to have a backdrop behind the dialog.
*/
backdrop: PropTypes.bool,
/**
* A list of actions to be displayed as buttons in the lower right corner of the dialog.
*/
Expand All @@ -79,6 +84,7 @@ const defaultProps: Optionals<InferProps<typeof propTypes>> = {
draggable: false,
full_screen: false,
children: null,
backdrop: true,
actions: [],
last_action_called: null,
actions_called: 0,
Expand All @@ -87,6 +93,12 @@ const defaultProps: Optionals<InferProps<typeof propTypes>> = {
},
};

const StyledMuiDialog = withStyles({
root: {
pointerEvents: "none",
},
})(MuiDialog);

/**
* A modal dialog component with optional buttons. Can be set to be draggable.
*/
Expand All @@ -106,7 +118,10 @@ export const Dialog: React.FC<InferProps<typeof propTypes>> = (props) => {
setOpen(adjustedProps.open || false);
}, [adjustedProps.open]);

const handleClose = () => {
const handleClose = (reason: string) => {
if (reason === "backdropClick" && !adjustedProps.backdrop) {
return;
}
setOpen(false);
adjustedProps.setProps({ open: false });
};
Expand All @@ -120,27 +135,8 @@ export const Dialog: React.FC<InferProps<typeof propTypes>> = (props) => {
setActionsCalled(actionsCalled + 1);
};

return (
<MuiDialog
id={props.id}
open={open}
onClose={() => handleClose()}
PaperComponent={
adjustedProps.draggable ? DraggablePaperComponent : Paper
}
aria-labelledby="dialog-title"
maxWidth={
(adjustedProps.max_width as
| "xs"
| "sm"
| "md"
| "lg"
| "xl"
| null) || false
}
fullScreen={adjustedProps.full_screen}
scroll="body"
>
const content = (
<>
<DialogTitle
style={{
cursor: adjustedProps.draggable ? "move" : "default",
Expand All @@ -151,7 +147,7 @@ export const Dialog: React.FC<InferProps<typeof propTypes>> = (props) => {
{adjustedProps.title}
<IconButton
aria-label="close"
onClick={() => handleClose()}
onClick={() => handleClose("buttonClick")}
style={{
position: "absolute",
right: 8,
Expand All @@ -174,8 +170,61 @@ export const Dialog: React.FC<InferProps<typeof propTypes>> = (props) => {
</Button>
))}
</DialogActions>
</MuiDialog>
</>
);

if (adjustedProps.backdrop) {
return (
<MuiDialog
id={props.id}
open={open}
onClose={(_, reason) => handleClose(reason)}
PaperComponent={
adjustedProps.draggable ? DraggablePaperComponent : Paper
}
aria-labelledby="dialog-title"
maxWidth={
(adjustedProps.max_width as
| "xs"
| "sm"
| "md"
| "lg"
| "xl"
| null) || false
}
fullScreen={adjustedProps.full_screen}
scroll="body"
>
{content}
</MuiDialog>
);
} else {
return (
<StyledMuiDialog
id={props.id}
open={open}
onClose={(_, reason) => handleClose(reason)}
hideBackdrop={true}
PaperComponent={
adjustedProps.draggable ? DraggablePaperComponent : Paper
}
aria-labelledby="dialog-title"
maxWidth={
(adjustedProps.max_width as
| "xs"
| "sm"
| "md"
| "lg"
| "xl"
| null) || false
}
fullScreen={adjustedProps.full_screen}
scroll="paper"
>
{content}
</StyledMuiDialog>
);
}
};

Dialog.propTypes = propTypes;
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import Draggable from "react-draggable";

export const DraggablePaperComponent: React.FC<PaperProps> = (props) => {
return (
<Draggable
handle="#draggable-dialog-title"
cancel={'[class*="MuiDialogContent-root"]'}
>
<Paper {...props} />
</Draggable>
<div style={{ pointerEvents: "all" }}>
<Draggable
handle="#draggable-dialog-title"
cancel={'[class*="MuiDialogContent-root"]'}
>
<Paper {...props} />
</Draggable>
</div>
);
};

0 comments on commit 8db7ed2

Please sign in to comment.