Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions components/Navigator/MainNavigator.module.css
Original file line number Diff line number Diff line change
@@ -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;
}
Comment thread
TechQuery marked this conversation as resolved.
Resolved

.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);
}
127 changes: 80 additions & 47 deletions components/Navigator/MainNavigator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import {
Container,
Drawer,
Image,
Menu,
MenuItem,
Navbar,
NavbarBrand,
NavbarNav,
Expand All @@ -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<HTMLAnchorElement>,
Expand Down Expand Up @@ -115,6 +117,26 @@ export class MainNavigator extends Component<MainNavigatorProps, typeof i18n> {

@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));
Expand All @@ -123,6 +145,45 @@ export class MainNavigator extends Component<MainNavigatorProps, typeof i18n> {
const { t } = this.context;
const menu = this.props.menu ?? topNavBarMenu(this.context);

const navigation = (
<>
<NavbarNav className="flex-fill me-auto my-3 lg:my-0 lg:flex-row">
{menu.map(({ href, title, subs }, index) =>
subs ? (
<NavItem key={title}>
<MainNavigatorMenu
id={`main-navigation-${index}`}
title={title}
desktop={this.desktop}
items={subs}
pathname={this.props.pathname}
onSelect={this.hide}
/>
</NavItem>
) : (
href && (
<NavItem key={`${href}-${title}`} as="li">
<NavLink
className={styles.link}
href={href}
active={this.props.pathname === href}
onClick={this.hide}
>
Comment thread
TechQuery marked this conversation as resolved.
Resolved
{title}
</NavLink>
</NavItem>
)
),
)}
</NavbarNav>

<div className="d-flex justify-content-around gap-5">
<SearchBar />
<LanguageMenu />
</div>
</>
);

return (
<Navbar theme="dark" className="fixed-top bg-body" expand="lg">
<Container>
Expand All @@ -132,55 +193,27 @@ export class MainNavigator extends Component<MainNavigatorProps, typeof i18n> {
</NavbarBrand>
<NavbarToggler
expanded={this.open}
aria-controls={NAVIGATION_ID}
aria-label={t('toggle_navigation')}
onClick={this.toggle}
/>
<Drawer
className="w-100"
bodyClassName="d-flex flex-column lg:flex-row lg:align-items-center"
title={t('open_source_bazaar')}
responsive="lg"
placement="end"
open={this.open}
onDismiss={this.hide}
>
<NavbarNav className="flex-fill me-auto my-3 lg:my-0 lg:flex-row">
{menu.map(({ href, title, subs }) =>
subs ? (
<Menu key={title} label={title} nav>
{subs.map(
({ href: subHref, title: subTitle }) =>
subHref && (
<MenuItem key={subHref} href={subHref} onClick={this.hide}>
{subTitle}
</MenuItem>
),
)}
</Menu>
) : (
href && (
<NavItem key={`${href}-${title}`} as="li">
<NavLink
href={href}
active={this.props.pathname === href}
className={`text-nowrap ${
this.props.pathname === href ? 'fw-bolder fg-white' : ''
}`}
onClick={this.hide}
>
{title}
</NavLink>
</NavItem>
)
),
)}
</NavbarNav>

<div className="d-flex justify-content-around gap-5">
<SearchBar />
<LanguageMenu />
{this.desktop ? (
<div id={NAVIGATION_ID} className="w-100 d-flex align-items-center">
{navigation}
</div>
</Drawer>
) : (
<Drawer
id={NAVIGATION_ID}
title={t('open_source_bazaar')}
className="w-100"
bodyClassName="d-flex flex-column"
placement="end"
open={this.open}
onDismiss={this.hide}
>
{navigation}
</Drawer>
)}
</Container>
</Navbar>
);
Expand Down
113 changes: 113 additions & 0 deletions components/Navigator/MainNavigatorMenu.tsx
Original file line number Diff line number Diff line change
@@ -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<MainNavigatorMenuProps> {
@observable accessor horizontalOffset = 0;

menu = createRef<Menu>();
closeTimer?: ReturnType<typeof setTimeout>;
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<HTMLElement>) => {
if (event.pointerType !== 'mouse') return;

this.cancelClose();
this.menu.current?.setOpen(true);
};

handlePointerLeave = (event: PointerEvent<HTMLElement>) => {
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);
Comment thread
TechQuery marked this conversation as resolved.
Resolved
}, 200);
};

render() {
const { id, title, desktop, items, pathname, onSelect } = this.props;
const active = items.some(({ href }) => href === pathname);

return (
<Menu
ref={this.menu}
id={id}
label={title}
nav
theme="dark"
className={desktop ? undefined : 'w-100'}
offset={desktop ? [this.horizontalOffset, 4] : undefined}
menuClassName={
desktop ? styles.menu : `${styles.inlineMenu} position-static min-w-0 max-w-100`
}
toggleProps={{
className: styles.link,
'aria-current': active ? 'true' : undefined,
}}
onPointerEnter={desktop ? this.handlePointerEnter : undefined}
onPointerLeave={desktop ? this.handlePointerLeave : undefined}
onOpenChange={this.cancelClose}
>
{items.map(
({ href, title }) =>
href && (
<MenuItem
key={href}
href={href}
active={pathname === href}
className={styles.menuItem}
onClick={onSelect}
>
{title}
</MenuItem>
),
)}
</Menu>
);
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion pages/_document.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export default class CustomDocument extends Document<CustomDocumentProps> {
rel="stylesheet"
href="https://unpkg.com/github-markdown-css@5.9.0/github-markdown.css"
/>
<link rel="stylesheet" href="https://unpkg.com/idea-react@3.0.0-rc.4/dist/style.css" />
<link rel="stylesheet" href="https://unpkg.com/idea-react@3.0.0-rc.6/dist/style.css" />
<link
rel="stylesheet"
href="https://unpkg.com/mobx-restful-table@3.0.0-rc.3/dist/index.css"
Expand Down
Loading