# Modal Component
The Modal API allows you to display an overlay that prevents interaction with the rest of the app until dismissed.
It is used by customizing your Modal content with the `Modal` component and then opening it with the `shopify.modal.show('modal-id')` API.
```jsx
import {Modal, TitleBar, useAppBridge} from '@shopify/app-bridge-react';
export function MyModal() {
const shopify = useAppBridge();
return (
<>
Message
>
);
}
```
## Modal component
The `Modal` component is available for use in your app. It configures a Modal to display in the Shopify Admin.
The content you provide can be simple React elements or a `src` prop with a URL that will be loaded.
### _UIModalAttributes
### children
value: `HTMLCollection & UITitleBarAttributes`
The content to display within a Modal. You can provide a single HTML element with children and the [ui-title-bar](https://shopify.dev/docs/api/app-bridge-library/web-components/ui-title-bar) element to configure the Modal title bar.
### id
value: `string`
A unique identifier for the Modal
### src
value: `string`
The URL of the content to display within a Modal. If provided, the Modal will display the content from the provided URL and any children other than the [ui-title-bar](https://shopify.dev/docs/api/app-bridge-library/web-components/ui-title-bar) and [ui-save-bar](https://shopify.dev/docs/api/app-bridge-library/web-components/ui-save-bar) elements will be ignored.
### variant
value: `'small' | 'base' | 'large' | 'max'`
The size of the modal.
Before the Modal is shown, this can be changed to any of the provided values. After the Modal is shown, this can can only be changed between `small`, `base`, and `large`.
### UITitleBarAttributes
### children
value: `any`
The children of the title bar.
### title
value: `string`
The title of the title bar. Can also be set via `document.title`.
## Related
- [Modal](https://shopify.dev/docs/api/app-bridge-library/apis/modal)
- [ui-modal](https://shopify.dev/docs/api/app-bridge-library/web-components/ui-modal)
- [useAppBridge](https://shopify.dev/docs/api/app-bridge-library/react-hooks/useappbridge)
- [TitleBar](https://shopify.dev/docs/api/app-bridge-library/react-components/titlebar-component)
- [SaveBar](https://shopify.dev/docs/api/app-bridge-library/react-components/savebar-component)
## Examples
The Modal API allows you to display an overlay that prevents interaction with the rest of the app until dismissed.
It is used by customizing your Modal content with the `Modal` component and then opening it with the `shopify.modal.show('modal-id')` API.
### Modals with different options
Modal with max size```jsx
import {Modal, TitleBar, useAppBridge} from '@shopify/app-bridge-react';
export function MyModal() {
const shopify = useAppBridge();
return (
<>
>
);
}
```
Modal with variant```jsx
import {Modal, TitleBar, useAppBridge} from '@shopify/app-bridge-react';
export function MyModal() {
const shopify = useAppBridge();
return (
<>
>
);
}
```
Modal with title```jsx
import {Modal, TitleBar, useAppBridge} from '@shopify/app-bridge-react';
export function MyModal() {
const shopify = useAppBridge();
return (
<>
Hello, World!
>
);
}
```
Modal with primary and secondary actions```jsx
import {Modal, TitleBar, useAppBridge} from '@shopify/app-bridge-react';
export function MyModal() {
const shopify = useAppBridge();
return (
<>
Hello, World!
>
);
}
```
Adding a critical button to a Modal```jsx
import {Modal, TitleBar, useAppBridge} from '@shopify/app-bridge-react';
export function MyModal() {
const shopify = useAppBridge();
return (
<>
If you delete this resource, it can't be undone.
>
);
}
```
### Modal events
Using the `onShow` callback which is called when the Modal is opened```jsx
import {Modal, useAppBridge} from '@shopify/app-bridge-react';
export function MyModal() {
const shopify = useAppBridge();
return (
<>
console.log('Modal is showing')}>
Message
>
);
}
```
Using the `onHide` callback which is called when the Modal is closed```jsx
import {Modal, useAppBridge} from '@shopify/app-bridge-react';
export function MyModal() {
const shopify = useAppBridge();
return (
<>
console.log('Modal is hiding')}>
>
);
}
```
### Modal controls
Modal controlled by the `open` prop```jsx
import {useState} from 'react';
import {Modal, TitleBar} from '@shopify/app-bridge-react';
export function MyModal() {
const [modalOpen, setModalOpen] = useState(false);
return (
<>
Message
>
);
}
```
### Using a src URL to load content
Loading content from a URL```jsx
// main app
import {Modal, TitleBar, useAppBridge} from '@shopify/app-bridge-react';
export function MyModal() {
const shopify = useAppBridge();
return (
<>
>
);
}
// my-route.html
My separate route
// my-route.jsx
export function MyRoute() {
return (
My separate route
);
}
```
Communicating between the Modal and the parent window```jsx
// main app
import {Modal, TitleBar, useAppBridge} from '@shopify/app-bridge-react';
import {useEffect} from 'react';
export function MyModal() {
const shopify = useAppBridge();
useEffect(() => {
function handleMessageFromModal(ev) {
console.log('Message received in main app:', ev.data);
}
window.addEventListener('message', handleMessageFromModal)
return () => {
window.removeEventListener('message', handleMessageFromModal)
}
}, [])
const openModal = async () => {
await shopify.modal.show('my-modal');
sendMessageToModal('Hello from the main app');
}
const sendMessageToModal = (message) => {
document.getElementById('my-modal').contentWindow.postMessage(message, location.origin);
}
return (
<>
>
);
}
// my-route.html
My separate route
// my-route.jsx
import {useEffect} from 'react';
export function MyRoute() {
useEffect(() => {
function handleMessageFromMainApp(ev) {
console.log('Message received in modal:', ev.data);
}
window.addEventListener('message', handleMessageFromMainApp)
return () => {
window.removeEventListener('message', handleMessageFromMainApp)
}
}, [])
const sendMessageToMainApp = (message) => {
window.opener.postMessage(message, location.origin);
}
return (
);
}
```
This pattern is useful for displaying a dialog or prompt from within a max variant modal.```jsx
// main app
import {Modal, useAppBridge} from '@shopify/app-bridge-react';
export function MyModal() {
const shopify = useAppBridge();
return (
<>
Message
>
);
}
// my-route.js
```
## Examples
The Modal API allows you to display an overlay that prevents interaction with the rest of the app until dismissed.
It is used by customizing your Modal content with the `Modal` component and then opening it with the `shopify.modal.show('modal-id')` API.
### Modals with different options
Modal with max size```jsx
import {Modal, TitleBar, useAppBridge} from '@shopify/app-bridge-react';
export function MyModal() {
const shopify = useAppBridge();
return (
<>
>
);
}
```
Modal with variant```jsx
import {Modal, TitleBar, useAppBridge} from '@shopify/app-bridge-react';
export function MyModal() {
const shopify = useAppBridge();
return (
<>
>
);
}
```
Modal with title```jsx
import {Modal, TitleBar, useAppBridge} from '@shopify/app-bridge-react';
export function MyModal() {
const shopify = useAppBridge();
return (
<>
Hello, World!
>
);
}
```
Modal with primary and secondary actions```jsx
import {Modal, TitleBar, useAppBridge} from '@shopify/app-bridge-react';
export function MyModal() {
const shopify = useAppBridge();
return (
<>
Hello, World!
>
);
}
```
Adding a critical button to a Modal```jsx
import {Modal, TitleBar, useAppBridge} from '@shopify/app-bridge-react';
export function MyModal() {
const shopify = useAppBridge();
return (
<>
If you delete this resource, it can't be undone.
>
);
}
```
### Modal events
Using the `onShow` callback which is called when the Modal is opened```jsx
import {Modal, useAppBridge} from '@shopify/app-bridge-react';
export function MyModal() {
const shopify = useAppBridge();
return (
<>
console.log('Modal is showing')}>
Message
>
);
}
```
Using the `onHide` callback which is called when the Modal is closed```jsx
import {Modal, useAppBridge} from '@shopify/app-bridge-react';
export function MyModal() {
const shopify = useAppBridge();
return (
<>
console.log('Modal is hiding')}>
>
);
}
```
### Modal controls
Modal controlled by the `open` prop```jsx
import {useState} from 'react';
import {Modal, TitleBar} from '@shopify/app-bridge-react';
export function MyModal() {
const [modalOpen, setModalOpen] = useState(false);
return (
<>
Message
>
);
}
```
### Using a src URL to load content
Loading content from a URL```jsx
// main app
import {Modal, TitleBar, useAppBridge} from '@shopify/app-bridge-react';
export function MyModal() {
const shopify = useAppBridge();
return (
<>
>
);
}
// my-route.html
My separate route
// my-route.jsx
export function MyRoute() {
return (
My separate route
);
}
```
Communicating between the Modal and the parent window```jsx
// main app
import {Modal, TitleBar, useAppBridge} from '@shopify/app-bridge-react';
import {useEffect} from 'react';
export function MyModal() {
const shopify = useAppBridge();
useEffect(() => {
function handleMessageFromModal(ev) {
console.log('Message received in main app:', ev.data);
}
window.addEventListener('message', handleMessageFromModal)
return () => {
window.removeEventListener('message', handleMessageFromModal)
}
}, [])
const openModal = async () => {
await shopify.modal.show('my-modal');
sendMessageToModal('Hello from the main app');
}
const sendMessageToModal = (message) => {
document.getElementById('my-modal').contentWindow.postMessage(message, location.origin);
}
return (
<>
>
);
}
// my-route.html
My separate route
// my-route.jsx
import {useEffect} from 'react';
export function MyRoute() {
useEffect(() => {
function handleMessageFromMainApp(ev) {
console.log('Message received in modal:', ev.data);
}
window.addEventListener('message', handleMessageFromMainApp)
return () => {
window.removeEventListener('message', handleMessageFromMainApp)
}
}, [])
const sendMessageToMainApp = (message) => {
window.opener.postMessage(message, location.origin);
}
return (
);
}
```
This pattern is useful for displaying a dialog or prompt from within a max variant modal.```jsx
// main app
import {Modal, useAppBridge} from '@shopify/app-bridge-react';
export function MyModal() {
const shopify = useAppBridge();
return (
<>