Skip to content

Add public API to make JavaScript components responsive to the same breakpoints as the CSS #7000

@romaricpascal

Description

@romaricpascal

What

Create a public API so that custom components can easily observe a MediaQueryList matching one of GOV.UK Frontend's breakpoints.

Proposed shape is two functions, in something like a common/breakpoints module (name up for change):

  1. Make the current getBreakpoints function public with a couple of tweaks:
    a. move the error throwing currently do from the components into the function
    b. this will allow to simplify the return value of the function to a string (never undefined as we'd throw if it is)
    c. cache the reading of the custom property from document.documentElement so we don't call getComputedProperties each time we need
  2. Add a new matchMedia(breakpointName, callback) function (or better suited name) to replace the setupResponsiveChecks from the Tabs and ServiceNavigation components.
    a. the function is responsible for creating a MediaQueryList observing a min-width media query for a given breakpoint, like the setupResponsiveChecks currently do, allowing components to pass in a callback that will be run when the media query is created and when there's change of whether the media query matches
    b. callback should receive either the whole MediaQueryList so it can react to whether the list matches or not

Once those are in, use the new matchMedia function in our components so they don't need to create and store a MediaQueryList themselves and update their checkSupported static method so it checks the presence of the breakpoints and prevent initialisation of components.

Why

It's a bit of logistics to read the breakpoint values from the DOM, throw errors and prevent component initialisation, and it's the same for both our responsive components and the mobile navigation on the Design System site.

Who needs to work on this

Developers

Who needs to review this

Developers

Done when

  • API for reading breakpoints has been implemented
  • Component's checkSupported method has been updated
  • Component's setupResponsiveChecks method has been replaced
  • Frontend docs documentation for creating your own components has an explanation on how to make components responsive
  • MobileNavigationSection in the Design System site uses the public API

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions