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(swatch): mixed-value state must be conveyed to screen readers using ARIA #3330

Merged
merged 15 commits into from
Jun 27, 2023
Merged
2 changes: 2 additions & 0 deletions packages/swatch/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,8 @@ The `mixed-value` attribute and `mixedValue` property outline when an `<sp-swatc
</sp-swatch-group>
```

Please note that the `aria-checked="mixed"` value only applies when the swatch is in a group with `selects="multiple"`

### Nothing

The `nothing` attribute/property outlines that the `<sp-swatch>` represents no color or that it represents "transparent".
Expand Down
9 changes: 8 additions & 1 deletion packages/swatch/src/Swatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,12 +223,19 @@ export class Swatch extends SizedMixin(Focusable, {
);
}
if (changes.has('label')) {
if (this.label) {
if (this.color !== '') {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think you placed this logic in the wrong order and it's what's causing the test failures in CI right now. Can you take a look at those?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should be good now except for the fact that few NumberFields tests are failing. Switching the order of if else did the trick!
Thanks

this.setAttribute('aria-label', this.color);
} else if (this.label !== this.color && this.label?.length) {
this.setAttribute('aria-label', this.label);
} else {
this.removeAttribute('aria-label');
}
}
if (changes.has('mixedValue')) {
if (this.mixedValue) {
this.setAttribute('aria-checked', 'mixed');
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Hey Rajdeep! Just following up on our conversation earlier today.

To add a label to the Swatch that matches the color attribute, you can add the following in the Swatch code's willUpdate function:

if (changes.has('label')) {
            // if there is a colour set,
            if (this.color !== '') { 
                this.setAttribute('aria-label', this.color); // set the aria-label to be this.color
           // otherwise, if the label is different from the colour and is a length longer than 0,
            } else if (this.label !== this.color && this.label.length) { 
                // override the label with this.label
                this.setAttribute('aria-label', this.label); 
            }
             else {
                this.removeAttribute('aria-label');
            }
        }

This obviously doesn't handle situations where our value would be 'mixed' or 'nothing' (since color='' in that situation), but it's better than no label at all. Hopefully, we can get some guidance on @dineshy87 's side as to what kind of aria-label would be best for those situations, too. 😄 Depending on how many different types of labels we need, we may want to abstract this out of willUpdate().

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@najikahalsema Thanks!!

Screenshot 2023-06-23 at 3 41 23 PM

Copy link
Collaborator

Choose a reason for hiding this comment

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

Just one thing--I'd remove the comments from the code!

}

protected override firstUpdated(changes: PropertyValues): void {
Expand Down
5 changes: 5 additions & 0 deletions packages/swatch/test/swatch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ describe('Swatch', () => {
it(`loads default swatch accessibly`, async () => {
await expect(el).to.be.accessible();
});
it('loads [mixed-value] swatch accessibly', async () => {
el.mixedValue = true;
await expect(el).to.be.accessible();
expect(el.getAttribute('aria-checked')).to.equal('mixed');
});
it('loads [nothing] swatch accessibly', async () => {
el.nothing = true;
el.removeAttribute('color');
Expand Down