viewport.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. * Ignored from documentation as it is for internal use only.
  87. *
  88. * @ignore
  89. *
  90. * @param {Object} values Breakpoint query matches.
  91. *
  92. * @return {Object} Action object.
  93. */
  94. function setIsMatching(values) {
  95. return {
  96. type: 'SET_IS_MATCHING',
  97. values
  98. };
  99. }
  100. ;// CONCATENATED MODULE: ./node_modules/@wordpress/viewport/build-module/store/selectors.js
  101. /**
  102. * Returns true if the viewport matches the given query, or false otherwise.
  103. *
  104. * @param {Object} state Viewport state object.
  105. * @param {string} query Query string. Includes operator and breakpoint name,
  106. * space separated. Operator defaults to >=.
  107. *
  108. * @example
  109. *
  110. * ```js
  111. * import { store as viewportStore } from '@wordpress/viewport';
  112. * import { useSelect } from '@wordpress/data';
  113. * import { __ } from '@wordpress/i18n';
  114. * const ExampleComponent = () => {
  115. * const isMobile = useSelect(
  116. * ( select ) => select( viewportStore ).isViewportMatch( '< small' ),
  117. * []
  118. * );
  119. *
  120. * return isMobile ? (
  121. * <div>{ __( 'Mobile' ) }</div>
  122. * ) : (
  123. * <div>{ __( 'Not Mobile' ) }</div>
  124. * );
  125. * };
  126. * ```
  127. *
  128. * @return {boolean} Whether viewport matches query.
  129. */
  130. function isViewportMatch(state, query) {
  131. // Default to `>=` if no operator is present.
  132. if (query.indexOf(' ') === -1) {
  133. query = '>= ' + query;
  134. }
  135. return !!state[query];
  136. }
  137. ;// CONCATENATED MODULE: ./node_modules/@wordpress/viewport/build-module/store/index.js
  138. /**
  139. * WordPress dependencies
  140. */
  141. /**
  142. * Internal dependencies
  143. */
  144. const STORE_NAME = 'core/viewport';
  145. /**
  146. * Store definition for the viewport namespace.
  147. *
  148. * @see https://github.com/WordPress/gutenberg/blob/HEAD/packages/data/README.md#createReduxStore
  149. *
  150. * @type {Object}
  151. */
  152. const store = (0,external_wp_data_namespaceObject.createReduxStore)(STORE_NAME, {
  153. reducer: store_reducer,
  154. actions: actions_namespaceObject,
  155. selectors: selectors_namespaceObject
  156. });
  157. (0,external_wp_data_namespaceObject.register)(store);
  158. ;// CONCATENATED MODULE: ./node_modules/@wordpress/viewport/build-module/listener.js
  159. /**
  160. * External dependencies
  161. */
  162. /**
  163. * WordPress dependencies
  164. */
  165. /**
  166. * Internal dependencies
  167. */
  168. const addDimensionsEventListener = (breakpoints, operators) => {
  169. /**
  170. * Callback invoked when media query state should be updated. Is invoked a
  171. * maximum of one time per call stack.
  172. */
  173. const setIsMatching = (0,external_lodash_namespaceObject.debounce)(() => {
  174. const values = (0,external_lodash_namespaceObject.mapValues)(queries, query => query.matches);
  175. (0,external_wp_data_namespaceObject.dispatch)(store).setIsMatching(values);
  176. }, {
  177. leading: true
  178. });
  179. /**
  180. * Hash of breakpoint names with generated MediaQueryList for corresponding
  181. * media query.
  182. *
  183. * @see https://developer.mozilla.org/en-US/docs/Web/API/Window/matchMedia
  184. * @see https://developer.mozilla.org/en-US/docs/Web/API/MediaQueryList
  185. *
  186. * @type {Object<string,MediaQueryList>}
  187. */
  188. const queries = (0,external_lodash_namespaceObject.reduce)(breakpoints, (result, width, name) => {
  189. Object.entries(operators).forEach(_ref => {
  190. let [operator, condition] = _ref;
  191. const list = window.matchMedia(`(${condition}: ${width}px)`);
  192. list.addListener(setIsMatching);
  193. const key = [operator, name].join(' ');
  194. result[key] = list;
  195. });
  196. return result;
  197. }, {});
  198. window.addEventListener('orientationchange', setIsMatching); // Set initial values.
  199. setIsMatching();
  200. setIsMatching.flush();
  201. };
  202. /* harmony default export */ var listener = (addDimensionsEventListener);
  203. ;// CONCATENATED MODULE: external ["wp","compose"]
  204. var external_wp_compose_namespaceObject = window["wp"]["compose"];
  205. ;// CONCATENATED MODULE: ./node_modules/@babel/runtime/helpers/esm/extends.js
  206. function _extends() {
  207. _extends = Object.assign ? Object.assign.bind() : function (target) {
  208. for (var i = 1; i < arguments.length; i++) {
  209. var source = arguments[i];
  210. for (var key in source) {
  211. if (Object.prototype.hasOwnProperty.call(source, key)) {
  212. target[key] = source[key];
  213. }
  214. }
  215. }
  216. return target;
  217. };
  218. return _extends.apply(this, arguments);
  219. }
  220. ;// CONCATENATED MODULE: external ["wp","element"]
  221. var external_wp_element_namespaceObject = window["wp"]["element"];
  222. ;// CONCATENATED MODULE: ./node_modules/@wordpress/viewport/build-module/with-viewport-match.js
  223. /**
  224. * External dependencies
  225. */
  226. /**
  227. * WordPress dependencies
  228. */
  229. /**
  230. * Higher-order component creator, creating a new component which renders with
  231. * the given prop names, where the value passed to the underlying component is
  232. * the result of the query assigned as the object's value.
  233. *
  234. * @see isViewportMatch
  235. *
  236. * @param {Object} queries Object of prop name to viewport query.
  237. *
  238. * @example
  239. *
  240. * ```jsx
  241. * function MyComponent( { isMobile } ) {
  242. * return (
  243. * <div>Currently: { isMobile ? 'Mobile' : 'Not Mobile' }</div>
  244. * );
  245. * }
  246. *
  247. * MyComponent = withViewportMatch( { isMobile: '< small' } )( MyComponent );
  248. * ```
  249. *
  250. * @return {Function} Higher-order component.
  251. */
  252. const withViewportMatch = queries => {
  253. const useViewPortQueriesResult = () => (0,external_lodash_namespaceObject.mapValues)(queries, query => {
  254. let [operator, breakpointName] = query.split(' ');
  255. if (breakpointName === undefined) {
  256. breakpointName = operator;
  257. operator = '>=';
  258. } // Hooks should unconditionally execute in the same order,
  259. // we are respecting that as from the static query of the HOC we generate
  260. // a hook that calls other hooks always in the same order (because the query never changes).
  261. // eslint-disable-next-line react-hooks/rules-of-hooks
  262. return (0,external_wp_compose_namespaceObject.useViewportMatch)(breakpointName, operator);
  263. });
  264. return (0,external_wp_compose_namespaceObject.createHigherOrderComponent)(WrappedComponent => {
  265. return (0,external_wp_compose_namespaceObject.pure)(props => {
  266. const queriesResult = useViewPortQueriesResult();
  267. return (0,external_wp_element_namespaceObject.createElement)(WrappedComponent, _extends({}, props, queriesResult));
  268. });
  269. }, 'withViewportMatch');
  270. };
  271. /* harmony default export */ var with_viewport_match = (withViewportMatch);
  272. ;// CONCATENATED MODULE: ./node_modules/@wordpress/viewport/build-module/if-viewport-matches.js
  273. /**
  274. * WordPress dependencies
  275. */
  276. /**
  277. * Internal dependencies
  278. */
  279. /**
  280. * Higher-order component creator, creating a new component which renders if
  281. * the viewport query is satisfied.
  282. *
  283. * @see withViewportMatches
  284. *
  285. * @param {string} query Viewport query.
  286. *
  287. * @example
  288. *
  289. * ```jsx
  290. * function MyMobileComponent() {
  291. * return <div>I'm only rendered on mobile viewports!</div>;
  292. * }
  293. *
  294. * MyMobileComponent = ifViewportMatches( '< small' )( MyMobileComponent );
  295. * ```
  296. *
  297. * @return {Function} Higher-order component.
  298. */
  299. const ifViewportMatches = query => (0,external_wp_compose_namespaceObject.createHigherOrderComponent)((0,external_wp_compose_namespaceObject.compose)([with_viewport_match({
  300. isViewportMatch: query
  301. }), (0,external_wp_compose_namespaceObject.ifCondition)(props => props.isViewportMatch)]), 'ifViewportMatches');
  302. /* harmony default export */ var if_viewport_matches = (ifViewportMatches);
  303. ;// CONCATENATED MODULE: ./node_modules/@wordpress/viewport/build-module/index.js
  304. /**
  305. * Internal dependencies
  306. */
  307. /**
  308. * Hash of breakpoint names with pixel width at which it becomes effective.
  309. *
  310. * @see _breakpoints.scss
  311. *
  312. * @type {Object}
  313. */
  314. const BREAKPOINTS = {
  315. huge: 1440,
  316. wide: 1280,
  317. large: 960,
  318. medium: 782,
  319. small: 600,
  320. mobile: 480
  321. };
  322. /**
  323. * Hash of query operators with corresponding condition for media query.
  324. *
  325. * @type {Object}
  326. */
  327. const OPERATORS = {
  328. '<': 'max-width',
  329. '>=': 'min-width'
  330. };
  331. listener(BREAKPOINTS, OPERATORS);
  332. (window.wp = window.wp || {}).viewport = __webpack_exports__;
  333. /******/ })()
  334. ;