Skip to content

Commit

Permalink
feat(vertical-nav): add trigger unique id and configurable aria-label
Browse files Browse the repository at this point in the history
  • Loading branch information
valentin-mladenov committed Feb 7, 2025
1 parent 2213a08 commit 3f6186e
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export default {
args: {
// inputs
clrVerticalNavGroupExpanded: false,
clrVerticalNavTriggerLabel: '',
// outputs
clrVerticalNavGroupExpandedChange: action('clrVerticalNavGroupExpandedChange'),
// story helpers
Expand All @@ -57,7 +58,7 @@ const NavGroupTemplate: StoryFn = args => ({
template: `
<div class="main-container">
<div class="content-container">
<clr-vertical-nav [clrVerticalNavCollapsible]="true">
<clr-vertical-nav [clrVerticalNavCollapsible]="true" [clrVerticalNavTriggerLabel]="clrVerticalNavTriggerLabel">
<clr-vertical-nav-group
[clrVerticalNavGroupExpanded]="clrVerticalNavGroupExpanded"
(clrVerticalNavGroupExpandedChange)="clrVerticalNavGroupExpandedChange($event)"
Expand Down
2 changes: 2 additions & 0 deletions .storybook/stories/vertical-nav/vertical-nav.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export default {
// inputs
clrVerticalNavCollapsible: false,
clrVerticalNavCollapsed: false,
clrVerticalNavTriggerLabel: '',
// outputs
clrVerticalNavCollapsedChange: action('clrVerticalNavCollapsedChange'),
// story helpers
Expand All @@ -57,6 +58,7 @@ const VerticalNavTemplate: StoryFn = args => ({
<div class="content-container">
<clr-vertical-nav
[clrVerticalNavCollapsible]="clrVerticalNavCollapsible"
[clrVerticalNavTriggerLabel]="clrVerticalNavTriggerLabel"
[clrVerticalNavCollapsed]="clrVerticalNavCollapsed"
(clrVerticalNavCollapsedChange)="clrVerticalNavCollapsedChange($event)"
>
Expand Down
5 changes: 3 additions & 2 deletions projects/angular/src/layout/vertical-nav/vertical-nav.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
type="button"
class="nav-trigger"
[class.on-collapse]="collapsed"
[attr.aria-controls]="triggerId"
[attr.aria-expanded]="ariaExpanded"
[attr.aria-label]="commonStrings.keys.verticalNavToggle"
[attr.aria-label]="verticalNavTriggerLabel"
(click)="toggleByButton()"
*ngIf="collapsible"
>
Expand All @@ -20,7 +21,7 @@
[attr.direction]="(this.collapsed) ? 'right' : 'left'"
></cds-icon>
</button>
<div class="nav-content">
<div [id]="triggerId" class="nav-content">
<ng-content></ng-content>
<button
type="button"
Expand Down
21 changes: 21 additions & 0 deletions projects/angular/src/layout/vertical-nav/vertical-nav.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { By } from '@angular/platform-browser';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';

import { ClrIconModule } from '../../icon/icon.module';
import { ClrCommonStringsService } from '../../utils/i18n/common-strings.service';
import { VerticalNavService } from './providers/vertical-nav.service';
import { ClrVerticalNav } from './vertical-nav';
import { ClrVerticalNavModule } from './vertical-nav.module';
Expand Down Expand Up @@ -470,6 +471,7 @@ export default function (): void {

describe('Template API', () => {
let vertNavService: VerticalNavService;
const commonStrings = new ClrCommonStringsService();

beforeEach(() => {
fixture = TestBed.createComponent(APITestComponent);
Expand All @@ -482,6 +484,23 @@ export default function (): void {
fixture.destroy();
});

it('supports an input to change aria-label on collapsible vertical nav trigger', () => {
fixture.componentInstance.collapsible = true;
fixture.detectChanges();

expect(vertNavService.collapsible).toBe(true);

const trigger: HTMLElement = compiled.querySelector('.nav-trigger');

expect(trigger.getAttribute('aria-label')).toBe(commonStrings.keys.verticalNavToggle);

const verticalNavTriggerLabel = 'Changed label string';
fixture.componentInstance.clrVerticalNavTriggerLabel = verticalNavTriggerLabel;
fixture.detectChanges();

expect(trigger.getAttribute('aria-label')).toBe(verticalNavTriggerLabel);
});

it('supports an input to enable the collapsible behavior of the nav', () => {
expect(vertNavService.collapsible).toBe(false);

Expand Down Expand Up @@ -643,11 +662,13 @@ class ViewBasicsTestComponent {
#nav
[clrVerticalNavCollapsible]="collapsible"
[clrVerticalNavCollapsed]="collapsed"
[clrVerticalNavTriggerLabel]="clrVerticalNavTriggerLabel"
(clrVerticalNavCollapsedChange)="updateCollapsed($event)"
></clr-vertical-nav>
`,
})
class APITestComponent {
clrVerticalNavTriggerLabel: string;
collapsible = false;
collapsed = false;
collapsedChange: boolean;
Expand Down
16 changes: 16 additions & 0 deletions projects/angular/src/layout/vertical-nav/vertical-nav.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Component, EventEmitter, Input, OnDestroy, Output } from '@angular/core
import { Subscription } from 'rxjs';

import { ClrCommonStringsService } from '../../utils/i18n/common-strings.service';
import { uniqueIdFactory } from '../../utils/id-generator/id-generator.service';
import { VerticalNavGroupRegistrationService } from './providers/vertical-nav-group-registration.service';
import { VerticalNavIconService } from './providers/vertical-nav-icon.service';
import { VerticalNavService } from './providers/vertical-nav.service';
Expand All @@ -25,9 +26,12 @@ import { VerticalNavService } from './providers/vertical-nav.service';
},
})
export class ClrVerticalNav implements OnDestroy {
triggerId = uniqueIdFactory();

@Output('clrVerticalNavCollapsedChange') private _collapsedChanged = new EventEmitter<boolean>(true);

private _sub: Subscription;
private _verticalNavTriggerLabel: string;

constructor(
private _navService: VerticalNavService,
Expand All @@ -40,6 +44,18 @@ export class ClrVerticalNav implements OnDestroy {
});
}

@Input('clrVerticalNavTriggerLabel')
get verticalNavTriggerLabel() {
if (this._verticalNavTriggerLabel) {
return this._verticalNavTriggerLabel;
}

return this.commonStrings.keys.verticalNavToggle;
}
set verticalNavTriggerLabel(value: string) {
this._verticalNavTriggerLabel = value;
}

@Input('clrVerticalNavCollapsible')
get collapsible(): boolean | string {
return this._navService.collapsible;
Expand Down

0 comments on commit 3f6186e

Please sign in to comment.