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

issue 1649: dialog onBeforeClose #1671

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
27 changes: 27 additions & 0 deletions docs/documentation/components/dialog.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,30 @@ function PreserveBodyScrollingDialogExample() {
)
}
```

## Handle overlay/close clicks

Use the `onBeforeClose` prop to handle overlay/close clicks. It is executed
when dialog is about to close. Return `false` to prevent the sheet from closing.

```jsx
function OnBeforeCloseExample() {
const [isShown, setIsShown] = React.useState(false)

return (
<Pane>
<Dialog
isShown={isShown}
title="Dialog with onBeforeClose callback
onBeforeClose={() => confirm('Are you sure you want to close?')}
onCloseComplete={() => setIsShown(false)}
>
Dialog content
</Dialog>

<Button onClick={() => setIsShown(true)}>Show Dialog</Button>
</Pane>
)
}
```

5 changes: 4 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -833,7 +833,10 @@ export interface CornerDialogProps {
export declare const CornerDialog: React.FC<CornerDialogProps>

export interface DialogProps
extends Pick<OverlayProps, 'isShown' | 'preventBodyScrolling' | 'shouldAutoFocus' | 'shouldCloseOnEscapePress'> {
extends Pick<
OverlayProps,
'isShown' | 'preventBodyScrolling' | 'shouldAutoFocus' | 'shouldCloseOnEscapePress' | 'onBeforeClose'
> {
/**
* Children can be a string, node or a function accepting `({ close })`.
* When passing a string, <Paragraph /> is used to wrap the string.
Expand Down
9 changes: 9 additions & 0 deletions src/dialog/src/Dialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ const Dialog = memo(function Dialog({
isConfirmLoading = false,
isShown = false,
minHeightContent = 80,
onBeforeClose,
onCancel = closeHandler,
onCloseComplete,
onConfirm = closeHandler,
Expand Down Expand Up @@ -195,6 +196,7 @@ const Dialog = memo(function Dialog({
justifyContent: 'center',
...overlayProps
}}
onBeforeClose={onBeforeClose}
preventBodyScrolling={preventBodyScrolling}
>
{({ close, state }) => (
Expand Down Expand Up @@ -403,6 +405,13 @@ Dialog.propTypes = {
*/
preventBodyScrolling: PropTypes.bool,

/**
* Function called when dialog is about to close.
* Return `false` to prevent the sheet from closing.
* type: `Function -> Boolean`
*/
onBeforeClose: PropTypes.func,

/**
* Props that are passed to the Overlay component.
*/
Expand Down
19 changes: 19 additions & 0 deletions src/dialog/stories/index.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -396,3 +396,22 @@ storiesOf('dialog', module)
</DialogManager>
</Box>
))
.add('Dialog with onBeforeClose', () => (
<Box padding={40}>
<DialogManager>
{({ hide, isShown, show }) => (
<Box marginBottom={16}>
<Dialog
isShown={isShown}
title="Dialog with onBeforeClose callback"
onBeforeClose={() => confirm('Are you sure you want to close?')}
onCloseComplete={hide}
>
<Paragraph>onBeforeClose: are you sure you want to close?</Paragraph>
</Dialog>
<Button onClick={show}>Show Dialog with onBeforeClose</Button>
</Box>
)}
</DialogManager>
</Box>
))