UNPKG

9.2 kBMarkdownView Raw
1# Angular2 Materialize
2
3[![travis build](https://img.shields.io/travis/InfomediaLtd/angular2-materialize.svg?style=flat-square)](https://travis-ci.org/InfomediaLtd/angular2-materialize)
4[![version](https://img.shields.io/npm/v/angular2-materialize.svg?style=flat-square)](https://www.npmjs.com/package/angular2-materialize)
5[![downloads](https://img.shields.io/npm/dm/angular2-materialize.svg?style=flat-square)](https://www.npmjs.com/package/angular2-materialize)
6[![MIT Licence](https://img.shields.io/npm/l/angular2-materialize.svg?style=flat-square)](https://opensource.org/licenses/MIT)
7[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg?style=flat-square)](https://github.com/semantic-release/semantic-release)
8[![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg?style=flat-square)](http://commitizen.github.io/cz-cli/)
9[![PRs Welcome](https://img.shields.io/badge/prs-welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com)
10
11[![NPM](https://nodei.co/npm/angular2-materialize.png?downloads=true)](https://www.npmjs.com/package/angular2-materialize)
12[![NPM](https://nodei.co/npm-dl/angular2-materialize.png?height=2&months=12)](https://www.npmjs.com/package/angular2-materialize)
13
14Angular 2 support for Materialize CSS framework [http://materializecss.com/](http://materializecss.com/)
15
16This library adds support for the Materialize CSS framework in Angular 2. It is needed to add the dynamic behavior of Materialize CSS that is using JavaScript rather than plain CSS.
17
18View demo here: [https://infomedialtd.github.io/angular2-materialize/](https://infomedialtd.github.io/angular2-materialize/)
19
20To use the library you need to import it once per project and then use its MaterializeDirective directive for binding it to any component that needs a dynamic behavior, like collapsible panels, tooltips, etc.
21
22## Using angular2-materialize
23
24Start by following the Angular CLI or webpack instructions below to add the required dependencies to your project.
25
26Add the MaterializeModule to your NgModule:
27```js
28import { MaterializeModule } from "angular2-materialize";
29
30@NgModule({
31 imports: [
32 //...
33 MaterializeModule,
34 ],
35 //...
36})
37```
38
39In your component, use it for dynamic behavior. For example, for collapsible panels:
40```js
41@Component({
42 selector: "my-component",
43 template: `
44 <ul materialize="collapsible" class="collapsible" data-collapsible="accordion">
45 <li>
46 <div class="collapsible-header"><i class="material-icons">filter_drama</i>First</div>
47 <div class="collapsible-body"><p>Lorem ipsum dolor sit amet.</p></div>
48 </li>
49 <li>
50 <div class="collapsible-header"><i class="material-icons">place</i>Second</div>
51 <div class="collapsible-body"><p>Lorem ipsum dolor sit amet.</p></div>
52 </li>
53 <li>
54 <div class="collapsible-header"><i class="material-icons">whatshot</i>Third</div>
55 <div class="collapsible-body"><p>Lorem ipsum dolor sit amet.</p></div>
56 </li>
57 </ul>
58
59```
60
61Apply an empty [MaterializeDirective](https://github.com/InfomediaLtd/angular2-materialize/blob/master/src/materialize-directive.ts) attribute directive for top level components, like forms:
62```html
63<form materialize class="col s12">
64 <div class="row">
65 <div class="input-field col s6">
66 <input placeholder="Placeholder" id="first_name" type="text" class="validate">
67 <label for="first_name">First Name</label>
68 </div>
69 </div>
70</form>
71```
72
73The [MaterializeDirective](https://github.com/InfomediaLtd/angular2-materialize/blob/master/src/materialize-directive.ts) attribute directive (**materialize**) accepts any MaterializeCSS initialization call to apply to the element. The list of supported functions are provided by MaterializeCSS. Examples: collapsible, modal, tooltip, dropdown, tabs, material_select, sideNav, etc.
74
75For example, to apply tooltip:
76```html
77<a materialize="tooltip" class="btn tooltipped" data-position="bottom" data-delay="50" data-tooltip="I am tooltip">Hover me!</a>
78```
79
80The [Materialize](https://github.com/InfomediaLtd/angular2-materialize/blob/master/src/materialize.ts) attribute directive also allows specifying parameters to be passed to the function, but providing a **materializeParams** attribute returning an array of params. Use it with a function call or even by inlining the params in the HTML.
81
82Another useful option is emitting actions on an element. You may want to do that for calling Materialize functions, like closing a modal dialog or triggering a toast. You can do that by setting the **materializeActions** attribute, which accepts an [EventEmitter](https://angular.io/docs/ts/latest/api/core/index/EventEmitter-class.html). The emitted events can either be a "string" type action (Materialize function call) or a structure with action and parameters:
83
84The example below shows how you'd create a modal dialog and use the actions to open or close it.
85```html
86<!-- Modal Trigger -->
87<a class="waves-effect waves-light btn modal-trigger" (click)="openModal()">Modal</a>
88
89<!-- Modal Structure -->
90<div id="modal1" class="modal bottom-sheet" materialize="modal" [materializeParams]="[{dismissible: false}]" [materializeActions]="modalActions">
91 <div class="modal-content">
92 <h4>Modal Header</h4>
93 <p>A bunch of text</p>
94 </div>
95 <div class="modal-footer">
96 <a class="waves-effect waves-green btn-flat" (click)="closeModal()">Close</a>
97 <a class="modal-action modal-close waves-effect waves-green btn-flat">Agree</a>
98 </div>
99</div>
100```
101```js
102 import {MaterializeAction} from 'angular2-materialize';
103 //...
104 modalActions = new EventEmitter<string|MaterializeAction>();
105 openModal() {
106 this.modalActions.emit({action:"modal",params:['open']});
107 }
108 closeModal() {
109 this.modalActions.emit({action:"modal",params:['close']});
110 }
111```
112
113For dynamic select elements apply the **materializeSelectOptions** directive to trigger element updates when the options list changes:
114```html
115<select materialize="material_select" [materializeSelectOptions]="selectOptions">
116 <option *ngFor="let option of selectOptions" [value]="option.value">{{option.name}}</option>
117</select>
118```
119
120## Installing & configuring angular2-materialize in projects created with the Angular CLI
121
122Install MaterializeCSS and angular2-materialize from npm
123```
124npm install materialize-css --save
125npm install angular2-materialize --save
126```
127
128jQuery 2.2 and Hammer.JS are required
129```
130npm install jquery@^2.2.4 --save
131npm install hammerjs --save
132```
133
134Edit the angular-cli.json :
135* Go to section apps and find styles array inside it (with only styles.css value by default), add the following line inside array before any styles:
136
137```
138 "../node_modules/materialize-css/dist/css/materialize.css"
139```
140
141* Go to section apps and find scripts array inside it, and add the following lines inside array
142
143```
144 "../node_modules/jquery/dist/jquery.js",
145 "../node_modules/hammerjs/hammer.js",
146 "../node_modules/materialize-css/dist/js/materialize.js"
147```
148
149Add to the top of app.module.ts
150
151```
152import { MaterializeModule } from 'angular2-materialize';
153
154```
155
156Add MaterializeModule inside imports array of @NgModule decorator in app.module.ts
157
158Add this line to header of index.html
159```
160<link href="http://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
161```
162
163## Installing and configuring angular2-materialize with webpack
164
165Install MaterializeCSS and angular2-materialize from npm
166```sh
167npm install materialize-css --save
168npm install angular2-materialize --save
169```
170
171MaterializeCSS required jQuery and HammerJS. Check the exact version materialize-css is compatible with:
172```sh
173npm install jquery@^2.2.4 --save
174npm install hammerjs --save
175```
176
177Add the Google MD fonts to your index.html:
178```html
179<link href="http://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
180```
181
182Import materialize-css styles:
183```html
184<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.98.2/css/materialize.min.css">
185```
186
187Add the following plugin to your webpack configuration to provide jQuery:
188```js
189const ProvidePlugin = require('webpack/lib/ProvidePlugin');
190module.exports = {
191 //...
192 plugins: [
193 new ProvidePlugin({
194 "window.jQuery": "jquery",
195 Hammer: "hammerjs/hammer"
196 })
197 ]
198 //...
199};
200```
201
202Import MaterializeCSS programatically, in the same place where you import angular2-materialize module (usually in your main module, or shared module):
203```js
204import 'materialize-css';
205import { MaterializeModule } from 'angular2-materialize';
206```
207
208#### Loading additional resources
209
210Another thing you would need to confirm is being able to load web fonts properly:
211```js
212{ test: /.(png|woff(2)?|eot|ttf|svg)(\?[a-z0-9=\.]+)?$/, loader: 'url-loader?limit=100000' },
213```
214Notice that the url-loader is required for this to work (npm install it).
215
216The following example project is a fork of the angular2-webpack-starter with the addition of angular2-materialize: [InfomediaLtd/angular2-webpack-starter](https://github.com/InfomediaLtd/angular2-webpack-starter)
217