# Modals > Modals are streamlined, but flexible dialog prompts powered by JavaScript and CSS. They support a > number of use cases from user notification to completely custom content and feature a handful of > helpful sub-components, sizes, variants, accessibility, and more. ```html
Launch demo modal

Hello from modal!

``` ## Overview ``, by default, has an **OK** and **Cancel** buttons in the footer. These buttons can be customized by setting various props on the component. You can customize the size of the buttons, disable buttons, hide the **Cancel** button (i.e. `ok-only`), choose a variant (e.g. `danger` for a red OK button) using the `ok-variant` and `cancel-variant` props, and provide custom button content using the `ok-title` and `cancel-title` props, or using the named slots `modal-ok` and `modal-cancel`. `` supports close on ESC (enabled by default), close on backdrop click (enabled by default), and the `X` close button in the header (enabled by default). These features may be disabled by setting the props `no-close-on-esc`, `no-close-on-backdrop`, and `hide-header-close` respectively. You can override the modal title via the named slot `modal-title`, override the header completely via the `modal-header` slot, and override the footer completely via the `modal-footer` slot. **Note**: when using the `modal-footer` slot, the default **OK** and **Cancel** buttons will not be present. Also, if you use the `modal-header` slot, the default header `X` close button will not be present, nor can you use the `modal-title` slot. Modals will not render their content in the document until they are shown (lazily rendered). Modals, when visible, are rendered **appended to the `` element**. The placement of the `` component will not affect layout, as it always renders as a placeholder comment node (``). You can revert to the behaviour of older BootstrapVue versions via the use of the [`static` prop](#lazy-loading-and-static-modals). ## Toggle modal visibility There are several methods that you can employ to toggle the visibility of ``. ### Using `v-b-modal` directive Other elements can easily show modals using the `v-b-modal` directive. ```html
Show Modal Show Modal Hello From My Modal!
``` This approach will automatically return focus to the trigger element once the modal closes (similar to default Bootstrap functionality). Other approaches for toggling modal visibility may require additional code to implement this accessibility feature. See the [Accessibility](#accessibility) section below for details. ### Using `this.$bvModal.show()` and `this.$bvModal.hide()` instance methods When BootstrapVue is installed as a plugin, or the `ModalPlugin` plugin is used, BootstrapVue will inject a `$bvModal` object into every Vue instance (components, apps). `this.$bvModal` exposes several methods, of which two are for showing and hiding modals: | Method | Description | | ------------------------ | -------------------------------------- | | `this.$bvModal.show(id)` | Show the modal with the specified `id` | | `this.$bvModal.hide(id)` | Hide the modal with the specified `id` | Both methods return immediately after being called. ```html
Open Modal

Hello From This Modal!

Close Me
``` The `this.$bvModal` object is also used for displaying [modal message boxes](#modal-message-boxes). ### Using `show()`, `hide()`, and `toggle()` component methods You can access modal using `ref` attribute and then call the `show()`, `hide()` or `toggle()` methods. ```html ``` The `hide()` method accepts an optional string `trigger` argument for defining what triggered the modal to close. See section [Prevent Closing](#prevent-closing) below for details. **Note:** It is recommended to use the `this.$bvModal.show()` and `this.$bvModal.hide()` methods (mentioned in the previous section) instead of using `$ref` methods. ### Using `v-model` property `v-model` property is always automatically synced with `` visible state and you can show/hide using `v-model`. ```html ``` When using the `v-model` prop, **do not** use the `visible` prop at the same time. ### Using scoped slot scope methods Refer to the [Custom rendering with slots](#custom-rendering-with-slots) section on using the various methods passed to scoped slots for closing modals. ### Emitting events on \$root You can emit `bv::show::modal`, `bv::hide::modal`, and `bv::toggle::modal` events on `$root` with the first argument set to the modal's id. An optional second argument can specify the element to return focus to once the modal is closed. The second argument can be a CSS selector, an element reference, or a component reference (the root element of the component will be focused). ```html
Open Modal Toggle Modal
Hello From My Modal!
Close Me Toggle Me
``` ```js export default { methods: { showModal() { this.$root.$emit('bv::show::modal', 'modal-1', '#btnShow') }, hideModal() { this.$root.$emit('bv::hide::modal', 'modal-1', '#btnShow') }, toggleModal() { this.$root.$emit('bv::toggle::modal', 'modal-1', '#btnToggle') } } } ``` **Note:** It is recommended to use the `this.$bvModal.show()` and `this.$bvModal.hide()` methods (mentioned in a previous section) instead of emitting `$root` events. ### Prevent closing To prevent `` from closing (for example when validation fails). you can call the `.preventDefault()` method of the event object passed to your `ok` (**OK** button), `cancel` (**Cancel** button), `close` (modal header close button) and `hide` event handlers. Note that `.preventDefault()`, when used, **must** be called synchronously, as async is not supported. ```html ``` **Note**: events `ok`, `cancel`, and `close` are emitted by modal's built in **OK**, **Cancel**, and header close (**X**) buttons respectively. These events will not be emitted, by default, if you have provided your own buttons in the `modal-footer` slot or have hidden the footer. In this case use the `hide` event to control cancelling of the modal close. Event `hide` is always emitted, even if `ok`, `cancel`, and `close` are emitted. The `ok`, `cancel`, `close` and `hide` event object (`BvModalEvent`) contains several properties and methods: | Property or Method | Type | Description | | ------------------ | -------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | `preventDefault()` | Method | When called prevents the modal from closing | | `trigger` | Property | Will be one of: `ok` (Default **OK** Clicked), `cancel` (Default **Cancel** clicked), `esc` (if the Esc key was pressed), `backdrop` (if the backdrop was clicked), `headerclose` (if the header X button was clicked), the first argument provided to the `hide()` method, or `null` otherwise. | | `target` | Property | A reference to the modal element | | `vueTarget` | property | A reference to the modal's Vue VM instance | | `componentId` | property | The modal's ID | You can set the value of `trigger` by passing an argument to the component's `hide()` method for advanced control (i.e. detecting what button or action triggered the modal to hide). **Note:** `ok`, `cancel`, or `close` events will be only emitted when the argument to `hide()` is strictly `'ok'`, `'cancel'`, or `'headerclose'` respectively. The argument passed to `hide()` will be placed into the `trigger` property of the event object. ## Modal content ### Using the grid Utilize the Bootstrap grid system within a modal by nesting `` within the modal-body. Then, use the normal grid system `` (or ``) and `` as you would anywhere else. ### Tooltips and popovers Tooltips and popovers can be placed within modals as needed. When modals are closed, any tooltips and popovers within are also automatically dismissed. Tooltips and popovers are automatically appended to the modal element (to ensure correct z-indexing), although you can override where they are appended by specifying a container ID (refer to tooltip and popover docs for details). ```html
Show Modal

This Button triggers a popover on click.

This Link will show a tooltip on hover.

``` ## Lazy loading and static modals By default, modals will not render their content in the document until they are shown (lazily rendered). Modals that, when visible, are rendered appended to the `` element. The `` component will not affect layout, as they render as a placeholder comment node (``) in the DOM position they are placed. Due to the portalling process, it can take two or more `$nextTick`s to render changes of the content into the target. Modals can be rendered _in-place_ in the document (i.e. where the `` component is placed in the document) by setting the `static` prop to `true`. Note that the content of the modal will be rendered in the DOM even if the modal is not visible/shown when `static` is `true`. To make `static` modals lazy rendered, also set the `lazy` prop to `true`. The modal will then appear in the document _only_ when it is visible. Note, when in `static` mode, placement of the `` component _may affect layout_ of your document and the modal. The `lazy` prop will have no effect if the prop `static` is not `true` (non-static modals will _always_ be lazily rendered). ## Styling, options, and customization ### Modal sizing Modals have three optional sizes, available via the prop `size`. These sizes kick in at certain breakpoints to avoid horizontal scrollbars on narrower viewports. Valid optional sizes are `sm`, `lg`, and `xl`. ```html
xl modal lg modal sm modal Hello Extra Large Modal! Hello Large Modal! Hello Small Modal!
``` The `size` prop maps the size to the `.modal-` classes. ### Scrolling long content When modals become too long for the user's viewport or device, they scroll independent of the page itself. Try the demo below to see what we mean. ```html
Launch overflowing modal

Cras mattis consectetur purus sit amet fermentum. Cras justo odio, dapibus ac facilisis in, egestas eget quam. Morbi leo risus, porta ac consectetur ac, vestibulum at eros.

``` You can also create a scrollable modal that allows the scrolling of the modal body by setting the prop `scrollable` to `true`. ```html
Launch scrolling modal

Cras mattis consectetur purus sit amet fermentum. Cras justo odio, dapibus ac facilisis in, egestas eget quam. Morbi leo risus, porta ac consectetur ac, vestibulum at eros.

``` ### Vertically centered modal Vertically center your modal in the viewport by setting the `centered` prop. ```html
Launch centered modal

Vertically centered modal!

``` Feel free to mix vertically `centered` with `scrollable`. ### Variants Control the header, footer, and body background and text variants by setting the `header-bg-variant`, `header-text-variant`, `body-bg-variant`, `body-text-variant`, `footer-bg-variant`, and `footer-text-variant` props. Use any of the standard Bootstrap variants such as `danger`, `warning`, `info`, `success`, `dark`, `light`, etc. The variants for the bottom border of the header and top border of the footer can be controlled by the `header-border-variant` and `footer-border-variant` props respectively. ```html ``` You can also apply arbitrary classes to the modal dialog container, content (modal window itself), header, body and footer via the `modal-class`, `content-class`, `header-class`, `body-class` and `footer-class` props, respectively. The props accept either a string or array of strings. ### Hiding the backdrop Hide the modal's backdrop via setting the `hide-backdrop` prop. ```html
Open modal

We've added the utility class 'shadow' to the modal content for added effect.

``` Note that clicking outside of the modal will still close the modal even though the backdrop is hidden. You can disable this behaviour by setting the `no-close-on-backdrop` prop on ``. ### Disable open and close animation To disable the fading transition/animation when modal opens and closes, just set the prop `no-fade` on the `` component. ### Footer button sizing Fancy smaller or larger buttons in the default footer? Simply set the `button-size` prop to `'sm'` for small buttons, or `'lg'` for larger buttons. ```html
Small Footer Buttons Large Footer Buttons

This modal has small footer buttons

This modal has large footer buttons

``` The prop `button-size` has no effect if you have provided your own footer via the [`modal-footer`](#custom-rendering-with-slots) slot. ### Disabling built-in footer buttons You can disable the built-in footer buttons programmatically. You can disable the **Cancel** and **OK** buttons individually by setting the `cancel-disabled` and `ok-disabled` props, respectively, to `true`. Set the prop to `false` to re-enable the button. To disable both **Cancel** and **OK** buttons at the same time, simply set the `busy` prop to `true`. Set it to `false` to re-enable both buttons. ### Custom rendering with slots `` provides several named slots (of which some are optionally scoped) that you can use to customize the content of various sections of the modal. | Slot | Optionally Scoped | Description | | -------------------- | ----------------- | ------------------------------------------------------------------------------------- | | `default` | Yes | Main content of the modal | | `modal-title` | Yes | Content to place in the modal's title | | `modal-header` | Yes | Content to place in the header. Replaces the entire header including the close button | | `modal-footer` | Yes | Content to place in the footer. Replaces the entire footer including the button(s) | | `modal-ok` | No | Content to place inside the footer OK button | | `modal-cancel` | No | Content to place inside the footer CANCEL button | | `modal-header-close` | No | Content to place inside the header CLOSE (`x`) button | The scope available to the slots that support optional scoping are: | Method or Property | Description | | ------------------ | ------------------------------------------------------------------------------------------------------------ | | `ok()` | Closes the modal and fires the `ok` and `hide` events, with `bvModalEvent.trigger = 'ok'` | | `cancel()` | Closes the modal and fires the `cancel` and `hide` events, with `bvModalEvent.trigger = 'cancel'` | | `close()` | Closes the modal and fires the `close` and `hide` events, with `bvModalEvent.trigger = 'headerclose'` | | `hide(trigger)` | Closes the modal and fires the `hide` event, with the `bvModalEvent.trigger = trigger` (trigger is optional) | | `visible` | The visibility state of the modal. `true` if the modal is visible and `false` if not visible | #### Example modal using custom scoped slots ```html ``` ## Multiple modal support Unlike native Bootstrap v4, BootstrapVue supports multiple modals opened at the same time. To disable stacking for a specific modal, just set the prop `no-stacking` on the `` component. This will hide the modal before another modal is shown. ```html
Open First Modal

First Modal

Open Second Modal

Second Modal

Open Third Modal

Third Modal

``` **Notes:** - Avoid nesting a `` _inside_ another ``, as it may get "constrained" to the boundaries of the parent modal dialog (specifically when static modals are used). - The opaque backdrop will appear progressively darker for each modal that is opened. This is expected behaviour as each backdrop is opened over top the other modals and backdrops. ## Modal message boxes BootstrapVue provides a few built in Message Box methods on the exposed `this.$bvModal` object. These methods provide a way to generate simple OK and Confirm style modal messages, from anywhere in your app without having to explicitly place a `` component in your pages. | Method | Description | | ----------------------------------------------- | -------------------------------------------------------------------- | | `this.$bvModal.msgBoxOk(message, options)` | Open a modal with `message` as the content and a single OK button | | `this.$bvModal.msgBoxConfirm(message, options)` | Open a modal with `message` as the content and CANCEL and OK buttons | The `options` argument is an optional configuration object for adding titles and styling the Message Box modal. The object properties correspond to `` props, except in camelCase format instead of kebab-case. Both methods return a `Promise` (requires a polyfill for IE 11 and older browser support) which resolve into a value when the modal hides. `.msgBoxOk()` always resolves to the value `true`, while `.msgBoxConfirm()` resolves to either `true` (OK button pressed), `false` (CANCEL button pressed), or `null` (if the modal was closed via backdrop click, Esc press, or some other means. If `message` is not provided, both methods will return immediately with the value `undefined`. You can use either the `.then(..).catch(...)` or async `await` code styles (async `await` requires modern browsers or a transpiler). ### OK message box Example OK Message boxes ```html ``` ### Confirm message box Example Confirm Message boxes ```html ``` ### Message box notes - The `this.$bvModal` injection is only available when using the full `BootstrapVue` plugin or the `ModalPlugin` plugin. It is not available if importing just the `b-modal` component. To just import the injection, use the `BVModalPlugin` plugin. - A new `$bvModal` injection (mixin) is created for each Vue virtual machine (i.e. each instantiated component), and is not usable via direct access to the `Vue.prototype`, as it needs access to the instance's `this` and `$root` contexts. - Message Boxes require `Promise` support in the browser. If targeting your app for older browsers, such as IE 11, please include a polyfill that provides `Promise` support. If `Promise` support is not detected, then the message box methods will immediately return `undefined`. - Message Boxes are an extension of the `` component, and hence support the majority of `` props (using camelCase format), with the exception of the following props: `lazy`, `static`, `busy`, `visible`, `noStacking`, `okOnly`, `okDisabled`, and `cancelDisabled`. - When a `title` (or `titleHtml`) _is not_ provided in the options, the header will not be shown. - When a `title` (or `titleHtml`) _is_ provided in the options, the header close button is not shown by default. You can enable the header close button by setting `hideHeaderClose: false` in the options. - Message Boxes will throw an error (promise rejection) if they are closed/destroyed before they are hidden. Always include a `.catch(errHandler)` reject handler, event if using the async `await` style code. - When using Vue Router (or similar), Message Boxes will close and reject if the route changes before the modal hides. If you wish for the message box to remain open when the route changes, use `this.$root.$bvModal` instead of `this.$bvModal`. - Message boxes cannot be generated during Server Side Rendering (SSR). - The Message Box `message` currently does not support HTML strings, however, you can pass an _array_ of `VNodes` as the `message` for fine grained control of the markup. You can use Vue's [`this.$createElement`](https://vuejs.org/v2/guide/render-function.html#createElement-Arguments) method to generate VNodes. This can also be done for the modal title (by passing VNodes to the `title` option), OK button text (via the `okTitle` option), and the CANCEL button text (via the `cancelTitle` option). ### Message box advanced usage When using the `this.$bvModal.msgBoxOk(...)` or `this.$bvModal.msgBoxConfirm(...)` methods for generating modals, you may want the modal content to be more than just a string message. As mentioned in the [message box notes](#message-box-notes) section above, you can pass _arrays_ of VNodes as the message and title for more complex content. Use Vue's [`this.$createElement`](https://vuejs.org/v2/guide/render-function.html#createElement-Arguments) method to generate VNodes. ```html ``` ## Listening to modal changes via \$root events To listen to any modal opening, use: ```js export default { mounted() { this.$root.$on('bv::modal::show', (bvEvent, modalId) => { console.log('Modal is about to be shown', bvEvent, modalId) }) } } ``` Refer to the [Events](#comp-ref-b-modal) section of this documentation for the full list of events emitted. ## Accessibility `` provides several accessibility features, including auto focus, return focus, keyboard (tab) _focus containment_, and automated `aria-*` attributes. **Note:** The animation effect of this component is dependent on the `prefers-reduced-motion` media query. See the [reduced motion section of our accessibility documentation](/docs/reference/accessibility) for additional details. ### Modal ARIA attributes The `aria-labelledby` and `aria-describedby` attributes will appear on the modal automatically in most cases. - The `aria-labelledby` attribute will **not** be present if you have the header hidden, or supplied your own header, or have not supplied a modal title. It is recommended to supply a title for your modals (when using the built in header). You can visually hide the header title, but still make it available to screen readers by setting the `title-sr-only` prop. If you do not have a header, you can supply a label for the modal by passing a string to the `aria-label` prop. - The `aria-describedby` attribute will always point to the modal's body content. - If the `aria-label` prop is specified with a string value, the `aria-labelledby` attribute will not be rendered, even if you have a title/header for your modal. The `aria-label` and `title-sr-only` props were added in version `v2.0.0-rc.27`. ### Auto focus on open `` will autofocus the modal _container_ when opened. You can pre-focus an element within the `` by listening to the `` `shown` event, and call the element's `focus()` method. `` will not attempt to autofocus if an element already has focus within the ``. ```html
I Don't Have Focus
``` ```js export default { methods: { focusMyElement() { this.$refs.focusThis.focus() } } } ``` Alternatively, if using `b-form-*` form controls, you can use the `autofocus` prop to automatically focus a form control when the modal opens. Note that the `autofocus` prop will not work with `b-modal` if the `static` prop is used without the `lazy` prop set, as `autofocus` happens when the `b-form-*` controls are _mounted in the DOM_. If you want to auto focus one of the _built-in_ modal buttons (`ok`, `cancel` or the header `close` button, you can set the prop `auto-focus-button` to one of the values `'ok'`, `'cancel'` or `'close'` and `` will focus the specified button if it exists. This feature is also available for modal message boxes.

Note: it is not recommended to autofocus an input or control inside of a modal for accessibility reasons, as screen reader users will not know the context of where the input is (the announcement of the modal may not be spoken). It is best to let <b-modal> focus the modal's container, allowing the modal information to be spoken to the user, and then allow the user to tab into the input.

### Returning focus to the triggering element For accessibility reasons, it is desirable to return focus to the element that triggered the opening of the modal, when the modal closes. `` will try and automatically determine which element had focus before the modal was opened, and will return the focus to that element when the modal has hidden if possible. However, several methods and options are provided to allow you to specify the element to return focus to once the modal has hidden. #### Specify return focus element via the `return-focus` prop You can also specify an element to return focus to, when modal closes, by setting the `return-focus` prop to one of the following: - A CSS Query Selector string (or an element ID prepended with `#`) - A component reference (which is mounted on a focusable element, such as ``) - A reference to a DOM element that is focusable If the passed in element is not focusable, then the browser will determine what has focus (usually ``, which is not desirable) This method for returning focus is handy when you use the `` methods `show()` and `hide()`, or the `v-model` prop. Note this property takes precedence over other methods of specifying the return focus element. #### Auto return focus When `` is opened via the `v-b-modal` directive on an element, focus will be returned to this element automatically when `` closes, unless an element has been specified via the `return-focus` prop. #### Specify return focus via event When using the `bv::show::modal` event (emitted on `$root`), you can specify a second argument which is the element to return focus to. This argument accepts the same types as the `return-focus` prop. ```js this.$root.$emit('bv::show::modal', 'modal-1', '#focusThisOnClose') ``` _Tip:_ if using a click event (or similar) to trigger modal to open, pass the event's `target` property: ```html
Open Modal
``` **Note:** If the `` has the `return-focus` prop set, then the element specified via the event will be ignored. ### Keyboard navigation When tabbing through elements within a ``, if focus attempts to leave the modal into the document, it will be brought back into the modal. Avoid setting `tabindex` on elements within the modal to any value other than `0` or `-1`. Doing so will make it difficult for people who rely on assistive technology to navigate and operate page content and can make some of your elements unreachable via keyboard navigation. If some elements outside the modal need to be focusable (i.e. for TinyMCE), you can add them as CSS selectors to the `ignore-enforce-focus-selector` prop 2.4.0+, e.g.: ```html ``` In some circumstances, you may need to disable the enforce focus feature completely. You can do this by setting the prop `no-enforce-focus`, although this is _highly discouraged_ for accessibility reasons. ### `v-b-modal` directive accessibility Notes on `v-b-modal` directive accessibility: - If the element is anything other than a `