Optional label
Modal heading
<!--
Copyright IBM Corp. 2016, 2018
This source code is licensed under the Apache-2.0 license found in the
LICENSE file in the root directory of this source tree.
-->
<button class="bx--btn bx--btn--primary" type="button" data-modal-target="#modal-36zr18ljf8i">Show
modal</button>
<div data-modal id="modal-36zr18ljf8i" class="bx--modal " role="dialog"
aria-modal="true" aria-labelledby="modal-36zr18ljf8i-label" aria-describedby="modal-36zr18ljf8i-heading" tabindex="-1">
<div class="bx--modal-container">
<div class="bx--modal-header">
<p class="bx--modal-header__label bx--type-delta" id="modal-36zr18ljf8i-label">Optional label</p>
<p class="bx--modal-header__heading bx--type-beta" id="modal-36zr18ljf8i-heading">Modal heading</p>
<button class="bx--modal-close" type="button" data-modal-close aria-label="close modal" >
<svg focusable="false" preserveAspectRatio="xMidYMid meet" style="will-change: transform;" xmlns="http://www.w3.org/2000/svg" class="bx--modal-close__icon" width="16" height="16" viewBox="0 0 16 16" aria-hidden="true"><path d="M12 4.7l-.7-.7L8 7.3 4.7 4l-.7.7L7.3 8 4 11.3l.7.7L8 8.7l3.3 3.3.7-.7L8.7 8z"></path></svg>
</button>
</div>
<!-- Note: Modals with content that scrolls, at any viewport, requires `tabindex="0"` on the `bx--modal-content` element -->
<div class="bx--modal-content" >
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean id accumsan augue. Phasellus consequat augue
vitae
tellus tincidunt posuere. Curabitur justo urna, consectetur vel elit iaculis, ultrices condimentum risus. Nulla
facilisi.
Etiam venenatis molestie tellus. Quisque consectetur non risus eu rutrum. </p>
</div>
<div class="bx--modal-footer">
<button class="bx--btn bx--btn--secondary" type="button" data-modal-close>Secondary button</button>
<button class="bx--btn bx--btn--primary" type="button" data-modal-primary-focus>Primary button</button> </div> </div>
</div>
Optional label
Modal heading
<!--
Copyright IBM Corp. 2016, 2018
This source code is licensed under the Apache-2.0 license found in the
LICENSE file in the root directory of this source tree.
-->
<button class="bx--btn bx--btn--primary" type="button" data-modal-target="#modal-lw5cx8zeuab">Show
modal</button>
<div data-modal id="modal-lw5cx8zeuab" class="bx--modal " role="dialog"
aria-modal="true" aria-labelledby="modal-lw5cx8zeuab-label" aria-describedby="modal-lw5cx8zeuab-heading" tabindex="-1">
<div class="bx--modal-container">
<div class="bx--modal-header">
<p class="bx--modal-header__label bx--type-delta" id="modal-lw5cx8zeuab-label">Optional label</p>
<p class="bx--modal-header__heading bx--type-beta" id="modal-lw5cx8zeuab-heading">Modal heading</p>
<button class="bx--modal-close" type="button" data-modal-close aria-label="close modal" data-modal-primary-focus>
<svg focusable="false" preserveAspectRatio="xMidYMid meet" style="will-change: transform;" xmlns="http://www.w3.org/2000/svg" class="bx--modal-close__icon" width="16" height="16" viewBox="0 0 16 16" aria-hidden="true"><path d="M12 4.7l-.7-.7L8 7.3 4.7 4l-.7.7L7.3 8 4 11.3l.7.7L8 8.7l3.3 3.3.7-.7L8.7 8z"></path></svg>
</button>
</div>
<!-- Note: Modals with content that scrolls, at any viewport, requires `tabindex="0"` on the `bx--modal-content` element -->
<div class="bx--modal-content" >
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean id accumsan augue. Phasellus consequat augue
vitae
tellus tincidunt posuere. Curabitur justo urna, consectetur vel elit iaculis, ultrices condimentum risus. Nulla
facilisi.
Etiam venenatis molestie tellus. Quisque consectetur non risus eu rutrum. </p>
</div>
</div>
</div>
Optional label
Modal heading
<!--
Copyright IBM Corp. 2016, 2018
This source code is licensed under the Apache-2.0 license found in the
LICENSE file in the root directory of this source tree.
-->
<button class="bx--btn bx--btn--danger" type="button" data-modal-target="#modal-j0fz2n8ko2">Show
modal</button>
<div data-modal id="modal-j0fz2n8ko2" class="bx--modal bx--modal--danger" role="dialog"
aria-modal="true" aria-labelledby="modal-j0fz2n8ko2-label" aria-describedby="modal-j0fz2n8ko2-heading" tabindex="-1">
<div class="bx--modal-container">
<div class="bx--modal-header">
<p class="bx--modal-header__label bx--type-delta" id="modal-j0fz2n8ko2-label">Optional label</p>
<p class="bx--modal-header__heading bx--type-beta" id="modal-j0fz2n8ko2-heading">Modal heading</p>
<button class="bx--modal-close" type="button" data-modal-close aria-label="close modal" >
<svg focusable="false" preserveAspectRatio="xMidYMid meet" style="will-change: transform;" xmlns="http://www.w3.org/2000/svg" class="bx--modal-close__icon" width="16" height="16" viewBox="0 0 16 16" aria-hidden="true"><path d="M12 4.7l-.7-.7L8 7.3 4.7 4l-.7.7L7.3 8 4 11.3l.7.7L8 8.7l3.3 3.3.7-.7L8.7 8z"></path></svg>
</button>
</div>
<!-- Note: Modals with content that scrolls, at any viewport, requires `tabindex="0"` on the `bx--modal-content` element -->
<div class="bx--modal-content" >
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean id accumsan augue. Phasellus consequat augue
vitae
tellus tincidunt posuere. Curabitur justo urna, consectetur vel elit iaculis, ultrices condimentum risus. Nulla
facilisi.
Etiam venenatis molestie tellus. Quisque consectetur non risus eu rutrum. </p>
</div>
<div class="bx--modal-footer">
<button class="bx--btn bx--btn--secondary" type="button" data-modal-close>Secondary button</button>
<button class="bx--btn bx--btn--danger" type="button" aria-label="Danger"
data-modal-primary-focus>Primary button</button> </div> </div>
</div>
Optional label
Modal heading
<!--
Copyright IBM Corp. 2016, 2018
This source code is licensed under the Apache-2.0 license found in the
LICENSE file in the root directory of this source tree.
-->
<button class="bx--btn bx--btn--primary" type="button" data-modal-target="#modal-yeqppqn6pn">Show
modal</button>
<div data-modal id="modal-yeqppqn6pn" class="bx--modal " role="dialog"
aria-modal="true" aria-labelledby="modal-yeqppqn6pn-label" aria-describedby="modal-yeqppqn6pn-heading" tabindex="-1">
<div class="bx--modal-container">
<div class="bx--modal-header">
<p class="bx--modal-header__label bx--type-delta" id="modal-yeqppqn6pn-label">Optional label</p>
<p class="bx--modal-header__heading bx--type-beta" id="modal-yeqppqn6pn-heading">Modal heading</p>
<button class="bx--modal-close" type="button" data-modal-close aria-label="close modal" >
<svg focusable="false" preserveAspectRatio="xMidYMid meet" style="will-change: transform;" xmlns="http://www.w3.org/2000/svg" class="bx--modal-close__icon" width="16" height="16" viewBox="0 0 16 16" aria-hidden="true"><path d="M12 4.7l-.7-.7L8 7.3 4.7 4l-.7.7L7.3 8 4 11.3l.7.7L8 8.7l3.3 3.3.7-.7L8.7 8z"></path></svg>
</button>
</div>
<!-- Note: Modals with content that scrolls, at any viewport, requires `tabindex="0"` on the `bx--modal-content` element -->
<div class="bx--modal-content" >
<div class="bx--form-item">
<label for="text-input-yeqppqn6pn" class="bx--label">Text Input label</label>
<input id="text-input-yeqppqn6pn" type="text" class="bx--text-input" placeholder="Optional placeholder text"
data-modal-primary-focus>
</div>
</div>
<div class="bx--modal-footer">
<button class="bx--btn bx--btn--secondary" type="button" data-modal-close>Secondary button</button>
<button class="bx--btn bx--btn--primary" type="button" >Primary button</button> </div> </div>
</div>
Use these modifiers with .bx--modal
class.
Name | Description |
---|---|
.bx--modal--danger |
Selector for applying danger modal styles |
import { Modal } from 'carbon-components';
carbon-components.min.js
)var Modal = CarbonComponents.Modal;
<button>
with data-modal-target
attribute)Modal.init();
// `#my-modal` is an element with `[data-modal]` attribute
Modal.create(document.getElementById('my-modal'));
Name | Params | Description |
---|---|---|
release |
Deletes the instance | |
show |
Show the modal | |
hide |
Hide the modal |
// `#my-modal` is an element with `[data-modal]` attribute
var modalInstance = Modal.create(document.getElementById('my-modal'));
modalInstance.show();
Option | Default selector | Description |
---|---|---|
selectorInit |
'[data-modal]' | The css selector for root modal component |
selectorModalClose |
'[data-modal-close]' | The selector to find elements that close the modal |
selectorPrimaryFocus |
'[data-modal-primary-focus]' | The CSS selector to determine the element to put focus when modal gets open |
classVisible |
'is-visible' | Class to toggle visibility of modal |
classBody |
'bx--body--with-modal-open' | Class on <body> that toggles when a modal opens/closes |
attribInitTarget |
'data-modal-target' | The attribute on the launching element to target the modal |
initEventNames |
'['click']' | On specified events, if event matches the attribInitTarget, then initialize the component and run createdByLauncher if method exists |
<div
data-modal
id="my-modal"
class="bx--modal "
role="dialog"
aria-modal="true"
aria-labelledby="my-modal-label"
aria-describedby="my-modal-heading"
tabindex="-1"
>
<div class="bx--modal-container">
<div class="bx--modal-header">
<p class="bx--modal-header__heading bx--type-beta" id="my-modal-heading">
Modal heading
</p>
<button
class="bx--modal-close"
type="button"
data-modal-close
aria-label="close modal"
>
(The close button image)
</button>
</div>
<div class="bx--modal-content">
<label for="my-text-input" class="bx--label">Text Input label</label>
<input
id="my-text-input"
type="text"
class="bx--text-input"
placeholder="Optional placeholder text"
data-modal-primary-focus
/>
</div>
</div>
</div>
Event option | Event name |
---|---|
eventBeforeShown |
'modal-beingshown' |
eventAfterShown |
'modal-shown' |
eventBeforeHidden |
'modal-beinghidden' |
eventAfterHidden |
'modal-hidden' |
document.addEventListener('modal-beinghidden', function(evt) {
if (myApplication.shouldModalKeptOpen(evt.target)) {
evt.preventDefault();
}
});
document.addEventListener('modal-hidden', function(evt) {
myAnalyticsLibrary.send({
action: 'Modal hidden',
id: evt.target.id,
});
});
To trigger the same modal, you need to add the data-modal-target
attribute to
a element, and then point it to the same id. For example
<button
class="bx--btn bx-btn--primary"
type="button"
data-modal-target="#modal"
>
A button
</button>
<button
class="bx--btn bx-btn--secondary"
type="button"
data-modal-target="#modal"
>
Another button
</button>
Both these buttons would trigger the modal with the id of modal.