diff --git a/app/src/components/Nav.js b/app/src/components/Nav.js index d47497ac..88c493d4 100644 --- a/app/src/components/Nav.js +++ b/app/src/components/Nav.js @@ -9,6 +9,7 @@ import {useReorganizationStatus} from "../contexts/FileWorkerStatusContext"; import {SearchIconButton} from "./Search"; import {Icon, Popup} from "./Theme"; import {HELP_VIEWER_URI, NAME, semanticUIColorMap} from "./Vars"; +import {useOverflowNav} from "../hooks/useOverflowNav"; import _ from "lodash"; function updateFavicon(colorName) { @@ -34,29 +35,22 @@ function updateFavicon(colorName) { if (themeColorMeta) themeColorMeta.content = hexColor; } -const links = [ +const help = {to: HELP_VIEWER_URI, text: 'Help', key: 'help', target: '_blank'}; +const admin = {to: '/admin', text: 'Admin', key: 'admin'}; +const rightLinks = [help, admin]; +const allLinks = [ {text: 'Videos', to: '/videos', key: 'videos'}, {text: 'Archive', to: '/archives', key: 'archive'}, {text: 'Docs', to: '/docs', key: 'docs'}, {text: 'Map', to: '/map', key: 'map'}, {text: 'Files', to: '/files', key: 'files'}, - { - text: 'More', key: 'more', - links: [ - {text: 'Zim', to: '/zim', key: 'zim'}, - {text: 'Inventory', to: '/inventory', key: 'inventory'}, - {to: '/more/otp', text: 'One Time Pad', end: true}, - {to: '/more/vin', text: 'VIN Decoder', end: true}, - {to: '/more/calculators', text: 'Calculators', end: true}, - {to: '/more/statistics', text: 'Statistics', end: true}, - ] - }, + {text: 'Zim', to: '/zim', key: 'zim'}, + {text: 'Inventory', to: '/inventory', key: 'inventory'}, + {to: '/more/otp', text: 'One Time Pad', key: 'otp', end: true}, + {to: '/more/vin', text: 'VIN Decoder', key: 'vin', end: true}, + {to: '/more/calculators', text: 'Calculators', key: 'calculators', end: true}, + {to: '/more/statistics', text: 'Statistics', key: 'statistics', end: true}, ]; -const help = {to: HELP_VIEWER_URI, text: 'Help', key: 'help', target: '_blank'}; -const admin = {to: '/admin', text: 'Admin', key: 'admin'}; -const rightLinks = [help, admin]; - -const collapsedLinks = links.concat([help, admin]); function DropdownLinks({link}) { return @@ -265,7 +259,7 @@ export function NavBar() { ; return <> - + {homeLink} @@ -273,23 +267,56 @@ export function NavBar() { - {collapsedLinks.map(i => )} + {allLinks.map(i => )} + {rightLinks.map(i => )} - - - {homeLink} - {links.map(i => )} - - - {icons} - - {rightLinks.map(i => )} - - + + } + +function DesktopNav({i, navColor, homeLink, icons}) { + const containerRef = React.useRef(null); + const homeRef = React.useRef(null); + const rightMenuRef = React.useRef(null); + const moreRef = React.useRef(null); + const {visibleLinks, overflowLinks, itemRefCallback, isReady} = useOverflowNav({ + links: allLinks, + containerRef, + homeRef, + rightMenuRef, + moreRef, + }); + + return ( +
+ + {homeLink} + {(isReady ? visibleLinks : allLinks).map((link, idx) => ( + + + + ))} + {!isReady && + + } + {isReady && overflowLinks.length > 0 && + } +
+ {icons} + + {rightLinks.map(i => )} +
+
+
+ ); +} diff --git a/app/src/hooks/useOverflowNav.js b/app/src/hooks/useOverflowNav.js new file mode 100644 index 00000000..0061f936 --- /dev/null +++ b/app/src/hooks/useOverflowNav.js @@ -0,0 +1,85 @@ +import {useState, useRef, useLayoutEffect, useCallback} from 'react'; + +export function useOverflowNav({links, containerRef, homeRef, rightMenuRef, moreRef}) { + const [visibleCount, setVisibleCount] = useState(links.length); + const [isReady, setIsReady] = useState(false); + const itemWidths = useRef([]); + const itemRefs = useRef([]); + const moreWidth = useRef(0); + + const itemRefCallback = useCallback((index) => (el) => { + itemRefs.current[index] = el; + }, []); + + const recalculate = useCallback(() => { + const container = containerRef.current; + if (!container || itemWidths.current.length === 0) return; + + const containerWidth = container.offsetWidth; + const homeEl = homeRef.current?.firstElementChild || homeRef.current; + const homeWidth = homeEl ? homeEl.getBoundingClientRect().width : 0; + const rightWidth = rightMenuRef.current ? rightMenuRef.current.getBoundingClientRect().width : 0; + + const totalAvailable = containerWidth - homeWidth - rightWidth; + const totalItemsWidth = itemWidths.current.reduce((a, b) => a + b, 0); + + // If all items fit without a More button, show them all. + if (totalItemsWidth <= totalAvailable) { + setVisibleCount(itemWidths.current.length); + return; + } + + // Otherwise, reserve space for the More button and fit as many as possible. + const available = totalAvailable - moreWidth.current; + let count = 0; + let usedWidth = 0; + + for (let i = 0; i < itemWidths.current.length; i++) { + const nextWidth = itemWidths.current[i]; + if (usedWidth + nextWidth <= available) { + usedWidth += nextWidth; + count++; + } else { + break; + } + } + + setVisibleCount(count); + }, [containerRef, homeRef, rightMenuRef]); + + // Measure item widths once after first render. + useLayoutEffect(() => { + if (!containerRef.current) return; + + itemWidths.current = itemRefs.current.map(el => { + if (!el) return 0; + const child = el.firstElementChild; + return child ? child.getBoundingClientRect().width : el.getBoundingClientRect().width; + }); + + const moreEl = moreRef.current?.firstElementChild || moreRef.current; + if (moreEl) { + moreWidth.current = moreEl.getBoundingClientRect().width; + } + + recalculate(); + setIsReady(true); + }, []); // eslint-disable-line react-hooks/exhaustive-deps + + // ResizeObserver for ongoing resizes. + useLayoutEffect(() => { + const container = containerRef.current; + if (!container) return; + + const observer = new ResizeObserver(() => { + recalculate(); + }); + observer.observe(container); + return () => observer.disconnect(); + }, [containerRef, recalculate]); + + const visibleLinks = links.slice(0, visibleCount); + const overflowLinks = links.slice(visibleCount); + + return {visibleLinks, overflowLinks, itemRefCallback, isReady}; +}