Material Components (MDC) help developers implement Material Design. Created by a team of engineers and UX designers at Google, MDC features dozens of beautiful and functional UI components and is available for Android, iOS, web and Flutter.

MDC Web is engineered to integrate into any front end framework while upholding the principles of Material Design. The following codelab guides you through building a React Component, which uses MDC Web as a foundation. The principles learned in this codelab can be applied to any JavaScript framework.

How MDC Web is built

MDC Web JavaScript's layer is comprised of three classes per component: the Component, Foundation, and Adapter. This pattern gives MDC Web the flexibility to integrate with frontend frameworks.

The Foundation contains the business logic that implements Material Design. The Foundation does not reference any HTML elements. This lets us abstract HTML interaction logic into the Adapter. Foundation has an Adapter.

The Adapter is an interface. The Adapter interface is referenced by the Foundation to implement Material Design business logic. You can implement the Adapter in different frameworks such as Angular or React. An implementation of an Adapter interacts with the DOM structure.

The Component has a Foundation, and its role is to

  1. Implement the Adapter, using non-framework JavaScript, and
  2. Provide public methods that proxy to methods in the Foundation.

What MDC Web provides

Every package in MDC Web comes with a Component, Foundation, and Adapter. To instantiate a Component you must pass the root element to the Component's constructor method. The Component implements an Adapter, which interacts with the DOM and HTML elements. The Component then instantiates the Foundation, which calls the Adapter methods.

To integrate MDC Web into a framework you need to create your own Component in that framework's language/syntax. The framework Component implements MDC Web's Adapter and uses MDC Web's Foundation.

What you'll build

This codelab demonstrates how to build a custom Adapter to use the Foundation logic to achieve a Material Design React Component. It covers the advanced topics found at Integrating into Frameworks. React is used in this codelab as an example framework, but this approach can be applied to any other framework.

In this codelab, you'll build the Top App Bar and recreate the top app bar demo page. The demo page layout is already setup so you can start working on the Top App Bar. The Top App Bar will include:

What you'll need:

How would you rate your level of experience with web development?

Novice Intermediate Proficient

Download the starter codelab app

Download starter app

The starter app is located within the material-components-web-codelabs-master/mdc-112/starter directory.

...or clone it from GitHub

To clone this codelab from GitHub, run the following commands:

git clone
cd material-components-web-codelabs/mdc-112/starter

Install project dependencies

From the starter directory material-components-web-codelabs/mdc-112/starter, run:

npm install

You will see a lot of activity and at the end, your terminal should show a successful install:

Run the starter app

In the same directory, run:

npm start

The webpack-dev-server will start. Point your browser to http://localhost:8080/ to see the page.

Success! The starter code for the Top App Bar React Demo page should be running in your browser. You should see a wall of lorem ipsum text, a Controls box (bottom right), and an unfinished Top App Bar:

Take a look at the code and project

If you open your code editor, the project directory should look something like:

Open the file App.js and look at the render method, which includes the <TopAppBar> Component:


render() {
    const {isFixed, isShort, isRtl, isProminent, isAlwaysCollapsed, shouldReinit} = this.state;

    return (
        dir={isRtl ? 'rtl' : 'ltr'}
          shouldReinit ? null :
            title='Mountain View, CA'
        <div className='demo-text-container'>


This is the entry point for the TopAppBar in the application.

Open the file TopAppBar.js which is a bare React Component class with a render method:


import React from 'react';

export default class TopAppBar extends React.Component {
  render() {
    return (
        TOP APP BAR

In React, the render method outputs the Component's HTML. In the above code, the Top App Bar Component will render a <header /> tag, and will be comprised of 2 main sections:

  1. Navigation icon and title section
  2. Action icons section

If you have questions about the elements that comprise the Top App Bar, visit the documentation on Github.

Modify the render() method in TopAppBar.js to look like this:


  render() {
    const {
    } = this.props;

    return (
        <div className='mdc-top-app-bar__row'>
          <section className='mdc-top-app-bar__section mdc-top-app-bar__section--align-start'>
            {navIcon ? navIcon : null}
            <span className="mdc-top-app-bar__title">

There are two section elements in this HTML. The first contains a navigation icon and title. The second contains action icons. Then add the renderActionItems method:


renderActionItems() {
  const {actionItems} = this.props;
  if (!actionItems) {

  return (
    <section className='mdc-top-app-bar__section mdc-top-app-bar__section--align-end' role='toolbar'>
      {/* need to close element to set key */}
      {, key) => React.cloneElement(item, {key}))}

A developer will import TopAppBar into their React application and pass action icons to the TopAppBar element. You can see example code initializing a TopAppBar in App.js.

The method this.getMergedStyles is missing, which is used in the render method. Please add the following JavaScript to the TopAppBar class in the TopAppBar.js file.


getMergedStyles = () => {
  const {style} = this.props;
  const {style: internalStyle} = this.state;
  return Object.assign({}, internalStyle, style);

this.classes is also missing from the render method, but will be covered in a later section. Besides the missing getter method, this.classes, there are still pieces of the TopAppBar you need to implement before Top App Bar can render correctly.

The pieces of the React Component that are still missing from Top App Bar are:

The approach

  1. Implement the Adapter methods.
  2. Initialize the Foundation in the componentDidMount.
  3. Call the Foundation.destroy method in the componentWillUnmount.
  4. Establish variant management via a getter method that combines appropriate class names.

The non-framework JS TopAppBar Component implements the following Adapter methods (listed in detail here):

Because React has synthetic events and different best coding practices and patterns, the Adapter methods need to be reimplemented.

Adapter Getter Method

In the TopAppBar.js file add the following JavaScript method to TopAppBar:


get adapter() {
   const {actionItems} = this.props;

   return {
    addClass: (className) => this.setState({classList: this.state.classList.add(className)}),
    removeClass: (className) => {
      const {classList} = this.state;
    hasClass: (className) => this.classes.split(' ').includes(className),
    setStyle: this.setStyle,
    getTopAppBarHeight: () => this.topAppBarElement.current.clientHeight,
    registerScrollHandler: (handler) => window.addEventListener('scroll', handler),
    deregisterScrollHandler: (handler) => window.removeEventListener('scroll', handler),
    getViewportScrollY: () => window.pageYOffset,
    getTotalActionItems: () => actionItems && actionItems.length,

The getViewPortScrollY, registerScrollHandler and deregisterScrollHandler code is identical to the non-framework JS version, because React doesn't have any Synthetic event for scrolling and defers to the native DOM event system. getViewPortScrollY also needs to defer to the native DOM since it is a function on the window object, which is not in React's API. Adapter implementations will be different for each framework.

Some dependencies will need to be imported in order for this to work. Imports are declared above the class declaration like so:


import React from 'react';
// TopAppBar is not using either of these classes yet, but you will need them in the next section.
import {MDCTopAppBarFoundation, MDCFixedTopAppBarFoundation, MDCShortTopAppBarFoundation} from '@material/top-app-bar';

export default class TopAppBar extends React.Component {

You may notice this.setStyle is missing, which is called by the get adapter method. In the TopAppBar.js file, add the missing JavaScript method to the TopAppBar:

setStyle = (varName, value) => {
  const updatedStyle = Object.assign({},;
  updatedStyle[varName] = value;
  this.setState({style: updatedStyle});

The next section will guide you through how to add and remove CSS classes.

Managing Variants and Classes

React doesn't have an API to manage classes. To mimic native JavaScript's add/remove CSS class methods, add the classList state variable. There are three pieces of code in TopAppBar that interact with CSS classes:

  1. <TopAppBar /> component via the className prop.
  2. The Adapter method via addClass or removeClass.
  3. Hard coded within the <TopAppBar /> React Component.

Copy the following code and paste it into the class declaration of the TopAppBar Component:


import classnames from 'classnames';


export default class TopAppBar extends React.Component {

  constructor(props) {
    this.topAppBarElement = React.createRef();

  state = {
    classList: new Set(),
    style: {},

  get classes() {
    const {classList} = this.state;
    const {
    } = this.props;

    return classnames('mdc-top-app-bar', Array.from(classList), className, {
      'mdc-top-app-bar--fixed': fixed,
      'mdc-top-app-bar--short': short,
      'mdc-top-app-bar--short-collapsed': alwaysCollapsed,
      'mdc-top-app-bar--prominent': prominent,


If you head to http://localhost:8080 the Controls checkboxes should now toggle on/off the class names from the DOM.

This code makes TopAppBar usable by many developers. Developers can interact with the TopAppBar API, without worrying about the implementation details of CSS classes.

You've now successfully implemented the Adapter. The next section will guide you through instantiating a Foundation.

Mounting and Unmounting Component

Foundation instantiation happens in the componentDidMount method.

Add the following JavaScript code into the TopAppBar Component:


export default class TopAppBar extends React.Component {

  foundation_ = null;

  componentDidMount() {

  componentWillUnmount() {

  initializeFoundation = () => {
    if (this.props.short) {
      this.foundation_ = new MDCShortTopAppBarFoundation(this.adapter);
    } else if(this.props.fixed) {
      this.foundation_ = new MDCFixedTopAppBarFoundation(this.adapter);
    } else {
      this.foundation_ = new MDCTopAppBarFoundation(this.adapter);



One good React coding practice is to define PropTypes and defaultProps. Please add the following code to the bottom of the TopAppBar.js file:

import PropTypes from 'prop-types';

export default class TopAppBar extends React.Component {


TopAppBar.propTypes = {
  alwaysCollapsed: PropTypes.bool,
  short: PropTypes.bool,
  fixed: PropTypes.bool,
  prominent: PropTypes.bool,
  title: PropTypes.string,
  actionItems: PropTypes.arrayOf(PropTypes.element),
  navIcon: PropTypes.element,

TopAppBar.defaultProps = {
  alwaysCollapsed: false,
  short: false,
  fixed: false,
  prominent: false,
  title: '',
  actionItems: null,
  navIcon: null,

You have now successfully implemented the Top App Bar React Component. If you navigate to http://localhost:8080 you can play with the demo page. The demo page will work the same as MDC Web's demo page. The demo page should look like this:

In this tutorial we covered how to wrap MDC Web's Foundation for use in a React application. There are a few libraries on Github and npm that wrap MDC Web Components as described in Integrating into Frameworks. We recommend you use the list found here. This list also includes other frameworks besides React such as Angular and Vue.

This tutorial highlights our decision to split MDC Web code into 3 parts, the Foundation, Adapter, and Component. These parts work with all frameworks. Thanks for trying Material Components React and please check out our new library MDC Web React. We hope you enjoyed this codelab!

I was able to complete this codelab with a reasonable amount of time and effort

Strongly agree Agree Neutral Disagree Strongly disagree

I would like to continue using Material Components in the future

Strongly agree Agree Neutral Disagree Strongly disagree