# Calendar

> A month of days to pick from. It's the grid from the APG's date picker, with all the keyboard support you'd expect.

`<slab-calendar>` · Data · built on `<table role=grid>`

## Install

```sh
pnpm add slab-elements lit
```

## Usage

```html
<script type="module">
  import 'slab-elements/calendar.js';
</script>

<slab-calendar value="2026-03-14"></slab-calendar>
```

## Example

Here's a styled version, `<slab-styled-calendar>`. It's not in the package — it's an example of what you can build with the headless element. Copy it and make it yours at the bottom of this page.

```html
<slab-styled-calendar min="2026-10-12" max="2026-12-18" value="2026-10-16"></slab-styled-calendar>
```

Swap `slab-styled-` for `slab-` in that markup and you've got the same example as it ships: the headless elements with no CSS at all. That's your starting point.

## Keyboard

| Key                | What it does                                           |
| ------------------ | ------------------------------------------------------ |
| Left, Right        | Moves a day, into the month before or after at an end. |
| Up, Down           | Moves a week.                                          |
| Home, End          | Moves to the start or end of the week.                 |
| Page Up, Page Down | Moves back or forward a month. With Shift, a year.     |
| Enter, Space       | Selects the day and dispatches "change".               |

## Behavior

A calendar with no visual styles: a month of days in a `[role=grid]`, to pick one from, as in the APG's date picker.

Dates are ISO "yyyy-mm-dd" strings, as on a Date Field, and are worked out in UTC, so a change to or from daylight saving time never skips or repeats a day.

One day is tabbable: the selected one, or else today, kept between `min` and `max`. The arrow keys move a day or a week, into the month before or after when they reach an end. Home and End go to the start and end of the week, Page Up and Page Down go back and forward a month, and with Shift a year. Enter, Space and a click select the day and dispatch "change". Days before `min` or after `max` can't be reached or selected.

The month's name, the weekdays' and each day's full date are the browser's own, from `Intl.DateTimeFormat`, in the language of the nearest `lang` attribute. Slab ships no text, so the only words that aren't the browser's are the previous and next buttons', which `previous-label` and `next-label` set.

Style it through its parts. A day is also `::part(selected)`, `::part(today)` and `::part(disabled)` when it's those.

## API

### `<slab-calendar>`

#### Attributes

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `max` | `string` |  | The latest date that can be selected, as "yyyy-mm-dd". |
| `min` | `string` |  | The earliest date that can be selected, as "yyyy-mm-dd". |
| `next-label` | `string` |  | The next month button's text and so its accessible name. "Next month" when unset. |
| `previous-label` | `string` |  | The previous month button's text and so its accessible name. "Previous month" when unset. |
| `value` | `string` | `''` | The selected date, as "yyyy-mm-dd", or an empty string. Like a native input, the attribute is the default value and the property is the current one. |
| `week-start` | `number` | `0` | The day weeks start on, from 0 for Sunday to 6 for Saturday. |
| `version` | `string` |  |  |

#### Methods

| Signature | Description |
| --- | --- |
| `focus(options?: FocusOptions): void` |  |

#### Events

| Name | Type | Description |
| --- | --- | --- |
| `change` | `Event` |  |

#### CSS parts

| Name | Description |
| --- | --- |
| `day` | A day of the month |
| `disabled` | A day before the earliest date or after the latest |
| `empty` | A cell before the first day of the month or after the last |
| `grid` | The days |
| `header` | The month and the buttons that change it |
| `heading` | The month and year |
| `next` | The button to the next month |
| `previous` | The button to the previous month |
| `selected` | The selected day |
| `today` | Today |
| `weekday` | A day of the week, above the days |

## Copy and own it

`<slab-styled-calendar>` isn't in the package. It's an example — copy it into your project and change whatever you want. Under the hood, it's just the headless element, subclassed, with `styles` set. That's it. Keyboard, focus, ARIA, forms, and events all come from the headless element, and the styles only reach in through `::part()` and `:state()`, same as yours would.

To use it, copy these files, swap the tag in `@customElement()` for one of your own, and import it. `tokens.styles.ts` holds the colors and fonts every styled example shares, so you only need it once.

### TypeScript

These files are TypeScript, and `@customElement()` is one of Lit's decorators (TypeScript's experimental flavor). You'll need two options in your tsconfig, per [Lit's docs](https://lit.dev/docs/components/decorators/#decorators-typescript): `experimentalDecorators` to compile them, and `useDefineForClassFields` turned off so class fields don't stomp on the accessors Lit sets up.

```json
{
  "compilerOptions": {
    "experimentalDecorators": true,
    "useDefineForClassFields": false
  }
}
```

Using plain JavaScript? Drop the types and register the class with `customElements.define()` instead of the decorator.

### calendar.ts

```ts
import { customElement } from 'lit/decorators.js';
import Calendar from 'slab-elements/calendar.js';
import styles from './calendar.styles.js';
import tokens from './tokens.styles.js';

declare global {
  interface HTMLElementTagNameMap {
    'slab-styled-calendar': StyledCalendar;
  }
}

/**
 * Calendar, styled: a raised slab of days, with today ringed in ember and
 * the selected day in ink.
 */
@customElement('slab-styled-calendar')
export default class StyledCalendar extends Calendar {
  static override styles = [tokens, styles];
}
```

### calendar.styles.ts

```ts
import { css } from 'lit';

/*
  The previous and next buttons' text is their accessible name, so it stays,
  at no size, and a chevron is drawn in its place.
*/
export default css`
  :host {
    background-color: var(--paper);
    border: var(--border);
    box-shadow: var(--shadow);
    color: var(--ink);
    display: inline-block;
    font-family: var(--font-body);
    padding: 1rem;
  }

  [part~='header'] {
    align-items: center;
    display: flex;
    gap: 1rem;
    justify-content: space-between;
    margin-block-end: 0.75rem;
  }

  [part~='heading'] {
    font-family: var(--font-display);
    font-size: 1.125rem;
    font-weight: 600;
  }

  [part~='previous'],
  [part~='next'] {
    align-items: center;
    background-color: var(--paper);
    block-size: 2.25rem;
    border: var(--border);
    color: var(--ink);
    cursor: pointer;
    display: flex;
    font-size: 0;
    inline-size: 2.25rem;
    justify-content: center;
    padding: 0;
    transition: background-color 100ms;

    &::before {
      block-size: 0.5rem;
      border-block-end: 2px solid currentcolor;
      border-inline-start: 2px solid currentcolor;
      content: '';
      inline-size: 0.5rem;
      rotate: 45deg;
      translate: 0.125rem 0;
    }

    &:hover {
      background-color: var(--chalk);
    }

    &:focus-visible {
      outline: 3px solid var(--ember);
      outline-offset: 2px;
    }
  }

  [part~='next']::before {
    rotate: -135deg;
    translate: -0.125rem 0;
  }

  /*
    Separate rather than collapsed, so each day draws its own border whole.
    Collapsed, a day's border merges with its neighbors' and today's ring
    breaks up.
  */
  [part~='grid'] {
    border-collapse: separate;
    border-spacing: 0;
  }

  [part~='weekday'] {
    color: var(--ash);
    font-family: var(--font-mono);
    font-size: 0.75rem;
    font-weight: 400;
    padding-block-end: 0.375rem;
    text-transform: uppercase;
  }

  [part~='day'] {
    block-size: 2.5rem;
    border: 2px solid transparent;
    box-sizing: border-box;
    cursor: pointer;
    inline-size: 2.5rem;
    text-align: center;
    transition:
      background-color 100ms,
      color 100ms;

    &:hover {
      background-color: var(--chalk);
    }

    &:focus-visible {
      outline: 3px solid var(--ember);
      outline-offset: -3px;
    }
  }

  [part~='today'] {
    border-color: var(--ember);
  }

  /*
    The ring is inside the day, so on the selected one it's on ink, where
    ember falls short of 3:1 in the dark. The text's color always has the
    contrast.
  */
  [part~='selected'] {
    background-color: var(--ink);
    color: var(--paper);
    font-weight: 600;

    &:hover {
      background-color: var(--ink);
    }

    &:focus-visible {
      outline-color: currentcolor;
    }
  }

  [part~='disabled'] {
    color: var(--ash);
    cursor: not-allowed;
    text-decoration: line-through;

    &:hover {
      background-color: transparent;
    }
  }

  @media (prefers-reduced-motion: reduce) {
    [part~='previous'],
    [part~='next'],
    [part~='day'] {
      transition: none;
    }
  }
`;
```

### tokens.styles.ts

```ts
import { css } from 'lit';

/*
  The styled components' palette and type, taken from the logo: ink for the
  courses, ember for the base, paper behind them.

  Each is a short alias for a "--slab-styled-*" property, with the default as
  its fallback. Retheming is "--slab-styled-ember" and the rest on ":root",
  which every shadow root inherits. Were the defaults declared under those
  names here instead, each host's own declaration would shadow the page's.

  Each default is "light-dark()", so the components follow the page's
  "color-scheme": "dark" for dark, "light" for light, and "light dark" to
  follow the operating system. Ink and paper trade places in the dark, so
  ink is always the text and paper always what's behind it. Contrast is given
  as light, then dark.
*/
export default css`
  :host {
    /* Secondary text. 6.8:1 and 6.9:1 on paper. */
    --ash: var(--slab-styled-ash, light-dark(#5e5a55, #a8a19a));
    --border: 2px solid var(--ink);

    /* Behind disabled and read-only controls, and hovered rows. */
    --chalk: var(--slab-styled-chalk, light-dark(#f4f1ec, #2b2926));

    /*
      The logo's orange, brighter in the dark. 4.25:1 and 5.2:1 on paper is
      enough for borders, fills and large text, but not always for small text,
      which takes "--ember-deep".
    */
    --ember: var(--slab-styled-ember, light-dark(#d9492a, #ec5f3b));

    /*
      Orange for small text on paper, and for paper text on it. 5.7:1 and
      7.4:1.
    */
    --ember-deep: var(--slab-styled-ember-deep, light-dark(#b3381a, #ff8566));
    --font-body: var(
      --slab-styled-font-body,
      'IBM Plex Sans',
      system-ui,
      sans-serif
    );
    --font-display: var(
      --slab-styled-font-display,
      'Zilla Slab',
      rockwell,
      'Roboto Slab',
      serif
    );
    --font-mono: var(
      --slab-styled-font-mono,
      'IBM Plex Mono',
      ui-monospace,
      monospace
    );
    --ink: var(--slab-styled-ink, light-dark(#1c1c1c, #f4f1ec));
    --paper: var(--slab-styled-paper, light-dark(#fff, #1a1918));

    /* The hard, offset shadow a raised surface casts. */
    --shadow: 4px 4px 0 var(--ink);
  }
`;
```
