# Accessibility

> The ARIA structure of the spreadsheet, how focus moves between its regions, what is announced, and what the automated checks cover.

The spreadsheet follows the WAI-ARIA grid pattern for the cells, the tabs pattern for the ribbon and the sheet tabs, and the dialog pattern for its dialogs. Everything can be done from the keyboard; the keys are listed under [keyboard shortcuts](https://docs.birchui.com/spreadsheet/keyboard.md).

## Structure

| Region | Element | Roles and attributes |
| --- | --- | --- |
| Ribbon tabs | `.lq-sheet-ribbon-tabs` | `tablist` with one `tab` per ribbon tab. The selected tab has `aria-selected="true"`. |
| Ribbon commands | `.lq-sheet-ribbon-panel`, `.lq-sheet-ribbon-bar` | `tabpanel` labelled by its tab, holding a `toolbar` named after the tab ("Home tools"). Each group is a `group` with its name. Toggle buttons use `aria-pressed`, menu buttons `aria-expanded`. |
| Command search | `.lq-sheet-ribbon-search-input` | `combobox` with `aria-expanded`, `aria-controls` and `aria-activedescendant`; its results are a `listbox` of `option`s, and commands that cannot run have `aria-disabled`. The number of matches is announced. `Alt` + `Q` focuses it. |
| Ribbon display options | `[data-ribbon-display]` | A menu button (`aria-haspopup="menu"`, `aria-expanded`) whose menu holds four `menuitemcheckbox` entries, one per [ribbon layout](https://docs.birchui.com/spreadsheet/toolbar.md#ribbon-height-classic-single-line-tabs-only). A collapsed ribbon hides its `tabpanel`; opening and closing it with `Ctrl` + `F1` is announced. |
| Formula bar | `.lq-sheet-formula-bar` | The name box and the formula input, each with a label. |
| Grid | `.lq-sheet-grid` inside `.lq-sheet-viewport` | `grid` with `aria-rowcount`, `aria-colcount` and `aria-multiselectable`. See below. |
| Side panel | `.lq-sheet-sidepanel` | `complementary`, labelled by its heading, with a close button. Used by comments, notes and the pivot field list. |
| Sheet tabs | `.lq-sheet-tabs` | `tablist` named "Sheets". Each tab has `aria-selected` and `aria-controls` pointing at the grid. |
| Status bar | `.lq-sheet-statusbar` | The mode, the selection statistics as a `group`, and the zoom controls as a `group` with labelled buttons and a slider. |
| Live regions | `.lq-sheet-live`, `.lq-sheet-alert` | A polite `status` region and an assertive `alert` region, both visually hidden. |

### The grid

The grid is a single tab stop. Focus stays on the grid element, and `aria-activedescendant` points at the active cell.

- The grid is virtualised: only the visible rows and cells are in the DOM. `aria-rowcount` and `aria-colcount` give the full size, each row has `aria-rowindex` and each cell and header has `aria-colindex`, so a screen reader reports the real position.
- Column headers are `columnheader` elements and row headers are `rowheader` elements. The select-all corner is a `columnheader` named "Select all"; it holds that name as visually hidden text.
- Each cell is a `gridcell` with `aria-selected` and a label that starts with the address and the displayed text, such as "B4, 1,539.29". The label adds "formula" and the formula, "link to" and the target, "has note" and "sparkline" where they apply. An empty cell reads "blank". A merged cell has `aria-colspan`.
- Rich text, rotated text and fills do not change the label: it is always the plain displayed text.
- Selection outlines, the fill handle and reference highlights are drawn in overlay layers with `aria-hidden="true"`.
- Filter buttons in header cells are buttons named after the column, with "(filtered)" and "sorted ascending" or "sorted descending" when that applies.
- The freeze-pane bars are focusable `separator` elements with `aria-valuenow`; the buttons that unhide rows and columns are named "Unhide rows 4 to 6" and similar.

### Dialogs, menus and popups

- Dialogs such as Format Cells are `dialog` elements with `aria-modal="true"`, labelled by their title. They trap `Tab`, close on `Escape` and return focus to where it was. Each dialog has a polite `status` line for validation messages.
- The tabs inside Format Cells are a `tablist` with `tabpanel`s. The line-style picker on the Border tab is a `radiogroup` of 13 named `radio` buttons, moved through with the arrow keys.
- Context menus and ribbon menus are `menu` elements with `menuitem`s. Arrow keys move, `Enter` chooses and `Escape` closes and returns focus to the opener.
- The filter dropdown is a non-modal `dialog` named after its column. Its sort buttons use `aria-pressed` for the current sort.
- The validation list is a `listbox` with `option`s.

## Moving between regions

`F6` and `Shift` + `F6` move focus between the grid, the side panel when it is open, the sheet tabs, the status bar, the ribbon and the formula bar. Inside the ribbon, the sheet tabs and the outline gutter, one element is in the tab order at a time and the arrow keys move between the others.

## Announcements

The polite region announces the result of an action: a selected range with its size ("B2:D6 selected, 5 rows, 3 columns") once a selection of more than one cell settles, unless the action that made it announces something itself, a cell that was set or cleared, undo and redo with the name of the action, the zoom level, the active sheet, how many rows a filter shows, a sort, an applied cell style, and hidden or shown rows and columns. The assertive region announces errors: a failed validation, a change blocked by protection or read-only mode, a circular reference, and import or export failures. The same errors fire the [`error` event](https://docs.birchui.com/spreadsheet/api.md#error-codes).

While a calculation runs in a worker, the root element has `aria-busy="true"`.

## Themes

`theme` is `'light'`, `'dark'` or `'auto'`, which follows `prefers-color-scheme`. Without it the component inherits the theme of the page.

`sheetTheme` decides what the cells do in a dark theme:

| `sheetTheme` | In a dark theme |
| --- | --- |
| `'light'` (the default) | The sheet area stays light: cells, gridlines, the selection, the cell editor and everything drawn over the cells, such as charts, shapes, pictures, embedded files, slicers, notes, filter buttons and presence labels. The toolbar, the formula bar, the row and column headers with the outline gutter, the sheet tabs, the status bar, menus, dialogs and side panels are dark. This is how Excel draws a workbook in its dark theme. |
| `'match'` | Everything is dark, cells included. |

```ts
import { createSpreadsheet } from '@birchui/spreadsheet';

declare const container: HTMLElement;

const sheet = createSpreadsheet(container, { theme: 'auto' });
sheet.update({ sheetTheme: 'match' });
```

The default is `'light'` because workbooks carry their own colours. A title in navy, a red negative number or a dark border was chosen for a white sheet, and nothing in the file says what it should become on a dark one, so on dark cells such text can be close to unreadable. With `'match'`, text without a colour of its own, fills and gridlines follow the dark theme, and colours the workbook sets are drawn as the file says.

- `sheetTheme` has no effect in a light theme, and it can be changed at any time with `update()`.
- It applies when the dark theme comes from `theme: 'dark'`, from `theme: 'auto'` on a dark system, or from a `lq-theme-dark` class on an ancestor. A page that makes the component dark only by setting the `--lq-*` custom properties itself gets dark cells; set `theme: 'dark'` as well to keep them light.
- The light sheet uses the bundled light palette. To change it, set the `--lq-sheet-*` custom properties on `.lq-sheet--light-sheet .lq-sheet-layer` with a selector at least as specific as the built-in one.
- Print output and PDF export are always dark text on white, whatever the theme.
- In forced-colours mode the system colours take over in both settings.

## Colour, contrast and motion

- Captions and labels drawn over the sheet take their colours from the theme tokens of the area they are in: the names of embedded files, slicer captions and items, the default text of shapes and charts, notes, presence labels and the buttons on a selected object. They meet 4.5:1 in the light theme, in the dark theme with a light sheet and in the dark theme with a matching sheet.
- A cell with a light fill and no text colour of its own gets dark text in both themes, so it stays readable in dark mode. Colours that a workbook sets itself, such as a text colour on a fill, a gradient or a pattern under text, are drawn as the file says and can have low contrast.
- A picture that a file stores for an embedded object is drawn as it is. Because such a picture usually holds the file name in dark text, it gets a white plate behind it when the cells are dark.
- In forced-colours mode, selection, focus and note markers use system colours.
- With `prefers-reduced-motion: reduce`, animations such as the moving copy outline are turned off.
- Focus is always visible: the active cell has a 2-pixel outline, and buttons, tabs and inputs have a focus ring.

## Automated checks

The end-to-end tests run axe-core in Chrome with the WCAG 2.0, 2.1 and 2.2 A and AA rule sets and the best-practice rules:

- every ribbon tab, the More commands popup, the colour palette, an open menu and an open dialog must have no serious or critical violation;
- the single-line ribbon with its More commands popup, the display menu, the collapsed ribbon closed and open, and the open command search list must have no serious or critical violation, in the light and in the dark theme;
- a sheet with a pivot table, slicers and a chart is scanned as a whole;
- a sheet with rich text, rotated text, gradient and pattern fills and every border style, and the Format Cells dialog on its Border tab, must have no violation of any impact. This scan leaves out `color-contrast`, because the sample cells carry their own colours.

- `e2e/tests/spreadsheet-a11y.spec.ts` scans the grid with a selection, all six ribbon tabs, the cell editor, function autocomplete, the context menu, twenty dialogs and the three side panels, in the light and in the dark theme, and fails on a violation of any impact, `color-contrast` included. The one exception is five samples in the cell styles gallery (Calculation, Check Cell, Explanatory Text, Linked Cell, Warning Text): they show Excel's own built-in style colours, which do not meet 4.5:1, and changing them would misrepresent the style.

- The same spec loads a sheet that has every kind of overlay at once (embedded files with an icon and with a stored picture, a slicer, shapes, a text box, a chart with title, legend, axis titles and data labels, a visible note, a comment marker, presence labels, a validation dropdown, filter buttons, a sparkline, icons and a data bar) and scans it in four combinations: light and dark theme, each with `sheetTheme: 'light'` and `'match'`. It scans the sheet, a selected embedded file with its buttons, the embedded-file menu, the properties dialog, the Insert object dialog, a selected chart and a focused slicer, `color-contrast` included, and it measures the contrast of text that axe cannot judge, such as text inside chart graphics.

That scan of the overlay sheet led to two fixes in slicers: the clear button was inside the header, which is itself a button, and it was 20 pixels wide where 24 are required. It now sits beside the header and is 24 by 24 pixels.

The first scan found and led to fixes for three things: the cell editor carried an `aria-expanded` attribute that its role does not allow, shortcut hints in menus had 4.39:1 contrast on the highlighted item in the dark theme, and a scrolling context menu had no focusable item in the tab order.

Three page-level rules are turned off in these scans because they concern the host page and not the component: `landmark-one-main`, `page-has-heading-one` and `region`.

## Known minor findings

- A right-to-left sheet mirrors the whole component while it is active, including the ribbon, even when the page is left-to-right.
- Accounting underlines are drawn under the text, not across the cell, and vertical justify and distributed alignment are drawn as the plain vertical alignment. Neither affects what a screen reader reads.
- Rich text runs are not exposed one by one: the cell label is the joined text, without the formatting of each run.
- Contrast inside cells depends on the colours of the workbook, as described above.
- When the ribbon is narrower than its commands, the commands that do not fit move to the **More commands** popup. A group that fits partly is split, and its second part is named "(continued)" there.
