Modal
A modal is a secondary window that communicates or provides an action inside the same process.
install | yarn add @clayui/modal |
---|---|
version | 3.122.0 |
Stable3.122.0View in LexiconCHANGELOG
You may want to compose your content and customize with your use cases or add your own components while still keeping the logic of a modal.
Usage
Modal can be controlled using the useModal
hook without the need for additional state, it handles the animations for you.
const Dialog = () => {
const {observer, onOpenChange, open} = useModal();
return (
<>
{open && (
<ClayModal observer={observer} spritemap={spritemap}>
<ClayModal.Header>Title</ClayModal.Header>
<ClayModal.Body>Body</ClayModal.Body>
<ClayModal.Footer
last={
<ClayButton onClick={() => onOpenChange(false)}>
Close
</ClayButton>
}
/>
</ClayModal>
)}
<ClayButton onClick={() => onOpenChange(true)}>
Open modal
</ClayButton>
</>
);
};
useModal hook
The useModal
hook is required to create a Modal component in Clay, it is responsible for communicating with ClayModal and controlling states to make animations, this is necessary because Modal needs animation when opening and closing so we need to delay calls of the onClose
so you can safely control Modal's visibility or do other things.
useModal
returns a signature with observer
and onClose
:
- observer - Observer is the communication channel that connects ClayModal with
useModal
. - onOpenChange - Callback to change open state.
- open - Flag to indicate the open state of the modal.
const {observer, onOpenChange, open} = useModal();
Header
ClayModal.Header
offers two different APIs for use by toggling the prop withTitle
. By default(withTitle={true}
), ClayModal.Header
behaves like a high-level component. If you want to use the lower-level components in Header, all you need to do is set withTitle={false}
.
Here is an example of both APIs:
Provider
When your application uses a lot of modals, you can use <ClayModalProvider />
to have only one open Modal on the screen and the component centered on your application, you can invoke the modal from the context through the API. It is not recommended to have multiple modals open at the same time.
Add in the project root:
<ClayModalProvider spritemap={spritemap}>
<MyApp />
</ClayModalProvider>
In any part of your application you can invoke modal.
import {Context} from '@clayui/modal';
import ClayButton from '@clayui/button';
const MyApp = () => {
const [state, dispatch] = useContext(Context);
return (
<ClayButton
displayType="primary"
onClick={() =>
dispatch({
payload: {
body: <h1>{'Hello world!'}</h1>,
footer: [
,
,
<ClayButton key={3} onClick={state.onClose}>
{'Primary'}
</ClayButton>,
],
header: 'Title',
size: 'lg',
},
type: 1,
})
}
>
{'Open modal'}
</ClayButton>
);
};
Provider contains two different states that you can control:
- Close - CLOSE
- Open - OPEN
The context signature is similar to a useReducer
, contains state
and dispatch
.
const [state, dispatch] = useContext(Context);
dispatch({payload: {...}, type: 'OPEN'});
state
returns all values that are passed to dispatch payload and you can pass body
, footer
, header
, size
, status
and url
to payload.