|
1 |
| -import { useEffect, useState } from "react"; |
2 |
| -import { MdHome, MdOutlineNavigateNext } from "react-icons/md"; |
3 |
| -import "./BreadCrumb.scss"; |
| 1 | +import { useEffect, useRef, useState } from "react"; |
| 2 | +import { MdHome, MdMoreHoriz, MdOutlineNavigateNext } from "react-icons/md"; |
4 | 3 | import { useFileNavigation } from "../../contexts/FileNavigationContext";
|
| 4 | +import { useDetectOutsideClick } from "../../hooks/useDetectOutsideClick"; |
| 5 | +import "./BreadCrumb.scss"; |
5 | 6 |
|
6 | 7 | const BreadCrumb = () => {
|
7 | 8 | const [folders, setFolders] = useState([]);
|
| 9 | + const [hiddenFolders, setHiddenFolders] = useState([]); |
| 10 | + const [hiddenFoldersWidth, setHiddenFoldersWidth] = useState([]); |
| 11 | + const [showHiddenFolders, setShowHiddenFolders] = useState(false); |
| 12 | + |
8 | 13 | const { currentPath, setCurrentPath } = useFileNavigation();
|
| 14 | + const breadCrumbRef = useRef(null); |
| 15 | + const foldersRef = useRef([]); |
| 16 | + const moreBtnRef = useRef(null); |
| 17 | + const popoverRef = useDetectOutsideClick(() => { |
| 18 | + setShowHiddenFolders(false); |
| 19 | + }); |
9 | 20 |
|
10 | 21 | useEffect(() => {
|
11 |
| - setFolders(currentPath?.split("/")); |
| 22 | + setFolders(() => { |
| 23 | + let path = ""; |
| 24 | + return currentPath?.split("/").map((item) => { |
| 25 | + return { |
| 26 | + name: item || "Home", |
| 27 | + path: item === "" ? item : (path += `/${item}`), |
| 28 | + }; |
| 29 | + }); |
| 30 | + }); |
| 31 | + setHiddenFolders([]); |
| 32 | + setHiddenFoldersWidth([]); |
12 | 33 | }, [currentPath]);
|
13 | 34 |
|
14 |
| - const switchPath = (index) => { |
15 |
| - if (index < folders.length - 1) { |
16 |
| - setCurrentPath(() => { |
17 |
| - const toSlice = folders.length - (index + 1); |
18 |
| - const switchFolders = folders.slice(0, -toSlice); |
19 |
| - return switchFolders.join("/"); |
20 |
| - }); |
21 |
| - } |
| 35 | + const switchPath = (path) => { |
| 36 | + setCurrentPath(path); |
| 37 | + }; |
| 38 | + |
| 39 | + const getBreadCrumbWidth = () => { |
| 40 | + const containerWidth = breadCrumbRef.current.clientWidth; |
| 41 | + const containerStyles = getComputedStyle(breadCrumbRef.current); |
| 42 | + const paddingLeft = parseFloat(containerStyles.paddingLeft); |
| 43 | + const moreBtnGap = hiddenFolders.length > 0 ? 1 : 0; |
| 44 | + const flexGap = parseFloat(containerStyles.gap) * (folders.length + moreBtnGap); |
| 45 | + return containerWidth - (paddingLeft + flexGap); |
22 | 46 | };
|
23 | 47 |
|
| 48 | + const checkAvailableSpace = () => { |
| 49 | + const availableSpace = getBreadCrumbWidth(); |
| 50 | + const remainingFoldersWidth = foldersRef.current.reduce((prev, curr) => { |
| 51 | + if (!curr) return prev; |
| 52 | + return prev + curr.clientWidth; |
| 53 | + }, 0); |
| 54 | + const moreBtnWidth = moreBtnRef.current?.clientWidth || 0; |
| 55 | + return availableSpace - (remainingFoldersWidth + moreBtnWidth); |
| 56 | + }; |
| 57 | + |
| 58 | + const isBreadCrumbOverflowing = () => { |
| 59 | + return breadCrumbRef.current.scrollWidth > breadCrumbRef.current.clientWidth; |
| 60 | + }; |
| 61 | + |
| 62 | + useEffect(() => { |
| 63 | + if (isBreadCrumbOverflowing()) { |
| 64 | + const hiddenFolder = folders[1]; |
| 65 | + const hiddenFolderWidth = foldersRef.current[1]?.clientWidth; |
| 66 | + setHiddenFoldersWidth((prev) => [...prev, hiddenFolderWidth]); |
| 67 | + setHiddenFolders((prev) => [...prev, hiddenFolder]); |
| 68 | + setFolders((prev) => prev.filter((_, index) => index !== 1)); |
| 69 | + } else if (hiddenFolders.length > 0 && checkAvailableSpace() > hiddenFoldersWidth.at(-1)) { |
| 70 | + const newFolders = [folders[0], hiddenFolders.at(-1), ...folders.slice(1)]; |
| 71 | + setFolders(newFolders); |
| 72 | + setHiddenFolders((prev) => prev.slice(0, -1)); |
| 73 | + setHiddenFoldersWidth((prev) => prev.slice(0, -1)); |
| 74 | + } |
| 75 | + }, [isBreadCrumbOverflowing]); |
| 76 | + |
24 | 77 | return (
|
25 |
| - <div className="breadcrumb"> |
26 |
| - {folders.map((folder, index) => ( |
27 |
| - <span key={index} className="folder-name" onClick={() => switchPath(index)}> |
28 |
| - {index === 0 ? ( |
29 |
| - <> |
30 |
| - <MdHome /> Home |
31 |
| - </> |
32 |
| - ) : ( |
33 |
| - <> |
34 |
| - <MdOutlineNavigateNext /> {folder} |
35 |
| - </> |
36 |
| - )} |
37 |
| - </span> |
38 |
| - ))} |
| 78 | + <div className="bread-crumb-container"> |
| 79 | + <div className="breadcrumb" ref={breadCrumbRef}> |
| 80 | + {folders.map((folder, index) => ( |
| 81 | + <div key={index} style={{ display: "contents" }}> |
| 82 | + <span |
| 83 | + className="folder-name" |
| 84 | + onClick={() => switchPath(folder.path)} |
| 85 | + ref={(el) => (foldersRef.current[index] = el)} |
| 86 | + > |
| 87 | + {index === 0 ? <MdHome /> : <MdOutlineNavigateNext />} |
| 88 | + {folder.name} |
| 89 | + </span> |
| 90 | + {hiddenFolders?.length > 0 && index === 0 && ( |
| 91 | + <button |
| 92 | + className="folder-name folder-name-btn" |
| 93 | + onClick={() => setShowHiddenFolders(true)} |
| 94 | + ref={moreBtnRef} |
| 95 | + title="Show more folders" |
| 96 | + > |
| 97 | + <MdMoreHoriz size={22} className="hidden-folders" /> |
| 98 | + </button> |
| 99 | + )} |
| 100 | + </div> |
| 101 | + ))} |
| 102 | + </div> |
| 103 | + |
| 104 | + {showHiddenFolders && ( |
| 105 | + <ul ref={popoverRef.ref} className="hidden-folders-container"> |
| 106 | + {hiddenFolders.map((folder, index) => ( |
| 107 | + <li |
| 108 | + key={index} |
| 109 | + onClick={() => { |
| 110 | + switchPath(folder.path); |
| 111 | + setShowHiddenFolders(false); |
| 112 | + }} |
| 113 | + > |
| 114 | + {folder.name} |
| 115 | + </li> |
| 116 | + ))} |
| 117 | + </ul> |
| 118 | + )} |
39 | 119 | </div>
|
40 | 120 | );
|
41 | 121 | };
|
|
0 commit comments