|
1 | | -import type { ButtonHTMLAttributes } from 'react'; |
| 1 | +import { forwardRef, type ButtonHTMLAttributes } from 'react'; |
2 | 2 | import { Icon } from '../Icon/Icon'; |
3 | 3 |
|
4 | 4 | interface ButtonProperties extends ButtonHTMLAttributes<HTMLButtonElement> { |
@@ -48,30 +48,43 @@ const sizeStyles = { |
48 | 48 | /** |
49 | 49 | * Primary UI component for user interaction |
50 | 50 | */ |
51 | | -export function Button({ |
52 | | - appearance = 'primary', |
53 | | - asLink = false, |
54 | | - size = 'default', |
55 | | - label, |
56 | | - className, |
57 | | - iconLeft, |
58 | | - iconRight, |
59 | | - ...properties |
60 | | -}: ButtonProperties): JSX.Element { |
61 | | - const styles = [ |
62 | | - ...baseStyles, |
63 | | - ...appearanceStyles[appearance], |
64 | | - ...sizeStyles[size] |
65 | | - ]; |
66 | | - if (asLink) styles.push('a-btn--link'); |
67 | | - if (className) styles.push(className); |
68 | | - if (properties.disabled) styles.push('a-btn--disabled'); |
| 51 | +export const Button = forwardRef<HTMLButtonElement, ButtonProperties>( |
| 52 | + ( |
| 53 | + { |
| 54 | + appearance = 'primary', |
| 55 | + asLink = false, |
| 56 | + size = 'default', |
| 57 | + label, |
| 58 | + className, |
| 59 | + iconLeft, |
| 60 | + iconRight, |
| 61 | + ...properties |
| 62 | + }, |
| 63 | + ref, // Receive the ref as the second argument |
| 64 | + ): JSX.Element => { |
| 65 | + const styles = [ |
| 66 | + ...baseStyles, |
| 67 | + ...appearanceStyles[appearance], |
| 68 | + ...sizeStyles[size], |
| 69 | + ]; |
| 70 | + if (asLink) styles.push('a-btn--link'); |
| 71 | + if (className) styles.push(className); |
| 72 | + if (properties.disabled) styles.push('a-btn--disabled'); |
69 | 73 |
|
70 | | - return ( |
71 | | - <button type='button' className={[...styles].join(' ')} {...properties}> |
72 | | - {iconLeft ? <Icon name={iconLeft} /> : null } |
73 | | - {iconLeft || iconRight ? <span>{label}</span> : label} |
74 | | - {iconRight ? <Icon name={iconRight} /> : null } |
75 | | - </button> |
76 | | - ); |
77 | | -} |
| 74 | + return ( |
| 75 | + <button |
| 76 | + ref={ref} // Attach the forwarded ref here |
| 77 | + type="button" |
| 78 | + className={[...styles].join(' ')} |
| 79 | + {...properties} |
| 80 | + > |
| 81 | + {iconLeft ? <Icon name={iconLeft} isPresentational /> : null} |
| 82 | + {iconLeft || iconRight ? <span>{label}</span> : label} |
| 83 | + {iconRight ? <Icon name={iconRight} isPresentational /> : null} |
| 84 | + </button> |
| 85 | + ); |
| 86 | + }, |
| 87 | +); |
| 88 | + |
| 89 | +// Optional: Set displayName for better debugging in React DevTools |
| 90 | +Button.displayName = 'Button'; |
0 commit comments