viewport.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /******/ (function() { // webpackBootstrap
  2. /******/ "use strict";
  3. /******/ // The require scope
  4. /******/ var __webpack_require__ = {};
  5. /******/
  6. /************************************************************************/
  7. /******/ /* webpack/runtime/define property getters */
  8. /******/ !function() {
  9. /******/ // define getter functions for harmony exports
  10. /******/ __webpack_require__.d = function(exports, definition) {
  11. /******/ for(var key in definition) {
  12. /******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
  13. /******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
  14. /******/ }
  15. /******/ }
  16. /******/ };
  17. /******/ }();
  18. /******/
  19. /******/ /* webpack/runtime/hasOwnProperty shorthand */
  20. /******/ !function() {
  21. /******/ __webpack_require__.o = function(obj, prop) { return Object.prototype.hasOwnProperty.call(obj, prop); }
  22. /******/ }();
  23. /******/
  24. /******/ /* webpack/runtime/make namespace object */
  25. /******/ !function() {
  26. /******/ // define __esModule on exports
  27. /******/ __webpack_require__.r = function(exports) {
  28. /******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
  29. /******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
  30. /******/ }
  31. /******/ Object.defineProperty(exports, '__esModule', { value: true });
  32. /******/ };
  33. /******/ }();
  34. /******/
  35. /************************************************************************/
  36. var __webpack_exports__ = {};
  37. // ESM COMPAT FLAG
  38. __webpack_require__.r(__webpack_exports__);
  39. // EXPORTS
  40. __webpack_require__.d(__webpack_exports__, {
  41. "ifViewportMatches": function() { return /* reexport */ if_viewport_matches; },
  42. "store": function() { return /* reexport */ store; },
  43. "withViewportMatch": function() { return /* reexport */ with_viewport_match; }
  44. });
  45. // NAMESPACE OBJECT: ./node_modules/@wordpress/viewport/build-module/store/actions.js
  46. var actions_namespaceObject = {};
  47. __webpack_require__.r(actions_namespaceObject);
  48. __webpack_require__.d(actions_namespaceObject, {
  49. "setIsMatching": function() { return setIsMatching; }
  50. });
  51. // NAMESPACE OBJECT: ./node_modules/@wordpress/viewport/build-module/store/selectors.js
  52. var selectors_namespaceObject = {};
  53. __webpack_require__.r(selectors_namespaceObject);
  54. __webpack_require__.d(selectors_namespaceObject, {
  55. "isViewportMatch": function() { return isViewportMatch; }
  56. });
  57. ;// CONCATENATED MODULE: external "lodash"
  58. var external_lodash_namespaceObject = window["lodash"];
  59. ;// CONCATENATED MODULE: external ["wp","data"]
  60. var external_wp_data_namespaceObject = window["wp"]["data"];
  61. ;// CONCATENATED MODULE: ./node_modules/@wordpress/viewport/build-module/store/reducer.js
  62. /**
  63. * Reducer returning the viewport state, as keys of breakpoint queries with
  64. * boolean value representing whether query is matched.
  65. *
  66. * @param {Object} state Current state.
  67. * @param {Object} action Dispatched action.
  68. *
  69. * @return {Object} Updated state.
  70. */
  71. function reducer() {
  72. let state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  73. let action = arguments.length > 1 ? arguments[1] : undefined;
  74. switch (action.type) {
  75. case 'SET_IS_MATCHING':
  76. return action.values;
  77. }
  78. return state;
  79. }
  80. /* harmony default export */ var store_reducer = (reducer);
  81. ;// CONCATENATED MODULE: ./node_modules/@wordpress/viewport/build-module/store/actions.js
  82. /**
  83. * Returns an action object used in signalling that viewport queries have been
  84. * updated. Values are specified as an object of breakpoint query keys where
  85. * value represents whether query matches.
  86. *
  87. * @param {Object} values Breakpoint query matches.
  88. *
  89. * @return {Object} Action object.
  90. */
  91. function setIsMatching(values) {
  92. return {
  93. type: 'SET_IS_MATCHING',
  94. values
  95. };
  96. }
  97. ;// CONCATENATED MODULE: ./node_modules/@wordpress/viewport/build-module/store/selectors.js
  98. /**
  99. * Returns true if the viewport matches the given query, or false otherwise.
  100. *
  101. * @param {Object} state Viewport state object.
  102. * @param {string} query Query string. Includes operator and breakpoint name,
  103. * space separated. Operator defaults to >=.
  104. *
  105. * @example
  106. *
  107. * ```js
  108. * isViewportMatch( state, '< huge' );
  109. * isViewPortMatch( state, 'medium' );
  110. * ```
  111. *
  112. * @return {boolean} Whether viewport matches query.
  113. */
  114. function isViewportMatch(state, query) {
  115. // Default to `>=` if no operator is present.
  116. if (query.indexOf(' ') === -1) {
  117. query = '>= ' + query;
  118. }
  119. return !!state[query];
  120. }
  121. ;// CONCATENATED MODULE: ./node_modules/@wordpress/viewport/build-module/store/index.js
  122. /**
  123. * WordPress dependencies
  124. */
  125. /**
  126. * Internal dependencies
  127. */
  128. const STORE_NAME = 'core/viewport';
  129. /**
  130. * Store definition for the viewport namespace.
  131. *
  132. * @see https://github.com/WordPress/gutenberg/blob/HEAD/packages/data/README.md#createReduxStore
  133. *
  134. * @type {Object}
  135. */
  136. const store = (0,external_wp_data_namespaceObject.createReduxStore)(STORE_NAME, {
  137. reducer: store_reducer,
  138. actions: actions_namespaceObject,
  139. selectors: selectors_namespaceObject
  140. });
  141. (0,external_wp_data_namespaceObject.register)(store);
  142. ;// CONCATENATED MODULE: ./node_modules/@wordpress/viewport/build-module/listener.js
  143. /**
  144. * External dependencies
  145. */
  146. /**
  147. * WordPress dependencies
  148. */
  149. /**
  150. * Internal dependencies
  151. */
  152. const addDimensionsEventListener = (breakpoints, operators) => {
  153. /**
  154. * Callback invoked when media query state should be updated. Is invoked a
  155. * maximum of one time per call stack.
  156. */
  157. const setIsMatching = (0,external_lodash_namespaceObject.debounce)(() => {
  158. const values = (0,external_lodash_namespaceObject.mapValues)(queries, query => query.matches);
  159. (0,external_wp_data_namespaceObject.dispatch)(store).setIsMatching(values);
  160. }, {
  161. leading: true
  162. });
  163. /**
  164. * Hash of breakpoint names with generated MediaQueryList for corresponding
  165. * media query.
  166. *
  167. * @see https://developer.mozilla.org/en-US/docs/Web/API/Window/matchMedia
  168. * @see https://developer.mozilla.org/en-US/docs/Web/API/MediaQueryList
  169. *
  170. * @type {Object<string,MediaQueryList>}
  171. */
  172. const queries = (0,external_lodash_namespaceObject.reduce)(breakpoints, (result, width, name) => {
  173. (0,external_lodash_namespaceObject.forEach)(operators, (condition, operator) => {
  174. const list = window.matchMedia(`(${condition}: ${width}px)`);
  175. list.addListener(setIsMatching);
  176. const key = [operator, name].join(' ');
  177. result[key] = list;
  178. });
  179. return result;
  180. }, {});
  181. window.addEventListener('orientationchange', setIsMatching); // Set initial values.
  182. setIsMatching();
  183. setIsMatching.flush();
  184. };
  185. /* harmony default export */ var listener = (addDimensionsEventListener);
  186. ;// CONCATENATED MODULE: external ["wp","compose"]
  187. var external_wp_compose_namespaceObject = window["wp"]["compose"];
  188. ;// CONCATENATED MODULE: ./node_modules/@babel/runtime/helpers/esm/extends.js
  189. function _extends() {
  190. _extends = Object.assign ? Object.assign.bind() : function (target) {
  191. for (var i = 1; i < arguments.length; i++) {
  192. var source = arguments[i];
  193. for (var key in source) {
  194. if (Object.prototype.hasOwnProperty.call(source, key)) {
  195. target[key] = source[key];
  196. }
  197. }
  198. }
  199. return target;
  200. };
  201. return _extends.apply(this, arguments);
  202. }
  203. ;// CONCATENATED MODULE: external ["wp","element"]
  204. var external_wp_element_namespaceObject = window["wp"]["element"];
  205. ;// CONCATENATED MODULE: ./node_modules/@wordpress/viewport/build-module/with-viewport-match.js
  206. /**
  207. * External dependencies
  208. */
  209. /**
  210. * WordPress dependencies
  211. */
  212. /**
  213. * Higher-order component creator, creating a new component which renders with
  214. * the given prop names, where the value passed to the underlying component is
  215. * the result of the query assigned as the object's value.
  216. *
  217. * @see isViewportMatch
  218. *
  219. * @param {Object} queries Object of prop name to viewport query.
  220. *
  221. * @example
  222. *
  223. * ```jsx
  224. * function MyComponent( { isMobile } ) {
  225. * return (
  226. * <div>Currently: { isMobile ? 'Mobile' : 'Not Mobile' }</div>
  227. * );
  228. * }
  229. *
  230. * MyComponent = withViewportMatch( { isMobile: '< small' } )( MyComponent );
  231. * ```
  232. *
  233. * @return {Function} Higher-order component.
  234. */
  235. const withViewportMatch = queries => {
  236. const useViewPortQueriesResult = () => (0,external_lodash_namespaceObject.mapValues)(queries, query => {
  237. let [operator, breakpointName] = query.split(' ');
  238. if (breakpointName === undefined) {
  239. breakpointName = operator;
  240. operator = '>=';
  241. } // Hooks should unconditionally execute in the same order,
  242. // we are respecting that as from the static query of the HOC we generate
  243. // a hook that calls other hooks always in the same order (because the query never changes).
  244. // eslint-disable-next-line react-hooks/rules-of-hooks
  245. return (0,external_wp_compose_namespaceObject.useViewportMatch)(breakpointName, operator);
  246. });
  247. return (0,external_wp_compose_namespaceObject.createHigherOrderComponent)(WrappedComponent => {
  248. return (0,external_wp_compose_namespaceObject.pure)(props => {
  249. const queriesResult = useViewPortQueriesResult();
  250. return (0,external_wp_element_namespaceObject.createElement)(WrappedComponent, _extends({}, props, queriesResult));
  251. });
  252. }, 'withViewportMatch');
  253. };
  254. /* harmony default export */ var with_viewport_match = (withViewportMatch);
  255. ;// CONCATENATED MODULE: ./node_modules/@wordpress/viewport/build-module/if-viewport-matches.js
  256. /**
  257. * WordPress dependencies
  258. */
  259. /**
  260. * Internal dependencies
  261. */
  262. /**
  263. * Higher-order component creator, creating a new component which renders if
  264. * the viewport query is satisfied.
  265. *
  266. * @see withViewportMatches
  267. *
  268. * @param {string} query Viewport query.
  269. *
  270. * @example
  271. *
  272. * ```jsx
  273. * function MyMobileComponent() {
  274. * return <div>I'm only rendered on mobile viewports!</div>;
  275. * }
  276. *
  277. * MyMobileComponent = ifViewportMatches( '< small' )( MyMobileComponent );
  278. * ```
  279. *
  280. * @return {Function} Higher-order component.
  281. */
  282. const ifViewportMatches = query => (0,external_wp_compose_namespaceObject.createHigherOrderComponent)((0,external_wp_compose_namespaceObject.compose)([with_viewport_match({
  283. isViewportMatch: query
  284. }), (0,external_wp_compose_namespaceObject.ifCondition)(props => props.isViewportMatch)]), 'ifViewportMatches');
  285. /* harmony default export */ var if_viewport_matches = (ifViewportMatches);
  286. ;// CONCATENATED MODULE: ./node_modules/@wordpress/viewport/build-module/index.js
  287. /**
  288. * Internal dependencies
  289. */
  290. /**
  291. * Hash of breakpoint names with pixel width at which it becomes effective.
  292. *
  293. * @see _breakpoints.scss
  294. *
  295. * @type {Object}
  296. */
  297. const BREAKPOINTS = {
  298. huge: 1440,
  299. wide: 1280,
  300. large: 960,
  301. medium: 782,
  302. small: 600,
  303. mobile: 480
  304. };
  305. /**
  306. * Hash of query operators with corresponding condition for media query.
  307. *
  308. * @type {Object}
  309. */
  310. const OPERATORS = {
  311. '<': 'max-width',
  312. '>=': 'min-width'
  313. };
  314. listener(BREAKPOINTS, OPERATORS);
  315. (window.wp = window.wp || {}).viewport = __webpack_exports__;
  316. /******/ })()
  317. ;