carbon-components-angular
Version:
Next generation components
1,341 lines (1,196 loc) • 67.2 kB
HTML
<!doctype html>
<html class="no-js" lang="">
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title>carbon-components-angular documentation</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="../images/favicon.ico">
<link rel="stylesheet" href="../styles/style.css">
<link rel="stylesheet" href="../styles/dark.css">
</head>
<body>
<div class="navbar navbar-default navbar-fixed-top d-block d-sm-none">
<a href="../" class="navbar-brand">carbon-components-angular documentation</a>
<button type="button" class="btn btn-default btn-menu ion-ios-menu" id="btn-menu"></button>
</div>
<div class="xs-menu menu" id="mobile-menu">
<div id="book-search-input" role="search"><input type="text" placeholder="Type to search"></div> <compodoc-menu></compodoc-menu>
</div>
<div class="container-fluid main">
<div class="row main">
<div class="hidden-xs menu">
<compodoc-menu mode="normal"></compodoc-menu>
</div>
<!-- START CONTENT -->
<div class="content component">
<div class="content-data">
<ol class="breadcrumb">
<li class="breadcrumb-item">Components</li>
<li class="breadcrumb-item"
>
Modal</li>
</ol>
<ul class="nav nav-tabs" role="tablist">
<li class="nav-item">
<a href="#info" class="nav-link active"
role="tab" id="info-tab" data-bs-toggle="tab" data-link="info">Info</a>
</li>
<li class="nav-item">
<a href="#source" class="nav-link"
role="tab" id="source-tab" data-bs-toggle="tab" data-link="source">Source</a>
</li>
<li class="nav-item">
<a href="#tree" class="nav-link"
role="tab" id="tree-tab" data-bs-toggle="tab" data-link="dom-tree">DOM Tree</a>
</li>
</ul>
<div class="tab-content">
<div class="tab-pane fade active in" id="info"><p class="comment">
<h3>File</h3>
</p>
<p class="comment">
<code>src/modal/modal.component.ts</code>
</p>
<p class="comment">
<h3>Description</h3>
</p>
<p class="comment">
<p>Component to create modals for presenting content.</p>
<p><a href="../../?path=/story/components-modal--basic">See demo</a></p>
<p>Using a modal in your application requires <code>cds-placeholder</code> which would generally be
placed near the end of your app component template (app.component.ts or app.component.html) as:</p>
<div><pre class="line-numbers"><code class="language-html"><cds-placeholder></cds-placeholder></code></pre></div><p>A more complete example for <code>Modal</code> is given as follows:</p>
<p>Example modal definition:</p>
<div><pre class="line-numbers"><code class="language-typescript">@Component({
selector: "app-sample-modal",
template: `
<cds-modal size="xl" (overlaySelected)="closeModal()">
<cds-modal-header (closeSelect)="closeModal()">Header text</cds-modal-header>
<section class="modal-body">
<h1>Sample modal works.</h1>
<button class="btn--icon-link" nPopover="Hello there" title="Popover title" placement="right" appendInline="true">
<svg cdsIcon="info" size="sm"></svg>
</button>
{{modalText}}
</section>
<cds-modal-footer><button cdsButton="primary" (click)="closeModal()">Close</button></cds-modal-footer>
</cds-modal>`,
styleUrls: ["./sample-modal.component.scss"]
})
export class SampleModal extends BaseModal {
modalText: string;
constructor(protected injector: Injector) {
super();
this.modalText = this.injector.get("modalText");
}
}</code></pre></div><p>Example of opening the modal:</p>
<div><pre class="line-numbers"><code class="language-typescript">@Component({
selector: "app-modal-demo",
template: `
<button cdsButton="primary" (click)="openModal('drill')">Drill-down modal</button>
<cds-placeholder></cds-placeholder>`
})
export class ModalDemo {
openModal() {
this.modalService.create({component: SampleModal, inputs: {modalText: "Hello universe."}});
}
}</code></pre></div>
</p>
<p class="comment">
<h3>Implements</h3>
</p>
<p class="comment">
<code>AfterViewInit</code>
<code>OnChanges</code>
<code>OnDestroy</code>
</p>
<section data-compodoc="block-metadata">
<h3>Metadata</h3>
<table class="table table-sm table-hover metadata">
<tbody>
<tr>
<td class="col-md-3">selector</td>
<td class="col-md-9"><code>cds-modal, ibm-modal</code></td>
</tr>
<tr>
<td class="col-md-3">template</td>
<td class="col-md-9"><pre class="line-numbers"><code class="language-html"><cds-overlay
[theme]="theme"
[open]="open"
(overlaySelect)="overlaySelected.emit()">
<div
class="cds--modal-container"
[ngClass]="{
'cds--modal-container--xs': size === 'xs',
'cds--modal-container--sm': size === 'sm',
'cds--modal-container--md': size === 'md',
'cds--modal-container--lg': size === 'lg'
}"
role="dialog"
aria-modal="true"
style="z-index:1;"
[attr.aria-label]="ariaLabel"
#modal>
<ng-content></ng-content>
<div
*ngIf="hasScrollingContent !== null ? hasScrollingContent : shouldShowScrollbar"
class="cds--modal-content--overflow-indicator">
</div>
</div>
</cds-overlay>
</code></pre></td>
</tr>
</tbody>
</table>
</section>
<section data-compodoc="block-index">
<h3 id="index">Index</h3>
<table class="table table-sm table-bordered index-table">
<tbody>
<tr>
<td class="col-md-4">
<h6><b>Properties</b></h6>
</td>
</tr>
<tr>
<td class="col-md-4">
<ul class="index-list">
<li>
<span class="modifier"></span>
<a href="#modal" >modal</a>
</li>
<li>
<span class="modifier">Public</span>
<a href="#modalService" >modalService</a>
</li>
<li>
<a href="#selectorPrimaryFocus" >selectorPrimaryFocus</a>
</li>
</ul>
</td>
</tr>
<tr>
<td class="col-md-4">
<h6><b>Methods</b></h6>
</td>
</tr>
<tr>
<td class="col-md-4">
<ul class="index-list">
<li>
<span class="modifier">Protected</span>
<a href="#focusInitialElement" >focusInitialElement</a>
</li>
<li>
<span class="modifier"></span>
<a href="#handleKeyboardEvent" >handleKeyboardEvent</a>
</li>
<li>
<a href="#ngAfterViewInit" >ngAfterViewInit</a>
</li>
<li>
<a href="#ngOnChanges" >ngOnChanges</a>
</li>
<li>
<a href="#ngOnDestroy" >ngOnDestroy</a>
</li>
</ul>
</td>
</tr>
<tr>
<td class="col-md-4">
<h6><b>Inputs</b></h6>
</td>
</tr>
<tr>
<td class="col-md-4">
<ul class="index-list">
<li>
<a href="#ariaLabel" >ariaLabel</a>
</li>
<li>
<a href="#hasScrollingContent" >hasScrollingContent</a>
</li>
<li>
<a href="#open" >open</a>
</li>
<li>
<a href="#size" >size</a>
</li>
<li>
<a href="#theme" >theme</a>
</li>
<li>
<a href="#trigger" >trigger</a>
</li>
</ul>
</td>
</tr>
<tr>
<td class="col-md-4">
<h6><b>Outputs</b></h6>
</td>
</tr>
<tr>
<td class="col-md-4">
<ul class="index-list">
<li>
<a href="#close" >close</a>
</li>
<li>
<a href="#overlaySelected" >overlaySelected</a>
</li>
</ul>
</td>
</tr>
<tr>
<td class="col-md-4">
<h6><b>HostListeners</b></h6>
</td>
</tr>
<tr>
<td class="col-md-4">
<ul class="index-list">
<li>
<a href="#keydown" >keydown</a>
</li>
</ul>
</td>
</tr>
<tr>
<td class="col-md-4">
<h6><b>Accessors</b></h6>
</td>
</tr>
<tr>
<td class="col-md-4">
<ul class="index-list">
<li>
<a href="#shouldShowScrollbar" >shouldShowScrollbar</a>
</li>
</ul>
</td>
</tr>
</tbody>
</table>
</section>
<section data-compodoc="block-constructor">
<h3 id="constructor">Constructor</h3>
<table class="table table-sm table-bordered">
<tbody>
<tr>
<td class="col-md-4">
<code>constructor(modalService: <a href="../injectables/BaseModalService.html" target="_self">BaseModalService</a>, document: Document, renderer: Renderer2)</code>
</td>
</tr>
<tr>
<td class="col-md-4">
<div class="io-line">Defined in <a href="" data-line="156" class="link-to-prism">src/modal/modal.component.ts:156</a></div>
</td>
</tr>
<tr>
<td class="col-md-4">
<div class="io-description"><p>Creates an instance of <code>Modal</code>.</p>
</div>
<div>
<b>Parameters :</b>
<table class="params">
<thead>
<tr>
<td>Name</td>
<td>Type</td>
<td>Optional</td>
</tr>
</thead>
<tbody>
<tr>
<td>modalService</td>
<td>
<code><a href="../injectables/BaseModalService.html" target="_self" >BaseModalService</a></code>
</td>
<td>
No
</td>
</tr>
<tr>
<td>document</td>
<td>
<code>Document</code>
</td>
<td>
No
</td>
</tr>
<tr>
<td>renderer</td>
<td>
<code>Renderer2</code>
</td>
<td>
No
</td>
</tr>
</tbody>
</table>
</div>
</td>
</tr>
</tbody>
</table>
</section>
<section data-compodoc="block-inputs">
<h3 id="inputs">Inputs</h3>
<table class="table table-sm table-bordered">
<tbody>
<tr>
<td class="col-md-4">
<a name="ariaLabel"></a>
<b>ariaLabel</b>
</td>
</tr>
<tr>
<td class="col-md-4">
<i>Type : </i> <code><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/string" target="_blank" >string</a></code>
</td>
</tr>
<tr>
<td class="col-md-4">
<i>Default value : </i><code>"default"</code>
</td>
</tr>
<tr>
<td class="col-md-2" colspan="2">
<div class="io-line">Defined in <a href="" data-line="120" class="link-to-prism">src/modal/modal.component.ts:120</a></div>
</td>
</tr>
<tr>
<td class="col-md-4">
<div class="io-description"><p>Label for the modal.</p>
</div>
</td>
</tr>
</tbody>
</table>
<table class="table table-sm table-bordered">
<tbody>
<tr>
<td class="col-md-4">
<a name="hasScrollingContent"></a>
<b>hasScrollingContent</b>
</td>
</tr>
<tr>
<td class="col-md-4">
<i>Type : </i> <code><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/boolean" target="_blank" >boolean</a></code>
</td>
</tr>
<tr>
<td class="col-md-4">
<i>Default value : </i><code>null</code>
</td>
</tr>
<tr>
<td class="col-md-2" colspan="2">
<div class="io-line">Defined in <a href="" data-line="138" class="link-to-prism">src/modal/modal.component.ts:138</a></div>
</td>
</tr>
<tr>
<td class="col-md-4">
<div class="io-description"><p>Specify whether the modal contains scrolling content. This property overrides the automatic
detection of the existence of scrolling content. Set this property to <code>true</code> to force
overflow indicator to show up or to <code>false</code> to force overflow indicator to disappear.
It is set to <code>null</code> by default which indicates not to override automatic detection.</p>
</div>
</td>
</tr>
</tbody>
</table>
<table class="table table-sm table-bordered">
<tbody>
<tr>
<td class="col-md-4">
<a name="open"></a>
<b>open</b>
</td>
</tr>
<tr>
<td class="col-md-4">
<i>Type : </i> <code><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/boolean" target="_blank" >boolean</a></code>
</td>
</tr>
<tr>
<td class="col-md-4">
<i>Default value : </i><code>false</code>
</td>
</tr>
<tr>
<td class="col-md-2" colspan="2">
<div class="io-line">Defined in <a href="" data-line="125" class="link-to-prism">src/modal/modal.component.ts:125</a></div>
</td>
</tr>
<tr>
<td class="col-md-4">
<div class="io-description"><p>Controls the visibility of the modal when used directly in a template</p>
</div>
</td>
</tr>
</tbody>
</table>
<table class="table table-sm table-bordered">
<tbody>
<tr>
<td class="col-md-4">
<a name="size"></a>
<b>size</b>
</td>
</tr>
<tr>
<td class="col-md-4">
<i>Type : </i> <code>"xs" | "sm" | "md" | "lg"</code>
</td>
</tr>
<tr>
<td class="col-md-4">
<i>Default value : </i><code>"md"</code>
</td>
</tr>
<tr>
<td class="col-md-2" colspan="2">
<div class="io-line">Defined in <a href="" data-line="111" class="link-to-prism">src/modal/modal.component.ts:111</a></div>
</td>
</tr>
<tr>
<td class="col-md-4">
<div class="io-description"><p>Size of the modal to display.</p>
</div>
</td>
</tr>
</tbody>
</table>
<table class="table table-sm table-bordered">
<tbody>
<tr>
<td class="col-md-4">
<a name="theme"></a>
<b>theme</b>
</td>
</tr>
<tr>
<td class="col-md-4">
<i>Type : </i> <code>"default" | "danger"</code>
</td>
</tr>
<tr>
<td class="col-md-4">
<i>Default value : </i><code>"default"</code>
</td>
</tr>
<tr>
<td class="col-md-2" colspan="2">
<div class="io-line">Defined in <a href="" data-line="115" class="link-to-prism">src/modal/modal.component.ts:115</a></div>
</td>
</tr>
<tr>
<td class="col-md-4">
<div class="io-description"><p>Classification of the modal.</p>
</div>
</td>
</tr>
</tbody>
</table>
<table class="table table-sm table-bordered">
<tbody>
<tr>
<td class="col-md-4">
<a name="trigger"></a>
<b>trigger</b>
</td>
</tr>
<tr>
<td class="col-md-4">
<i>Type : </i> <code>HTMLElement</code>
</td>
</tr>
<tr>
<td class="col-md-2" colspan="2">
<div class="io-line">Defined in <a href="" data-line="130" class="link-to-prism">src/modal/modal.component.ts:130</a></div>
</td>
</tr>
<tr>
<td class="col-md-4">
<div class="io-description"><p>The element that triggers the modal, which should receive focus when the modal closes</p>
</div>
</td>
</tr>
</tbody>
</table>
</section>
<section data-compodoc="block-outputs">
<h3 id="outputs">Outputs</h3>
<table class="table table-sm table-bordered">
<tbody>
<tr>
<td class="col-md-4">
<a name="close"></a>
<b>close</b>
</td>
</tr>
<tr>
<td class="col-md-4">
<i>Type : </i> <code>EventEmitter</code>
</td>
</tr>
<tr>
<td class="col-md-2" colspan="2">
<div class="io-line">Defined in <a href="" data-line="147" class="link-to-prism">src/modal/modal.component.ts:147</a></div>
</td>
</tr>
<tr>
<td class="col-md-4">
<div class="io-description"><p>To emit the closing event of the modal window.</p>
</div>
</td>
</tr>
</tbody>
</table>
<table class="table table-sm table-bordered">
<tbody>
<tr>
<td class="col-md-4">
<a name="overlaySelected"></a>
<b>overlaySelected</b>
</td>
</tr>
<tr>
<td class="col-md-4">
<i>Type : </i> <code>EventEmitter</code>
</td>
</tr>
<tr>
<td class="col-md-2" colspan="2">
<div class="io-line">Defined in <a href="" data-line="143" class="link-to-prism">src/modal/modal.component.ts:143</a></div>
</td>
</tr>
<tr>
<td class="col-md-4">
<div class="io-description"><p>Emits event when click occurs within <code>n-overlay</code> element. This is to track click events occurring outside bounds of the <code>Modal</code> object.</p>
</div>
</td>
</tr>
</tbody>
</table>
</section>
<section data-compodoc="block-methods">
<h3>HostListeners</h3> <table class="table table-sm table-bordered">
<tbody>
<tr>
<td class="col-md-4">
<a name="keydown"></a>
<span class="name">
<span ><b>keydown</b></span>
<a href="#keydown"><span class="icon ion-ios-link"></span></a>
</span>
</td>
</tr>
<tr>
<td class="col-md-4">
<i>Arguments : </i><code>'$event' </code>
</td>
</tr>
<tr>
<td class="col-md-4">
<code>keydown(event: KeyboardEvent)</code>
</td>
</tr>
<tr>
<td class="col-md-4">
<div class="io-line">Defined in <a href="" data-line="195"
class="link-to-prism">src/modal/modal.component.ts:195</a></div>
</td>
</tr>
<tr>
<td class="col-md-4">
<div class="io-description"><p>Handle keyboard events to close modal and tab through the content within the modal.</p>
</div>
</td>
</tr>
</tbody>
</table>
</section>
<section data-compodoc="block-methods">
<h3 id="methods">
Methods
</h3>
<table class="table table-sm table-bordered">
<tbody>
<tr>
<td class="col-md-4">
<a name="focusInitialElement"></a>
<span class="name">
<span class="modifier">Protected</span>
<span ><b>focusInitialElement</b></span>
<a href="#focusInitialElement"><span class="icon ion-ios-link"></span></a>
</span>
</td>
</tr>
<tr>
<td class="col-md-4">
<span class="modifier-icon icon ion-ios-reset"></span>
<code>focusInitialElement()</code>
</td>
</tr>
<tr>
<td class="col-md-4">
<div class="io-line">Defined in <a href="" data-line="237"
class="link-to-prism">src/modal/modal.component.ts:237</a></div>
</td>
</tr>
<tr>
<td class="col-md-4">
<div class="io-description">
<b>Returns : </b> <code><a href="https://www.typescriptlang.org/docs/handbook/basic-types.html" target="_blank" >void</a></code>
</div>
</td>
</tr>
</tbody>
</table>
<table class="table table-sm table-bordered">
<tbody>
<tr>
<td class="col-md-4">
<a name="handleKeyboardEvent"></a>
<span class="name">
<span class="modifier"></span>
<span ><b>handleKeyboardEvent</b></span>
<a href="#handleKeyboardEvent"><span class="icon ion-ios-link"></span></a>
</span>
</td>
</tr>
<tr>
<td class="col-md-4">
<span class="modifier-icon icon ion-ios-reset"></span>
<code>handleKeyboardEvent(event: KeyboardEvent)</code>
</td>
</tr>
<tr>
<td class="col-md-4">
<b>Decorators : </b>
<br />
<code>@HostListener('keydown', ['$event'])<br /></code>
</td>
</tr>
<tr>
<td class="col-md-4">
<div class="io-line">Defined in <a href="" data-line="195"
class="link-to-prism">src/modal/modal.component.ts:195</a></div>
</td>
</tr>
<tr>
<td class="col-md-4">
<div class="io-description"><p>Handle keyboard events to close modal and tab through the content within the modal.</p>
</div>
<div class="io-description">
<b>Parameters :</b>
<table class="params">
<thead>
<tr>
<td>Name</td>
<td>Type</td>
<td>Optional</td>
</tr>
</thead>
<tbody>
<tr>
<td>event</td>
<td>
<code>KeyboardEvent</code>
</td>
<td>
No
</td>
</tr>
</tbody>
</table>
</div>
<div>
</div>
<div class="io-description">
<b>Returns : </b> <code><a href="https://www.typescriptlang.org/docs/handbook/basic-types.html" target="_blank" >void</a></code>
</div>
<div class="io-description">
</div>
</td>
</tr>
</tbody>
</table>
<table class="table table-sm table-bordered">
<tbody>
<tr>
<td class="col-md-4">
<a name="ngAfterViewInit"></a>
<span class="name">
<span ><b>ngAfterViewInit</b></span>
<a href="#ngAfterViewInit"><span class="icon ion-ios-link"></span></a>
</span>
</td>
</tr>
<tr>
<td class="col-md-4">
<code>ngAfterViewInit()</code>
</td>
</tr>
<tr>
<td class="col-md-4">
<div class="io-line">Defined in <a href="" data-line="187"
class="link-to-prism">src/modal/modal.component.ts:187</a></div>
</td>
</tr>
<tr>
<td class="col-md-4">
<div class="io-description"><p>Set document focus to be on the modal component after it is initialized.</p>
</div>
<div class="io-description">
<b>Returns : </b> <code><a href="https://www.typescriptlang.org/docs/handbook/basic-types.html" target="_blank" >void</a></code>
</div>
</td>
</tr>
</tbody>
</table>
<table class="table table-sm table-bordered">
<tbody>
<tr>
<td class="col-md-4">
<a name="ngOnChanges"></a>
<span class="name">
<span ><b>ngOnChanges</b></span>
<a href="#ngOnChanges"><span class="icon ion-ios-link"></span></a>
</span>
</td>
</tr>
<tr>
<td class="col-md-4">
<code>ngOnChanges(undefined: SimpleChanges)</code>
</td>
</tr>
<tr>
<td class="col-md-4">
<div class="io-line">Defined in <a href="" data-line="167"
class="link-to-prism">src/modal/modal.component.ts:167</a></div>
</td>
</tr>
<tr>
<td class="col-md-4">
<div class="io-description">
<b>Parameters :</b>
<table class="params">
<thead>
<tr>
<td>Name</td>
<td>Type</td>
<td>Optional</td>
</tr>
</thead>
<tbody>
<tr>
<td>
<code>SimpleChanges</code>
</td>
<td>
No
</td>
</tr>
</tbody>
</table>
</div>
<div>
</div>
<div class="io-description">
<b>Returns : </b> <code><a href="https://www.typescriptlang.org/docs/handbook/basic-types.html" target="_blank" >void</a></code>
</div>
<div class="io-description">
</div>
</td>
</tr>
</tbody>
</table>
<table class="table table-sm table-bordered">
<tbody>
<tr>
<td class="col-md-4">
<a name="ngOnDestroy"></a>
<span class="name">
<span ><b>ngOnDestroy</b></span>
<a href="#ngOnDestroy"><span class="icon ion-ios-link"></span></a>
</span>
</td>
</tr>
<tr>
<td class="col-md-4">
<code>ngOnDestroy()</code>
</td>
</tr>
<tr>
<td class="col-md-4">
<div class="io-line">Defined in <a href="" data-line="233"
class="link-to-prism">src/modal/modal.component.ts:233</a></div>
</td>
</tr>
<tr>
<td class="col-md-4">
<div class="io-description">
<b>Returns : </b> <code><a href="https://www.typescriptlang.org/docs/handbook/basic-types.html" target="_blank" >void</a></code>
</div>
</td>
</tr>
</tbody>
</table>
</section>
<section data-compodoc="block-properties">
<h3 id="inputs">
Properties
</h3>
<table class="table table-sm table-bordered">
<tbody>
<tr>
<td class="col-md-4">
<a name="modal"></a>
<span class="name">
<span class="modifier"></span>
<span ><b>modal</b></span>
<a href="#modal"><span class="icon ion-ios-link"></span></a>
</span>
</td>
</tr>
<tr>
<td class="col-md-4">
<i>Type : </i> <code>ElementRef</code>
</td>
</tr>
<tr>
<td class="col-md-4">
<b>Decorators : </b>
<br />
<code>
@ViewChild('modal', {static: true})<br />
</code>
</td>
</tr>
<tr>
<td class="col-md-4">
<div class="io-line">Defined in <a href="" data-line="151" class="link-to-prism">src/modal/modal.component.ts:151</a></div>
</td>
</tr>
<tr>
<td class="col-md-4">
<div class="io-description"><p>Maintains a reference to the view DOM element of the <code>Modal</code>.</p>
</div>
</td>
</tr>
</tbody>
</table>
<table class="table table-sm table-bordered">
<tbody>
<tr>
<td class="col-md-4">
<a name="modalService"></a>
<span class="name">
<span class="modifier">Public</span>
<span ><b>modalService</b></span>
<a href="#modalService"><span class="icon ion-ios-link"></span></a>
</span>
</td>
</tr>
<tr>
<td class="col-md-4">
<i>Type : </i> <code><a href="../injectables/BaseModalService.html" target="_self" >BaseModalService</a></code>
</td>
</tr>
<tr>
<td class="col-md-4">
<div class="io-line">Defined in <a href="" data-line="162" class="link-to-prism">src/modal/modal.component.ts:162</a></div>
</td>
</tr>
</tbody>
</table>
<table class="table table-sm table-bordered">
<tbody>
<tr>
<td class="col-md-4">
<a name="selectorPrimaryFocus"></a>
<span class="name">
<span ><b>selectorPrimaryFocus</b></span>
<a href="#selectorPrimaryFocus"><span class="icon ion-ios-link"></span></a>
</span>
</td>
</tr>
<tr>
<td class="col-md-4">
<i>Type : </i> <code><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/string" target="_blank" >string</a></code>
</td>
</tr>
<tr>
<td class="col-md-4">
<i>Default value : </i><code>"[modal-primary-focus]"</code>
</td>
</tr>
<tr>
<td class="col-md-4">
<div class="io-line">Defined in <a href="" data-line="156" class="link-to-prism">src/modal/modal.component.ts:156</a></div>
</td>
</tr>
<tr>
<td class="col-md-4">
<div class="io-description"><p>An element should have 'modal-primary-focus' as an attribute to receive initial focus within the <code>Modal</code> component.</p>
</div>
</td>
</tr>
</tbody>
</table>
</section>
<section data-compodoc="block-accessors">
<h3 id="accessors">
Accessors
</h3>
<table class="table table-sm table-bordered">
<tbody>
<tr>
<td class="col-md-4">
<a name="shouldShowScrollbar"></a>
<span class="name"><b>shouldShowScrollbar</b><a href="#shouldShowScrollbar"><span class="icon ion-ios-link"></span></a></span>
</td>
</tr>
<tr>
<td class="col-md-4">
<span class="accessor"><b>get</b><code>shouldShowScrollbar()</code></span>
</td>
</tr>
<tr>
<td class="col-md-4">
<div class="io-line">Defined in <a href="" data-line="220" class="link-to-prism">src/modal/modal.component.ts:220</a></div>
</td>
</tr>
<tr>
<td class="col-md-4">
<div class="io-description"><p>This detects whether or not the modal contains scrolling content.</p>
<p>To force trigger a detection (ie. on window resize), change or reset the value of the modal content.</p>
<p>Use the <code>hasScrollingContent</code> input to manually override the overflow indicator.</p>
</div>
</td>
</tr>
</tbody>
</table>
</section>
</div>
<div class="tab-pane fade tab-source-code" id="source">
<pre class="line-numbers compodoc-sourcecode"><code class="language-typescript">import {
AfterViewInit,
Component,
EventEmitter,
HostListener,
Input,
Output,
ElementRef,
ViewChild,
SimpleChanges,
OnChanges,
Renderer2,
Inject,
OnDestroy
} from "@angular/core";
import { DOCUMENT } from "@angular/common";
import { cycleTabs, getFocusElementList } from "carbon-components-angular/common";
import { BaseModalService } from "./base-modal.service";
/**
* Component to create modals for presenting content.
*
* [See demo](../../?path=/story/components-modal--basic)
*
* Using a modal in your application requires `cds-placeholder` which would generally be
* placed near the end of your app component template (app.component.ts or app.component.html) as:
*
```html
<cds-placeholder></cds-placeholder>
```
*
* A more complete example for `Modal` is given as follows:
*
* Example modal definition:
*
```typescript
@Component({
selector: "app-sample-modal",
template: `
<cds-modal size="xl" (overlaySelected)="closeModal()">
<cds-modal-header (closeSelect)="closeModal()">Header text</cds-modal-header>
<section class="modal-body">
<h1>Sample modal works.</h1>
<button class="btn--icon-link" nPopover="Hello there" title="Popover title" placement="right" appendInline="true">
<svg cdsIcon="info" size="sm"></svg>
</button>
{{modalText}}
</section>
<cds-modal-footer><button cdsButton="primary" (click)="closeModal()">Close</button></cds-modal-footer>
</cds-modal>`,
styleUrls: ["./sample-modal.component.scss"]
})
export class SampleModal extends BaseModal {
modalText: string;
constructor(protected injector: Injector) {
super();
this.modalText = this.injector.get("modalText");
}
}
```
*
* Example of opening the modal:
*
```typescript
@Component({
selector: "app-modal-demo",
template: `
<button cdsButton="primary" (click)="openModal('drill')">Drill-down modal</button>
<cds-placeholder></cds-placeholder>`
})
export class ModalDemo {
openModal() {
this.modalService.create({component: SampleModal, inputs: {modalText: "Hello universe."}});
}
}
```
*/
@Component({
selector: "cds-modal, ibm-modal",
template: `
<cds-overlay
[theme]="theme"
[open]="open"
(overlaySelect)="overlaySelected.emit()">
<div
class="cds--modal-container"
[ngClass]="{
'cds--modal-container--xs': size === 'xs',
'cds--modal-container--sm': size === 'sm',
'cds--modal-container--md': size === 'md',
'cds--modal-container--lg': size === 'lg'
}"
role="dialog"
aria-modal="true"
style="z-index:1;"
[attr.aria-label]="ariaLabel"
#modal>
<ng-content></ng-content>
<div
*ngIf="hasScrollingContent !== null ? hasScrollingContent : shouldShowScrollbar"
class="cds--modal-content--overflow-indicator">
</div>
</div>
</cds-overlay>
`
})
export class Modal implements AfterViewInit, OnChanges, OnDestroy {
/**
* Size of the modal to display.
*/
@Input() size: "xs" | "sm"| "md" | "lg" = "md";
/**
* Classification of the modal.
*/
@Input() theme: "default" | "danger" = "default";
/**
* Label for the modal.
*/
@Input() ariaLabel = "default";
/**
* Controls the visibility of the modal when used directly in a template
*/
@Input() open = false;
/**
* The element that triggers the modal, which should receive focus when the modal closes
*/
@Input() trigger: HTMLElement;
/**
* Specify whether the modal contains scrolling content. This property overrides the automatic
* detection of the existence of scrolling content. Set this property to `true` to force
* overflow indicator to show up or to `false` to force overflow indicator to disappear.
* It is set to `null` by default which indicates not to override automatic detection.
*/
@Input() hasScrollingContent: boolean = null;
/**
* Emits event when click occurs within `n-overlay` element. This is to track click events occurring outside bounds of the `Modal` object.
*/
@Output() overlaySelected = new EventEmitter();
/**
* To emit the closing event of the modal window.
*/
@Output() close = new EventEmitter();
/**
* Maintains a reference to the view DOM element of the `Modal`.
*/
@ViewChild("modal", { static: true }) modal: ElementRef;
/**
* An element should have 'modal-primary-focus' as an attribute to receive initial focus within the `Modal` component.
*/
selectorPrimaryFocus = "[modal-primary-focus]";
/**
* Creates an instance of `Modal`.
*/
constructor(
public modalService: BaseModalService,
@Inject(DOCUMENT) private document: Document,
private renderer: Renderer2
) { }
ngOnChanges({ open }: SimpleChanges) {
if (open) {
if (open.currentValue) {
// `100` is just enough time to allow the modal
// to become visible, so that we can set focus
setTimeout(() => this.focusInitialElement(), 100);
// Prevent scrolling on open
this.renderer.addClass(this.document.body, "cds--body--with-modal-open");
} else if (!open.currentValue) {
// Enable scrolling on close
this.renderer.removeClass(this.document.body, "cds--body--with-modal-open");
} else if (this.trigger) {
this.trigger.focus();
}
}
}
/**
* Set document focus to be on the modal component after it is initialized.
*/
ngAfterViewInit() {
this.focusInitialElement();
}
/**
* Handle keyboard events to close modal and tab through the content within the modal.
*/
@HostListener("keydown", ["$event"])
handleKeyboardEvent(event: KeyboardEvent) {
switch (event.key) {
case "Escape": {
event.stopImmediatePropagation(); // prevents events being fired for multiple modals if more than 2 open
// Manually close modal
this.open = false;
this.close.emit();
this.modalService.destroy(); // destroy top (latest) modal
break;
}
case "Tab": {
cycleTabs(event, this.modal.nativeElement);
break;
}
}
}
/**
* This detects whether or not the modal contains scrolling content.
*
* To force trigger a detection (ie. on window resize), change or reset the value of the modal content.
*
* Use the `hasScrollingContent` input to manually override the overflow indicator.
*/
get shouldShowScrollbar() {
const modalContent = this.modal ? this.modal.nativeElement.querySelector(".cds--modal-content") : null;
if (modalContent) {
// get rounded value