Using the Breakpoint module

The Breakpoint module provides a method for creating media query breakpoint definitions within Drupal. These can be used by other components, such as the responsive image and toolbar modules, to make Drupal responsive.

Breakpoints are a type of plugin that can be defined in a module's or theme's breakpoints.yml in its directory. In this recipe, we will define three different breakpoints under a custom group.

Note

Breakpoints are defined solely in YAML files from installed modules and themes and are not configurable through the user interface.

Getting ready

Ensure that the Breakpoint module is enabled. If you have used the standard Drupal installation, the module is enabled.

This recipe assumes that you have a custom module created. When you see mymodule, use the machine name of the module that you created.

How to do it...

  1. Create mymodule.breakpoints.yml in your module's base directory. This file will hold the breakpoint configurations.
  2. Firstly, we will add a standard mobile breakpoint that does not have a media query, following mobile first practices:
    mymodule.mobile:
      label: Mobile
      mediaQuery: ''
      weight: 0
  3. Secondly, we will create a standard breakpoint that will run on a larger viewport:
    mymodule.standard:
      label: Standard
      mediaQuery: 'only screen and (min-width: 60em)'
      weight: 1
  4. Thirdly, we will create a wide breakpoint for devices that have a large viewport:
    mymodule.wide:
      label: Wide
      mediaQuery: 'only screen and (min-width: 70em)'
      weight: 2
  5. Go to the Configuration tab and then to Development to rebuild Drupal's cache and make the system aware of the new breakpoints.

How it works...

The Breakpoint module defines the breakpoint configuration entity. Breakpoints do not have any specific form of direct functionalities beyond providing a way to save media queries and grouping them.

The Breakpoint module provides a default manager service. This service is used by other modules to discover breakpoint groups and then all of the breakpoints within a group.

There's more...

Caveat for providing breakpoints from themes

Themes have the ability to provide breakpoints; however, they cannot be automatically discovered if new ones are added once they have been installed. Drupal only reads breakpoints provided by themes when a theme is either installed or uninstalled.

Inside breakpoint.manager, there are two hooks: one for the theme install and one for the theme uninstall. Each hook retrieves the breakpoint manager service and rebuilds the breakpoint definitions. Without any extra deployment steps, new breakpoints added to a theme will not be discovered unless these hooks are fired.

Accessing breakpoints programmatically

Breakpoints are utility configurations for other modules. Breakpoints can be loaded by using the breakpoint manager service and specifying a group. For example, the following code returns all breakpoints used by the Toolbar module:

Drupal::service('breakpoint.manager') getBreakpointsByGroup('toolbar');

This code invokes the Drupal container to return the service to manage breakpoints, which, by default, is DrupalreakpointBreakpointManager. The getBreakpointsByGroup method returns all breakpoints within a group, which are initiated as the DrupalreakpointBreakpointInterface objects.

The Toolbar element class utilizes this workflow to push the breakpoint media query values as JavaScript settings for the JavaScript model to interact with.

Multipliers

The multipliers value is used to support pixel resolution multipliers. This multiplier is used in coordination with retina displays. It is a measure of the viewport's device resolution as a ratio of the device's physical size and independent pixel size. The following is an example of standard multipliers:

  • 1x is normal
  • 1.5x supports Android
  • 2x supports Mac retina devices

See also

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset