Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ui-color-picker): add hex to aria-label #1857

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,7 @@ describe('<ColorPreset />', () => {

testColors.forEach((testColor, index) => {
const tooltip = tooltips[index]
const indicator = indicators[index]

expect(tooltip.getAttribute('id')).toBe(
indicator.getAttribute('aria-describedby')
)
expect(tooltip).toHaveTextContent(testColor)
})
})
Expand Down
28 changes: 24 additions & 4 deletions packages/ui-color-picker/src/ColorPreset/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,11 @@ class ColorPreset extends Component<ColorPresetProps, ColorPresetState> {
cursor={this.props.disabled ? 'not-allowed' : 'auto'}
as="button"
{...(selectOnClick ? { onClick: () => this.props.onSelect(color) } : {})}
{...(this.isSelectedColor(color) ? { 'aria-label': 'selected' } : {})}
{...{
'aria-label': `${color}${
this.isSelectedColor(color) ? ' selected' : ''
Copy link
Contributor Author

@joyenjoyer joyenjoyer Feb 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the color is selected, I append the 'selected' text to the hex code so the user gets information about that through the screenreader.

}`
}}
>
<div>
<ColorIndicator color={color} shape="rectangle" role="presentation" />
Expand All @@ -282,9 +286,25 @@ class ColorPreset extends Component<ColorPresetProps, ColorPresetState> {
</View>
)

renderIndicatorTooltip = (child: React.ReactElement, color: string) => (
<Tooltip renderTip={<div>{color}</div>}>{child}</Tooltip>
)
renderIndicatorTooltip = (child: React.ReactElement, color: string) => {
return (
<Tooltip
renderTip={<div>{color}</div>}
elementRef={(element) => {
if (
Copy link
Contributor Author

@joyenjoyer joyenjoyer Feb 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aria-describedby on the Tooltip's button has to be removed otherwise the Tooltip text is read aloud by screenreaders - we want to avoid this here because the aria-label has the same content in it already. I am using the Tooltip's elementRef property and the Element's API call to access the button.

element &&
element.firstElementChild instanceof HTMLButtonElement
) {
// The tooltip and the button's aria-label has the same text content. This is redundant for screenreaders.
// Aria-describedby is removed to bypass reading the tooltip twice
element.firstElementChild.removeAttribute('aria-describedby')
}
}}
>
{child}
</Tooltip>
)
}

renderSettingsMenu = (color: string, index: number) => (
<Drilldown
Expand Down
Loading