Revisiting Material Design For The Web
Material Design 3 is a very good design library coming from Google and it's the latest version of their [Material Design](https://m3.material.io/) design language. There are versions of Material for all the languages and libraries Google makes available: * [Web](https://m3.material.io/develop/web) * [Flutter](https://m3.material.io/develop/android/jetpack-compose) * Android * [MDC Android](https://m3.material.io/develop/android/mdc-android) * [Jetpack Compose](https://m3.material.io/develop/android/jetpack-compose) * [Angular](https://material.angular.io/) This post will concentrate on Material Design Web and try to do the following: * Explain what is Material Design * Explain how to build a design/layout with Material Design Web * Decide if the library is still worth using since it went into [maintenance mode](https://github.com/material-components/material-web/discussions/5642#discussion-6805266) due to Google reassigning the engineering team working on Material Web to work on implementing Material for Wiz (before the merge with Angular) ## Getting Started The Material Design Web [Quick Start](https://github.com/material-components/material-web/blob/main/docs/quick-start.md) provides a good way to look at Material Design for evaluation. ### The Kitchen Sink Approach The easiest way to build material design is to use the `all.js` import, what I call the kitchen sink. The `all.js` import will import all available Material Design components, making them available for you to play with. The first step is to load Roboto from Google Fonts ```html ``` Then we create an [import map](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script/type/importmap), a JSON object object that allows developers to control how the browser resolves module specifiers when importing JavaScript modules. It maps the text used as the module specifier in an import statement or import() operator and the corresponding value that will replace the text when resolving the specifier. For more information about import maps, check the [Importing Modules Using Import Maps](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules#importing_modules_using_import_maps) section of the [Javascript Modules](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules) page in MDN. ```js ``` The next block is a module script, (`script` tag with `type=module`). In it, we can use `import` statements to load all the material design modules that we need. The import statements use the local reference (`@material/web`) instead of the full remote URL. In theory, this should also work with references to the node_modules directory so we could work offline during development but I haven't quite figured out how to do it. ```js ``` We then use the classes and elements we imported to build the content of the page. You can use whatever is available on the [documentation website](https://material-web.dev/) to build your site. ```html
Hello Material!
``` Note that this is not meant for production since it will invariably load code that your project won't use. For production code, you should import individual components and use a build system to bundle them together for wide browser support. ### Loading Individual Components Rather than use the kitchen sink approach, we'll look at a granular approach towards importing components to build the project. We first install the packages from Material Web Components by installing the full `@material/web` package. ```bash npm install @material/web ``` Next, we create an `index.js` file to hold the imports for the project. This will not be referenced in the HTML; we'll use it to build the bundle that we link to in the HTML. After importing the typography component, we use [adoptedStyleSheets](https://developer.mozilla.org/en-US/docs/Web/API/Document/adoptedStyleSheets) to append the stylesheet to the existing style sheets array. ```js import '@material/web/button/filled-button.js'; import '@material/web/button/outlined-button.js'; import '@material/web/checkbox/checkbox.js'; import '@material/web/list/list.js'; import '@material/web/list/list-item.js'; import {styles as typescaleStyles} from '@material/web/typography/md-typescale-styles.js'; document.adoptedStyleSheets.push(typescaleStyles.styleSheet); ``` Either in the `head` or the `body` of the page, we reference `bundle.js` in a `script` tag. We'll look at how to generate the bundle later in the post. Use the <component-name> tag in HTML markup. Remember that whenever you add a component, you have to add the corresponding import declaration to the `index.js` file. ```htmlHello Material!
Cucumber
Cucumbers are long green fruits that are just as long as this multi-line description
Shop for Kiwis
This will link you out in a new tab