import { Meta } from '@storybook/addon-docs/blocks';

<Meta title="Forms/Calendar/Accessibility" />

# Accessibility

Calendar is a fully keyboard-accessible date picker grid. All dates — including disabled ones — are reachable via keyboard so that screen reader users can discover which dates are unavailable and why.

## Keyboard Support

<table>
  <thead>
    <tr>
      <th>Key</th>
      <th>Action</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>
        <code>ArrowLeft</code>
      </td>
      <td>Move focus to previous day / month / year</td>
    </tr>
    <tr>
      <td>
        <code>ArrowRight</code>
      </td>
      <td>Move focus to next day / month / year</td>
    </tr>
    <tr>
      <td>
        <code>ArrowUp</code>
      </td>
      <td>Move focus up by one week (7 days) / 4 months / 4 years</td>
    </tr>
    <tr>
      <td>
        <code>ArrowDown</code>
      </td>
      <td>Move focus down by one week (7 days) / 4 months / 4 years</td>
    </tr>
    <tr>
      <td>
        <code>Enter</code> / <code>Space</code>
      </td>
      <td>Select the focused date (if enabled)</td>
    </tr>
  </tbody>
</table>

<br /> Navigation **does not skip** disabled dates. Users can arrow through every date in the grid
regardless of its enabled state.

## Screen Reader Announcements

Each date cell is a `<button>` with an `aria-label` in a human-readable long format (e.g. "6 July 2024") constructed via `Intl.DateTimeFormat` for full locale support.

### Active (Selected) Date

The currently selected date includes "selected day" in its aria-label:

> "6 July 2024, selected day"

### Disabled Dates (via `disabledRules`)

Dates disabled via `disabledRules` remain focusable and keyboard-navigable. They are announced with additional context:

- `aria-disabled="true"` signals the date is not selectable
- The aria-label appends "not available" (e.g. "6 July 2024, not available")
- `aria-describedby` links to the InlinePrompt element containing the reason text

This means a screen reader will announce something like:

> "6 July 2024, not available — We are closed on weekends. button, dimmed."

### Disabled + Selected

If the focused date is both active and disabled:

> "6 July 2024, selected day, not available"

### Out-of-Range Dates (via `min`/`max`)

Dates outside `min`/`max` boundaries use `aria-disabled="true"` (not the native HTML `disabled` attribute). They are rendered as interactive buttons in the grid: they can be clicked and the `aria-label` appends "not available". Arrow key navigation clamps at the boundary, so keyboard users cannot navigate into out-of-range cells — but they remain visible and clickable for pointer users.

This distinction is intentional: `min`/`max` defines a hard boundary (the calendar simply doesn't extend beyond it), while `disabledRules` marks dates that exist but are unavailable (users should be able to discover them and understand why).

## InlinePrompt Behaviour

An `InlinePrompt` renders below the day grid when the current month contains disabled dates. It displays the `disabledRules.reason` text.

### When It Appears

The prompt is visible when **all** of the following are true:

1. The Calendar is in day view
2. `disabledRules.reason` is provided
3. The currently displayed month contains at least one disabled date

### How It's Announced

The prompt is linked to disabled date cells via `aria-describedby`. This means the reason text is read as a description when a disabled cell receives focus — screen readers announce it after the label and role.

The prompt element has an `id` that matches the `aria-describedby` on each disabled cell. It is **not** a live region (`role="alert"` or `role="status"`) because:

- It does not change dynamically while focus is on a disabled cell
- Announcing it on every arrow key press would be disruptive
- The `aria-describedby` relationship already ensures it's read on focus

### Sentiment

The prompt's visual sentiment changes based on the currently focused date:

- **Negative** (red) — when the focused date is disabled
- **Neutral** — when the focused date is enabled

This is a visual-only change and does not affect screen reader output.
