Material Web Components

repository·main·Indexed 11 days ago

https://github.com/material-components/material-web

A library of web components based on the Material 3 design system (v2.5.0). It features a three-tier design token hierarchy (Reference, System, and Component tokens) implemented as CSS custom properties for granular theming of typography, colors, and shapes.

Tokens
63.1K
Snippets
208
Records
249
Agent score
45%

What's inside Material Web

  1. Understand the current status of Material Web Components

    main

    Material Web Components (MWC) is currently in maintenance mode.

    This means:

    • There is no current work planned for new features or new components.
    • Development is focused on bug fixes or specific contributions on a case-by-case basis.
    • Planned future features (such as new components or TSX support) are currently on hold.
  2. Use Sliders for value selection

    main

    Sliders allow users to view and select a value or a range of values along a track. They are suitable for adjusting settings like volume, brightness, or image filters. Sliders can be configured as continuous (subjective range) or discrete (predetermined values using steps and ticks).

    <md-slider></md-slider>
    <md-slider ticks value="50"></md-slider>
    <md-slider range value-start="25" value-end="75"></md-slider>
  3. Use experimental features in Labs

    main
    The labs/ directory contains experimental features. These components and APIs are not recommended for production use because they are subject to breaking changes that may occur without a major version bump (vX.0.0). Use them only for testing or prototyping new ideas.
  4. Overview of Chip types and usage

    main

    Chips help users enter information, make selections, filter content, or trigger actions. They are intended to appear dynamically as a group of multiple interactive elements rather than as consistent, standalone buttons.

    There are four main types of chips:

    • Assist chips: Represent smart or automated actions (e.g., adding an event to a calendar).
    • Filter chips: Tags used to filter content (e.g., shopping categories).
    • Input chips: Discrete pieces of information entered by a user (e.g., event attendees or contacts).
    • Suggestion chips: Dynamic suggestions for user input (e.g., text message replies).
    <md-chip-set>
      <md-assist-chip label="Assist"></md-assist-chip>
      <md-filter-chip label="Filter"></md-filter-chip>
      <md-input-chip label="Input"></md-input-chip>
      <md-suggestion-chip label="Suggestion"></md-suggestion-chip>
    </md-chip-set>
  5. Use `@material/web/tokens/versions` for custom implementations

    main

    The @material/web/tokens/versions directory contains auto-generated Material Design tokens. This path is intended for advanced users performing custom implementations or experimenting with the Material Design system.

    Warning: This directory is unstable. It may introduce breaking changes at any time, including during minor and patch updates. Most users should instead use the stable APIs provided by @material/web/tokens.

  6. Accessibility for Lists

    main

    The List component manages ARIA roles and keyboard navigation automatically:

    • <md-list>: Defaults to role="list" and tabindex="-1".
    • <md-list-item>: Defaults to role="listitem" and tabindex="0".

    You can override these using the role and tabindex attributes. Note that <md-list-item> has a limited set of valid type values to ensure accessibility is maintained; refer to the ListItemType TypeScript type for the allowed values.

  7. Understand Material Web bundle size metrics

    main

    Material Web components are distributed in bundles (single .js files containing necessary JavaScript and CSS). To help developers manage performance and payload, three metrics are tracked:

    • gzip: The minified and compressed size. This affects the download size and network latency.
    • minified: The minified but unpacked size. This affects the time it takes for a page to become interactive (parsing/execution time).
    • % CSS: The ratio of CSS to JavaScript within the bundle. This helps distinguish between changes in visual styles and changes in logic.
  8. Understand the DOM structure of the Switch component

    main

    The Switch component is built using a nested structure that separates the interactive elements from the visual styling. The outermost element is a <button> with the class .switch, which contains the visual track, the handle, and a hidden <input type="checkbox"> used to manage the component's state.

    When customizing or inspecting the component, note that the .switch element intentionally lacks a border to simplify the application of the focus ring.

    <button class="switch">
      <div class="track">
        ::before
        <div class="handle">
          ::before
          <div class="icons">
          </div>
        </div>
      </div>
      <input type="checkbox" aria-hidden="true">
    </button>
  9. How Switch animations work

    main

    The Switch component uses several CSS properties to animate transitions between the selected and unselected states:

    • Opacity: Applied to .track::before and .handle::before to transition between unselected and selected colors/styles.
    • Transform (scale): Applied to .handle to animate the growing and shrinking effect of the handle.
    • Margin (inline-start & inline-end): Applied to .handle to animate the physical movement of the handle across the track.
  10. Use md-chip-set to group chips

    main

    Chips should always be contained within a <md-chip-set>. A chip set acts as a toolbar that can display various types of chips or other toolbar items.

    <md-chip-set>
      <md-filter-chip label="All day"></md-filter-chip>
      <md-assist-chip label="Add to calendar"></md-assist-chip>
      <md-assist-chip label="Set a reminder"></md-assist-chip>
    </md-chip-set>
  11. Style Material Web Components using Design Tokens

    main

    Material Web Components use Design Tokens for styling. These tokens are implemented as CSS custom properties (CSS variables). You can customize the appearance of components (such as colors and typefaces) by defining these properties on a parent element or the :root selector.

    :root {
      --md-sys-color-primary: olive;
      --md-sys-color-secondary: tomato;
      --md-ref-typeface-brand: 'Open Sans';
      --md-ref-typeface-plain: system-ui;
    }