Make GlChartLegend series rows keyboard operable

What does this MR do?

Makes the series rows in GlChartLegend keyboard operable.

Each series row was a <div role="button"> with only a click handler, no tabindex and no key handler. Keyboard and screen reader users could not focus a row or toggle a series with Enter or Space, which fails WCAG 2.1.1 Keyboard (Level A). For GlLineChart, GlAreaChart, GlStackedColumnChart and GlHeatmap, the legend is the only way to toggle a series, so these users had no way to do it at all.

This MR:

  • Renders each series row as a native <button type="button"> in both the inline and tabular layouts. This makes each row focusable and lets Enter and Space toggle it, with the correct role.
  • Adds aria-pressed so screen readers announce whether each series is shown or hidden.
  • Keeps aria-disabled rather than disabled for series that can't be toggled (the last visible series, or a series with disabled: true), so they stay focusable and are still announced.
  • Aligns aria-disabled in the tabular layout with the inline layout. The tabular layout didn't mark the row as disabled when there is only one series.
  • Adds a focus ring and resets the native button styles, so the legend looks the same as before.

The focus ring uses the gl-focus SCSS mixin with $inset: true instead of the focus-visible:gl-focus-inset utility. The utility sets outline: none with no fallback, so the ring would disappear in forced-colors mode (Windows High Contrast). The mixin adds an outline in that mode. The inset variant also keeps the ring from being clipped by the tabular layout's scroll container.

Closes #3652 (closed)

How to test

  1. Open the charts/chart-legend stories in this MR's review app.
  2. Press Tab to move through the legend. Each series row receives a visible focus ring.
  3. Press Enter or Space on a row. The series hides and shows in the chart, and aria-pressed changes between true and false.
  4. Repeat in With Tabular Layout And Disabled Legend Item. The disabled row is focusable, has aria-disabled="true", and does not toggle.

Screenshots or screen recordings

Layout Focused row
Inline image
Tabular image

Integrations

Legend rows no longer have role="button". Any selector in consuming projects that finds legend rows with [role="button"] needs to use button instead.

Merge request reports

Loading
Loading