mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-12-05 14:19:56 -05:00
Navigation links automatically hide based on screen width, rather than hard breaks.
This commit is contained in:
parent
74c04fe5f6
commit
4b34d37838
2 changed files with 142 additions and 30 deletions
|
|
@ -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 <Dropdown item text={link.text} direction='left'>
|
||||
|
|
@ -265,7 +259,7 @@ export function NavBar() {
|
|||
</React.Fragment>;
|
||||
|
||||
return <>
|
||||
<Media between={['mobile', 'wideTablet']}>
|
||||
<Media between={['mobile', 'tablet']}>
|
||||
<Menu {...i} attached='top' color={navColor} id='global_navbar'>
|
||||
{homeLink}
|
||||
<Menu.Menu position='right'>
|
||||
|
|
@ -273,23 +267,56 @@ export function NavBar() {
|
|||
<SearchIconButton/>
|
||||
<Dropdown item icon="bars">
|
||||
<Dropdown.Menu>
|
||||
{collapsedLinks.map(i => <MenuLink link={i} key={i.key}/>)}
|
||||
{allLinks.map(i => <MenuLink link={i} key={i.key}/>)}
|
||||
{rightLinks.map(i => <MenuLink link={i} key={i.key}/>)}
|
||||
</Dropdown.Menu>
|
||||
</Dropdown>
|
||||
</Menu.Menu>
|
||||
</Menu>
|
||||
</Media>
|
||||
<Media greaterThanOrEqual='wideTablet'>
|
||||
<Menu {...i} attached='top' color={navColor} id='global_navbar'>
|
||||
{homeLink}
|
||||
{links.map(i => <MenuLink link={i} key={i.key}/>)}
|
||||
|
||||
<Menu.Menu position='right'>
|
||||
{icons}
|
||||
<SearchIconButton/>
|
||||
{rightLinks.map(i => <MenuLink link={i} key={i.key}/>)}
|
||||
</Menu.Menu>
|
||||
</Menu>
|
||||
<Media greaterThanOrEqual='tablet'>
|
||||
<DesktopNav i={i} navColor={navColor} homeLink={homeLink} icons={icons}/>
|
||||
</Media>
|
||||
</>
|
||||
}
|
||||
|
||||
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 (
|
||||
<div ref={containerRef}>
|
||||
<Menu {...i} attached='top' color={navColor} id='global_navbar'>
|
||||
<span ref={homeRef} style={{display: 'contents'}}>{homeLink}</span>
|
||||
{(isReady ? visibleLinks : allLinks).map((link, idx) => (
|
||||
<span
|
||||
key={link.key}
|
||||
ref={itemRefCallback(idx)}
|
||||
style={isReady ? {display: 'contents'} : {visibility: 'hidden'}}
|
||||
>
|
||||
<MenuLink link={link}/>
|
||||
</span>
|
||||
))}
|
||||
{!isReady && <span ref={moreRef} style={{visibility: 'hidden'}}>
|
||||
<Dropdown item text='More'><Dropdown.Menu/></Dropdown>
|
||||
</span>}
|
||||
{isReady && overflowLinks.length > 0 &&
|
||||
<DropdownLinks link={{text: 'More', key: 'more', links: overflowLinks}}/>}
|
||||
<div ref={rightMenuRef} className='right menu'>
|
||||
{icons}
|
||||
<SearchIconButton/>
|
||||
{rightLinks.map(i => <MenuLink link={i} key={i.key}/>)}
|
||||
</div>
|
||||
</Menu>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
|
|
|||
85
app/src/hooks/useOverflowNav.js
Normal file
85
app/src/hooks/useOverflowNav.js
Normal file
|
|
@ -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};
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue