-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #176 from open-formulieren/feature/add-modal-compo…
…nent Add modal component
- Loading branch information
Showing
6 changed files
with
223 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import {ArgTypes, Canvas, Meta} from '@storybook/blocks'; | ||
|
||
import * as ModalStories from './Modal.stories'; | ||
|
||
<Meta of={ModalStories} /> | ||
|
||
# Modals | ||
|
||
Modals are built with the `react-modal` library, which helps enforce the accessible behaviours. | ||
|
||
Pass the content of the modal as children: | ||
|
||
```tsx | ||
<Modal isOpen closeModal={() => setClosed()}> | ||
<h1>Modal title</h1> | ||
<p>Modal body</p> | ||
</Modal> | ||
``` | ||
|
||
## Props | ||
|
||
<ArgTypes /> | ||
|
||
## Context | ||
|
||
For Storybook purposes, we use the `ModalContext` React context so that the nodes are injected in | ||
the correct location. The context is considered private API and it is not necessary to make use of | ||
the modal component. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import {Meta, StoryObj} from '@storybook/react'; | ||
import {fn} from '@storybook/test'; | ||
|
||
import Modal from './Modal'; | ||
|
||
export default { | ||
title: 'Generic/Modal', | ||
component: Modal, | ||
parameters: { | ||
modal: {noModal: true}, | ||
}, | ||
args: { | ||
isOpen: true, | ||
closeModal: fn(), | ||
className: 'additional-classnames', | ||
children: 'Modal content', | ||
}, | ||
} satisfies Meta<typeof Modal>; | ||
|
||
type Story = StoryObj<typeof Modal>; | ||
|
||
export const Default: Story = {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import clsx from 'clsx'; | ||
import {createContext, useContext} from 'react'; | ||
import {FormattedMessage} from 'react-intl'; | ||
import ReactModal from 'react-modal'; | ||
|
||
export interface ModalContextType { | ||
parentSelector?: () => HTMLElement; | ||
ariaHideApp?: boolean; | ||
} | ||
|
||
export const ModalContext = createContext<ModalContextType>({}); | ||
ModalContext.displayName = 'ModalContext'; | ||
|
||
export interface ModalProps { | ||
/** | ||
* Modal open/close state, typically the state from `useState` in your component. | ||
*/ | ||
isOpen: boolean; | ||
/** | ||
* Callback to invoke when closing of the modal is requested. | ||
* | ||
* Example modal close requests: | ||
* - Clicking the close button | ||
* - Hitting `ESC` on the keyboard | ||
* - Clicking outside of the modal | ||
*/ | ||
closeModal: () => void; | ||
/** | ||
* Additional class name(s) to pass to the modal root DOM node. Optional. | ||
*/ | ||
className?: string; | ||
/** | ||
* Modal content. | ||
*/ | ||
children?: React.ReactNode; | ||
} | ||
|
||
const Modal: React.FC<ModalProps> = ({isOpen, closeModal, className, children}: ModalProps) => { | ||
const {parentSelector, ariaHideApp} = useContext(ModalContext); | ||
return ( | ||
<ReactModal | ||
isOpen={isOpen} | ||
onRequestClose={closeModal} | ||
parentSelector={parentSelector} | ||
ariaHideApp={ariaHideApp} | ||
portalClassName={ | ||
isOpen ? clsx('formio-dialog', 'formio-dialog-theme-default', className) : undefined | ||
} | ||
overlayClassName="formio-dialog-overlay" | ||
className="formio-dialog-content" | ||
overlayElement={(props, contentElement) => ( | ||
<> | ||
<div {...props}></div> | ||
{contentElement} | ||
</> | ||
)} | ||
> | ||
<button | ||
type="button" | ||
className="formio-dialog-close float-right btn btn-secondary btn-sm" | ||
onClick={closeModal} | ||
> | ||
<span className="sr-only"> | ||
<FormattedMessage description="Modal close button label" defaultMessage="Close" /> | ||
</span> | ||
</button> | ||
{children} | ||
</ReactModal> | ||
); | ||
}; | ||
|
||
export default Modal; |