diff --git a/components/Navigator/MainNavigator.module.css b/components/Navigator/MainNavigator.module.css new file mode 100644 index 0000000..efc1749 --- /dev/null +++ b/components/Navigator/MainNavigator.module.css @@ -0,0 +1,66 @@ +.link { + color: var(--bs-nav-link-color); +} + +.link:hover, +.link:focus-visible, +.link[aria-expanded='true'] { + color: var(--bs-nav-link-hover-color); +} + +.link[aria-current] { + color: var(--bs-nav-link-active-color); + font-weight: var(--bs-font-weight-bold); +} + +.link:focus-visible, +.menuItem:focus-visible { + outline: 2px solid currentColor; + outline-offset: -2px; +} + +.menu, +.inlineMenu { + --bs-menu-item-active-bg: var(--bs-bg-2); + --bs-menu-item-active-color: var(--bs-fg-body); +} + +.menu { + transform-origin: top center; +} + +.menu[data-bs-placement^='top'] { + transform-origin: bottom center; +} + +/* Keep the four-pixel visual gap within the menu's pointer hit area. */ +.menu::before { + position: absolute; + inset-inline: 0; + top: -5px; + height: 5px; + content: ''; +} + +.menu[data-bs-placement^='top']::before { + top: auto; + bottom: -5px; +} + +/* Allow the submenu to expand within the drawer. */ +.inlineMenu { + max-height: none !important; +} + +.menuItem { + color: var(--bs-menu-item-color); +} + +.menuItem:hover, +.menuItem:focus-visible { + color: var(--bs-menu-item-hover-color); +} + +.menuItem[aria-current] { + color: var(--bs-menu-item-active-color); +} diff --git a/components/Navigator/MainNavigator.tsx b/components/Navigator/MainNavigator.tsx index a09262c..7d60010 100644 --- a/components/Navigator/MainNavigator.tsx +++ b/components/Navigator/MainNavigator.tsx @@ -2,8 +2,6 @@ import { Container, Drawer, Image, - Menu, - MenuItem, Navbar, NavbarBrand, NavbarNav, @@ -19,9 +17,13 @@ import { AnchorHTMLAttributes, Component } from 'react'; import { DefaultImage } from '../../models/configuration'; import { i18n, I18nContext } from '../../models/Translation'; +import styles from './MainNavigator.module.css'; +import { MainNavigatorMenu } from './MainNavigatorMenu'; import { SearchBar } from './SearchBar'; const LanguageMenu = dynamic(() => import('./LanguageMenu'), { ssr: false }); +const NAVIGATION_ID = 'main-navigation'; +const DESKTOP_QUERY = '(min-width: 1024px)'; export interface NavigatorMenuItem extends Pick< AnchorHTMLAttributes, @@ -115,6 +117,26 @@ export class MainNavigator extends Component { @observable accessor open = false; + @observable accessor desktop = false; + + desktopMedia?: MediaQueryList; + + componentDidMount() { + this.desktopMedia = window.matchMedia(DESKTOP_QUERY); + this.desktopMedia.addEventListener('change', this.updateLayout); + this.updateLayout(); + } + + componentWillUnmount() { + this.desktopMedia?.removeEventListener('change', this.updateLayout); + } + + @action + updateLayout = () => { + this.desktop = this.desktopMedia?.matches ?? false; + if (this.desktop) this.hide(); + }; + toggle = action(() => (this.open = !this.open)); hide = action(() => (this.open = false)); @@ -123,6 +145,45 @@ export class MainNavigator extends Component { const { t } = this.context; const menu = this.props.menu ?? topNavBarMenu(this.context); + const navigation = ( + <> + + {menu.map(({ href, title, subs }, index) => + subs ? ( + + + + ) : ( + href && ( + + + {title} + + + ) + ), + )} + + +
+ + +
+ + ); + return ( @@ -132,55 +193,27 @@ export class MainNavigator extends Component { - - - {menu.map(({ href, title, subs }) => - subs ? ( - - {subs.map( - ({ href: subHref, title: subTitle }) => - subHref && ( - - {subTitle} - - ), - )} - - ) : ( - href && ( - - - {title} - - - ) - ), - )} - - -
- - + {this.desktop ? ( +
+ {navigation}
- + ) : ( + + {navigation} + + )} ); diff --git a/components/Navigator/MainNavigatorMenu.tsx b/components/Navigator/MainNavigatorMenu.tsx new file mode 100644 index 0000000..4dd2ee6 --- /dev/null +++ b/components/Navigator/MainNavigatorMenu.tsx @@ -0,0 +1,113 @@ +import { Menu, MenuItem } from 'idea-react'; +import { action, observable } from 'mobx'; +import { observer } from 'mobx-react'; +import { Component, createRef, PointerEvent } from 'react'; + +import type { NavigatorMenuItem } from './MainNavigator'; +import styles from './MainNavigator.module.css'; + +interface MainNavigatorMenuProps { + id: string; + title: NavigatorMenuItem['title']; + desktop: boolean; + items: NavigatorMenuItem[]; + pathname: string; + onSelect: () => void; +} + +@observer +export class MainNavigatorMenu extends Component { + @observable accessor horizontalOffset = 0; + + menu = createRef(); + closeTimer?: ReturnType; + resize?: ResizeObserver; + + componentDidMount() { + const { trigger, panel } = this.menu.current ?? {}; + if (!this.props.desktop || !trigger?.current || !panel?.current) return; + + this.resize = new ResizeObserver(this.centerMenu); + this.resize.observe(trigger.current); + this.resize.observe(panel.current); + } + + componentWillUnmount() { + this.cancelClose(); + this.resize?.disconnect(); + } + + @action + centerMenu = () => { + const { trigger, panel } = this.menu.current ?? {}; + if (!trigger?.current || !panel?.current || panel.current.hidden) return; + + // Shift start alignment to the center; Menu still clamps it to the viewport. + this.horizontalOffset = (trigger.current.offsetWidth - panel.current.offsetWidth) / 2; + }; + + cancelClose = () => { + clearTimeout(this.closeTimer); + this.closeTimer = undefined; + }; + + handlePointerEnter = (event: PointerEvent) => { + if (event.pointerType !== 'mouse') return; + + this.cancelClose(); + this.menu.current?.setOpen(true); + }; + + handlePointerLeave = (event: PointerEvent) => { + if (event.pointerType !== 'mouse') return; + + this.cancelClose(); + // Allow diagonal movement into a menu shifted away from the viewport edge. + this.closeTimer = setTimeout(() => { + const menu = this.menu.current; + if (!menu?.panel.current?.contains(document.activeElement)) menu?.setOpen(false); + }, 200); + }; + + render() { + const { id, title, desktop, items, pathname, onSelect } = this.props; + const active = items.some(({ href }) => href === pathname); + + return ( + + {items.map( + ({ href, title }) => + href && ( + + {title} + + ), + )} + + ); + } +} diff --git a/package.json b/package.json index 21d4845..260cd92 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,7 @@ "core-js": "^3.51.0", "echarts-jsx": "^0.6.0", "file-type": "^22.3.0", - "idea-react": "^3.0.0-rc.4", + "idea-react": "^3.0.0-rc.6", "jsonwebtoken": "^9.0.3", "koa": "^3.3.0", "koa-jwt": "^4.0.4", diff --git a/pages/_document.tsx b/pages/_document.tsx index 22ba9f1..182264a 100644 --- a/pages/_document.tsx +++ b/pages/_document.tsx @@ -38,7 +38,7 @@ export default class CustomDocument extends Document { rel="stylesheet" href="https://unpkg.com/github-markdown-css@5.9.0/github-markdown.css" /> - + =20.9.0'} cpu: [arm64] os: [linux] - libc: [glibc] '@img/sharp-linux-arm@0.35.5': resolution: {integrity: sha512-LEaXK2WdXVK5ykcw0buWyPMsmLLL2vpHLD6yrNSW+JGEL3BZPA4tpKN6iaMc4AxTTAoaX/sU1rOL51lcIz48ZQ==} engines: {node: '>=20.9.0'} cpu: [arm] os: [linux] - libc: [glibc] '@img/sharp-linux-ppc64@0.35.5': resolution: {integrity: sha512-QVxAAq8evVRI9ia2vqgwrmWucn5Dfv+JdWzj75pD8omHLPSP7f8p20O8jxzjCcuCEQEOtYOZUmX1hkiZ0kdevA==} engines: {node: '>=20.9.0'} cpu: [ppc64] os: [linux] - libc: [glibc] '@img/sharp-linux-riscv64@0.35.5': resolution: {integrity: sha512-LtdreXguaavKODPIfzJ4kffx7UNt1omwtK0rch4EBbbSTXPnxWmYSayXdLJw0fJzQ97kHt1gL/yh4tvU+nCyRQ==} engines: {node: '>=20.9.0'} cpu: [riscv64] os: [linux] - libc: [glibc] '@img/sharp-linux-s390x@0.35.5': resolution: {integrity: sha512-UZasTOFiYzotTsGOCu42BfUzP6Tu6Do/947iRm1RsLKvlllxwGcn4RN27LibGWceix4Y+Pmw3jsnTcCQIgWjqA==} engines: {node: '>=20.9.0'} cpu: [s390x] os: [linux] - libc: [glibc] '@img/sharp-linux-x64@0.35.5': resolution: {integrity: sha512-SxFtLTeJInhAA9Q836kux2vZNeOBQEx658qvbboZScr0wIARym3IcGmW7KpVD5sbVg0Ojy+udFQdayYIZyoNog==} engines: {node: '>=20.9.0'} cpu: [x64] os: [linux] - libc: [glibc] '@img/sharp-linuxmusl-arm64@0.35.5': resolution: {integrity: sha512-9HbMclmI1zlNkFRs3z9/eBtDjfD0sGlrX1z6b1qwmiFY5ElDLh4BC0LPBdVp7z1DXFiKlIcznf+ZlsuZzLxQqg==} engines: {node: '>=20.9.0'} cpu: [arm64] os: [linux] - libc: [musl] '@img/sharp-linuxmusl-x64@0.35.5': resolution: {integrity: sha512-4KOphqB035HrVdqLZfCgMzzERrQkkzOwRhl4OAkRO1YCldbaFjySXMaK534Mo0V+LndnlJk+sbUyLeU0ULyD1A==} engines: {node: '>=20.9.0'} cpu: [x64] os: [linux] - libc: [musl] '@img/sharp-wasm32@0.35.5': resolution: {integrity: sha512-Ptsga1su4tQx+LLF1ECS9U6nz5kmrXKo6XVbtR48Ke3ZRxxgaWBu7IDtEe1quo8hiupwm6WFqxVlXaSf7IINGQ==} @@ -784,28 +770,24 @@ packages: engines: {node: '>= 10'} cpu: [arm64] os: [linux] - libc: [glibc] '@next/swc-linux-arm64-musl@16.4.6': resolution: {integrity: sha512-7/qPhTxDp2ScluOJOhLuhWmlA17o1yZ0SIgpISBCpBxXgtnK4+KGywBpFkfLPiU1rnSM1faFGzYkH6i7JdTrqA==} engines: {node: '>= 10'} cpu: [arm64] os: [linux] - libc: [musl] '@next/swc-linux-x64-gnu@16.4.6': resolution: {integrity: sha512-oOpbNa8XOW1qCGd9VvzC4fbkkRxEls7/WI6tz+YHC0xRA7KpV1U7DrAnCPjzVyOk/karzWEepCOcTIDmwK8FwQ==} engines: {node: '>= 10'} cpu: [x64] os: [linux] - libc: [glibc] '@next/swc-linux-x64-musl@16.4.6': resolution: {integrity: sha512-aM8CZ+OR2EwXaAYw7KGkW2GDmNDsLBIRe1MMRZCJPTdw/xhTD0IMUVO4sf3KqrzuRouGzwwLZn34+gFeVXiUQA==} engines: {node: '>= 10'} cpu: [x64] os: [linux] - libc: [musl] '@next/swc-win32-arm64-msvc@16.4.6': resolution: {integrity: sha512-U0p7/tEtqvHX2crhd841CDwKtp5CFkKXVjOhmfccR3Pvp64sj8KBwRqsvltqLOwVBbj0NtkxwXSDpEHYGbxeXQ==} @@ -1927,8 +1909,8 @@ packages: idb@8.0.3: resolution: {integrity: sha512-LtwtVyVYO5BqRvcsKuB2iUMnHwPVByPCXFXOpuU96IZPPoPN6xjOGxZQ74pgSVVLQWtUOYgyeL4GE98BY5D3wg==} - idea-react@3.0.0-rc.4: - resolution: {integrity: sha512-giPY9A5x7jKwozs3d0FSb/48HLoJ8zWrjUE3veLCR+2WAqrGlkTkv0pN2R+KCWQi7AqbbmtQ/Dc7DnPEimg+wA==} + idea-react@3.0.0-rc.6: + resolution: {integrity: sha512-LXLdUkO0BJ2tSuabEEp+vZsiDIHfBD8QMgji1sbh0RHjdBH8ykfKxZOpFK3OM14KQwaPbq7OC5702FVZ9KQprA==} peerDependencies: mobx: '>=6' mobx-react: '>=9' @@ -3325,6 +3307,13 @@ packages: web-utility@4.8.2: resolution: {integrity: sha512-SbA2XIlo2UUinxIwbQ0MYhwoyXnepzMZ/bU4rR+EyEz2WcFirQtFAPNHw4+qFd4A1oi2S0ao8itHic1Vl9qlwg==} + deprecated: Don't use versions with old API & bugs + peerDependencies: + element-internals-polyfill: '>=1' + typescript: '>=4.1' + + web-utility@4.8.3: + resolution: {integrity: sha512-bckkx77G9KPHUx60YELdzf/afdfMUTMacBGDv4EkdoIt5VcqFxiY1JyzpV3fyJ5YBzkbLOi2UFzZtIct5754Bw==} peerDependencies: element-internals-polyfill: '>=1' typescript: '>=4.1' @@ -5367,11 +5356,10 @@ snapshots: idb@8.0.3: {} - idea-react@3.0.0-rc.4(mobx-react@10.0.4(mobx@7.1.7)(react@19.3.1))(mobx@7.1.7)(react-dom@19.3.1(react@19.3.1))(react@19.3.1)(typescript@6.0.3): + idea-react@3.0.0-rc.6(mobx-react@10.0.4(mobx@7.1.7)(react@19.3.1))(mobx@7.1.7)(react-dom@19.3.1(react@19.3.1))(react@19.3.1)(typescript@6.0.3): dependencies: '@shikijs/langs': 4.6.0 '@shikijs/themes': 4.6.0 - '@swc/helpers': 0.5.24 bootstrap: 6.0.0-alpha.1(vanilla-calendar-pro@3.4.0) classnames: 2.5.1 html2canvas: 1.4.1 @@ -5386,7 +5374,7 @@ snapshots: react-live: 5.0.1(react-dom@19.3.1(react@19.3.1))(react@19.3.1) shiki: 4.6.0 vanilla-calendar-pro: 3.4.0 - web-utility: 4.8.2(typescript@6.0.3) + web-utility: 4.8.3(typescript@6.0.3) transitivePeerDependencies: - '@floating-ui/dom' - element-internals-polyfill @@ -6215,7 +6203,7 @@ snapshots: dependencies: '@swc/helpers': 0.5.24 classnames: 2.5.1 - idea-react: 3.0.0-rc.4(mobx-react@10.0.4(mobx@7.1.7)(react@19.3.1))(mobx@7.1.7)(react-dom@19.3.1(react@19.3.1))(react@19.3.1)(typescript@6.0.3) + idea-react: 3.0.0-rc.6(mobx-react@10.0.4(mobx@7.1.7)(react@19.3.1))(mobx@7.1.7)(react-dom@19.3.1(react@19.3.1))(react@19.3.1)(typescript@6.0.3) lodash: 4.18.1 mobx: 7.1.7 mobx-i18n: 0.7.9(mobx@7.1.7)(typescript@6.0.3) @@ -7262,6 +7250,13 @@ snapshots: regenerator-runtime: 0.14.1 typescript: 6.0.3 + web-utility@4.8.3(typescript@6.0.3): + dependencies: + '@swc/helpers': 0.5.24 + mime: 4.1.2 + regenerator-runtime: 0.14.1 + typescript: 6.0.3 + which-boxed-primitive@1.1.1: dependencies: is-bigint: 1.1.0