|
8 | 8 | import { Close, Menu } from '@carbon/icons-react'; |
9 | 9 |
|
10 | 10 | import cx from 'classnames'; |
11 | | -import React from 'react'; |
| 11 | +import React, { type ComponentProps } from 'react'; |
12 | 12 | import PropTypes from 'prop-types'; |
13 | 13 | import { AriaLabelPropType } from '../../prop-types/AriaPropTypes'; |
14 | 14 | import { usePrefix } from '../../internal/usePrefix'; |
15 | 15 |
|
16 | | -function HeaderMenuButton({ |
| 16 | +type HeaderMenuButtonBaseProps = Omit< |
| 17 | + ComponentProps<'button'>, |
| 18 | + 'title' | 'type' |
| 19 | +>; |
| 20 | + |
| 21 | +interface HeaderMenuButtonProps extends HeaderMenuButtonBaseProps { |
| 22 | + 'aria-label'?: string; |
| 23 | + 'aria-labelledby'?: string; |
| 24 | + className?: string; |
| 25 | + renderMenuIcon?: JSX.Element; |
| 26 | + renderCloseIcon?: JSX.Element; |
| 27 | + isActive?: boolean; |
| 28 | + isCollapsible?: boolean; |
| 29 | +} |
| 30 | + |
| 31 | +export default function HeaderMenuButton({ |
17 | 32 | 'aria-label': ariaLabel, |
18 | 33 | 'aria-labelledby': ariaLabelledBy, |
19 | 34 | className: customClassName, |
20 | 35 | renderMenuIcon, |
21 | 36 | renderCloseIcon, |
22 | | - onClick, |
23 | 37 | isActive, |
24 | 38 | isCollapsible, |
25 | 39 | ...rest |
26 | | -}) { |
| 40 | +}: HeaderMenuButtonProps) { |
27 | 41 | const prefix = usePrefix(); |
28 | 42 | const className = cx({ |
29 | | - [customClassName]: !!customClassName, |
| 43 | + ...(typeof customClassName === 'string' && { |
| 44 | + [customClassName]: !!customClassName, |
| 45 | + }), |
30 | 46 | [`${prefix}--header__action`]: true, |
31 | 47 | [`${prefix}--header__menu-trigger`]: true, |
32 | 48 | [`${prefix}--header__action--active`]: isActive, |
33 | 49 | [`${prefix}--header__menu-toggle`]: true, |
34 | 50 | [`${prefix}--header__menu-toggle__hidden`]: !isCollapsible, |
35 | 51 | }); |
36 | | - const accessibilityLabel = { |
37 | | - 'aria-label': ariaLabel, |
38 | | - 'aria-labelledby': ariaLabelledBy, |
39 | | - }; |
40 | 52 | const menuIcon = renderMenuIcon ? renderMenuIcon : <Menu size={20} />; |
41 | 53 | const closeIcon = renderCloseIcon ? renderCloseIcon : <Close size={20} />; |
42 | 54 |
|
43 | 55 | return ( |
44 | 56 | <button |
45 | 57 | {...rest} |
46 | | - {...accessibilityLabel} |
| 58 | + aria-label={ariaLabel} |
| 59 | + aria-labelledby={ariaLabelledBy} |
47 | 60 | className={className} |
48 | 61 | title={ariaLabel} |
49 | | - type="button" |
50 | | - onClick={onClick}> |
| 62 | + type="button"> |
51 | 63 | {isActive ? closeIcon : menuIcon} |
52 | 64 | </button> |
53 | 65 | ); |
@@ -76,5 +88,3 @@ HeaderMenuButton.propTypes = { |
76 | 88 | */ |
77 | 89 | onClick: PropTypes.func, |
78 | 90 | }; |
79 | | - |
80 | | -export default HeaderMenuButton; |
|
0 commit comments