AngularJS Material

repository·master·Indexed 12 days ago

https://github.com/angular/material

A library of reusable, accessible UI components that implements Google's Material Design specification for AngularJS (v1.x) single-page applications. Version 1.2.5 requires AngularJS 1.7.2 or higher. Note: Official support ended in January 2022; developers using Angular (v2+) should use the Angular Material project instead.

Tokens
15.1K
Snippets
44
Records
76
Agent score
96%

What's inside AngularJS Material

  1. Important: AngularJS Material End-of-Life Notice

    master

    AngularJS Material support officially ended in January 2022.

    This project is an implementation of the Material Design Specification (2014-2017) for AngularJS (v1.x). For developers using the modern Material Design Specification (2018+) and the Angular (v2+) framework, please use the Angular Material project instead.

  2. Typography in Angular Material

    master

    Angular Material typography classes are aligned with those in AngularJS Material. However, Angular Material provides additional capabilities for customization:

    • Sass Mixins: You can customize typography via Sass mixins, similar to how themes are configured.
    • Utility Mixins: Provides utility Sass mixins and functions to assist in styling your own custom components.
  3. Migrating Layout: Flex Layout vs. Angular CDK Layout

    master

    The AngularJS Material layout system (using flex, show, hide, etc.) has been split into two distinct paths for Angular developers:

    1. Flex Layout (Separate Library)

    If you require a comprehensive, declarative API similar to AngularJS Material, use the separate Flex Layout project. It provides:

    • Declarative API: Attributes like fxLayout, fxFlex, fxHide, and fxShow.
    • Responsive API: Attributes like fxLayout.lt-md or fxFlex.gt-sm.
    • Features: Supports both CSS Flexbox and CSS Grid, custom breakpoints, and a MediaObserver service.
    • Note: This library carries a non-trivial payload cost (approx. 46KB - 60KB).

    2. Angular CDK Layout (Lightweight Alternative)

    For a more lightweight approach, use the Angular CDK's layout capabilities. This is ideal if you want to use native CSS (Flexbox/Grid) and only need programmatic access to breakpoints. It includes:

    • MediaMatcher service: Access to native MediaQueryList.
    • Breakpoints: A set of predefined Material Design responsive breakpoints.
    • BreakpointObserver service: APIs for responding to viewport size changes based on those breakpoints.
    • Note: This adds significantly less weight (approx. 3.06KB) than the full Flex Layout library.
  4. Theming: Transitioning from Run-time to Compile-time

    master

    AngularJS Material performed most theming at run-time using JavaScript, which incurred performance costs.

    Angular Material performs theming at compile-time using Sass. This approach removes JavaScript execution costs associated with theming but requires up-front setup.

    Key differences:

    • Palettes: In Angular Material, you create custom palettes by copying provided palettes, modifying them, and using them in your custom theme creation.
    • Themes: Unlike AngularJS Material, Angular Material has no default theme. You must include a pre-built theme CSS file or define a custom theme with Sass mixins.
    • Dynamic Theming: While run-time generation is not supported, you can dynamically swap themes by loading different theme CSS files at run-time or using Angular's [class] or [ngClass] bindings to toggle between pre-defined theme classes.
  5. Coding Patterns and Architecture

    master

    The project follows specific architectural patterns to ensure maintainability and minification efficiency:

    • Module Pattern: All source files are automatically wrapped in an anonymous closure using the Module Pattern.
    • Revealing Pattern: Use the Revealing Pattern as the primary coding style.
    • Object Composition: Prefer simple objects over complex inheritance hierarchies.
    • Inheritance: Avoid prototypal inheritance unless performance requirements dictate it.
    • Functions vs Objects: Prefer functions and closures over objects whenever possible.
    • Minification Optimization: Use internal aliases that map to the external API to allow for more concise, minifiable code.
  6. How AngularJS Material generates CSS themes

    master

    AngularJS Material uses a runtime injection mechanism to apply themes. It does not rely on static CSS files for component styles, but instead dynamically generates and injects <style> tags into the <head> of your application based on registered themes.

    The Theming Process

    1. Build Time: During the build process, all component-specific *-theme.scss files are compiled into a single CSS string. This string is injected into the material.core module as a constant named $MD_THEME_CSS.
    2. Runtime Initialization: The generateAllThemes() function parses the $MD_THEME_CSS constant. It uses an internal closure variable GENERATED to track which themes have already been processed to prevent duplicate generation.
    3. Style Tag Injection: For every registered theme (including the default theme), the system generates 16 <style> tags. These tags are distributed across the four standard palettes:
      • primary
      • accent
      • warn
      • background

    Each palette results in four specific hue-based <style> tags (e.g., .md-primary, .md-primary.md-hue-1, .md-primary.md-hue-2, and .md-primary.md-hue-3).

  7. Project Structure for Components

    master

    The project organizes component modules within the /src/components directory. Each component has its own folder containing the logic, tests, and styles, along with a demo subfolder. Compiled assets are distributed in the /dist directory both as individual modules and as a combined library.

     -- /src
        -- /components
           -- /<component folder>
    
              -- <component>.js
              -- <component>.spec.js
              -- <component>.scss
              -- <component>-theme.scss
    
              -- /demo<name>
    
                 -- index.html
                 -- style.css
                 -- script.js
  8. Use Virtual Repeat to reduce DOM size in Internet Explorer

    master

    Slow layout calculations in Internet Explorer are often caused by large numbers of elements in the DOM, especially when using ng-repeat. To mitigate this, use the md-virtual-repeat directive.

    md-virtual-repeat only adds elements to the DOM that are currently visible in the viewport and reuses elements as the user scrolls. This allows for efficient scrolling through millions of records.

    Requirement: All list items must have an equal and static height for virtual repeat to function correctly.

    Implementation Example:

    Wrap your list in an md-virtual-repeat-container and use the md-virtual-repeat directive on the repeating item.

    <md-content layout="column" ng-controller="ListCtrl">
      <md-virtual-repeat-container id="vertical-container">
        <md-list>
          <md-list-item md-virtual-repeat="person in people"
                        ng-click="goToPerson(person.name, $event)">
            <img alt="{{ person.name }}" ng-src="{{ person.img }}" class="md-avatar">
            <p>{{ person.name }}</p>
            <md-checkbox class="md-secondary" ng-model="person.selected"></md-checkbox>
            <md-icon md-svg-icon="communication:email" ng-click="doSecondaryAction($event)"
                     aria-label="Send Email" class="md-secondary md-hue-3"></md-icon>
            <md-icon class="md-secondary" ng-click="doSecondaryAction($event)" aria-label="Chat"
                     md-svg-icon="communication:message"></md-icon>
          </md-list-item>
        </md-list>
      </md-virtual-repeat-container>
    </md-content>
  9. Understand the AngularJS Material theming model

    master

    AngularJS Material uses a hierarchical theming system to provide a consistent visual tone across all UI components. The system is built on four key concepts:

    1. Hues / Shades: Individual colors within a palette.
    2. Palettes: Collections of hues. AngularJS Material includes built-in palettes based on the Material Design specification (e.g., red, blue, green, grey).
    3. Color Intentions: The role a color plays in your UI. There are three standard intentions:
      • primary: Represents primary interface elements.
      • accent: Represents secondary interface elements.
      • warn: Represents elements requiring user caution.
    4. Themes: A complete configuration that maps specific palettes to color intentions and defines the application's background color palette.
  10. Mapping AngularJS Material features to Angular CDK

    master

    When migrating from AngularJS Material to Angular Material, many features have been moved to the more generic Angular Component Dev Kit (CDK). Use the following mapping to find the equivalent functionality in the CDK:

    AngularJS MaterialCDK Equivalent
    $mdPanelOverlay
    md-virtual-repeat*cdkVirtualFor
    md-virtual-repeat-containercdk-virtual-scroll-viewport
    $mdLiveAnnouncerLiveAnnouncer
    $mdUtil.bidi()Directionality service and Dir directive
    md-input (md-no-autogrow, max-rows)CdkTextareaAutosize
    layout systemSee Angular CDK Layout section