|
1 |
| -import { CSSProperties, forwardRef } from 'react' |
| 1 | +import { forwardRef } from 'react' |
2 | 2 |
|
3 | 3 | import cn from 'classnames'
|
4 | 4 |
|
5 |
| -import { useMediaQueryListener } from 'hooks/useMediaQueryListener' |
6 | 5 | import { CSSCustomProperties } from 'styles/types'
|
7 | 6 | import { toCSSVariableName } from 'utils/styles'
|
8 | 7 |
|
| 8 | +import { BaseButton } from './BaseButton' |
9 | 9 | import styles from './HarmonyButton.module.css'
|
10 | 10 | import {
|
11 | 11 | HarmonyButtonProps,
|
12 |
| - HarmonyButtonType, |
13 |
| - HarmonyButtonSize |
| 12 | + HarmonyButtonSize, |
| 13 | + HarmonyButtonType |
14 | 14 | } from './types'
|
15 | 15 |
|
16 | 16 | const SIZE_STYLE_MAP: { [k in HarmonyButtonSize]: [string, string, string] } = {
|
@@ -48,69 +48,36 @@ export const HarmonyButton = forwardRef<HTMLButtonElement, HarmonyButtonProps>(
|
48 | 48 | function HarmonyButton(props, ref) {
|
49 | 49 | const {
|
50 | 50 | color,
|
51 |
| - text, |
52 | 51 | variant = HarmonyButtonType.PRIMARY,
|
53 | 52 | size = HarmonyButtonSize.DEFAULT,
|
54 |
| - iconLeft: LeftIconComponent, |
55 |
| - iconRight: RightIconComponent, |
56 | 53 | disabled,
|
57 |
| - widthToHideText, |
58 |
| - minWidth, |
59 |
| - className, |
60 |
| - 'aria-label': ariaLabelProp, |
61 |
| - fullWidth, |
62 |
| - ...other |
| 54 | + ...baseProps |
63 | 55 | } = props
|
64 |
| - const { isMatch: textIsHidden } = useMediaQueryListener( |
65 |
| - `(max-width: ${widthToHideText}px)` |
66 |
| - ) |
67 |
| - |
68 |
| - const isTextVisible = !!text && !textIsHidden |
69 |
| - |
70 |
| - const getAriaLabel = () => { |
71 |
| - if (ariaLabelProp) return ariaLabelProp |
72 |
| - // Use the text prop as the aria-label if the text becomes hidden |
73 |
| - // and no aria-label was provided to keep the button accessible. |
74 |
| - else if (textIsHidden && typeof text === 'string') return text |
75 |
| - return undefined |
76 |
| - } |
77 | 56 |
|
78 | 57 | const style: CSSCustomProperties = {
|
79 |
| - minWidth: minWidth && isTextVisible ? `${minWidth}px` : 'unset', |
80 | 58 | '--button-color':
|
81 | 59 | !disabled && color ? `var(${toCSSVariableName(color)})` : undefined
|
82 | 60 | }
|
83 | 61 |
|
84 | 62 | const [buttonSizeClass, iconSizeClass, textSizeClass] = SIZE_STYLE_MAP[size]
|
85 | 63 |
|
86 | 64 | return (
|
87 |
| - <button |
88 |
| - aria-label={getAriaLabel()} |
89 |
| - className={cn( |
90 |
| - styles.button, |
91 |
| - buttonSizeClass, |
92 |
| - TYPE_STYLE_MAP[variant], |
93 |
| - { |
94 |
| - [styles.disabled]: disabled, |
95 |
| - [styles.fullWidth]: fullWidth |
96 |
| - }, |
97 |
| - className |
98 |
| - )} |
99 |
| - disabled={disabled} |
| 65 | + <BaseButton |
100 | 66 | ref={ref}
|
101 |
| - style={style as CSSProperties} |
102 |
| - {...other} |
103 |
| - > |
104 |
| - {LeftIconComponent ? ( |
105 |
| - <LeftIconComponent className={cn(styles.icon, iconSizeClass)} /> |
106 |
| - ) : null} |
107 |
| - {isTextVisible ? ( |
108 |
| - <span className={cn(styles.text, textSizeClass)}>{text}</span> |
109 |
| - ) : null} |
110 |
| - {RightIconComponent ? ( |
111 |
| - <RightIconComponent className={cn(styles.icon, iconSizeClass)} /> |
112 |
| - ) : null} |
113 |
| - </button> |
| 67 | + disabled={disabled} |
| 68 | + styles={{ |
| 69 | + button: cn( |
| 70 | + styles.button, |
| 71 | + TYPE_STYLE_MAP[variant], |
| 72 | + { [styles.disabled]: disabled }, |
| 73 | + buttonSizeClass |
| 74 | + ), |
| 75 | + icon: cn(styles.icon, iconSizeClass), |
| 76 | + text: cn(styles.text, textSizeClass) |
| 77 | + }} |
| 78 | + style={style} |
| 79 | + {...baseProps} |
| 80 | + /> |
114 | 81 | )
|
115 | 82 | }
|
116 | 83 | )
|
0 commit comments