From f19f5e3e583bf81b4f17b2d83e17602d345137cf Mon Sep 17 00:00:00 2001 From: droshev Date: Fri, 1 Dec 2023 10:13:56 -0500 Subject: [PATCH] fix(core): segmented button accessibility improvements --- libs/core/src/lib/button/button.component.ts | 5 ++-- .../segmented-button.component.ts | 23 ++++++++++++++----- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/libs/core/src/lib/button/button.component.ts b/libs/core/src/lib/button/button.component.ts index f438ff17454..65a9de4f465 100644 --- a/libs/core/src/lib/button/button.component.ts +++ b/libs/core/src/lib/button/button.component.ts @@ -12,7 +12,7 @@ import { } from '@angular/core'; import { BaseButton } from './base-button'; import { Subscription } from 'rxjs'; -import { applyCssClass, CssClassBuilder } from '@fundamental-ngx/cdk/utils'; +import { applyCssClass, CssClassBuilder, FocusableItemDirective } from '@fundamental-ngx/cdk/utils'; import { ContentDensityObserver, contentDensityObserverProviders } from '@fundamental-ngx/core/content-density'; import { FD_BUTTON_COMPONENT } from './tokens'; @@ -47,7 +47,8 @@ import { FD_BUTTON_COMPONENT } from './tokens'; provide: FD_BUTTON_COMPONENT, useExisting: ButtonComponent } - ] + ], + hostDirectives: [FocusableItemDirective] }) export class ButtonComponent extends BaseButton implements OnChanges, CssClassBuilder, OnInit, OnDestroy { /** The property allows user to pass additional css classes. */ diff --git a/libs/core/src/lib/segmented-button/segmented-button.component.ts b/libs/core/src/lib/segmented-button/segmented-button.component.ts index c9ee326070e..3ca5a73214d 100644 --- a/libs/core/src/lib/segmented-button/segmented-button.component.ts +++ b/libs/core/src/lib/segmented-button/segmented-button.component.ts @@ -6,17 +6,19 @@ import { ContentChildren, ElementRef, forwardRef, + Host, HostBinding, HostListener, Input, OnDestroy, + Optional, QueryList, ViewEncapsulation } from '@angular/core'; import { ButtonComponent, FD_BUTTON_COMPONENT } from '@fundamental-ngx/core/button'; import { filter, observeOn, startWith, takeUntil, tap } from 'rxjs/operators'; import { Subject, merge, fromEvent, asyncScheduler } from 'rxjs'; -import { DestroyedService, KeyUtil } from '@fundamental-ngx/cdk/utils'; +import { DestroyedService, FocusableListDirective, KeyUtil } from '@fundamental-ngx/cdk/utils'; import { ENTER, SPACE } from '@angular/cdk/keycodes'; import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms'; @@ -40,7 +42,7 @@ export type SegmentedButtonValue = string | (string | null)[] | null; templateUrl: './segmented-button.component.html', styleUrls: ['./segmented-button.component.scss'], host: { - role: 'group' + role: 'listbox' }, encapsulation: ViewEncapsulation.None, changeDetection: ChangeDetectionStrategy.OnPush, @@ -51,7 +53,8 @@ export type SegmentedButtonValue = string | (string | null)[] | null; multi: true }, DestroyedService - ] + ], + hostDirectives: [FocusableListDirective] }) export class SegmentedButtonComponent implements AfterViewInit, ControlValueAccessor, OnDestroy { /** Whether segmented button is on toggle mode, which allows to toggle more than 1 button */ @@ -89,8 +92,13 @@ export class SegmentedButtonComponent implements AfterViewInit, ControlValueAcce constructor( private readonly _changeDetRef: ChangeDetectorRef, private readonly _elementRef: ElementRef, - private readonly _onDestroy$: DestroyedService - ) {} + private readonly _onDestroy$: DestroyedService, + @Optional() @Host() private focusableList: FocusableListDirective + ) { + if (this.focusableList) { + this.focusableList.navigationDirection = 'horizontal'; + } + } /** @hidden */ ngAfterViewInit(): void { @@ -150,7 +158,10 @@ export class SegmentedButtonComponent implements AfterViewInit, ControlValueAcce this._onRefresh$.next(); this._toggleDisableButtons(this._isDisabled); this._pickButtonsByValues(this._currentValue); - this._buttons.forEach((button) => this._listenToTriggerEvents(button)); + this._buttons.forEach((button) => { + button.elementRef.nativeElement.role = 'option'; + this._listenToTriggerEvents(button); + }); }); }