From 53be182b1cd49f20f7db454ab5b4c001a547871e Mon Sep 17 00:00:00 2001 From: Mitchell McCaffrey Date: Mon, 14 Jun 2021 17:06:57 +1000 Subject: [PATCH] Add lazy loading for tiles and token bar tokens --- package.json | 2 + src/components/tile/Tile.js | 172 ++++++++++++++------------ src/components/token/TokenBarToken.js | 40 +++--- src/index.js | 5 + yarn.lock | 10 ++ 5 files changed, 126 insertions(+), 103 deletions(-) diff --git a/package.json b/package.json index 71bab11..e6a485a 100644 --- a/package.json +++ b/package.json @@ -27,6 +27,7 @@ "file-saver": "^2.0.5", "fuse.js": "^6.4.6", "image-outline": "^0.1.0", + "intersection-observer": "^0.12.0", "konva": "^7.2.5", "lodash.clonedeep": "^4.5.0", "lodash.get": "^4.4.2", @@ -39,6 +40,7 @@ "raw.macro": "^0.4.2", "react": "^17.0.1", "react-dom": "^17.0.1", + "react-intersection-observer": "^8.32.0", "react-konva": "^17.0.1-3", "react-markdown": "4", "react-media": "^2.0.0-rc.1", diff --git a/src/components/tile/Tile.js b/src/components/tile/Tile.js index 610c19e..9977dc8 100644 --- a/src/components/tile/Tile.js +++ b/src/components/tile/Tile.js @@ -1,5 +1,6 @@ import React from "react"; import { Flex, IconButton, Box, Text, Badge } from "theme-ui"; +import { useInView } from "react-intersection-observer"; import EditTileIcon from "../../icons/EditTileIcon"; @@ -14,6 +15,8 @@ function Tile({ editTitle, children, }) { + const [ref, inView] = useInView({ triggerOnce: true }); + return ( - - {children} - - - - {title} - - - - - {badges.map((badge, i) => ( - - {badge} - - ))} - - {canEdit && ( - - { - e.preventDefault(); - e.stopPropagation(); - onEdit(); + {inView && ( + <> + - - - + {children} + + + + {title} + + + + + {badges.map((badge, i) => ( + + {badge} + + ))} + + {canEdit && ( + + { + e.preventDefault(); + e.stopPropagation(); + onEdit(); + }} + bg="overlay" + sx={{ borderRadius: "50%" }} + m={2} + > + + + + )} + )} ); diff --git a/src/components/token/TokenBarToken.js b/src/components/token/TokenBarToken.js index df6f336..cd89691 100644 --- a/src/components/token/TokenBarToken.js +++ b/src/components/token/TokenBarToken.js @@ -1,31 +1,29 @@ -import React, { useRef } from "react"; +import React from "react"; import { Box } from "theme-ui"; - -import usePreventTouch from "../../hooks/usePreventTouch"; +import { useInView } from "react-intersection-observer"; import TokenImage from "./TokenImage"; function TokenBarToken({ token }) { - const imageRef = useRef(); - // Stop touch to prevent 3d touch gesutre on iOS - usePreventTouch(imageRef); + const [ref, inView] = useInView({ triggerOnce: true }); return ( - - + + {inView && ( + + )} ); } diff --git a/src/index.js b/src/index.js index d21cd5b..7bf9ecf 100644 --- a/src/index.js +++ b/src/index.js @@ -16,6 +16,11 @@ if (!("PointerEvent" in window)) { import("pepjs"); } +// Intersection observer polyfill +if (!("IntersectionObserver" in window)) { + import("intersection-observer"); +} + if (process.env.REACT_APP_LOGGING === "true") { Sentry.init({ dsn: process.env.REACT_APP_SENTRY_DSN, diff --git a/yarn.lock b/yarn.lock index 39aed16..59a13fe 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7317,6 +7317,11 @@ internal-slot@^1.0.2: has "^1.0.3" side-channel "^1.0.2" +intersection-observer@^0.12.0: + version "0.12.0" + resolved "https://registry.yarnpkg.com/intersection-observer/-/intersection-observer-0.12.0.tgz#6c84628f67ce8698e5f9ccf857d97718745837aa" + integrity sha512-2Vkz8z46Dv401zTWudDGwO7KiGHNDkMv417T5ItcNYfmvHR/1qCTVBO9vwH8zZmQ0WkA/1ARwpysR9bsnop4NQ== + invariant@^2.2.2: version "2.2.4" resolved "https://registry.yarnpkg.com/invariant/-/invariant-2.2.4.tgz#610f3c92c9359ce1db616e538008d23ff35158e6" @@ -10857,6 +10862,11 @@ react-input-autosize@^3.0.0: dependencies: prop-types "^15.5.8" +react-intersection-observer@^8.32.0: + version "8.32.0" + resolved "https://registry.yarnpkg.com/react-intersection-observer/-/react-intersection-observer-8.32.0.tgz#47249332e12e8bb99ed35a10bb7dd10446445a7b" + integrity sha512-RlC6FvS3MFShxTn4FHAy904bVjX5Nn4/eTjUkurW0fHK+M/fyQdXuyCy9+L7yjA+YMGogzzSJNc7M4UtfSKvtw== + react-is@^16.6.0, react-is@^16.7.0, react-is@^16.8.1, react-is@^16.8.6: version "16.13.1" resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.13.1.tgz#789729a4dc36de2999dc156dd6c1d9c18cea56a4"