@atlaskit/primitives
Version:
Primitives are token-backed low-level building blocks.
70 lines (69 loc) • 3.57 kB
JavaScript
import _defineProperty from "@babel/runtime/helpers/defineProperty";
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
// eslint-disable-next-line @atlaskit/ui-styling-standard/use-compiled -- Ignored via go/DSP-18766
import { css } from '@emotion/react';
import { UNSAFE_BREAKPOINTS_ORDERED_LIST } from './constants';
import { media, UNSAFE_media } from './media-helper';
/**
* Build a map of breakpoints to css with media queries and nested styles.
*
* @internal Not intended to be used outside of DST at this stage.
* @experimental Not intended to be used outside of DST at this stage.
*
* @example
* A map to build optional `display:none` for consumption on a div.
* ```ts
* const hideMediaQueries = buildAboveMediaQueryCSS({ display: 'none' });
*
* const Component = ({ hideAtBreakpoints: ('xs' | 'sm')[], children: ReactNode }) => {
* return <div css={hideAtBreakpoints.map(b => hideMediaQueries[b])}>{children}</div>;
* }
* ```
*
* This roughly builds a map that will look roughly like this (if done manually):
* ```ts
* {
* xxs: css({ '@media all': { display: 'none' } }),
* xs: css({ '@media (min-width: 30rem)': { display: 'none' } }),
* sm: css({ '@media (min-width: 48rem)': { display: 'none' } }),
* }
* ```
*/
export var UNSAFE_buildAboveMediaQueryCSS = function UNSAFE_buildAboveMediaQueryCSS(input) {
return UNSAFE_BREAKPOINTS_ORDERED_LIST.reduce(function (acc, breakpoint) {
return _objectSpread(_objectSpread({}, acc), {}, _defineProperty({}, breakpoint, css(_defineProperty({}, media.above[breakpoint], typeof input === 'function' ? input(breakpoint) : input))));
}, {});
};
/**
* Build a map of breakpoints to css with media queries and nested styles.
*
* @internal Not intended to be used outside of DST at this stage.
* @experimental Not intended to be used outside of DST at this stage.
*
* @example
* A map to build optional `display:none` for consumption on a div.
* ```ts
* const hideMediaQueries = buildBelowMediaQueryCSS({ display: 'none' });
*
* const Component = ({ hideAtBreakpoints: ('xs' | 'sm')[], children: ReactNode }) => {
* return <div css={hideAtBreakpoints.map(b => hideMediaQueries[b])}>{children}</div>;
* }
* ```
*
* This roughly builds a map that will look roughly like this (if done manually):
* ```ts
* {
* xs: css({ '@media not all and (min-width: 30rem)': { display: 'none' } }),
* sm: css({ '@media not all and (min-width: 48rem)': { display: 'none' } }),
* }
* ```
*/
export var UNSAFE_buildBelowMediaQueryCSS = function UNSAFE_buildBelowMediaQueryCSS(input) {
return UNSAFE_BREAKPOINTS_ORDERED_LIST.reduce(function (acc, breakpoint) {
if (breakpoint === 'xxs') {
return acc;
}
return _objectSpread(_objectSpread({}, acc), {}, _defineProperty({}, breakpoint, css(_defineProperty({}, UNSAFE_media.below[breakpoint], typeof input === 'function' ? input(breakpoint) : input))));
}, {});
};