Skip to content

Commit

Permalink
chore: set aria-pressed when selected
Browse files Browse the repository at this point in the history
  • Loading branch information
DRiFTy17 committed Feb 23, 2024
1 parent 1e13604 commit 1c35c24
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/lib/chips/chip/chip-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export interface IChipAdapter extends IBaseAdapter {
toggleFieldVariant(value: boolean): void;
getChipSetState(): IChipState | null;
setDisabled(value: boolean): void;
setSelected(value: boolean): void;
focusTrigger(options?: FocusOptions): void;
tryFocusRemoveButton(): void;
clickRemoveButton(): void;
Expand Down Expand Up @@ -132,6 +133,13 @@ export class ChipAdapter extends BaseAdapter<IChipComponent> implements IChipAda
}
}

public setSelected(value: boolean): void {
if (this._triggerElement instanceof HTMLAnchorElement) {
return;
}
this._triggerElement.setAttribute('aria-pressed', String(value));
}

public toggleFieldVariant(value: boolean): void {
if (value) {
if (!this._stateLayerElement.isConnected) {
Expand Down Expand Up @@ -199,6 +207,13 @@ export class ChipAdapter extends BaseAdapter<IChipComponent> implements IChipAda
buttonEl.setAttribute('aria-label', `Remove ${this._component.innerText}`);
buttonEl.setAttribute('part', 'remove-button');

if (this._component.disabled) {
buttonEl.disabled = true;
}
if (this._component.selected) {
buttonEl.setAttribute('aria-pressed', 'true');
}

const iconEl = document.createElement('forge-icon');
iconEl.name = 'close';
buttonEl.appendChild(iconEl);
Expand Down
2 changes: 2 additions & 0 deletions src/lib/chips/chip/chip-foundation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,8 @@ export class ChipFoundation implements IChipFoundation {
}

private _applySelected(): void {
this._adapter.setSelected(this._selected);

// If using the filter type, we need to hide the leading slot to ensure that
// the checkmark shows in place of any leading elements
if (this._type === 'filter') {
Expand Down
8 changes: 8 additions & 0 deletions src/lib/chips/chips.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,14 @@ describe('Chips', () => {
expect(firstChip.hasAttribute(CHIP_CONSTANTS.attributes.SELECTED)).to.be.false;
});

it('should be accessible when selected', async () => {
const el = await fixture<IChipComponent>(html`<forge-chip selected>Test</forge-chip>`);
const triggerEl = getTriggerElement(el);

expect(triggerEl.getAttribute('aria-pressed')).to.equal('true');
await expect(el).to.be.accessible();
});

['filter', 'choice', 'input'].forEach(type => {
describe(`${type} chips keyboard selection`, () => {
it(`should select focused chip when pressing enter on ${type} chips`, async () => {
Expand Down

0 comments on commit 1c35c24

Please sign in to comment.