|
| 1 | +# Local Development |
| 2 | + |
| 3 | +Use this document to run demos for local development of Material Components Web project. |
| 4 | + |
| 5 | +## Create demo assets |
| 6 | + |
| 7 | +Create `demo/` folder inside component package folder that you're working on. For example, `packages/mdc-checkbox/demo/` |
| 8 | + |
| 9 | +### HTML |
| 10 | + |
| 11 | +Add HTML structure to required to render component (Copy HTML structure from component README files). |
| 12 | + |
| 13 | +Sample HTML structure for checkbox: |
| 14 | + |
| 15 | +```html |
| 16 | +<div class="mdc-form-field"> |
| 17 | + <div class="mdc-checkbox"> |
| 18 | + <input type="checkbox" |
| 19 | + class="mdc-checkbox__native-control" |
| 20 | + id="checkbox-1"/> |
| 21 | + <div class="mdc-checkbox__background"> |
| 22 | + <svg class="mdc-checkbox__checkmark" |
| 23 | + viewBox="0 0 24 24"> |
| 24 | + <path class="mdc-checkbox__checkmark-path" |
| 25 | + fill="none" |
| 26 | + d="M1.73,12.91 8.1,19.28 22.79,4.59"/> |
| 27 | + </svg> |
| 28 | + <div class="mdc-checkbox__mixedmark"></div> |
| 29 | + </div> |
| 30 | + <div class="mdc-checkbox__ripple"></div> |
| 31 | + </div> |
| 32 | + <label for="checkbox-1">Checkbox 1</label> |
| 33 | +</div> |
| 34 | + |
| 35 | +<script type="module" src="./index.ts"></script> |
| 36 | +``` |
| 37 | + |
| 38 | +We'll create demo `index.ts` file in following steps. |
| 39 | + |
| 40 | +### Sass |
| 41 | + |
| 42 | +Include Sass files required to style target components using relative path (relative to `demo/` folder). |
| 43 | + |
| 44 | +```scss |
| 45 | +@use "../../mdc-form-field/mdc-form-field"; |
| 46 | +@use "../mdc-checkbox"; |
| 47 | +``` |
| 48 | + |
| 49 | +### TypeScript |
| 50 | + |
| 51 | +```ts |
| 52 | +import {MDCFormField} from '../../mdc-form-field/component'; |
| 53 | +import {MDCCheckbox} from '../component'; |
| 54 | + |
| 55 | +const checkboxEl = document.querySelector<HTMLElement>('.mdc-checkbox'); |
| 56 | +const formFieldEl = document.querySelector<HTMLElement>('.mdc-form-field'); |
| 57 | + |
| 58 | +if (checkboxEl && formFieldEl) { |
| 59 | + const checkbox = new MDCCheckbox(); |
| 60 | + const formField = new MDCFormField(); |
| 61 | + formField.input = checkbox; |
| 62 | +} |
| 63 | +``` |
| 64 | + |
| 65 | +## Run dev server |
| 66 | + |
| 67 | +We'll use [Parcel.js](https://parceljs.org) bundler to compile & run dev server. Use `index.html` created above as entry point to parcel bundler CLI. |
| 68 | + |
| 69 | +```shell |
| 70 | +npm i |
| 71 | +npx parcel packages/mdc-checkbox/demo/index.html |
| 72 | +``` |
| 73 | + |
| 74 | +Now open http://localhost:1234/ in your browser. |
0 commit comments