--- title: useOverlay description: 'A composable to programmatically control overlays.' --- ## Usage Use the auto-imported `useOverlay` composable to programmatically control [Modal](/components/modal) and [Slideover](/components/slideover) components. ```vue ``` - The `useOverlay` composable is created using `createSharedComposable`, ensuring that the same overlay state is shared across your entire application. ::note In order to return a value from the overlay, the `overlay.open().instance.result` can be awaited. In order for this to work, however, the **overlay component must emit a `close` event**. See example below for details. :: ## API ### `create(component: T, options: OverlayOptions): OverlayInstance` Create an overlay, and return a factory instance. - Parameters: - `component`: The overlay component. - `options`: - `defaultOpen?: boolean` Open the overlay immediately after being created. Defaults to `false`. - `props?: ComponentProps`: An optional object of props to pass to the rendered component. - `destroyOnClose?: boolean` Removes the overlay from memory when closed. Defaults to `false`. ### `open(id: symbol, props?: ComponentProps): OpenedOverlay` Open an overlay by its `id`. - Parameters: - `id`: The identifier of the overlay. - `props`: An optional object of props to pass to the rendered component. ### `close(id: symbol, value?: any): void` Close an overlay by its `id`. - Parameters: - `id`: The identifier of the overlay. - `value`: A value to resolve the overlay promise with. ### `patch(id: symbol, props: ComponentProps): void` Update an overlay by its `id`. - Parameters: - `id`: The identifier of the overlay. - `props`: An object of props to update on the rendered component. ### `unmount(id: symbol): void` Remove an overlay from the DOM by its `id`. - Parameters: - `id`: The identifier of the overlay. ### `isOpen(id: symbol): boolean` Check if an overlay is open using its `id`. - Parameters: - `id`: The identifier of the overlay. ### `overlays: Overlay[]` In-memory list of all overlays that were created. ## Instance API ### `open(props?: ComponentProps): Promise>` Open the overlay. - Parameters: - `props`: An optional object of props to pass to the rendered component. ```vue ``` ### `close(value?: any): void` Close the overlay. - Parameters: - `value`: A value to resolve the overlay promise with. ### `patch(props: ComponentProps)` Update the props of the overlay. - Parameters: - `props`: An object of props to update on the rendered component. ```vue ``` ## Example Here's a complete example of how to use the `useOverlay` composable: ```vue ``` In this example, we're using the `useOverlay` composable to control multiple modals and slideovers. ## Caveats ### Provide / Inject When opening overlays programmatically (e.g. modals, slideovers, etc), the overlay component can only access injected values from the component containing `UApp` (typically `app.vue` or layout components). This is because overlays are mounted outside of the page context by the `UApp` component. As such, using `provide()` in pages or parent components isn't supported directly. To pass provided values to overlays, the recommended approach is to use props instead: ```vue ```