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

enabled arrow navigation with Tabbable #33293

Draft
wants to merge 6 commits into
base: charting/web-components
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,21 @@ import { ChartDataPoint } from './horizontal-bar-chart.options.js';
*
* @public
*/
export function horizontalbarchartTemplate<T extends HorizontalBarChart>(): ElementViewTemplate<T> {
export function horizontalbarchartTemplate<T extends HorizontalBarChart>(this: any): ElementViewTemplate<T> {
return html<T>`
<template ${ref('rootDiv')}>
<div ${ref('chartContainer')}></div>
<div ${ref('chartContainer')} @keydown="${(x, c) => x.handleRootDivKeydown(c.event as KeyboardEvent)}"></div>
${when(
x => !x.hideLegends,
html<T>`
<div class="legendcontainer" role="listbox" aria-label="${x => x.legendListLabel || 'Legends'}">
<div
${ref('legendContainer')}
id="legend-div"
Anush2303 marked this conversation as resolved.
Show resolved Hide resolved
class="legendcontainer"
role="listbox"
aria-label="${x => x.legendListLabel || 'Legends'}"
@keydown="${(x, c) => x.handleLegendKeydown(c.event as KeyboardEvent)}"
>
${repeat(
x => x.uniqueLegends,
html<ChartDataPoint, T>` <button
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { attr, FASTElement, observable } from '@microsoft/fast-element';
import { create as d3Create, select as d3Select } from 'd3-selection';
import * as tabbable from 'tabbable';
import { getRTL, jsonConverter, SVG_NAMESPACE_URI, validateChartPropsArray } from '../utils/chart-helpers.js';
import { ChartDataPoint, ChartProps, Variant } from './horizontal-bar-chart.options.js';

Expand Down Expand Up @@ -48,10 +49,12 @@ export class HorizontalBarChart extends FASTElement {

public rootDiv!: HTMLDivElement;
public chartContainer!: HTMLDivElement;

public legendContainer!: HTMLDivElement;
private _isRTL: boolean = false;
private barHeight: number = 12;
private _bars: SVGRectElement[] = [];
public currentLegendIndex: number = 0;
public currentRootdivIndex: number = 0;

connectedCallback() {
super.connectedCallback();
Expand All @@ -70,9 +73,40 @@ export class HorizontalBarChart extends FASTElement {
}
this.hydrateLegends();
}
private handleArrowNavigation(
event: KeyboardEvent,
elements: tabbable.FocusableElement[],
currentIndex: number,
isRTL: boolean,
): number {
let nextIndex = currentIndex;

// Handle ArrowRight and ArrowLeft
switch (event.key) {
case 'ArrowRight':
event.preventDefault();
nextIndex = isRTL
? (currentIndex - 1 + elements.length) % elements.length
: (currentIndex + 1) % elements.length;
break;

case 'ArrowLeft':
event.preventDefault();
nextIndex = isRTL
? (currentIndex + 1) % elements.length
: (currentIndex - 1 + elements.length) % elements.length;
break;

default:
return currentIndex;
}

// Focus the next element
elements[nextIndex]?.focus();
return nextIndex;
}
public renderChart() {
const chartContainerDiv = d3Select(this.chartContainer);
const chartContainerDiv = d3Select(this.chartContainer).attr('id', 'root-div');
Anush2303 marked this conversation as resolved.
Show resolved Hide resolved
chartContainerDiv
.selectAll('div')
.data(this.data!)
Expand Down Expand Up @@ -451,4 +485,25 @@ export class HorizontalBarChart extends FASTElement {
});
}
};

// Legend keydown event handler
public handleLegendKeydown(event: KeyboardEvent): void {
const legendButtons = tabbable.tabbable(this.legendContainer);
if (legendButtons.length > 0) {
this.currentLegendIndex = this.handleArrowNavigation(event, legendButtons, this.currentLegendIndex, this._isRTL);
}
}

// Root div keydown event handler
public handleRootDivKeydown(event: KeyboardEvent): void {
const tabbableElements = tabbable.tabbable(this.chartContainer);
if (tabbableElements.length > 0 && event.target && tabbableElements.includes(event.target as HTMLElement)) {
this.currentRootdivIndex = this.handleArrowNavigation(
event,
tabbableElements,
this.currentRootdivIndex,
this._isRTL,
);
}
}
}
Loading