react-google-photo.cjs.development.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
  4. var React = require('react');
  5. var React__default = _interopDefault(React);
  6. var ReactDom = _interopDefault(require('react-dom'));
  7. var noScroll = _interopDefault(require('no-scroll'));
  8. var cx = _interopDefault(require('classnames'));
  9. var screenfull = _interopDefault(require('screenfull'));
  10. function _objectWithoutPropertiesLoose(source, excluded) {
  11. if (source == null) return {};
  12. var target = {};
  13. var sourceKeys = Object.keys(source);
  14. var key, i;
  15. for (i = 0; i < sourceKeys.length; i++) {
  16. key = sourceKeys[i];
  17. if (excluded.indexOf(key) >= 0) continue;
  18. target[key] = source[key];
  19. }
  20. return target;
  21. }
  22. function CloseArrow(_ref) {
  23. var className = _ref.className,
  24. props = _objectWithoutPropertiesLoose(_ref, ["className"]);
  25. return React__default.createElement("div", Object.assign({
  26. className: className
  27. }, props), React__default.createElement("svg", {
  28. fill: "#ffffff",
  29. width: "24px",
  30. height: "24px",
  31. viewBox: "0 0 24 24"
  32. }, React__default.createElement("path", {
  33. d: "M20 11H7.83l5.59-5.59L12 4l-8 8 8 8 1.41-1.41L7.83 13H20v-2z"
  34. })));
  35. }
  36. function PrevArrowButton(_ref2) {
  37. var className = _ref2.className,
  38. props = _objectWithoutPropertiesLoose(_ref2, ["className"]);
  39. return React__default.createElement("div", Object.assign({
  40. className: className
  41. }, props), React__default.createElement("svg", {
  42. fill: "#ffffff",
  43. width: "36px",
  44. height: "36px",
  45. viewBox: "0 0 24 24"
  46. }, React__default.createElement("path", {
  47. d: "M15.41 16.09l-4.58-4.59 4.58-4.59L14 5.5l-6 6 6 6z"
  48. })));
  49. }
  50. function NextArrowButton(_ref3) {
  51. var className = _ref3.className,
  52. props = _objectWithoutPropertiesLoose(_ref3, ["className"]);
  53. return React__default.createElement("div", Object.assign({
  54. className: className
  55. }, props), React__default.createElement("svg", {
  56. fill: "#ffffff",
  57. width: "36px",
  58. height: "36px",
  59. viewBox: "0 0 24 24"
  60. }, React__default.createElement("path", {
  61. d: "M8.59 16.34l4.58-4.59-4.58-4.59L10 5.75l6 6-6 6z"
  62. })));
  63. }
  64. var useEventListener = function useEventListener(active, eventName, handler, element) {
  65. // Create a ref that stores handler
  66. var savedHandler = React.useRef(); // Update ref.current value if handler changes.
  67. // This allows our effect below to always get latest handler ...
  68. // ... without us needing to pass it in effect deps array ...
  69. // ... and potentially cause effect to re-run every render.
  70. React.useEffect(function () {
  71. savedHandler.current = handler;
  72. }, [handler]);
  73. React.useEffect(function () {
  74. if (!element || !element.addEventListener) {
  75. return;
  76. } // Create event listener that calls handler function stored in ref
  77. var eventListener = function eventListener(event) {
  78. return savedHandler.current(event);
  79. };
  80. if (active) {
  81. // Add event listener
  82. element.addEventListener(eventName, eventListener);
  83. } // Remove event listener on cleanup
  84. return function () {
  85. element.removeEventListener(eventName, eventListener);
  86. };
  87. }, // Re-run if eventName or element changes
  88. [active, eventName, element]);
  89. };
  90. var keycodes = {
  91. esc: 27,
  92. left: 37,
  93. right: 39
  94. };
  95. var classes = {
  96. overlay: 'react-google-photo-overlay',
  97. column: 'react-google-photo-column',
  98. leftColumn: 'react-google-photo-left-column',
  99. rightColumn: 'react-google-photo-right-column',
  100. arrowButton: 'react-google-photo-arrow-button',
  101. arrowButtonHide: 'react-google-photo-arrow-button-hide',
  102. arrowButtonLeft: 'react-google-photo-arrow-button-left',
  103. arrowButtonRight: 'react-google-photo-arrow-button-right',
  104. arrowButtonReturn: 'react-google-photo-arrow-button-return',
  105. image: 'react-google-photo-overlay-image',
  106. imageOpen: 'react-google-photo-overlay-image-open',
  107. animationIn: 'react-google-photo-fade-in',
  108. animationOut: 'react-google-photo-fade-out'
  109. };
  110. var isBrowser = typeof window !== 'undefined';
  111. var Direction;
  112. (function (Direction) {
  113. Direction[Direction["Prev"] = 0] = "Prev";
  114. Direction[Direction["Next"] = 1] = "Next";
  115. })(Direction || (Direction = {}));
  116. var GooglePhoto = function GooglePhoto(_ref) {
  117. var _classNames$animation, _classNames$animation2, _cx2, _cx3, _cx4;
  118. var open = _ref.open,
  119. src = _ref.src,
  120. _ref$srcIndex = _ref.srcIndex,
  121. srcIndexProp = _ref$srcIndex === void 0 ? 0 : _ref$srcIndex,
  122. fullscreen = _ref.fullscreen,
  123. _ref$keyboardNavigati = _ref.keyboardNavigation,
  124. keyboardNavigation = _ref$keyboardNavigati === void 0 ? true : _ref$keyboardNavigati,
  125. _ref$closeOnEsc = _ref.closeOnEsc,
  126. closeOnEsc = _ref$closeOnEsc === void 0 ? true : _ref$closeOnEsc,
  127. _ref$mouseIdleTimeout = _ref.mouseIdleTimeout,
  128. mouseIdleTimeout = _ref$mouseIdleTimeout === void 0 ? 5000 : _ref$mouseIdleTimeout,
  129. _ref$animationDuratio = _ref.animationDuration,
  130. animationDuration = _ref$animationDuratio === void 0 ? 250 : _ref$animationDuratio,
  131. classNames = _ref.classNames,
  132. onClose = _ref.onClose,
  133. onChangeIndex = _ref.onChangeIndex;
  134. var refContainer = React.useRef(null);
  135. var refTimeoutMouseIdle = React.useRef(null);
  136. var _useState = React.useState(open),
  137. showPortal = _useState[0],
  138. setShowPortal = _useState[1];
  139. var _useState2 = React.useState({
  140. width: isBrowser ? window.innerWidth : 0,
  141. height: isBrowser ? window.innerHeight : 0
  142. }),
  143. windowSizes = _useState2[0],
  144. setWindowSizes = _useState2[1];
  145. var _useState3 = React.useState(false),
  146. mouseIdle = _useState3[0],
  147. setMouseIdle = _useState3[1];
  148. var _useState4 = React.useState(srcIndexProp),
  149. srcIndex = _useState4[0],
  150. setSrcIndex = _useState4[1]; // Lazily create the ref instance
  151. // https://reactjs.org/docs/hooks-faq.html#how-to-create-expensive-objects-lazily
  152. if (refContainer.current === null && isBrowser) {
  153. refContainer.current = document.createElement('div');
  154. }
  155. var handleOpen = function handleOpen() {
  156. noScroll.on();
  157. window.addEventListener('resize', handleWindowResize);
  158. if (refContainer.current && !document.body.contains(refContainer.current)) {
  159. document.body.appendChild(refContainer.current);
  160. }
  161. if (fullscreen && screenfull.isEnabled) {
  162. screenfull.request();
  163. screenfull.on('change', handleScreenfullChange);
  164. }
  165. };
  166. var handleClose = function handleClose() {
  167. window.removeEventListener('resize', handleWindowResize);
  168. if (refContainer.current && document.body.contains(refContainer.current)) {
  169. document.body.removeChild(refContainer.current);
  170. }
  171. if (screenfull.isEnabled) {
  172. screenfull.exit();
  173. screenfull.off('change', handleScreenfullChange);
  174. }
  175. noScroll.off();
  176. };
  177. var handleKeydown = function handleKeydown(e) {
  178. if (e.keyCode === keycodes.left && keyboardNavigation) {
  179. handleChangeIndex(Direction.Prev);
  180. } else if (e.keyCode === keycodes.right && keyboardNavigation) {
  181. handleChangeIndex(Direction.Next);
  182. } else if (e.keyCode === keycodes.esc && closeOnEsc) {
  183. onClose();
  184. }
  185. };
  186. useEventListener(open, 'keydown', handleKeydown, isBrowser ? document : undefined);
  187. var handleMousemove = function handleMousemove() {
  188. // Hide the actions buttons when move do not move for x seconds
  189. if (refTimeoutMouseIdle.current) {
  190. clearTimeout(refTimeoutMouseIdle.current);
  191. }
  192. if (mouseIdle === true) {
  193. setMouseIdle(false);
  194. }
  195. refTimeoutMouseIdle.current = setTimeout(function () {
  196. setMouseIdle(true);
  197. }, mouseIdleTimeout);
  198. };
  199. useEventListener(open, 'mousemove', handleMousemove, isBrowser ? document.querySelector('*') : undefined); // We listen to the srcIndexProp to update the internal state if the user manage the component
  200. React.useEffect(function () {
  201. setSrcIndex(srcIndexProp);
  202. }, [srcIndexProp]);
  203. React.useEffect(function () {
  204. // When the modal is rendered first time we want to block the scroll
  205. if (open) {
  206. handleOpen();
  207. }
  208. return function () {
  209. // When the component is unmounted directly we want to unblock the scroll
  210. if (showPortal) {
  211. handleClose();
  212. }
  213. };
  214. }, []);
  215. React.useEffect(function () {
  216. // If the open prop is changing, we need to open the modal
  217. if (open && !showPortal) {
  218. setShowPortal(true);
  219. handleOpen();
  220. }
  221. }, [open]);
  222. var handleScreenfullChange = function handleScreenfullChange() {
  223. if (screenfull.isEnabled && !screenfull.isFullscreen && open) {
  224. onClose();
  225. }
  226. };
  227. var handleWindowResize = function handleWindowResize() {
  228. setWindowSizes({
  229. width: window.innerWidth,
  230. height: window.innerHeight
  231. });
  232. };
  233. var handleChangeIndex = function handleChangeIndex(direction) {
  234. if (direction === Direction.Prev && srcIndex !== 0) {
  235. var newIndex = srcIndex - 1;
  236. onChangeIndex ? onChangeIndex(newIndex) : setSrcIndex(newIndex);
  237. } else if (direction === Direction.Next && src[srcIndex + 1]) {
  238. var _newIndex = srcIndex + 1;
  239. onChangeIndex ? onChangeIndex(_newIndex) : setSrcIndex(_newIndex);
  240. }
  241. };
  242. var handleClickCloseArrow = function handleClickCloseArrow() {
  243. onClose();
  244. };
  245. var handleAnimationEnd = function handleAnimationEnd() {
  246. if (!open) {
  247. setShowPortal(false);
  248. handleClose();
  249. } // if (onAnimationEnd) {
  250. // onAnimationEnd();
  251. // }
  252. };
  253. var image = src[srcIndex];
  254. var wrapperImageStyle = {
  255. position: 'absolute',
  256. overflow: 'hidden',
  257. userSelect: 'none'
  258. };
  259. var imageWidth = image.width;
  260. var imageHeight = image.height; // Adjust image ratio max with window size
  261. if (imageWidth > windowSizes.width) {
  262. var ratio = windowSizes.width / imageWidth;
  263. imageHeight *= ratio;
  264. imageWidth *= ratio;
  265. }
  266. if (imageHeight > windowSizes.height) {
  267. var _ratio = windowSizes.height / imageHeight;
  268. imageHeight *= _ratio;
  269. imageWidth *= _ratio;
  270. }
  271. if (imageHeight > imageWidth || imageWidth < windowSizes.width) {
  272. wrapperImageStyle.left = (windowSizes.width - imageWidth) / 2;
  273. wrapperImageStyle.height = windowSizes.height;
  274. wrapperImageStyle.width = imageWidth;
  275. } else {
  276. wrapperImageStyle.top = (windowSizes.height - imageHeight) / 2;
  277. wrapperImageStyle.height = imageHeight;
  278. wrapperImageStyle.width = windowSizes.width;
  279. }
  280. if (windowSizes.height > imageHeight) {
  281. wrapperImageStyle.height = imageHeight;
  282. wrapperImageStyle.top = (windowSizes.height - imageHeight) / 2;
  283. } else if (windowSizes.width > imageWidth) {
  284. wrapperImageStyle.height = windowSizes.height;
  285. wrapperImageStyle.left = (windowSizes.width - imageWidth) / 2;
  286. }
  287. return showPortal && refContainer.current ? ReactDom.createPortal(React__default.createElement("div", {
  288. className: cx(classes.overlay, classNames === null || classNames === void 0 ? void 0 : classNames.overlay),
  289. style: {
  290. animation: (open ? (_classNames$animation = classNames === null || classNames === void 0 ? void 0 : classNames.animationIn) !== null && _classNames$animation !== void 0 ? _classNames$animation : classes.animationIn : (_classNames$animation2 = classNames === null || classNames === void 0 ? void 0 : classNames.animationOut) !== null && _classNames$animation2 !== void 0 ? _classNames$animation2 : classes.animationOut) + " " + animationDuration + "ms"
  291. },
  292. onAnimationEnd: handleAnimationEnd
  293. }, React__default.createElement("div", {
  294. style: wrapperImageStyle
  295. }, src.map(function (source, index) {
  296. var _cx;
  297. return React__default.createElement("img", {
  298. key: index,
  299. src: source.src,
  300. alt: source.alt,
  301. width: wrapperImageStyle.width,
  302. height: wrapperImageStyle.height,
  303. className: cx(classes.image, classNames === null || classNames === void 0 ? void 0 : classNames.image, (_cx = {}, _cx[classes.imageOpen] = index === srcIndex, _cx))
  304. });
  305. })), srcIndex !== 0 && React__default.createElement("div", {
  306. className: cx(classes.column, classes.leftColumn),
  307. onClick: function onClick() {
  308. return handleChangeIndex(Direction.Prev);
  309. }
  310. }, React__default.createElement(PrevArrowButton, {
  311. className: cx(classes.arrowButton, classes.arrowButtonLeft, (_cx2 = {}, _cx2[classes.arrowButtonHide] = mouseIdle, _cx2))
  312. })), src[srcIndex + 1] && React__default.createElement("div", {
  313. className: cx(classes.column, classes.rightColumn),
  314. onClick: function onClick() {
  315. return handleChangeIndex(Direction.Next);
  316. }
  317. }, React__default.createElement(NextArrowButton, {
  318. className: cx(classes.arrowButton, classes.arrowButtonRight, (_cx3 = {}, _cx3[classes.arrowButtonHide] = mouseIdle, _cx3))
  319. })), React__default.createElement("div", {
  320. onClick: handleClickCloseArrow
  321. }, React__default.createElement(CloseArrow, {
  322. className: cx(classes.arrowButtonReturn, (_cx4 = {}, _cx4[classes.arrowButtonHide] = mouseIdle, _cx4))
  323. }))), refContainer.current) : null;
  324. };
  325. exports.GooglePhoto = GooglePhoto;
  326. exports.default = GooglePhoto;
  327. //# sourceMappingURL=react-google-photo.cjs.development.js.map