File size: 6,401 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
import { Gridicon, ExternalLink } from '@automattic/components';
import { useLocale, localizeUrl } from '@automattic/i18n-utils';
import { useTranslate } from 'i18n-calypso';
import { debounce } from 'lodash';
import { useState, useEffect, useRef } from 'react';
import { isMobile, sortByMenuOrder, onLinkClick, closeOnFocusOut, isValidLink } from '../utils';
import BundlesSection from './bundles-section';
import Product from './product';
import type { MenuItem } from 'calypso/data/jetpack/use-jetpack-masterbar-data-query';
import type { FC, MouseEvent } from 'react';
interface MainMenuItemProps {
section: MenuItem | null;
bundles: MenuItem | null;
}
const MainMenuItem: FC< MainMenuItemProps > = ( { section, bundles } ) => {
const locale = useLocale();
const translate = useTranslate();
const [ isOpen, setIsOpen ] = useState< boolean >( false );
const submenu = useRef< HTMLDivElement >( null );
const setSubMenuPosition = ( btn: HTMLAnchorElement, menu: HTMLDivElement ) => {
const subMenuWrapper = menu.querySelector( '.header__submenu-wrapper' ) as HTMLElement;
if ( ! subMenuWrapper ) {
return;
}
const btnRect = btn.getBoundingClientRect();
const btnPosition = btnRect.left;
const isOnRightSide = window.innerWidth - btnPosition < btnPosition;
// Handle mobile/tablet view
if ( window.innerWidth < 1100 ) {
if ( btn.classList.contains( 'js-menu-btn' ) ) {
subMenuWrapper.style.margin = '0 auto';
}
subMenuWrapper.style.left = '0';
return;
}
// Reset margin for desktop view
subMenuWrapper.style.margin = '';
// Calculate arrow and wrapper positions
const offset = isOnRightSide ? 100 : 10;
const arrowPosition = btnRect.width / 2 + offset;
let wrapperPosition = btnPosition - offset;
// Ensure submenu doesn't overflow viewport
const subMenuRect = subMenuWrapper.getBoundingClientRect();
if ( wrapperPosition + subMenuRect.width > window.innerWidth ) {
wrapperPosition = window.innerWidth - subMenuRect.width - 20;
}
if ( wrapperPosition < 0 ) {
wrapperPosition = 20;
}
// Apply positions
subMenuWrapper.style.setProperty( '--arrow-left', `${ arrowPosition }px` );
subMenuWrapper.style.left = `${ wrapperPosition }px`;
};
const desktopOnKeyDown = ( e: KeyboardEvent ) => {
if ( e.key === 'Escape' ) {
setIsOpen( false );
}
};
useEffect( () => {
document.addEventListener( 'keydown', desktopOnKeyDown );
return () => {
document.removeEventListener( 'keydown', desktopOnKeyDown );
};
}, [] );
useEffect( () => {
const handleResize = debounce( () => {
const expandedBtn = document.querySelector(
'.js-menu-btn[aria-expanded="true"]'
) as HTMLAnchorElement;
if ( expandedBtn && submenu.current ) {
setSubMenuPosition( expandedBtn, submenu.current );
}
}, 100 );
window.addEventListener( 'resize', handleResize );
return () => {
window.removeEventListener( 'resize', handleResize );
};
}, [ isOpen ] );
if ( ! section ) {
return <></>;
}
const toggleMenuItem = ( e?: MouseEvent< HTMLElement > ) => {
setIsOpen( ( open ) => {
const newIsOpen = ! open;
if ( newIsOpen && e?.currentTarget instanceof HTMLAnchorElement ) {
// Use the clicked button directly
if ( submenu.current ) {
setSubMenuPosition( e.currentTarget, submenu.current );
}
}
return newIsOpen;
} );
};
const onBlur = () => {
closeOnFocusOut( submenu, isOpen, toggleMenuItem );
};
const onMainMenuTagClick = ( e: MouseEvent< HTMLAnchorElement > ) => {
onLinkClick( e, 'calypso_jetpack_nav_item_click' );
};
const { label, id, href, items } = section;
const hasChildren = Object.keys( items ).length > 0;
const MainMenuTag = hasChildren ? 'a' : ExternalLink;
return (
<>
<MainMenuTag
className={ hasChildren ? 'header__menu-btn js-menu-btn' : '' }
href={ isValidLink( href ) ? localizeUrl( href, locale ) : `#${ id }` }
aria-expanded={ hasChildren ? isOpen : undefined }
onClick={ isValidLink( href ) ? onMainMenuTagClick : toggleMenuItem }
onBlur={ onBlur }
data-target={ id }
>
{ label }
{ hasChildren && <Gridicon icon="chevron-down" size={ 18 } /> }
</MainMenuTag>
{ hasChildren && (
// eslint-disable-next-line jsx-a11y/click-events-have-key-events, jsx-a11y/no-static-element-interactions
<div
id={ id }
className="header__submenu js-menu js"
tabIndex={ -1 }
hidden={ ! isOpen }
onClick={ ! isMobile() ? toggleMenuItem : undefined }
ref={ submenu }
>
<div className="header__submenu-content">
<div className="header__submenu-wrapper">
<button className="header__back-btn js-menu-back" onClick={ toggleMenuItem }>
<Gridicon icon="chevron-left" size={ 18 } />
{ translate( 'Back' ) }
</button>
<div className="header__submenu-category-wrapper">
<p className="header__submenu-category-title">
{ translate( 'Individual products' ) }
</p>
<ul className="header__submenu-categories-list">
{ Array.from( Object.values( items ) )
.sort( sortByMenuOrder )
.map( ( { label, href, items } ) => {
return (
<li key={ `submenu-category-${ href }${ label }` }>
{ isValidLink( href ) ? (
<ExternalLink
className="header__submenu-category header__submenu-link"
href={ localizeUrl( href, locale ) }
onClick={ onLinkClick }
>
<span className="header__submenu-label">{ label }</span>
</ExternalLink>
) : (
<p className="header__submenu-category header__submenu-link">
<span className="header__submenu-label">{ label }</span>
</p>
) }
<ul className="header__submenu-links-list">
{ Array.from( Object.values( items ) )
.sort( sortByMenuOrder )
.map( ( { label, href, id } ) => (
<Product
key={ `products-${ href }-${ label }` }
product={ { label, href, id } }
/>
) ) }
</ul>
</li>
);
} ) }
</ul>
</div>
<BundlesSection bundles={ bundles } />
</div>
</div>
</div>
) }
</>
);
};
export default MainMenuItem;
|