Foundry Industrial Strength JavaScript

Responsive Modules Using CSS3 Media Queries

Responsive Design and CSS Media Queries have enabled Mobile First Design. With Foundry, your JavaScript can be just as responsive and Mobile First as your CSS.

Download The Demo

View The Demo: Responsive Modules

What You'll Need For This Tutorial

  1. The Foundry Starter Project
  2. A basic understanding of how Modules work in Foundry
  3. Moderate knowledge of Responsive Design and CSS Media Queries.

What You'll Learn

What Is A Responsive Module?

Responsive Modules are just like every other module you create for Foundry, except they only get loaded by the framework if the data-module-media attribute matches the current viewport dimensions.

Not every module we build would look good or even function well on every screen size. With that in mind, you are free to create as big and complex a module as you need to solve the problem. If it brings mobile devices to their knees, no big deal:

Hey Foundry, only load this module on 1000px and wider screens

<div data-modules="ComplexModule" data-module-media="screen and (min-width: 1000px)"></div>

You may also have a small, simple module that works well for small devices, but adds little value to larger ones:

Hey Foundry, only load this module on 300px and narrower screens

<div data-modules="MiniModule" data-module-media="screen and (max-width: 300px)"></div>

You can do all this, and still mix in modules that work across the board:

<div data-modules="MiniModule" data-module-media="screen and (max-width: 300px)"></div>

<div data-modules="GoldilocksModule">
    I'm not too hot and not too cold!
</div>

<div data-modules="ComplexModule" data-module-media="screen and (min-width: 1000px)"></div>

Responsive Module Best Practices

When creating Responsive Modules, you must bear in mind that those modules might not always need to appear on screen. Given that, you should follow these guidelines when making a module responsive:

Eager and Lazy Loading Responsive Modules

Responsive Modules can be Eager or Lazy Loaded by Foundry just like any other module:

<!-- Eager Load -->
<div class="foo"
    data-modules="ComplexModule"
    data-module-media="screen and (min-width: 1000px)"></div>

<!-- Lazy Load -->
<div class="foo"
    data-modules="ComplexModule"
    data-module-media="screen and (min-width: 1000px)"
    data-module-lazyload="any"></div>

Demo: Responsive Modules

Browser Support For Responsive Modules

Foundry uses the native matchMedia method available in newer browsers:

Source: matchMedia Browser Compatibility (MDN)

A JavaScript error will get thrown in older browsers, referring to a Pollyfill that can be utilized to enable this feature.

Up Next: Integrating Pollyfills With Foundry

Foundry wasn't built with older browsers in mind, which means many older browsers aren't supported. The next tutorial shows you how to enable support for older browsers using Pollyfills. We'll explore this by enabling Responsive Modules for browsers that do not support the matchMedia function in JavaScript.