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

fix: Close Dialog without submit #1547

Merged
merged 6 commits into from
Aug 29, 2024
Merged
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
5 changes: 5 additions & 0 deletions packages/css/src/components/dialog/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ Also, this approach keeps the order of buttons consistent on both narrow and wid
| Shift + Tab | Moves focus to the previous focusable element inside the dialog. |
| Escape | Closes the dialog. |

## Closing Dialog without submit

You can close a Dialog without submitting by using `<button type="button" onClick={closeDialog}>`.
This uses the `closeDialog` function from the React package.

## References

- [HTMLDialogElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLDialogElement)
Expand Down
10 changes: 7 additions & 3 deletions packages/react/src/Dialog/Dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

import clsx from 'clsx'
import { forwardRef } from 'react'
import { forwardRef, MouseEvent } from 'react'
import type { DialogHTMLAttributes, ForwardedRef, PropsWithChildren, ReactNode } from 'react'
import { Heading } from '../Heading'
import { IconButton } from '../IconButton'
Expand All @@ -18,16 +18,20 @@ export type DialogProps = {
heading: string
} & PropsWithChildren<DialogHTMLAttributes<HTMLDialogElement>>

export const closeDialog = (event: MouseEvent<HTMLButtonElement>) => event.currentTarget.closest('dialog')?.close()

export const openDialog = (id: string) => (document.querySelector(id) as HTMLDialogElement)?.showModal()

export const Dialog = forwardRef(
(
{ actions, children, className, closeButtonLabel = 'Sluiten', heading, ...restProps }: DialogProps,
ref: ForwardedRef<HTMLDialogElement>,
) => (
<dialog {...restProps} ref={ref} className={clsx('ams-dialog', className)}>
<form method="dialog" className="ams-dialog__form">
<form className="ams-dialog__form" method="dialog">
<header className="ams-dialog__header">
<Heading size="level-4">{heading}</Heading>
<IconButton formNoValidate label={closeButtonLabel} size="level-4" />
<IconButton label={closeButtonLabel} onClick={closeDialog} size="level-4" type="button" />
</header>
<article className="ams-dialog__article">{children}</article>
{actions && <footer className="ams-dialog__footer">{actions}</footer>}
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/Dialog/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { Dialog } from './Dialog'
export { closeDialog, Dialog, openDialog } from './Dialog'
export type { DialogProps } from './Dialog'
9 changes: 3 additions & 6 deletions storybook/src/components/Dialog/Dialog.docs.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,10 @@ Click or tap the button to open a dialog.

<Canvas of={DialogStories.TriggerButton} />

Some basic example code to open and close a dialog:
To open the dialog, use the `openDialog` function from the React package.
Pass the Dialog’s `id` to the function to select it.

```ts
const openDialog = () => (document.querySelector("#openDialog") as HTMLDialogElement)?.showModal();

const closeDialog = (e: MouseEvent<HTMLButtonElement>) => e.currentTarget.closest("dialog")?.close();
```
To close the Dialog, use the `closeDialog` function.

## Vertically Stacked Buttons

Expand Down
13 changes: 3 additions & 10 deletions storybook/src/components/Dialog/Dialog.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,9 @@
* Copyright Gemeente Amsterdam
*/

import { Button, Heading, Paragraph } from '@amsterdam/design-system-react'
import { Dialog } from '@amsterdam/design-system-react/src'
import { Button, closeDialog, Heading, Paragraph } from '@amsterdam/design-system-react'
import { Dialog, openDialog } from '@amsterdam/design-system-react/src'
import { Meta, StoryObj } from '@storybook/react'
import { MouseEvent } from 'react'

const closeDialog = (event: MouseEvent<HTMLButtonElement>) => {
return event.currentTarget.closest('dialog')?.close()
}

const meta = {
title: 'Components/Containers/Dialog',
Expand Down Expand Up @@ -130,9 +125,7 @@ export const TriggerButton: Story = {
decorators: [
(Story) => (
<article>
<Button onClick={() => (document.querySelector('#openDialog') as HTMLDialogElement)?.showModal()}>
Open Dialog
</Button>
<Button onClick={() => openDialog('#openDialog')}>Open Dialog</Button>
<Story />
</article>
),
Expand Down