|
| 1 | +/** |
| 2 | + * HeaderMenu MenuItem component. |
| 3 | + * |
| 4 | + * Site Kit by Google, Copyright 2026 Google LLC |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +/** |
| 20 | + * External dependencies |
| 21 | + */ |
| 22 | +import classnames from 'classnames'; |
| 23 | +import PropTypes from 'prop-types'; |
| 24 | + |
| 25 | +export default function MenuItem( { |
| 26 | + id, |
| 27 | + className, |
| 28 | + role, |
| 29 | + isInteractive = true, |
| 30 | + onClick, |
| 31 | + icon, |
| 32 | + label, |
| 33 | + description, |
| 34 | + trailing, |
| 35 | + children, |
| 36 | + itemClassName = 'googlesitekit-header-menu__item', |
| 37 | + iconClassName = 'googlesitekit-header-menu__item-icon', |
| 38 | + labelClassName = 'googlesitekit-header-menu__item-label', |
| 39 | + descriptionClassName = 'googlesitekit-header-menu__item-description', |
| 40 | + trailingClassName = 'googlesitekit-header-menu__item-trailing', |
| 41 | +} ) { |
| 42 | + const content = children || ( |
| 43 | + <div className={ itemClassName }> |
| 44 | + { icon && <div className={ iconClassName }>{ icon }</div> } |
| 45 | + { label && <span className={ labelClassName }>{ label }</span> } |
| 46 | + { description && ( |
| 47 | + <span className={ descriptionClassName }>{ description }</span> |
| 48 | + ) } |
| 49 | + { trailing && ( |
| 50 | + <span className={ trailingClassName }>{ trailing }</span> |
| 51 | + ) } |
| 52 | + </div> |
| 53 | + ); |
| 54 | + |
| 55 | + const listRole = role === undefined && isInteractive ? 'menuitem' : role; |
| 56 | + const listClassName = classnames( className, { |
| 57 | + 'mdc-list-item': isInteractive, |
| 58 | + } ); |
| 59 | + const hasAction = typeof onClick === 'function'; |
| 60 | + |
| 61 | + return ( |
| 62 | + <li |
| 63 | + id={ id } |
| 64 | + className={ listClassName || undefined } |
| 65 | + role={ listRole } |
| 66 | + > |
| 67 | + { hasAction ? ( |
| 68 | + <button |
| 69 | + type="button" |
| 70 | + className="googlesitekit-header-menu__item-button" |
| 71 | + onClick={ onClick } |
| 72 | + > |
| 73 | + { content } |
| 74 | + </button> |
| 75 | + ) : ( |
| 76 | + content |
| 77 | + ) } |
| 78 | + </li> |
| 79 | + ); |
| 80 | +} |
| 81 | + |
| 82 | +MenuItem.propTypes = { |
| 83 | + id: PropTypes.string, |
| 84 | + className: PropTypes.string, |
| 85 | + role: PropTypes.string, |
| 86 | + isInteractive: PropTypes.bool, |
| 87 | + onClick: PropTypes.func, |
| 88 | + icon: PropTypes.node, |
| 89 | + label: PropTypes.node, |
| 90 | + description: PropTypes.node, |
| 91 | + trailing: PropTypes.node, |
| 92 | + children: PropTypes.node, |
| 93 | + itemClassName: PropTypes.string, |
| 94 | + iconClassName: PropTypes.string, |
| 95 | + labelClassName: PropTypes.string, |
| 96 | + descriptionClassName: PropTypes.string, |
| 97 | + trailingClassName: PropTypes.string, |
| 98 | +}; |
0 commit comments