81 lines
3.9 KiB
JavaScript
81 lines
3.9 KiB
JavaScript
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
|
|
|
|
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
|
|
|
|
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
|
|
import { getBreakpoints, getMediaqueries, getQueriesObjects } from '../utils';
|
|
import PropTypes from 'prop-types';
|
|
import React, { useMemo, useRef } from 'react';
|
|
import ResponsiveContext from '../ResponsiveContext';
|
|
import useDebugResponsive from './useDebugResponsive';
|
|
import useMediaType from './useMediaType';
|
|
import useOrientation from './useOrientation';
|
|
|
|
var ResponsiveProvider = function ResponsiveProvider(_ref) {
|
|
var initialMediaType = _ref.initialMediaType,
|
|
defaultOrientation = _ref.defaultOrientation,
|
|
children = _ref.children,
|
|
breakpoints = _ref.breakpoints,
|
|
breakpointsMax = _ref.breakpointsMax,
|
|
mediaQueries = _ref.mediaQueries,
|
|
mobileBreakpoint = _ref.mobileBreakpoint;
|
|
|
|
var breakpointsWithInitialValue = _objectSpread({
|
|
_initial: '0em'
|
|
}, breakpoints);
|
|
|
|
var breakpointNames = Object.keys(mediaQueries || breakpointsWithInitialValue);
|
|
|
|
if (!mediaQueries) {
|
|
mediaQueries = getMediaqueries(breakpointsWithInitialValue, breakpointsMax, breakpointNames);
|
|
}
|
|
|
|
var breakpointsRef = useRef(getBreakpoints(mediaQueries, breakpointNames));
|
|
|
|
var _useMediaType = useMediaType(breakpointsRef, initialMediaType),
|
|
currentMediaType = _useMediaType.currentMediaType,
|
|
isCalculated = _useMediaType.isCalculated;
|
|
|
|
var queriesObjects = useMemo(function () {
|
|
return getQueriesObjects(currentMediaType, breakpointsRef.current);
|
|
}, [currentMediaType, breakpointsRef]);
|
|
var currentOrientation = useOrientation(defaultOrientation);
|
|
var contextObject = useMemo(function () {
|
|
return _objectSpread({
|
|
mediaType: currentMediaType,
|
|
orientation: currentOrientation,
|
|
isCalculated: isCalculated,
|
|
mobileBreakpoint: mobileBreakpoint
|
|
}, queriesObjects);
|
|
}, [currentMediaType, currentOrientation, isCalculated, mobileBreakpoint, queriesObjects]);
|
|
useDebugResponsive(contextObject, currentMediaType);
|
|
return /*#__PURE__*/React.createElement(ResponsiveContext.Provider, {
|
|
value: contextObject
|
|
}, children);
|
|
};
|
|
|
|
var breakpointsPropTypes = {
|
|
xs: PropTypes.string,
|
|
sm: PropTypes.string,
|
|
md: PropTypes.string,
|
|
lg: PropTypes.string,
|
|
xl: PropTypes.string
|
|
};
|
|
ResponsiveProvider.defaultProps = {
|
|
initialMediaType: 'xs',
|
|
defaultOrientation: null,
|
|
mobileBreakpoint: 'md'
|
|
};
|
|
ResponsiveProvider.propTypes = {
|
|
initialMediaType: PropTypes.oneOf(['_initial', 'xs', 'sm', 'md', 'lg', 'xl']),
|
|
defaultOrientation: PropTypes.oneOf(['landscape', 'portrait']),
|
|
children: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.node), PropTypes.node]).isRequired,
|
|
breakpoints: PropTypes.exact(breakpointsPropTypes),
|
|
breakpointsMax: PropTypes.exact(breakpointsPropTypes),
|
|
mediaQueries: PropTypes.exact(_objectSpread(_objectSpread({}, breakpointsPropTypes), {}, {
|
|
_initial: PropTypes.string
|
|
})),
|
|
mobileBreakpoint: PropTypes.oneOf(['xs', 'sm', 'md', 'lg', 'xl'])
|
|
};
|
|
export default ResponsiveProvider; |