Skip to content

jseriff/angular2-materialize

 
 

Repository files navigation

Angular2 Materialize

travis build version downloads MIT Licence semantic-release Commitizen friendly PRs Welcome

Angular 2 support for Materialize CSS framework https://github.com/Dogfalo/materialize

This 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.

View minimal demo here: http://angular2-materialize.surge.sh

To 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.

Using angular2-materialize

Import it once per project, for example in your main.ts:

import "angular2-materialize";

In your component, use it for dynamic behavior. For example, for collapsible panels:

import {MaterializeDirective} from "angualr2-materialize";

@Component({
    selector: "my-component",
    directives: [MaterializeDirective],
    template: `
        <ul materialize="collapsible" class="collapsible" data-collapsible="accordion">
          <li>
            <div class="collapsible-header"><i class="material-icons">filter_drama</i>First</div>
            <div class="collapsible-body"><p>Lorem ipsum dolor sit amet.</p></div>
          </li>
          <li>
            <div class="collapsible-header"><i class="material-icons">place</i>Second</div>
            <div class="collapsible-body"><p>Lorem ipsum dolor sit amet.</p></div>
          </li>
          <li>
            <div class="collapsible-header"><i class="material-icons">whatshot</i>Third</div>
            <div class="collapsible-body"><p>Lorem ipsum dolor sit amet.</p></div>
          </li>
        </ul>

Apply an empty MaterializeDirective attribute directive for top level components, like forms:

<form materialize class="col s12">
  <div class="row">
    <div class="input-field col s6">
      <input placeholder="Placeholder" id="first_name" type="text" class="validate">
      <label for="first_name">First Name</label>
    </div>
  </div>
</form>

The MaterializeDirective attribute directive (materialize) accepts any MaterializeCSS initialization call to apply to the element. The list of supported functions are provided by MaterializeCSS. Examples: collapsible, leanModal, tooltip, dropdown, tabs, material_select, sideNav, etc.

For example, to apply tooltip:

<a materialize="tooltip" class="btn tooltipped" data-position="bottom" data-delay="50" data-tooltip="I am tooltip">Hover me!</a>

The Materialize 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:

<!-- Modal Trigger -->
<a materialize="leanModal" [materializeParams]="[{dismissible: false}]" class="waves-effect waves-light btn modal-trigger" href="#modal1">Modal</a>
<!-- Modal Structure -->
<div id="modal1" class="modal">
  <div class="modal-content">
    <h4>Modal Header</h4>
    <p>A bunch of text</p>
  </div>
  <div class="modal-footer">
    <a href="#!" class=" modal-action modal-close waves-effect waves-green btn-flat">Agree</a>
  </div>
</div>

Installing and configuring angular2-materialize with webpack

Install MaterializeCSS and angular2-materialize from npm

npm install materialize-css --save
npm install angular2-materialize --save

Add the Google MD fonts to your index.html:

<link href="http://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">

Add the following aliases and loader to your webpack configuration:

module.exports = {
  //...
  resolve: {
    alias: {
    materializecss: 'materialize-css/dist/css/materialize.css',
    materialize: 'materialize-css/dist/js/materialize.js',
    //...
  },
  module: {
    loaders: [
      {
        test: /materialize-css\/dist\/js\/materialize\.js/,
        loader: 'imports?materializecss'
      },
      //...
    ]
  }
  //...
};

Notice that the imports loader is required for this setup.

Loading CSS as styles

If you are loading CSS with raw-loader, the above setup will not be able to load the MaterializeCSS styles properly.

To work around this, without changing the way CSS is handled across the app, add the following loader to match the materialize.css specifically and load it with the style loader:

{ test: /materialize\.css$/,   loader: 'style-loader!css-loader' },

Then, update the css loader to apply only to CSS that is not "materialize":

// Support for CSS as raw text (do not match 'materialize')
{ test: /^((?!materialize).)*\.css$/,   loader: 'raw-loader' },
Loading additional resources

Another thing you would need to confirm is being able to load web fonts properly:

{ test: /.(png|woff(2)?|eot|ttf|svg)(\?[a-z0-9=\.]+)?$/, loader: 'url-loader?limit=100000' },

Notice that the url loader is required for this to work.

Installing and configuring angular2-materialize with jspm

Install MaterializeCSS, by providing overrides for its dependencies:

jspm install materialize=github:Dogfalo/materialize -o "{'main': 'js/materialize','shim': {'js/materialize': {'deps': ['jquery','../css/materialize.css!'],'exports': '$'}},'dependencies': {'jquery': 'github:components/jquery','css': 'github:systemjs/plugin-css'}}"

Install angular2-materialize

jspm install npm:angular2-materialize

Add the Google MD fonts to your index.html:

<link href="http://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">

Add a package configuration to specify the main entry point for MaterializeCSS:

System.config({
  ...
  packages: {
    ...
    "materialize": {
      "main": "js/materialize"
    }
  },

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 54.4%
  • TypeScript 44.5%
  • HTML 1.1%