JavaScript in responsive web design

This topic describes the JavaScript used in out-of-the-box Blank and Luma themes to relocate certain elements and change their behavior depending on the breakpoint.

Scripts general overview

The Blank and Luma themes use the following scripts to responsively relocate page elements by breakpoint:

The script files are located in the file system as follows:

├── lib/web/
    ├── matchMedia.js
    ├── mage/
       ├── menu.js

In a mobile view, on the 768px breakpoint, menu.js changes the navigation menu look and behavior the following way:

The following image illustrates the mobile-view navigation menu.

responsive menu

Re-using scripts in your theme

You can use the menu.js and matchMedia.js to add responsive behavior in your custom theme. If your theme inherits from Blank or Luma, you do not even need to additionally include the script files in your theme.

If your theme does not inherit from Blank or Luma, to be able to use the scripts, you must configure RequireJS for your theme.