import { Canvas, Meta, Source, ArgTypes } from '@storybook/blocks'; import * as Stories from './index.stories'; import * as PreBuiltStories from './PreBuilt.stories'; import { Modal } from './index'; # Modal Cunningham provides a versatile Modal component for displaying any kind of information. > ⚠️ If you want to try dark theme on the modal, you need to go on individual stories. It will not work on this page due to iframe wrapping. ## Usage The component is easy to use. You need to use the `useModal()` hook to get all the needed props and helper functions. { const modal = useModal(); return ( My modal ); }; `}/> Here you go! You bring to live your first modal! 🥳 ### About `useModal()` hook This hook is just a simple wrapper around the `useState()` hook to store some internal states, avoiding you to do it yourself each time you use a modal. It returns an object with the following properties: - `isOpen`: A boolean indicating if the modal is open or not. - `onClose`: A function called when modal closes. - `open`: A function to open the modal. - `close`: A function to close the modal. It does the same as `onClose` but it makes more semantic sense to use it in your code. ### Programatically close the modal You can close the modal by calling the `close()` function returned by the `useModal()` hook. Here is an example of a modal automatically closing after 2 seconds. { const modal = useModal(); useEffect(() => { modal.open(); setTimeout(() => { modal.close(); }, 2000); }, []); return ( My modal ); }; `}/> ## Sizes The modal component comes with multiple sizes: `ModalSize.SMALL`, `ModalSize.MEDIUM`, `ModalSize.LARGE` and `ModalSize.FULL`. You can set the size of the modal by passing the `size` prop. ### Small ### Medium ### Large ### Full ## Close button You can hide the close button by passing the `hideCloseButton` prop. ## Buttons ### Right buttons You can add buttons on the right side of the modal by passing the `rightActions` prop. ### Left buttons You can add buttons on the left side of the modal by passing the `leftActions` prop. ### Center buttons You can add buttons on the center of the modal by passing the `actions` prop. ## Icon You can add an icon to the modal by passing the `titleIcon` prop. ## Close on click outside By default, the modal will not be closed when you click outside of it in order to match the default behavior of the `` element. You can change this behavior by passing the `closeOnClickOutside` prop. ## Close on escape By default, the modal will be closed when you press the `esc` key. You can change this behavior by passing the `closeOnEsc` prop. ## Pre Built Modals As we know that developers love to have handy shortcuts for common use cases, we provide some pre built modals that we prevent you from adding `` and using `useModal()` each time you want to use those pre built modals. The way you will be able to use those pre built modals is by using async calls that return the decision of the user. > For instance the following code shows how to display a confirmation modal asking the user if he wants to continue or not. { const modals = useModals(); const ask = async () => { const decision = await modals.confirmationModal(); alert("You decided: " + decision); }; return ; }; `}/> **About the `decision` variable:** - `decision` is truthy if the user accepted. - `decision` is null if the user denied. - `decision` is undefined if the user closed the modal via `esc` or close button. **Of course you can customize the title and the content of the modal by using the `props` first argument of the functions.** ### Confirmation modal ### Delete confirmation modal ### Message modal This modal can be used to display a message to the user. It is possible to use multiple variant of it by using `messageType` with `VariantType` enum. #### Neutral ## Props These are the props of `Modal`. ## Design tokens Here a the custom design tokens defined by the Toast. | Token | Description | |--------------- |----------------------------- | | background-color | Default background color | | backdrop-color | Default backdrop color | | border-radius | Border radius of the modal | | border-color | Border color of the modal | | box-shadow | Box shadow of the modal | | title-font-weight | Font weight of the modal title | | color | Color of the modal title | | content-font-size | Font size of the modal content | | content-font-weight | Font weight of the modal content | | content-color | Font Color of the modal content | | width-small | Width of the small modal size | | width-medium | Width of the medium modal size | | width-large | Width of the large modal size |