carbon-components
Version:
Carbon Components is a component library for IBM Cloud
110 lines (78 loc) • 3.96 kB
Markdown
with `.bx--modal` class.
| Name | Description |
|-------------------------|-------------------------------------------------------|
| .bx--modal--danger | Selector for applying danger modal styles |
```javascript
import { Modal } from 'carbon-components';
```
```javascript
var Modal = CarbonComponents.Modal;
```
```javascript
Modal.init();
```
```javascript
// `#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 |
```javascript
// `#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 |
| classVisible | 'is-visible' | Class to toggle visibility of modal |
| 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 |
| Event Option | Event Name |
|-------------------|---------------------|
| eventBeforeShown | 'modal-beingshown' |
| eventAfterShown | 'modal-shown' |
| eventBeforeHidden | 'modal-beinghidden' |
| eventAfterHidden | 'modal-hidden' |
```javascript
document.addEventListener('modal-beinghidden', function (evt) {
if (myApplication.shouldModalKeptOpen(evt.target)) {
evt.preventDefault();
}
});
```
```javascript
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
```html
<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.`
Use these modifiers