All files / helpers responsive-helpers.js

100% Statements 8/8
100% Branches 3/3
100% Functions 4/4
100% Lines 8/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35                                  6x       130x   130x 131x     130x     6x 42x 126x    
/*
Copyright (c) 2018-2019 Uber Technologies, Inc.
 
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
*/
// @flow
 
import type {BreakpointsT} from '../styles/types.js';
 
/**
 * Helper function that generates media queries based on given parameters
 *
 * E.g.
 * getMediaQuery({'max-width': '1280px', 'min-height': '720px'}, 'and') =>
 *   '@media screen and (max-width: 1280px) and (min-height: 720px)'
 */
export const getMediaQuery = (
  options: {[string]: string},
  booleanOperator: 'AND' | 'OR' = 'OR',
): string => {
  const mediaFeatureSeparator = booleanOperator === 'OR' ? ', ' : ' and ';
 
  const mediaFeatures = Object.keys(options).map(
    key => `(${key}: ${options[key]})`,
  );
 
  return `@media screen and ${mediaFeatures.join(mediaFeatureSeparator)}`;
};
 
export const getMediaQueries = (breakpoints: BreakpointsT): string[] =>
  Object.keys(breakpoints).map(size =>
    getMediaQuery({'min-width': `${breakpoints[size]}px`}),
  );