|
1 |
| -This is a placeholder for the MDC-based implementation of slide toggle. |
| 1 | +This is prototype of an alternate version of `<mat-slide-toggle>` built on top of |
| 2 | +[MDC Web](https://github.com/material-components/material-components-web). It demonstrates how |
| 3 | +Angular Material could use MDC Web under the hood while still exposing the same API Angular users as |
| 4 | +the existing `<mat-slide-toggle>`. This component is experimental and should not be used in production. |
| 5 | + |
| 6 | +## How to use |
| 7 | +Assuming your application is already up and running using Angular Material, you can add this |
| 8 | +component by following these steps: |
| 9 | + |
| 10 | +1. Install Angular Material Experimental & MDC WEB: |
| 11 | + |
| 12 | + ```bash |
| 13 | + npm i material-components-web @angular/material-experimental |
| 14 | + ``` |
| 15 | + |
| 16 | +2. In your `angular.json`, make sure `node_modules/` is listed as a Sass include path. This is |
| 17 | + needed for the Sass compiler to be able to find the MDC Web Sass files. |
| 18 | + |
| 19 | + ```json |
| 20 | + ... |
| 21 | + "styles": [ |
| 22 | + "src/styles.scss" |
| 23 | + ], |
| 24 | + "stylePreprocessorOptions": { |
| 25 | + "includePaths": [ |
| 26 | + "node_modules/" |
| 27 | + ] |
| 28 | + }, |
| 29 | + ... |
| 30 | + ``` |
| 31 | + |
| 32 | +3. Import the experimental `MatSlideToggleModule` and add it to the module that declares your |
| 33 | + component: |
| 34 | + |
| 35 | + ```ts |
| 36 | + import {MatSlideToggleModule} from '@angular/material-experimental/mdc-slide-toggle'; |
| 37 | + |
| 38 | + @NgModule({ |
| 39 | + declarations: [MyComponent], |
| 40 | + imports: [MatSlideToggleModule], |
| 41 | + }) |
| 42 | + export class MyModule {} |
| 43 | + ``` |
| 44 | + |
| 45 | +4. Add use `<mat-slide-toggle>` in your component's template, just like you would the normal |
| 46 | + `<mat-slide-toggle>`: |
| 47 | + |
| 48 | + ```html |
| 49 | + <mat-slide-toggle [checked]="isChecked">Toggle me</mat-slide-toggle> |
| 50 | + ``` |
| 51 | + |
| 52 | +5. Add the theme and typography mixins to your Sass. (There is currently no pre-built CSS option for |
| 53 | + the experimental `<mat-slide-toggle>`): |
| 54 | + |
| 55 | + ```scss |
| 56 | + @import '~@angular/material/theming'; |
| 57 | + @import '~@angular/material-experimental/mdc-slide-toggle'; |
| 58 | + |
| 59 | + $my-primary: mat-palette($mat-indigo); |
| 60 | + $my-accent: mat-palette($mat-pink, A200, A100, A400); |
| 61 | + $my-theme: mat-light-theme($my-primary, $my-accent); |
| 62 | + |
| 63 | + @include mat-slide-toggle-theme-mdc($my-theme); |
| 64 | + @include mat-slide-toggle-typography-mdc(); |
| 65 | + ``` |
| 66 | + |
| 67 | +## API differences |
| 68 | +The experimental slide toggle API closely matches the |
| 69 | +[API of the standard slide toggle](https://material.angular.io/components/slide-toggle/api). |
| 70 | +`@angular/material-experimental/mdc-slide-toggle` exports symbols with the same name and public |
| 71 | +interface as all of the symbols found under `@angular/material/slide-toggle`, except for the |
| 72 | +following differences: |
| 73 | + |
| 74 | +* The MDC-based `mat-slide-toggle` drops the dependency on Hammer.js and as a result doesn't support |
| 75 | +dragging gestures. |
| 76 | +* As a result of dragging gestures not being supported, the `dragChange` event won't emit. |
| 77 | + |
| 78 | +## Replacing the standard slide toggle in an existing app |
| 79 | +Because the experimental API mirrors the API for the standard slide toggle, it can easily be swapped |
| 80 | +in by just changing the import paths. There is currently no schematic for this, but you can run the |
| 81 | +following string replace across your TypeScript files: |
| 82 | + |
| 83 | +```bash |
| 84 | +grep -lr --include="*.ts" --exclude-dir="node_modules" \ |
| 85 | + --exclude="*.d.ts" "['\"]@angular/material/slide-toggle['\"]" | xargs sed -i \ |
| 86 | + "s/['\"]@angular\/material\/slide-toggle['\"]/'@angular\/material-experimental\/mdc-slide-toggle'/g" |
| 87 | +``` |
| 88 | + |
| 89 | +CSS styles and tests that depend on implementation details of mat-slide-toggle (such as getting |
| 90 | +elements from the template by class name) will need to be manually updated. |
| 91 | + |
| 92 | +There are some small visual differences between this slide and the standard mat-slide. This |
| 93 | +slide has a slightly larger ripple and different spacing between the label and the toggle. |
0 commit comments