Skip to main contentSkip to search
Clay
  • Get Started
    • How to Use Clay
    • Composition Philosophy
    • How to Read This Documentation
    • Migrating From v2.x
    • Using Clay in JSPs
  • Components
    • Alert
    • Application Bar
    • Aspect Ratio
    • Autocomplete
    • Badge
    • Breadcrumb
    • Button Group
    • Buttons
    • Card
    • Chart
    • Color Picker
    • Data Provider
    • Date Picker
    • DropDown
    • Empty State
    • Form
      • Checkbox
      • Dual List Box
      • Input
      • Radio Group
      • Select
      • Select Box
      • Toggle Switch
    • Forms Hierarchy
    • Heading
    • Icon
    • Label
    • Layout
    • Link
    • List
    • Loading Indicator
    • Localized Input
    • Management Toolbar
    • Modal
    • Multi Select
    • Multi Step Nav
    • Nav
    • Navigation Bar
    • OverlayMask
    • Pagination
    • Pagination Bar
    • Panel
    • Picker
    • Popover
    • Progress Bar
    • Provider
    • Sidebar
    • Slider
    • Sticker
    • Table
    • Tabs
    • Text
    • Timelines
    • Time Picker
    • Toolbar
    • Tooltip
    • TreeView
    • Upper Toolbar
    • VerticalBar
    • Vertical Navigation
  • Contributing
  • CSS Framework
    • Paver
    • SCSS
    • Color
    • Grid
    • Content
      • Typography
      • C Kbd
    • Utilities
      • Accessibility
      • Autofit
      • Border
      • C Focus Inset
      • C Inner
      • Color Utilities
      • C Spacing Utilities
      • Display
      • Flex
      • Float
      • Inline Item
      • Overflow
      • Position
      • Shadow
      • Text
      • Vertical Align
      • Visibility
      • Width and Height
    • Playground
  • Examples
K
  • Docs
  • Sass API
  • Blog
  • Storybook
  • Codesandbox
  • Github
  • Use this menu to toggle between Atlas and Base Themes.

Modal

yarn add @clayui/modal

A modal is a secondary window that communicates or provides an action inside the same process.

  • Examples
  • Markup
  • API

Stable3.85.0View in LexiconCHANGELOGstorybook demos

  • Usage
    • useModal hook
  • Header
  • Provider

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.

Copied!
Code Sample (expand to see it)

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();
To avoid future problems, do not use the information or change the values ​​of `observer`, it is considered a bad practice.

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:

Copied!
Code Sample (expand to see it)

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.

How can this be improved? Create an issue!