File size: 8,415 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 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 |
import { Gridicon, Button } from '@automattic/components';
import clsx from 'clsx';
import PropTypes from 'prop-types';
import React, { Component, Fragment, forwardRef } from 'react';
import { navigate } from 'calypso/lib/navigate';
import type { ReactNode, LegacyRef } from 'react';
// eslint-disable-next-line @typescript-eslint/no-empty-function
const noop = () => {};
interface MasterbarSubItemProps {
label: string | React.ReactNode;
url?: string;
onClick?: () => void;
className?: string;
}
interface MasterbarItemProps {
url?: string;
innerRef?: LegacyRef< HTMLButtonElement | HTMLAnchorElement >;
tipTarget?: string;
onClick?: () => void;
tooltip?: string;
icon?: ReactNode;
className?: string;
wrapperClassName?: string;
isActive?: boolean;
preloadSection?: () => void;
hasUnseen?: boolean;
children?: ReactNode;
alwaysShowContent?: boolean;
disabled?: boolean;
subItems?: Array< Array< MasterbarSubItemProps > >;
hasGlobalBorderStyle?: boolean;
as?: React.ComponentType;
variant?: string;
ariaLabel?: string;
}
class MasterbarItem extends Component< MasterbarItemProps > {
static propTypes = {
url: PropTypes.string,
tipTarget: PropTypes.string,
onClick: PropTypes.func,
tooltip: PropTypes.string,
icon: PropTypes.oneOfType( [ PropTypes.element, PropTypes.string ] ),
className: PropTypes.string,
isActive: PropTypes.bool,
preloadSection: PropTypes.func,
hasUnseen: PropTypes.bool,
alwaysShowContent: PropTypes.bool,
subItems: PropTypes.array,
hasGlobalBorderStyle: PropTypes.bool,
as: PropTypes.elementType,
variant: PropTypes.string,
ariaLabel: PropTypes.string,
};
static defaultProps = {
icon: '',
onClick: noop,
hasUnseen: false,
url: '',
};
state = {
isOpenForNonMouseFlow: false,
};
wrapperRef = React.createRef< HTMLDivElement >();
_preloaded = false;
componentDidMount() {
document.addEventListener( 'touchstart', this.closeMenuOnOutsideInteraction );
document.addEventListener( 'keydown', this.closeMenuOnOutsideInteraction );
document.addEventListener( 'click', this.closeMenuOnOutsideInteraction );
return () => {
document.removeEventListener( 'touchstart', this.closeMenuOnOutsideInteraction );
document.removeEventListener( 'keydown', this.closeMenuOnOutsideInteraction );
document.addEventListener( 'click', this.closeMenuOnOutsideInteraction );
};
}
preload = () => {
if ( ! this._preloaded && typeof this.props.preloadSection === 'function' ) {
this._preloaded = true;
this.props.preloadSection();
}
};
renderChildren() {
const { children, hasUnseen, icon } = this.props;
return (
<Fragment>
{ hasUnseen && (
<span className="masterbar__item-bubble" aria-label="You have unseen content" />
) }
{ !! icon && ( typeof icon !== 'string' ? icon : <Gridicon icon={ icon } size={ 24 } /> ) }
{ children && <span className="masterbar__item-content">{ children }</span> }
</Fragment>
);
}
renderSubItems() {
const { subItems } = this.props;
if ( ! subItems ) {
return null;
}
return <ul className="masterbar__item-subitems">{ this.renderSubItemGroups( subItems ) }</ul>;
}
renderSubItemGroups = ( subItemGroups: Array< Array< MasterbarSubItemProps > > ) => {
return subItemGroups
.map( ( subItems, groupIndex ) =>
subItems.map( ( item, i ) => (
<li
key={ `${ groupIndex }-${ i }` }
className={ clsx( 'masterbar__item-subitems-item', item.className, {
'masterbar__item-subitems-item--odd': groupIndex % 2 === 1,
} ) }
>
{ item.onClick && (
<Button
className="is-link"
onClick={ item.onClick }
onTouchEnd={ ( ev: React.TouchEvent ) =>
this.submenuButtonTouch( ev, item.onClick )
}
onKeyDown={ ( ev: React.KeyboardEvent ) =>
this.submenuButtonByKey( ev, item.onClick )
}
>
{ item.label }
</Button>
) }
{ ! item.onClick && item.url && (
<a
href={ item.url }
onTouchEnd={ this.navigateSubAnchorTouch }
onKeyDown={ this.navigateSubAnchorByKey }
>
{ item.label }
</a>
) }
{ ! item.onClick && ! item.url && <div>{ item.label }</div> }
</li>
) )
)
.flat();
};
toggleMenuByTouch = ( event: React.TouchEvent | React.KeyboardEvent ) => {
// If there are no subItems, there is nothing to toggle.
if ( ! this.props.subItems ) {
return;
}
// Prevent navigation by touching the parent menu item, and trigger toggling the menu instead.
event.preventDefault();
this.setState( { isOpenForNonMouseFlow: ! this.state.isOpenForNonMouseFlow } );
};
toggleMenuByKey = ( event: React.KeyboardEvent ) => {
if ( event.key === 'Enter' || event.key === ' ' ) {
this.toggleMenuByTouch( event );
}
};
navigateSubAnchorTouch = ( event: React.TouchEvent | React.KeyboardEvent ) => {
// We must prevent the default anchor behavior and navigate manually. Otherwise there is a
// race condition between the click on the anchor firing and the menu closing before that
// can happen.
event.preventDefault();
const url = event.currentTarget.getAttribute( 'href' );
if ( url ) {
navigate( url );
}
this.setState( { isOpenForNonMouseFlow: false } );
};
navigateSubAnchorByKey = ( event: React.KeyboardEvent ) => {
if ( event.key === 'Enter' || event.key === ' ' ) {
this.navigateSubAnchorTouch( event );
}
};
submenuButtonTouch = (
event: React.TouchEvent | React.KeyboardEvent,
onClick: ( () => void ) | undefined
) => {
event.preventDefault();
this.setState( { isOpenForNonMouseFlow: false } );
onClick && onClick();
};
submenuButtonByKey = ( event: React.KeyboardEvent, onClick: ( () => void ) | undefined ) => {
if ( event.key === 'Enter' || event.key === ' ' ) {
this.submenuButtonTouch( event, onClick );
}
};
closeMenuOnOutsideInteraction = ( event: TouchEvent | KeyboardEvent | MouseEvent ) => {
// If no subItems or the menu is already closed, there is nothing to close.
if ( ! this.props.subItems || ! this.state.isOpenForNonMouseFlow ) {
return;
}
// Check refs to see if the touch event started inside our component, if it didn't, close the menu.
const isInWrapper = this.wrapperRef.current?.contains( event.target as Node );
if ( ! isInWrapper ) {
this.setState( { isOpenForNonMouseFlow: false } );
}
};
render() {
const itemClasses = clsx( 'masterbar__item', this.props.className, {
'is-active': this.props.isActive,
'has-unseen': this.props.hasUnseen,
'masterbar__item--always-show-content': this.props.alwaysShowContent,
'has-subitems': this.props.subItems,
'is-open': this.state.isOpenForNonMouseFlow,
'has-global-border': this.props.hasGlobalBorderStyle,
} );
const attributes = {
'data-tip-target': this.props.tipTarget,
onClick: this.props.onClick,
title: this.props.tooltip,
className: itemClasses,
onTouchStart: this.preload,
onMouseEnter: this.preload,
disabled: this.props.disabled,
'aria-label': this.props.ariaLabel,
};
return (
<div
className={ clsx( 'masterbar__item-wrapper', this.props.wrapperClassName ) }
ref={ this.wrapperRef }
>
<MenuItem
as={ this.props.as }
variant={ this.props.variant }
url={ this.props.url }
innerRef={ this.props.innerRef }
{ ...attributes }
onKeyDown={ this.props.subItems && this.toggleMenuByKey }
onTouchEnd={ this.props.subItems && this.toggleMenuByTouch }
>
{ this.renderChildren() }
</MenuItem>
{ this.renderSubItems() }
</div>
);
}
}
// eslint-disable-next-line react/display-name
export default forwardRef< HTMLButtonElement | HTMLAnchorElement, MasterbarItemProps >(
( props, ref ) => <MasterbarItem innerRef={ ref } { ...props } />
);
type MenuItemProps< R > = {
url?: string;
innerRef?: R;
as?: React.ComponentType;
variant?: string;
} & React.HTMLAttributes< HTMLElement >;
function MenuItem< R >( { url, innerRef, as: Component, ...props }: MenuItemProps< R > ) {
if ( Component ) {
return (
<Component
{ ...props }
{ ...( innerRef ? { ref: innerRef } : {} ) }
{ ...( url ? { url } : {} ) }
/>
);
}
return url ? (
<a href={ url } ref={ innerRef as LegacyRef< HTMLAnchorElement > } { ...props } />
) : (
<button ref={ innerRef as LegacyRef< HTMLButtonElement > } { ...props } />
);
}
|