bottom-sheet.mjs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. import * as i2 from '@angular/cdk/dialog';
  2. import { CdkDialogContainer, DialogModule, Dialog } from '@angular/cdk/dialog';
  3. import * as i5 from '@angular/cdk/portal';
  4. import { PortalModule } from '@angular/cdk/portal';
  5. import * as i0 from '@angular/core';
  6. import { EventEmitter, Component, ChangeDetectionStrategy, ViewEncapsulation, Optional, Inject, NgModule, InjectionToken, Injectable, SkipSelf } from '@angular/core';
  7. import { AnimationDurations, AnimationCurves, MatCommonModule } from '@angular/material/core';
  8. import * as i1 from '@angular/cdk/a11y';
  9. import * as i4 from '@angular/cdk/layout';
  10. import { Breakpoints } from '@angular/cdk/layout';
  11. import * as i3 from '@angular/cdk/overlay';
  12. import { DOCUMENT } from '@angular/common';
  13. import { trigger, state, style, transition, group, animate, query, animateChild } from '@angular/animations';
  14. import { ESCAPE, hasModifierKey } from '@angular/cdk/keycodes';
  15. import { Subject, merge } from 'rxjs';
  16. import { filter, take } from 'rxjs/operators';
  17. /** Animations used by the Material bottom sheet. */
  18. const matBottomSheetAnimations = {
  19. /** Animation that shows and hides a bottom sheet. */
  20. bottomSheetState: trigger('state', [
  21. state('void, hidden', style({ transform: 'translateY(100%)' })),
  22. state('visible', style({ transform: 'translateY(0%)' })),
  23. transition('visible => void, visible => hidden', group([
  24. animate(`${AnimationDurations.COMPLEX} ${AnimationCurves.ACCELERATION_CURVE}`),
  25. query('@*', animateChild(), { optional: true }),
  26. ])),
  27. transition('void => visible', group([
  28. animate(`${AnimationDurations.EXITING} ${AnimationCurves.DECELERATION_CURVE}`),
  29. query('@*', animateChild(), { optional: true }),
  30. ])),
  31. ]),
  32. };
  33. /**
  34. * Internal component that wraps user-provided bottom sheet content.
  35. * @docs-private
  36. */
  37. class MatBottomSheetContainer extends CdkDialogContainer {
  38. constructor(elementRef, focusTrapFactory, document, config, checker, ngZone, overlayRef, breakpointObserver, _changeDetectorRef, focusMonitor) {
  39. super(elementRef, focusTrapFactory, document, config, checker, ngZone, overlayRef, focusMonitor);
  40. this._changeDetectorRef = _changeDetectorRef;
  41. /** The state of the bottom sheet animations. */
  42. this._animationState = 'void';
  43. /** Emits whenever the state of the animation changes. */
  44. this._animationStateChanged = new EventEmitter();
  45. this._breakpointSubscription = breakpointObserver
  46. .observe([Breakpoints.Medium, Breakpoints.Large, Breakpoints.XLarge])
  47. .subscribe(() => {
  48. this._toggleClass('mat-bottom-sheet-container-medium', breakpointObserver.isMatched(Breakpoints.Medium));
  49. this._toggleClass('mat-bottom-sheet-container-large', breakpointObserver.isMatched(Breakpoints.Large));
  50. this._toggleClass('mat-bottom-sheet-container-xlarge', breakpointObserver.isMatched(Breakpoints.XLarge));
  51. });
  52. }
  53. /** Begin animation of bottom sheet entrance into view. */
  54. enter() {
  55. if (!this._destroyed) {
  56. this._animationState = 'visible';
  57. this._changeDetectorRef.detectChanges();
  58. }
  59. }
  60. /** Begin animation of the bottom sheet exiting from view. */
  61. exit() {
  62. if (!this._destroyed) {
  63. this._animationState = 'hidden';
  64. this._changeDetectorRef.markForCheck();
  65. }
  66. }
  67. ngOnDestroy() {
  68. super.ngOnDestroy();
  69. this._breakpointSubscription.unsubscribe();
  70. this._destroyed = true;
  71. }
  72. _onAnimationDone(event) {
  73. if (event.toState === 'visible') {
  74. this._trapFocus();
  75. }
  76. this._animationStateChanged.emit(event);
  77. }
  78. _onAnimationStart(event) {
  79. this._animationStateChanged.emit(event);
  80. }
  81. _captureInitialFocus() { }
  82. _toggleClass(cssClass, add) {
  83. this._elementRef.nativeElement.classList.toggle(cssClass, add);
  84. }
  85. static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.0.0", ngImport: i0, type: MatBottomSheetContainer, deps: [{ token: i0.ElementRef }, { token: i1.FocusTrapFactory }, { token: DOCUMENT, optional: true }, { token: i2.DialogConfig }, { token: i1.InteractivityChecker }, { token: i0.NgZone }, { token: i3.OverlayRef }, { token: i4.BreakpointObserver }, { token: i0.ChangeDetectorRef }, { token: i1.FocusMonitor }], target: i0.ɵɵFactoryTarget.Component }); }
  86. static { this.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "16.0.0", type: MatBottomSheetContainer, selector: "mat-bottom-sheet-container", host: { attributes: { "tabindex": "-1" }, listeners: { "@state.start": "_onAnimationStart($event)", "@state.done": "_onAnimationDone($event)" }, properties: { "attr.role": "_config.role", "attr.aria-modal": "_config.ariaModal", "attr.aria-label": "_config.ariaLabel", "@state": "_animationState" }, classAttribute: "mat-bottom-sheet-container" }, usesInheritance: true, ngImport: i0, template: "<ng-template cdkPortalOutlet></ng-template>\r\n", styles: [".mat-bottom-sheet-container{padding:8px 16px;min-width:100vw;box-sizing:border-box;display:block;outline:0;max-height:80vh;overflow:auto}.cdk-high-contrast-active .mat-bottom-sheet-container{outline:1px solid}.mat-bottom-sheet-container-xlarge,.mat-bottom-sheet-container-large,.mat-bottom-sheet-container-medium{border-top-left-radius:4px;border-top-right-radius:4px}.mat-bottom-sheet-container-medium{min-width:384px;max-width:calc(100vw - 128px)}.mat-bottom-sheet-container-large{min-width:512px;max-width:calc(100vw - 256px)}.mat-bottom-sheet-container-xlarge{min-width:576px;max-width:calc(100vw - 384px)}"], dependencies: [{ kind: "directive", type: i5.CdkPortalOutlet, selector: "[cdkPortalOutlet]", inputs: ["cdkPortalOutlet"], outputs: ["attached"], exportAs: ["cdkPortalOutlet"] }], animations: [matBottomSheetAnimations.bottomSheetState], changeDetection: i0.ChangeDetectionStrategy.Default, encapsulation: i0.ViewEncapsulation.None }); }
  87. }
  88. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.0.0", ngImport: i0, type: MatBottomSheetContainer, decorators: [{
  89. type: Component,
  90. args: [{ selector: 'mat-bottom-sheet-container', changeDetection: ChangeDetectionStrategy.Default, encapsulation: ViewEncapsulation.None, animations: [matBottomSheetAnimations.bottomSheetState], host: {
  91. 'class': 'mat-bottom-sheet-container',
  92. 'tabindex': '-1',
  93. '[attr.role]': '_config.role',
  94. '[attr.aria-modal]': '_config.ariaModal',
  95. '[attr.aria-label]': '_config.ariaLabel',
  96. '[@state]': '_animationState',
  97. '(@state.start)': '_onAnimationStart($event)',
  98. '(@state.done)': '_onAnimationDone($event)',
  99. }, template: "<ng-template cdkPortalOutlet></ng-template>\r\n", styles: [".mat-bottom-sheet-container{padding:8px 16px;min-width:100vw;box-sizing:border-box;display:block;outline:0;max-height:80vh;overflow:auto}.cdk-high-contrast-active .mat-bottom-sheet-container{outline:1px solid}.mat-bottom-sheet-container-xlarge,.mat-bottom-sheet-container-large,.mat-bottom-sheet-container-medium{border-top-left-radius:4px;border-top-right-radius:4px}.mat-bottom-sheet-container-medium{min-width:384px;max-width:calc(100vw - 128px)}.mat-bottom-sheet-container-large{min-width:512px;max-width:calc(100vw - 256px)}.mat-bottom-sheet-container-xlarge{min-width:576px;max-width:calc(100vw - 384px)}"] }]
  100. }], ctorParameters: function () { return [{ type: i0.ElementRef }, { type: i1.FocusTrapFactory }, { type: undefined, decorators: [{
  101. type: Optional
  102. }, {
  103. type: Inject,
  104. args: [DOCUMENT]
  105. }] }, { type: i2.DialogConfig }, { type: i1.InteractivityChecker }, { type: i0.NgZone }, { type: i3.OverlayRef }, { type: i4.BreakpointObserver }, { type: i0.ChangeDetectorRef }, { type: i1.FocusMonitor }]; } });
  106. class MatBottomSheetModule {
  107. static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.0.0", ngImport: i0, type: MatBottomSheetModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule }); }
  108. static { this.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "16.0.0", ngImport: i0, type: MatBottomSheetModule, declarations: [MatBottomSheetContainer], imports: [DialogModule, MatCommonModule, PortalModule], exports: [MatBottomSheetContainer, MatCommonModule] }); }
  109. static { this.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "16.0.0", ngImport: i0, type: MatBottomSheetModule, imports: [DialogModule, MatCommonModule, PortalModule, MatCommonModule] }); }
  110. }
  111. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.0.0", ngImport: i0, type: MatBottomSheetModule, decorators: [{
  112. type: NgModule,
  113. args: [{
  114. imports: [DialogModule, MatCommonModule, PortalModule],
  115. exports: [MatBottomSheetContainer, MatCommonModule],
  116. declarations: [MatBottomSheetContainer],
  117. }]
  118. }] });
  119. /** Injection token that can be used to access the data that was passed in to a bottom sheet. */
  120. const MAT_BOTTOM_SHEET_DATA = new InjectionToken('MatBottomSheetData');
  121. /**
  122. * Configuration used when opening a bottom sheet.
  123. */
  124. class MatBottomSheetConfig {
  125. constructor() {
  126. /** Data being injected into the child component. */
  127. this.data = null;
  128. /** Whether the bottom sheet has a backdrop. */
  129. this.hasBackdrop = true;
  130. /** Whether the user can use escape or clicking outside to close the bottom sheet. */
  131. this.disableClose = false;
  132. /** Aria label to assign to the bottom sheet element. */
  133. this.ariaLabel = null;
  134. /** Whether this is a modal bottom sheet. Used to set the `aria-modal` attribute. */
  135. this.ariaModal = true;
  136. /**
  137. * Whether the bottom sheet should close when the user goes backwards/forwards in history.
  138. * Note that this usually doesn't include clicking on links (unless the user is using
  139. * the `HashLocationStrategy`).
  140. */
  141. this.closeOnNavigation = true;
  142. // Note that this is set to 'dialog' by default, because while the a11y recommendations
  143. // are to focus the first focusable element, doing so prevents screen readers from reading out the
  144. // rest of the bottom sheet content.
  145. /**
  146. * Where the bottom sheet should focus on open.
  147. * @breaking-change 14.0.0 Remove boolean option from autoFocus. Use string or
  148. * AutoFocusTarget instead.
  149. */
  150. this.autoFocus = 'dialog';
  151. /**
  152. * Whether the bottom sheet should restore focus to the
  153. * previously-focused element, after it's closed.
  154. */
  155. this.restoreFocus = true;
  156. }
  157. }
  158. /**
  159. * Reference to a bottom sheet dispatched from the bottom sheet service.
  160. */
  161. class MatBottomSheetRef {
  162. /** Instance of the component making up the content of the bottom sheet. */
  163. get instance() {
  164. return this._ref.componentInstance;
  165. }
  166. constructor(_ref, config, containerInstance) {
  167. this._ref = _ref;
  168. /** Subject for notifying the user that the bottom sheet has opened and appeared. */
  169. this._afterOpened = new Subject();
  170. this.containerInstance = containerInstance;
  171. this.disableClose = config.disableClose;
  172. // Emit when opening animation completes
  173. containerInstance._animationStateChanged
  174. .pipe(filter(event => event.phaseName === 'done' && event.toState === 'visible'), take(1))
  175. .subscribe(() => {
  176. this._afterOpened.next();
  177. this._afterOpened.complete();
  178. });
  179. // Dispose overlay when closing animation is complete
  180. containerInstance._animationStateChanged
  181. .pipe(filter(event => event.phaseName === 'done' && event.toState === 'hidden'), take(1))
  182. .subscribe(() => {
  183. clearTimeout(this._closeFallbackTimeout);
  184. this._ref.close(this._result);
  185. });
  186. _ref.overlayRef.detachments().subscribe(() => {
  187. this._ref.close(this._result);
  188. });
  189. merge(this.backdropClick(), this.keydownEvents().pipe(filter(event => event.keyCode === ESCAPE))).subscribe(event => {
  190. if (!this.disableClose &&
  191. (event.type !== 'keydown' || !hasModifierKey(event))) {
  192. event.preventDefault();
  193. this.dismiss();
  194. }
  195. });
  196. }
  197. /**
  198. * Dismisses the bottom sheet.
  199. * @param result Data to be passed back to the bottom sheet opener.
  200. */
  201. dismiss(result) {
  202. if (!this.containerInstance) {
  203. return;
  204. }
  205. // Transition the backdrop in parallel to the bottom sheet.
  206. this.containerInstance._animationStateChanged
  207. .pipe(filter(event => event.phaseName === 'start'), take(1))
  208. .subscribe(event => {
  209. // The logic that disposes of the overlay depends on the exit animation completing, however
  210. // it isn't guaranteed if the parent view is destroyed while it's running. Add a fallback
  211. // timeout which will clean everything up if the animation hasn't fired within the specified
  212. // amount of time plus 100ms. We don't need to run this outside the NgZone, because for the
  213. // vast majority of cases the timeout will have been cleared before it has fired.
  214. this._closeFallbackTimeout = setTimeout(() => {
  215. this._ref.close(this._result);
  216. }, event.totalTime + 100);
  217. this._ref.overlayRef.detachBackdrop();
  218. });
  219. this._result = result;
  220. this.containerInstance.exit();
  221. this.containerInstance = null;
  222. }
  223. /** Gets an observable that is notified when the bottom sheet is finished closing. */
  224. afterDismissed() {
  225. return this._ref.closed;
  226. }
  227. /** Gets an observable that is notified when the bottom sheet has opened and appeared. */
  228. afterOpened() {
  229. return this._afterOpened;
  230. }
  231. /**
  232. * Gets an observable that emits when the overlay's backdrop has been clicked.
  233. */
  234. backdropClick() {
  235. return this._ref.backdropClick;
  236. }
  237. /**
  238. * Gets an observable that emits when keydown events are targeted on the overlay.
  239. */
  240. keydownEvents() {
  241. return this._ref.keydownEvents;
  242. }
  243. }
  244. /** Injection token that can be used to specify default bottom sheet options. */
  245. const MAT_BOTTOM_SHEET_DEFAULT_OPTIONS = new InjectionToken('mat-bottom-sheet-default-options');
  246. /**
  247. * Service to trigger Material Design bottom sheets.
  248. */
  249. class MatBottomSheet {
  250. /** Reference to the currently opened bottom sheet. */
  251. get _openedBottomSheetRef() {
  252. const parent = this._parentBottomSheet;
  253. return parent ? parent._openedBottomSheetRef : this._bottomSheetRefAtThisLevel;
  254. }
  255. set _openedBottomSheetRef(value) {
  256. if (this._parentBottomSheet) {
  257. this._parentBottomSheet._openedBottomSheetRef = value;
  258. }
  259. else {
  260. this._bottomSheetRefAtThisLevel = value;
  261. }
  262. }
  263. constructor(_overlay, injector, _parentBottomSheet, _defaultOptions) {
  264. this._overlay = _overlay;
  265. this._parentBottomSheet = _parentBottomSheet;
  266. this._defaultOptions = _defaultOptions;
  267. this._bottomSheetRefAtThisLevel = null;
  268. this._dialog = injector.get(Dialog);
  269. }
  270. open(componentOrTemplateRef, config) {
  271. const _config = { ...(this._defaultOptions || new MatBottomSheetConfig()), ...config };
  272. let ref;
  273. this._dialog.open(componentOrTemplateRef, {
  274. ..._config,
  275. // Disable closing since we need to sync it up to the animation ourselves.
  276. disableClose: true,
  277. // Disable closing on detachments so that we can sync up the animation.
  278. closeOnOverlayDetachments: false,
  279. maxWidth: '100%',
  280. container: MatBottomSheetContainer,
  281. scrollStrategy: _config.scrollStrategy || this._overlay.scrollStrategies.block(),
  282. positionStrategy: this._overlay.position().global().centerHorizontally().bottom('0'),
  283. templateContext: () => ({ bottomSheetRef: ref }),
  284. providers: (cdkRef, _cdkConfig, container) => {
  285. ref = new MatBottomSheetRef(cdkRef, _config, container);
  286. return [
  287. { provide: MatBottomSheetRef, useValue: ref },
  288. { provide: MAT_BOTTOM_SHEET_DATA, useValue: _config.data },
  289. ];
  290. },
  291. });
  292. // When the bottom sheet is dismissed, clear the reference to it.
  293. ref.afterDismissed().subscribe(() => {
  294. // Clear the bottom sheet ref if it hasn't already been replaced by a newer one.
  295. if (this._openedBottomSheetRef === ref) {
  296. this._openedBottomSheetRef = null;
  297. }
  298. });
  299. if (this._openedBottomSheetRef) {
  300. // If a bottom sheet is already in view, dismiss it and enter the
  301. // new bottom sheet after exit animation is complete.
  302. this._openedBottomSheetRef.afterDismissed().subscribe(() => ref.containerInstance?.enter());
  303. this._openedBottomSheetRef.dismiss();
  304. }
  305. else {
  306. // If no bottom sheet is in view, enter the new bottom sheet.
  307. ref.containerInstance.enter();
  308. }
  309. this._openedBottomSheetRef = ref;
  310. return ref;
  311. }
  312. /**
  313. * Dismisses the currently-visible bottom sheet.
  314. * @param result Data to pass to the bottom sheet instance.
  315. */
  316. dismiss(result) {
  317. if (this._openedBottomSheetRef) {
  318. this._openedBottomSheetRef.dismiss(result);
  319. }
  320. }
  321. ngOnDestroy() {
  322. if (this._bottomSheetRefAtThisLevel) {
  323. this._bottomSheetRefAtThisLevel.dismiss();
  324. }
  325. }
  326. static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.0.0", ngImport: i0, type: MatBottomSheet, deps: [{ token: i3.Overlay }, { token: i0.Injector }, { token: MatBottomSheet, optional: true, skipSelf: true }, { token: MAT_BOTTOM_SHEET_DEFAULT_OPTIONS, optional: true }], target: i0.ɵɵFactoryTarget.Injectable }); }
  327. static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "16.0.0", ngImport: i0, type: MatBottomSheet, providedIn: MatBottomSheetModule }); }
  328. }
  329. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.0.0", ngImport: i0, type: MatBottomSheet, decorators: [{
  330. type: Injectable,
  331. args: [{ providedIn: MatBottomSheetModule }]
  332. }], ctorParameters: function () { return [{ type: i3.Overlay }, { type: i0.Injector }, { type: MatBottomSheet, decorators: [{
  333. type: Optional
  334. }, {
  335. type: SkipSelf
  336. }] }, { type: MatBottomSheetConfig, decorators: [{
  337. type: Optional
  338. }, {
  339. type: Inject,
  340. args: [MAT_BOTTOM_SHEET_DEFAULT_OPTIONS]
  341. }] }]; } });
  342. /**
  343. * Generated bundle index. Do not edit.
  344. */
  345. export { MAT_BOTTOM_SHEET_DATA, MAT_BOTTOM_SHEET_DEFAULT_OPTIONS, MatBottomSheet, MatBottomSheetConfig, MatBottomSheetContainer, MatBottomSheetModule, MatBottomSheetRef, matBottomSheetAnimations };
  346. //# sourceMappingURL=bottom-sheet.mjs.map