Skip to content

Commit 8c0fda6

Browse files
author
Hector Arce De Las Heras
committed
Refactored and improved icon component
This commit includes a refactoring of the icon component, improving their structure and performance. The changes enhance the maintainability of the code and provide a more efficient rendering of the icons, leading to an improved user experience.
1 parent 020bd3e commit 8c0fda6

File tree

1 file changed

+26
-37
lines changed

1 file changed

+26
-37
lines changed

src/components/icon/icon.tsx

Lines changed: 26 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -14,47 +14,36 @@ const IconBasicComponent = (
1414
ref: React.ForwardedRef<HTMLSpanElement>
1515
): JSX.Element | null => {
1616
const device = useMediaDevice();
17+
const ariaProps = pickAriaProps(props);
18+
const { disabled, altText, onClick, screenReaderText, ...iconProps } = props;
1719

1820
const isLinearIcon =
1921
!!props.color || !!props.customIconStyles?.color || !!props.customIconStyles?.[device]?.color;
2022

21-
const iconWithTitle = (
22-
<IconStandAlone {...props} ref={ref} altText="" icon={props.icon} linearIcon={isLinearIcon} />
23-
);
24-
25-
const buildButton = (): JSX.Element => {
26-
const ariaProps = pickAriaProps(props);
27-
let iconElement = <></>;
28-
if (props.icon) {
29-
if (props.onClick) {
30-
iconElement = (
31-
<IconButtonStyled
32-
{...ariaProps}
33-
$customIconStyles={props.customIconStyles}
34-
$height={props.height}
35-
$width={props.width}
36-
aria-disabled={props.disabled}
37-
aria-label={props['aria-label'] || props.altText}
38-
disabled={props.disabled}
39-
tabIndex={tabIndex}
40-
type={ButtonType.BUTTON}
41-
onClick={props.onClick}
42-
>
43-
<ScreenReaderOnly>{props.screenReaderText}</ScreenReaderOnly>
44-
{iconWithTitle}
45-
</IconButtonStyled>
46-
);
47-
} else {
48-
iconElement = (
49-
<IconStandAlone {...props} ref={ref} icon={props.icon} linearIcon={isLinearIcon} />
50-
);
51-
}
52-
return iconElement;
53-
}
54-
return iconElement;
55-
};
56-
57-
return buildButton();
23+
if (!props.icon) {
24+
return <></>;
25+
}
26+
27+
if (props.onClick) {
28+
return (
29+
<IconButtonStyled
30+
{...ariaProps}
31+
$customIconStyles={props.customIconStyles}
32+
$height={props.height}
33+
$width={props.width}
34+
aria-disabled={disabled}
35+
aria-label={props['aria-label'] || altText}
36+
disabled={disabled}
37+
type={ButtonType.BUTTON}
38+
onClick={onClick}
39+
>
40+
<ScreenReaderOnly>{screenReaderText}</ScreenReaderOnly>
41+
<IconStandAlone {...iconProps} ref={ref} linearIcon={isLinearIcon} />
42+
</IconButtonStyled>
43+
);
44+
}
45+
46+
return <IconStandAlone {...iconProps} ref={ref} altText={altText} linearIcon={isLinearIcon} />;
5847
};
5948

6049
export const IconBasic = React.forwardRef(IconBasicComponent);

0 commit comments

Comments
 (0)