|
| 1 | +The `MatBottomSheet` service can be used to open Material Design panels to the bottom of the screen. |
| 2 | +These panels are intended primarily as an interaction on mobile devices where they can be used as an |
| 3 | +alternative to dialogs and menus. |
| 4 | + |
| 5 | +<!-- example(bottom-sheet-overview) --> |
| 6 | + |
| 7 | +You can open a bottom sheet by calling the `open` method with a component to be loaded and an |
| 8 | +optional config object. The `open` method will return an instance of `MatBottomSheetRef`: |
| 9 | + |
| 10 | +```ts |
| 11 | +const bottomSheetRef = bottomSheet.open(SocialShareComponent, { |
| 12 | + ariaLabel: 'Share on social media' |
| 13 | +}); |
| 14 | +``` |
| 15 | + |
| 16 | +The `MatBottomSheetRef` is a reference to the currently-opened bottom sheet and can be used to close |
| 17 | +it or to subscribe to events. Note that only one bottom sheet can be open at a time. Any component |
| 18 | +contained inside of a bottom sheet can inject the `MatBottomSheetRef` as well. |
| 19 | + |
| 20 | +```ts |
| 21 | +bottomSheetRef.afterDismissed().subscribe(() => { |
| 22 | + console.log('Bottom sheet has been dismissed.'); |
| 23 | +}); |
| 24 | + |
| 25 | +bottomSheetRef.dismiss(); |
| 26 | +``` |
| 27 | + |
| 28 | +### Sharing data with the bottom sheet component. |
| 29 | +If you want to pass in some data to the bottom sheet, you can do so using the `data` property: |
| 30 | + |
| 31 | +```ts |
| 32 | +const bottomSheetRef = bottomSheet.open(HobbitSheet, { |
| 33 | + data: { names: ['Frodo', 'Bilbo'] }, |
| 34 | +}); |
| 35 | +``` |
| 36 | + |
| 37 | +Afterwards you can access the injected data using the `MAT_BOTTOM_SHEET_DATA` injection token: |
| 38 | + |
| 39 | +```ts |
| 40 | +import {Component, Inject} from '@angular/core'; |
| 41 | +import {MAT_BOTTOM_SHEET_DATA} from '@angular/material'; |
| 42 | + |
| 43 | +@Component({ |
| 44 | + selector: 'hobbit-sheet', |
| 45 | + template: 'passed in {{ data.names }}', |
| 46 | +}) |
| 47 | +export class HobbitSheet { |
| 48 | + constructor(@Inject(MAT_BOTTOM_SHEET_DATA) public data: any) { } |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +### Configuring bottom sheet content via `entryComponents` |
| 53 | + |
| 54 | +Similarly to `MatDialog`, `MatBottomSheet` instantiates components at run-time. In order for it to |
| 55 | +work, the Angular compiler needs extra information to create the necessary `ComponentFactory` for |
| 56 | +your bottom sheet content component. |
| 57 | + |
| 58 | +Any components that are include inside of a bottom sheet have to be added to the `entryComponents` |
| 59 | +inside your `NgModule`. |
| 60 | + |
| 61 | + |
| 62 | +```ts |
| 63 | +@NgModule({ |
| 64 | + imports: [ |
| 65 | + // ... |
| 66 | + MatBottomSheetModule |
| 67 | + ], |
| 68 | + |
| 69 | + declarations: [ |
| 70 | + AppComponent, |
| 71 | + ExampleBottomSheetComponent |
| 72 | + ], |
| 73 | + |
| 74 | + entryComponents: [ |
| 75 | + ExampleBottomSheetComponent |
| 76 | + ], |
| 77 | + |
| 78 | + providers: [], |
| 79 | + bootstrap: [AppComponent] |
| 80 | +}) |
| 81 | +export class AppModule {} |
| 82 | +``` |
| 83 | + |
| 84 | +### Accessibility |
| 85 | +By default, the bottom sheet has `role="dialog"` on the root element and can be labelled using the |
| 86 | +`ariaLabel` property on the `MatBottomSheetConfig`. |
| 87 | + |
| 88 | +When a bottom sheet is opened, it will move focus to the first focusable element that it can find. |
| 89 | +In order to prevent users from tabbing into elements in the background, the Material bottom sheet |
| 90 | +uses a [focus trap](https://material.angular.io/cdk/a11y/overview#focustrap) to contain focus |
| 91 | +within itself. Once a bottom sheet is closed, it will return focus to the element that was focused |
| 92 | +before it was opened. |
| 93 | + |
| 94 | +#### Focus management |
| 95 | +By default, the first tabbable element within the bottom sheet will receive focus upon open. |
| 96 | +This can be configured by setting the `cdkFocusInitial` attribute on another focusable element. |
| 97 | + |
| 98 | +#### Keyboard interaction |
| 99 | +By default pressing the escape key will close the bottom sheet. While this behavior can |
| 100 | +be turned off via the `disableClose` option, users should generally avoid doing so |
| 101 | +as it breaks the expected interaction pattern for screen-reader users. |
0 commit comments