Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/mrloop/ember-material-modifier

Lightweight emberjs material design integration
https://github.com/mrloop/ember-material-modifier

Last synced: 5 days ago
JSON representation

Lightweight emberjs material design integration

Awesome Lists containing this project

README

        

# ember-material-modifier

[![Node CI](https://github.com/mrloop/ember-material-modifier/actions/workflows/ci.yml/badge.svg)](https://github.com/mrloop/ember-material-modifier/actions/workflows/ci.yml)
[![npm version](https://img.shields.io/npm/v/ember-material-modifier.svg)](https://www.npmjs.com/package/ember-material-modifier)

Lightweight Ember.js material design integration.

An Ember add-on for [Material Component for web](https://github.com/material-components/material-components-web/).

## Compatibility

* Ember.js v3.20 or above
* Ember CLI v3.20 or above
* Node.js v12 or above

## Installation

```
ember install ember-material-modifier
```

## Usage

ember-material-modifier is designed to be as minimal and unobtrusive as possible. It only downloads the JavaScript and CSS of google material design components that you use.

### Button Example

For instance if you want to use `@material/button` install the package

```sh
npm install @material/button
```

Have a look at the material design docs for the component https://github.com/material-components/material-components-web/tree/master/packages/mdc-button. Then copy the example html to one of your templates.

```hbs



Text Button

```

Next you add the `ember-material-modifier` modifier `{{material}}`

```hbs



Text Button

```

Event listeners are added as usual

```hbs



Text Button

```

If you find yourself duplicating this HTML you can extract it to a component that suits your application.

_app/components/mdc-button.hbs_
```hbs



{{yield}}

```

_app/templates/application.hbs_
```hbs
Text Button
```

| :warning: WARNING |
|:---------------------------|
| Many of the material.io HTML examples use the [Material Icons google font](https://fonts.google.com/icons?selected=Material+Icons), you either need to [install the font](https://developers.google.com/fonts/docs/material_icons#setup_method_1_using_via_google_fonts) or edit the example HTML to use an [alternative](https://github.com/ivanvotti/ember-svg-jar). |

### Register component

The `{{material}}` modifier takes two positional arguments. The first is the name of the material design component. For instance you want to use the package `@material/menu` then the modifier is `{{material 'menu'}}`. You can also pass a second arguement to register the instance of material design component class with your application code.

```hbs


```

```js
@action
registerMaterialMenu(mdcMenu) {
this.mdcMenu = mdcMenu;
}
```

You now have access to the MDCMenu properites and methods.

### Custom Events

Many on the material design components emit custom events. You can add event listeners for these as you would any other event.

```hbs




  • Item 1 - Division 1


```

Though in the case of a list item being selected you probably want to use a standard 'click' event listener on the specific list item.

```hbs




  • Item 1 - Division 1


```

### Theming

Google material design components can be themed using CSS variables.

You can find a list of CSS variables in the google style sheets. For instance if you install any of the `@material/*` packages they have a dependencies on `@material/theme`. You can open `./node_modules/@material/theme/dist/mdc.theme.css` which gives a list of CSS variables.

For example to change the primary text color add the following to your `app.css`

_app.css_
```css
:root {
--mdc-theme-text-primary-on-background: purple;
};
```

You can use the color tool to preview material design themes.

### CSS Loading

The CSS is lazy loaded when the material design components class is instantiated. It is loaded when the modifier is run after the HTML has been rendered. If it is the first time the material design component has been loaded there will be a noticeable delay until the CSS is applied, you will see the component unstyled.

To prevent this you can eager load the CSS for the material design components you use in the application router. For instance if you use `{{material 'button'}}` in one of your templates add `importCss('button')` to the beforeModel hook of your application route.

_app/controller/application.js_
```js
import Route from '@ember/routing/route';

import importCss from 'ember-material-modifier/import-css';

export default class ApplicationRoute extends Route {
beforeModel() {
return importCss('button');
}
}
```

It should be possible to remove this `importCss` and eager load the needed material design CSS without it. The need to call `importCss` will hopefully be removed.

## Contributing

See the [Contributing](CONTRIBUTING.md) guide for details.

## License

This project is licensed under the [MIT License](LICENSE.md).