IntersectionObserver.js
182 lines
| 1 | /** |
| 2 | * External Dependencies |
| 3 | */ |
| 4 | import React, { useEffect, useRef, useState } from "react"; |
| 5 | import { |
| 6 | HStack, |
| 7 | IconButton, |
| 8 | Menu, |
| 9 | MenuButton, |
| 10 | MenuItem, |
| 11 | MenuList, |
| 12 | } from "@chakra-ui/react"; |
| 13 | import { NavLink, useLocation } from "react-router-dom"; |
| 14 | import PropTypes from "prop-types"; |
| 15 | |
| 16 | /** |
| 17 | * Internal Dependencies |
| 18 | */ |
| 19 | import { DotsHorizontal } from "../Icon/Icon"; |
| 20 | |
| 21 | const IntersectionStyles = { |
| 22 | visible: { |
| 23 | order: 0, |
| 24 | visibility: "visible", |
| 25 | opacity: 1, |
| 26 | }, |
| 27 | inVisible: { |
| 28 | order: 100, |
| 29 | visibility: "hidden", |
| 30 | pointerEvents: "none", |
| 31 | }, |
| 32 | toolbarWrapper: { |
| 33 | overflow: "hidden", |
| 34 | display: "flex", |
| 35 | border: "1px solid black", |
| 36 | alignItem: "center", |
| 37 | }, |
| 38 | overflowStyle: { |
| 39 | order: 99, |
| 40 | position: "sticky", |
| 41 | right: "0", |
| 42 | backgroundColor: "white", |
| 43 | }, |
| 44 | }; |
| 45 | |
| 46 | const IntersectObserver = ({ children, routes }) => { |
| 47 | const ref = useRef(null); |
| 48 | const [visibleMap, setVisibleMap] = useState({}); |
| 49 | const location = useLocation(); |
| 50 | const hiddenRoutes = routes.filter((route) => !visibleMap[route.route]); |
| 51 | |
| 52 | const selectedHiddenRoute = hiddenRoutes.find( |
| 53 | (h) => h.route === location.pathname |
| 54 | ); |
| 55 | |
| 56 | useEffect(() => { |
| 57 | if (!ref.current) return; |
| 58 | const observer = new IntersectionObserver( |
| 59 | (entries) => { |
| 60 | const updatedEntries = {}; |
| 61 | entries.forEach((entry) => { |
| 62 | const target = entry.target.dataset?.["target"]; |
| 63 | if (entry.isIntersecting && target) { |
| 64 | updatedEntries[target] = true; |
| 65 | } |
| 66 | if (!entry.isIntersecting && target) { |
| 67 | updatedEntries[target] = false; |
| 68 | } |
| 69 | }); |
| 70 | |
| 71 | setVisibleMap((prev) => ({ |
| 72 | ...prev, |
| 73 | ...updatedEntries, |
| 74 | })); |
| 75 | }, |
| 76 | { |
| 77 | root: ref.current, |
| 78 | threshold: 0.98, |
| 79 | } |
| 80 | ); |
| 81 | |
| 82 | Array.from(ref.current.children).forEach((item) => { |
| 83 | if (item.getAttribute("data-target")) { |
| 84 | observer.observe(item); |
| 85 | } |
| 86 | }); |
| 87 | |
| 88 | return () => observer.disconnect(); |
| 89 | }, []); |
| 90 | |
| 91 | const shouldShowMenu = Object.values(visibleMap).some((v) => v === false); |
| 92 | |
| 93 | return ( |
| 94 | <HStack |
| 95 | ref={ref} |
| 96 | width={{ |
| 97 | base: "50px", |
| 98 | sm: "240px", |
| 99 | md: "520px", |
| 100 | lg: "570px", |
| 101 | xl: "680px", |
| 102 | }} |
| 103 | overflow={"hidden"} |
| 104 | h="full" |
| 105 | > |
| 106 | {React.Children.map(children, (child) => { |
| 107 | const otherSX = visibleMap[child.props["data-target"]] |
| 108 | ? IntersectionStyles.visible |
| 109 | : IntersectionStyles.inVisible; |
| 110 | |
| 111 | return React.cloneElement(child, { |
| 112 | sx: { ...children?.props?.sx, ...otherSX }, |
| 113 | }); |
| 114 | })} |
| 115 | |
| 116 | {shouldShowMenu && ( |
| 117 | <Menu> |
| 118 | <MenuButton |
| 119 | as={IconButton} |
| 120 | aria-label="Options" |
| 121 | sx={IntersectionStyles.overflowStyle} |
| 122 | style={{ |
| 123 | background: "#FFFFFF", |
| 124 | boxShadow: "none", |
| 125 | marginLeft: "0px", |
| 126 | }} |
| 127 | color={ |
| 128 | !!selectedHiddenRoute ? "primary.500" : "#383838" |
| 129 | } |
| 130 | visibility={"visible"} |
| 131 | icon={<DotsHorizontal w="24px" h="24px" />} |
| 132 | ></MenuButton> |
| 133 | <MenuList display="flex" flexDirection="column"> |
| 134 | {hiddenRoutes.map(({ label, route }) => { |
| 135 | return ( |
| 136 | <MenuItem |
| 137 | key={route} |
| 138 | as={NavLink} |
| 139 | to={route} |
| 140 | marginBottom={"0px"} |
| 141 | data-target={route} |
| 142 | fontSize="sm" |
| 143 | fontWeight="semibold" |
| 144 | lineHeight="150%" |
| 145 | color="#383838" |
| 146 | _hover={{ |
| 147 | color: "primary.500", |
| 148 | }} |
| 149 | _focus={{ |
| 150 | boxShadow: "none", |
| 151 | }} |
| 152 | _activeLink={{ |
| 153 | color: "primary.500", |
| 154 | }} |
| 155 | display="inline-flex" |
| 156 | alignItems="center" |
| 157 | px="2" |
| 158 | py="10px" |
| 159 | > |
| 160 | {label} |
| 161 | </MenuItem> |
| 162 | ); |
| 163 | })} |
| 164 | </MenuList> |
| 165 | </Menu> |
| 166 | )} |
| 167 | </HStack> |
| 168 | ); |
| 169 | }; |
| 170 | |
| 171 | IntersectObserver.propTypes = { |
| 172 | children: PropTypes.any, |
| 173 | routes: PropTypes.arrayOf( |
| 174 | PropTypes.shape({ |
| 175 | label: PropTypes.string.isRequired, |
| 176 | route: PropTypes.string.isRequired, |
| 177 | }) |
| 178 | ).isRequired, |
| 179 | }; |
| 180 | |
| 181 | export default IntersectObserver; |
| 182 |