react-google-photo.esm.js 12 KB

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