IntersectionObserver.js
141 lines
| 1 | /** |
| 2 | * External Dependencies |
| 3 | */ |
| 4 | import React, { useEffect, useRef, useState } from "react"; |
| 5 | import { |
| 6 | HStack, |
| 7 | IconButton, |
| 8 | Link, |
| 9 | Menu, |
| 10 | MenuButton, |
| 11 | MenuItem, |
| 12 | MenuList, |
| 13 | } from "@chakra-ui/react"; |
| 14 | import { NavLink, useLocation } from "react-router-dom"; |
| 15 | import PropTypes from "prop-types"; |
| 16 | |
| 17 | /** |
| 18 | * Internal Dependencies |
| 19 | */ |
| 20 | import { DotsHorizontal } from "../Icon/Icon"; |
| 21 | import { convertRoute, isExternalRoute } from "../../Constants"; |
| 22 | |
| 23 | const IntersectionStyles = { |
| 24 | visible: { |
| 25 | order: 0, |
| 26 | visibility: "visible", |
| 27 | opacity: 1, |
| 28 | }, |
| 29 | inVisible: { |
| 30 | order: 100, |
| 31 | visibility: "hidden", |
| 32 | pointerEvents: "none", |
| 33 | }, |
| 34 | toolbarWrapper: { |
| 35 | overflow: "hidden", |
| 36 | display: "flex", |
| 37 | border: "1px solid black", |
| 38 | alignItem: "center", |
| 39 | }, |
| 40 | overflowStyle: { |
| 41 | order: 99, |
| 42 | position: "sticky", |
| 43 | right: "0", |
| 44 | backgroundColor: "white", |
| 45 | }, |
| 46 | }; |
| 47 | |
| 48 | const IntersectObserver = ({ children, routes }) => { |
| 49 | const ref = useRef(null); |
| 50 | const [visibleMap, setVisibleMap] = useState({}); |
| 51 | const location = useLocation(); |
| 52 | const hiddenRoutes = routes.filter((route) => !visibleMap[route.route]); |
| 53 | |
| 54 | const selectedHiddenRoute = hiddenRoutes.find( |
| 55 | (h) => h.route === location.pathname |
| 56 | ); |
| 57 | |
| 58 | const { pageType, adminURL } = |
| 59 | typeof _EVF_DASHBOARD_ !== "undefined" && _EVF_DASHBOARD_; |
| 60 | const isSettingsPage = pageType === "settings"; |
| 61 | const isEntriesPage = pageType === "entries"; |
| 62 | const isFormsPage = pageType === "forms"; |
| 63 | const isAnalyticsPage = pageType === "analytics"; |
| 64 | const isNonDashboardPage = isSettingsPage || isEntriesPage || isAnalyticsPage || isFormsPage; |
| 65 | |
| 66 | useEffect(() => { |
| 67 | if (!ref.current) return; |
| 68 | const observer = new IntersectionObserver( |
| 69 | (entries) => { |
| 70 | const updatedEntries = {}; |
| 71 | entries.forEach((entry) => { |
| 72 | const target = entry.target.dataset?.["target"]; |
| 73 | if (entry.isIntersecting && target) { |
| 74 | updatedEntries[target] = true; |
| 75 | } |
| 76 | if (!entry.isIntersecting && target) { |
| 77 | updatedEntries[target] = false; |
| 78 | } |
| 79 | }); |
| 80 | |
| 81 | setVisibleMap((prev) => ({ |
| 82 | ...prev, |
| 83 | ...updatedEntries, |
| 84 | })); |
| 85 | }, |
| 86 | { |
| 87 | root: ref.current, |
| 88 | threshold: 0.98, |
| 89 | } |
| 90 | ); |
| 91 | |
| 92 | Array.from(ref.current.children).forEach((item) => { |
| 93 | if (item.getAttribute("data-target")) { |
| 94 | observer.observe(item); |
| 95 | } |
| 96 | }); |
| 97 | |
| 98 | return () => observer.disconnect(); |
| 99 | }, []); |
| 100 | |
| 101 | const shouldShowMenu = Object.values(visibleMap).some((v) => v === false); |
| 102 | |
| 103 | return ( |
| 104 | <HStack |
| 105 | ref={ref} |
| 106 | width={{ |
| 107 | base: "50px", |
| 108 | sm: "240px", |
| 109 | md: "520px", |
| 110 | lg: "570px", |
| 111 | xl: "680px", |
| 112 | }} |
| 113 | overflow={"hidden"} |
| 114 | h="full" |
| 115 | > |
| 116 | {React.Children.map(children, (child) => { |
| 117 | const otherSX = visibleMap[child.props["data-target"]] |
| 118 | ? IntersectionStyles.visible |
| 119 | : IntersectionStyles.inVisible; |
| 120 | |
| 121 | return React.cloneElement(child, { |
| 122 | sx: { ...children?.props?.sx, ...otherSX }, |
| 123 | }); |
| 124 | })} |
| 125 | |
| 126 | </HStack> |
| 127 | ); |
| 128 | }; |
| 129 | |
| 130 | IntersectObserver.propTypes = { |
| 131 | children: PropTypes.any, |
| 132 | routes: PropTypes.arrayOf( |
| 133 | PropTypes.shape({ |
| 134 | label: PropTypes.string.isRequired, |
| 135 | route: PropTypes.string.isRequired, |
| 136 | }) |
| 137 | ).isRequired, |
| 138 | }; |
| 139 | |
| 140 | export default IntersectObserver; |
| 141 |