Skip to content

Commit 6cb4faa

Browse files
committed
Fix truncatable-part keyboard accessibility
1 parent 1efa886 commit 6cb4faa

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

src/app/shared/truncatable/truncatable-part/truncatable-part.component.html

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@
22
<div #content class="content dont-break-out preserve-line-breaks">
33
<ng-content></ng-content>
44
</div>
5-
<button class="btn btn-link p-0 expandButton" dsDragClick (actualClick)="toggle()">
6-
<i class="fas fa-angle-down"></i>
7-
<span class="ml-1">{{ 'item.truncatable-part.show-more' | translate }}</span>
8-
</button>
9-
<button class="btn btn-link p-0 collapseButton" dsDragClick (actualClick)="toggle()" *ngIf="expand && expandable">
10-
<i class="fas fa-angle-up"></i>
11-
<span class="ml-1">{{ 'item.truncatable-part.show-less' | translate }}</span>
5+
<button
6+
class="btn btn-link p-0 {{isExpanded ? 'collapseButton' : 'expandButton'}}"
7+
dsDragClick
8+
(actualClick)="toggle()"
9+
(keyup.Enter)="toggle()"
10+
(keyup.Space)="toggle()"
11+
role="button"
12+
[attr.aria-expanded]="isExpanded"
13+
>
14+
<i class="fas {{isExpanded ? 'fa-angle-up' : 'fa-angle-down'}}"></i>
15+
<span class="ml-1">{{ 'item.truncatable-part.show-' + (isExpanded ? 'less' : 'more') | translate }}</span>
1216
</button>
1317
</div>

src/app/shared/truncatable/truncatable-part/truncatable-part.component.spec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ describe('TruncatablePartComponent', () => {
8787
const a = fixture.debugElement.query(By.css('.collapseButton'));
8888
expect(a).toBeNull();
8989
});
90+
91+
it('expandButton aria-expanded should be false', () => {
92+
const btn = fixture.debugElement.query(By.css('.expandButton'));
93+
expect(btn.nativeElement.getAttribute('aria-expanded')).toEqual('false');
94+
});
9095
});
9196

9297
describe('When the item is expanded', () => {
@@ -115,6 +120,14 @@ describe('TruncatablePartComponent', () => {
115120
const a = fixture.debugElement.query(By.css('.collapseButton'));
116121
expect(a).not.toBeNull();
117122
});
123+
124+
it('collapseButton aria-expanded should be true', () => {
125+
(comp as any).setLines();
126+
(comp as any).expandable = true;
127+
fixture.detectChanges();
128+
const btn = fixture.debugElement.query(By.css('.collapseButton'));
129+
expect(btn.nativeElement.getAttribute('aria-expanded')).toEqual('true');
130+
});
118131
});
119132
});
120133

src/app/shared/truncatable/truncatable-part/truncatable-part.component.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,13 @@ export class TruncatablePartComponent implements AfterViewChecked, OnInit, OnDes
150150
}
151151
}
152152

153+
/**
154+
* Indicates if the content is expanded, button state is 'Collapse'
155+
*/
156+
public get isExpanded() {
157+
return this.expand && this.expandable;
158+
}
159+
153160
/**
154161
* Unsubscribe from the subscription
155162
*/

0 commit comments

Comments
 (0)