query

A utility function for composing an inline media query string in CSS.

Specification

The query function is great for a shorthand declaration of inline media query in CSS. This function support the same call signature as the useResponsiveQuery hook, but unlike the mentioned hook it returns a plain media query string:

query({ from: 'md' })
// (min-width: 768px)

The main purpose of this utility function is to align your inline media queries declarations with the list of defined breakpoints in Atomic Layout, making those a source of truth for both React and non-react usage of responsive behavior.

Example

Exact breakpoint

import styled from 'styled-components'
import { query } from 'atomic-layout'

export const Example = styled.div`
  font-size: 14px;
  
  /* Targets the "lg" breakpoint only */
  @media ${query({ for: 'lg' })} {
    font-size: 16px;
  }
`

High-pass breakpoint range

To target a breakpoint range starting from a certain breakpoint use the from property on query:

Low-pass breakpoint range

Bell breakpoint range

To create a range from one breakpoint to another use the from and to properties:

Notch breakpoint range

Using custom breakpoints

It is also possible to provide a Breakpoint object as the value of for, from, and to properties.

Last updated