Skip to content

Commit ddd9cb5

Browse files
committed
fix(BBButton): ensure acessibility and stacked layout displacement
Ensures that the button has accessibility tags by indicating the label tag in stacked layout via `aria-labelledBy` attribute. Additionaly, corrects a malformed css style that caused the stacked layout button to render its icons out of place when `helperOnClick` prop is specified.
1 parent 728b9cb commit ddd9cb5

File tree

2 files changed

+28
-19
lines changed

2 files changed

+28
-19
lines changed

src/components/Button/component.tsx

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,10 @@ function Button(props: ButtonProps): JSX.Element {
7171
'aria-labelledby'?: string;
7272
'aria-describedby'?: string;
7373
} = {};
74-
if (ariaLabel) accessibilityProps['aria-label'] = ariaLabel;
75-
if (ariaLabelledBy) accessibilityProps['aria-labelledby'] = ariaLabelledBy;
74+
const generatedId = useId();
75+
const defaultLabelId = `${id || generatedId}-label`;
76+
accessibilityProps['aria-label'] = ariaLabel || label;
77+
accessibilityProps['aria-labelledby'] = ariaLabelledBy || defaultLabelId;
7678
if (ariaDescribedBy) accessibilityProps['aria-describedby'] = ariaDescribedBy;
7779

7880
const buttonElement = (() => {
@@ -121,11 +123,12 @@ function Button(props: ButtonProps): JSX.Element {
121123
disabled={disabled}
122124
>
123125
{!hideHelperIcon && (
124-
<Styled.helperIconContainer
126+
<Styled.HelperIconContainer
125127
// Add this property only when there is a onClick function for the auxiliary icon.
126128
// It controls whether the auxiliary button is going to have an independent hover state.
127129
{...(helperOnClick && { 'data-is-aux-icon': true })}
128-
role="button"
130+
{...(helperOnClick && { 'aria-label': accessibilityProps['aria-label'] + ' helper' })}
131+
{...(helperOnClick && { role: "button" })}
129132
$hover={helperOnClick !== null}
130133
$color={color}
131134
$variant={variant}
@@ -137,12 +140,12 @@ function Button(props: ButtonProps): JSX.Element {
137140
}
138141
}}
139142
>
140-
<Styled.helperIcon>{helperIcon}</Styled.helperIcon>
141-
</Styled.helperIconContainer>
143+
<Styled.HelperIcon>{helperIcon}</Styled.HelperIcon>
144+
</Styled.HelperIconContainer>
142145
)}
143146
{icon && <Styled.IconWrapper>{icon}</Styled.IconWrapper>}
144147
</Styled.Button>
145-
<Styled.ButtonText>{label}</Styled.ButtonText>
148+
<Styled.ButtonText id={defaultLabelId}>{label}</Styled.ButtonText>
146149
</Styled.ButtonWrapper>
147150
);
148151
}
@@ -164,7 +167,7 @@ function Button(props: ButtonProps): JSX.Element {
164167
disabled={disabled}
165168
>
166169
{iconStart && iconStart}
167-
{label && <span>{label}</span>}
170+
{label && <span id={defaultLabelId}>{label}</span>}
168171
{children}
169172
{iconEnd && iconEnd}
170173
</Styled.Button>

src/components/Button/styles.ts

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -163,16 +163,22 @@ export const Button = styled.button<StyledButtonProps>`
163163
}}
164164
`;
165165

166-
export const helperIconContainer = styled.div<StyledHelperIconContainer>`
167-
${({ $hover, $variant, $color }) => $hover && `
168-
&&:hover,
169-
&&:active,
170-
background-color: ${CSS_COLOR_PROPERTIES[$variant][$color].hoverBackground};
171-
}
172-
&&:focus {
173-
outline: 1px solid ${CSS_COLOR_PROPERTIES[$variant][$color].outline};
174-
}
175-
`}
166+
export const HelperIconContainer = styled.div<StyledHelperIconContainer>`
167+
${({ $hover, $variant, $color }) => {
168+
if (!$hover) return null;
169+
170+
return css`
171+
&&:hover,
172+
&&:active {
173+
${CSS_COLOR_PROPERTIES[$variant][$color].hoverBackground &&
174+
`background-color: ${CSS_COLOR_PROPERTIES[$variant][$color].hoverBackground};`}
175+
}
176+
177+
&&:focus {
178+
outline: 1px solid ${CSS_COLOR_PROPERTIES[$variant][$color].outline};
179+
}
180+
`;
181+
}}
176182
border-radius: 50%;
177183
padding: 0.2rem;
178184
position: absolute;
@@ -183,7 +189,7 @@ export const helperIconContainer = styled.div<StyledHelperIconContainer>`
183189
}
184190
`;
185191

186-
export const helperIcon = styled.div`
192+
export const HelperIcon = styled.div`
187193
display: flex;
188194
align-items: center;
189195
justify-content: center;

0 commit comments

Comments
 (0)