# 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. ### Modal ```jsx import {Modal, TitleBar, useAppBridge} from '@shopify/app-bridge-react'; export function MyModal() { const shopify = useAppBridge(); return ( <> <button onClick={() => shopify.modal.show('my-modal')}>Open Modal</button> <Modal id="my-modal"> <p>Message</p> <TitleBar title="Title"> <button variant="primary">Label</button> <button onClick={() => shopify.modal.hide('my-modal')}>Label</button> </TitleBar> </Modal> </> ); } ``` ## 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 The content to display within a Modal. You can provide a single HTML element with children and the [ui-title-bar](/docs/api/app-bridge-library/web-components/ui-title-bar) element to configure the Modal title bar. ### id A unique identifier for the Modal ### src 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](/docs/api/app-bridge-library/web-components/ui-title-bar) and [ui-save-bar](/docs/api/app-bridge-library/web-components/ui-save-bar) elements will be ignored. ### variant 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 The children of the title bar. ### title The title of the title bar. Can also be set via <code>document.title</code>. ## Related - [Modal](/docs/api/app-bridge-library/apis/modal) - [ui-modal](/docs/api/app-bridge-library/web-components/ui-modal) - [useAppBridge](/docs/api/app-bridge-library/react-hooks/useappbridge) - [TitleBar](/docs/api/app-bridge-library/react-components/titlebar-component) - [SaveBar](/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 ### Opening a max size Modal ```jsx import {Modal, TitleBar, useAppBridge} from '@shopify/app-bridge-react'; export function MyModal() { const shopify = useAppBridge(); return ( <> <button onClick={() => shopify.modal.show('my-modal')}>Open Modal</button> <Modal id="my-modal" variant="max"> <div></div> <TitleBar> <button variant="primary">Primary action</button> <button>Secondary action</button> </TitleBar> </Modal> </> ); } ``` ### Specifying a Modal size ```jsx import {Modal, TitleBar, useAppBridge} from '@shopify/app-bridge-react'; export function MyModal() { const shopify = useAppBridge(); return ( <> <button onClick={() => shopify.modal.show('my-modal')}>Open Modal</button> <Modal id="my-modal" variant="large"> <div></div> <TitleBar> <button variant="primary">Primary action</button> <button>Secondary action</button> </TitleBar> </Modal> </> ); } ``` ### Specifying a title for the Modal ```jsx import {Modal, TitleBar, useAppBridge} from '@shopify/app-bridge-react'; export function MyModal() { const shopify = useAppBridge(); return ( <> <button onClick={() => shopify.modal.show('my-modal')}>Open Modal</button> <Modal id="my-modal"> <p>Hello, World!</p> <TitleBar title="My Modal"></TitleBar> </Modal> </> ); } ``` ### Adding primary and secondary actions to a Modal ```jsx import {Modal, TitleBar, useAppBridge} from '@shopify/app-bridge-react'; export function MyModal() { const shopify = useAppBridge(); return ( <> <button onClick={() => shopify.modal.show('my-modal')}>Open Modal</button> <Modal id="my-modal"> <p>Hello, World!</p> <TitleBar> <button variant="primary" onClick={() => console.log('Saving')}> Save </button> <button onClick={() => console.log('Cancelling')}>Cancel</button> </TitleBar> </Modal> </> ); } ``` ### Using a modal for a destructive action ```jsx import {Modal, TitleBar, useAppBridge} from '@shopify/app-bridge-react'; export function MyModal() { const shopify = useAppBridge(); return ( <> <button onClick={() => shopify.modal.show('my-modal')}>Open Modal</button> <Modal id="my-modal"> <p>If you delete this resource, it can't be undone.</p> <TitleBar title="Delete this resource"> <button variant="primary" tone="critical" onClick={() => console.log('Deleting')} > Delete </button> <button onClick={() => console.log('Cancelling')}>Cancel</button> </TitleBar> </Modal> </> ); } ``` ### Modal events ### Subscribing to Show ```jsx import {Modal, useAppBridge} from '@shopify/app-bridge-react'; export function MyModal() { const shopify = useAppBridge(); return ( <> <button onClick={() => shopify.modal.show('my-modal')}>Open Modal</button> <Modal id="my-modal" onShow={() => console.log('Modal is showing')}> <p>Message</p> </Modal> </> ); } ``` ### Subscribing to Hide ```jsx import {Modal, useAppBridge} from '@shopify/app-bridge-react'; export function MyModal() { const shopify = useAppBridge(); return ( <> <button onClick={() => shopify.modal.show('my-modal')}>Open Modal</button> <Modal id="my-modal" onHide={() => console.log('Modal is hiding')}> <button onclick="document.getElementById('my-modal').hide()"> Hide Modal </button> </Modal> </> ); } ``` ### Modal controls ### Showing a Modal with 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 ( <> <button onClick={() => setModalOpen(true)}>Open Modal</button> <Modal id="my-modal" open={modalOpen}> <p>Message</p> <TitleBar title="My Modal"> <button onClick={() => setModalOpen(false)}>Label</button> </TitleBar> </Modal> </> ); } ``` ### 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 ( <> <button onClick={() => shopify.modal.show('my-modal')}>Open Modal</button> <Modal id="my-modal" src="/my-route"> <TitleBar title="Title"> <button variant="primary">Label</button> <button onClick={() => shopify.modal.hide('my-modal')}>Label</button> </TitleBar> </Modal> </> ); } // my-route.html <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <meta name="shopify-api-key" content="%SHOPIFY_API_KEY%" /> <script src="https://cdn.shopify.com/shopifycloud/app-bridge.js"></script> </head> <body> <h1>My separate route</h1> <script src="./my-route.jsx" type="module"></script> </body> </html> // my-route.jsx export function MyRoute() { return ( <h1>My separate route</h1> ); } ``` ### 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 ( <> <button onClick={openModal}>Open Modal</button> <Modal id="my-modal" src="/my-route"> <TitleBar title="Title"> <button variant="primary">Label</button> <button onClick={() => shopify.modal.hide('my-modal')}>Label</button> </TitleBar> </Modal> </> ); } // my-route.html <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <meta name="shopify-api-key" content="%SHOPIFY_API_KEY%" /> <script src="https://cdn.shopify.com/shopifycloud/app-bridge.js"></script> </head> <body> <h1>My separate route</h1> <script src="./my-route.jsx" type="module"></script> </body> </html> // 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 ( <button onClick={() => sendMessageToMainApp('Hello from the modal')}> Send message </button> ); } ``` ### Opening a base modal within a max src modal ```jsx // main app import {Modal, useAppBridge} from '@shopify/app-bridge-react'; export function MyModal() { const shopify = useAppBridge(); return ( <> <Modal src="/my-route" variant="max" id="my-modal" /> <button onClick={() => shopify.modal.show("my-modal")}>Open</button> <Modal id="my-nested-modal"> <p>Message</p> </Modal> </> ); } // my-route.js <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <meta name="shopify-api-key" content="%SHOPIFY_API_KEY%" /> <script src="https://cdn.shopify.com/shopifycloud/app-bridge.js"></script> </head> <body> <button onclick="window.opener.shopify.modal.show('my-nested-modal')">Open</button> </body> </html> ```