bootstrap: navbar collapse jQuery -> React state (BS4->5 prep)
Bootstrap's jQuery collapse (`data-toggle="collapse"` in Navigation.js, plus the per-NavItem `data-target=".navbar-collapse.show"` auto-close) disappears when we drop `alexwine:bootstrap-4` for `bootstrap@5`. Replace it with a React `useState` that toggles `.show`, and close the mobile menu via an onClick on the nav <ul>. Works on the current BS4 CSS (`.collapse.show` is pure CSS; jQuery only added the height animation) and removes one of the two jQuery/BS4 JS blockers to the CSS swap (the home carousel is the other). Also drops the dead sr-only toggler button that targeted a non-existent id. No CSS changes.
This commit is contained in:
parent
6b6f02d92d
commit
8dead27d22
3 changed files with 57 additions and 44 deletions
|
|
@ -15,44 +15,51 @@ import './Navigation.scss';
|
|||
|
||||
// removed class: fixed-top
|
||||
// md instead of lg: no menu in medium
|
||||
const Navigation = ({ name = '', ...props }) => (
|
||||
<nav className="navbar navbar-expand-lg navbar-dark bg-dark">
|
||||
<div style={{ overflow: 'hidden' } /* for ribbon */} className="container">
|
||||
{/* <BetaRibbon /> */}
|
||||
<Link to="/" className={`navbar-brand ${window.location.pathname === '/' ? 'hide-brand' : ''}`} >
|
||||
{props.t('AppNameFull')}
|
||||
</Link>
|
||||
<button className="sr-only navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
|
||||
<span className="navbar-toggler-icon" />
|
||||
</button>
|
||||
<button className="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavDropdown" aria-controls="navbarNavDropdown" aria-expanded="false" aria-label="Toggle navigation">
|
||||
<span className="navbar-toggler-icon" />
|
||||
</button>
|
||||
{/* <Navbar.Collapse> */}
|
||||
<div className="collapse navbar-collapse" id="navbarNavDropdown">
|
||||
<ul className="navbar-nav ml-auto ">
|
||||
{/* <LinkContainer className="nav-item" anchorClassName="nav-link" to="/sandbox">
|
||||
<NavItem eventKey={1.1} href="/sandbox">Sandbox</NavItem>
|
||||
</LinkContainer> */}
|
||||
<LinkContainer id={testId('subscriptions')} className="nav-item" anchorClassName="nav-link" to="/subscriptions">
|
||||
<NavItem eventKey={1.2} href="/subscriptions">
|
||||
{props.authenticated ? <Trans>Mis zonas</Trans> : <Trans>Participar</Trans>}
|
||||
</NavItem>
|
||||
</LinkContainer>
|
||||
<LinkContainer id={testId('moniZones')} className="nav-item" anchorClassName="nav-link" to="/zones">
|
||||
<NavItem eventKey={2} href="/zones">{props.t('Zonas vigiladas')}</NavItem>
|
||||
</LinkContainer>
|
||||
<LinkContainer id={testId('activeFires')} className="nav-item" anchorClassName="nav-link" to="/fires">
|
||||
<NavItem eventKey={2.1} href="/fires">{props.t('activeFires')}</NavItem>
|
||||
</LinkContainer>
|
||||
//
|
||||
// Bootstrap 4→5 prep: the collapse used to be driven by Bootstrap's jQuery JS
|
||||
// (`data-toggle="collapse"`), which goes away when we drop `alexwine:bootstrap-4`
|
||||
// for `bootstrap@5`. Toggling the `.show` class from React state removes that
|
||||
// dependency and keeps working on the current BS4 CSS (`.collapse.show` is pure
|
||||
// CSS; jQuery only added the height animation). Clicking a nav link collapses the
|
||||
// mobile menu again — the old per-NavItem `data-target=".navbar-collapse.show"`.
|
||||
const Navigation = ({ name = '', ...props }) => {
|
||||
const [open, setOpen] = React.useState(false);
|
||||
const close = () => setOpen(false);
|
||||
return (
|
||||
<nav className="navbar navbar-expand-lg navbar-dark bg-dark">
|
||||
<div style={{ overflow: 'hidden' } /* for ribbon */} className="container">
|
||||
{/* <BetaRibbon /> */}
|
||||
<Link to="/" className={`navbar-brand ${window.location.pathname === '/' ? 'hide-brand' : ''}`} >
|
||||
{props.t('AppNameFull')}
|
||||
</Link>
|
||||
<button className="navbar-toggler" type="button" onClick={() => setOpen(o => !o)} aria-controls="navbarNavDropdown" aria-expanded={open} aria-label="Toggle navigation">
|
||||
<span className="navbar-toggler-icon" />
|
||||
</button>
|
||||
<div className={`collapse navbar-collapse${open ? ' show' : ''}`} id="navbarNavDropdown">
|
||||
{/* onClick closes the mobile menu when a main nav link is tapped */}
|
||||
<ul className="navbar-nav ml-auto " onClick={close}>
|
||||
{/* <LinkContainer className="nav-item" anchorClassName="nav-link" to="/sandbox">
|
||||
<NavItem eventKey={1.1} href="/sandbox">Sandbox</NavItem>
|
||||
</LinkContainer> */}
|
||||
<LinkContainer id={testId('subscriptions')} className="nav-item" anchorClassName="nav-link" to="/subscriptions">
|
||||
<NavItem eventKey={1.2} href="/subscriptions">
|
||||
{props.authenticated ? <Trans>Mis zonas</Trans> : <Trans>Participar</Trans>}
|
||||
</NavItem>
|
||||
</LinkContainer>
|
||||
<LinkContainer id={testId('moniZones')} className="nav-item" anchorClassName="nav-link" to="/zones">
|
||||
<NavItem eventKey={2} href="/zones">{props.t('Zonas vigiladas')}</NavItem>
|
||||
</LinkContainer>
|
||||
<LinkContainer id={testId('activeFires')} className="nav-item" anchorClassName="nav-link" to="/fires">
|
||||
<NavItem eventKey={2.1} href="/fires">{props.t('activeFires')}</NavItem>
|
||||
</LinkContainer>
|
||||
|
||||
</ul>
|
||||
{!props.authenticated ? <PublicNavigation /> : <AuthenticatedNavigation name={name} {...props} />}
|
||||
{/* </Navbar.Collapse> */}
|
||||
</ul>
|
||||
{!props.authenticated ? <PublicNavigation /> : <AuthenticatedNavigation name={name} {...props} />}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</nav>
|
||||
);
|
||||
</nav>
|
||||
);
|
||||
};
|
||||
|
||||
Navigation.propTypes = {
|
||||
t: PropTypes.func.isRequired,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue