# Cron input

> A schedule field people can read: cron or plain English in, the meaning in words and the next runs out, with clock changes flagged.

Schedules for jobs, reports and backups usually come down to a bare text box for "0 9 * * 1-5" and a link to a cron cheat sheet. Here each part of the expression has its name under it, and the part your caret is in lights up with a hairline that glides from part to part. The line below says it in words ("At 09:00, Monday to Friday") and the next five runs are listed in the schedule's time zone; as you edit, their hours and minutes roll to the new times. You can also type "every weekday at 9am": it's read as you type and Enter turns the words into cron. Presets have a highlight that slides to whichever one matches, and picking one morphs the expression into it, with the characters both share staying put. A mistake turns its part red and says what's wrong while the last good runs stay, dimmed, so nothing jumps. A run that a clock change skips or repeats says so, which is the bug nobody notices until March.

## Install

```bash
npx shadcn@latest add @hairline/cron-input
```

Needs React 19, Tailwind CSS v4 and shadcn/ui theme variables (any style, light and dark). Icons from lucide-react.

- npm: none
- Hairline UI components it needs (the CLI installs them too): `text-morph`, `number-roll`
- Docs and live preview: https://hairlineui.com/ui/cron-input
- Registry item (source): https://hairlineui.com/r/cron-input.json

## Usage

```tsx
import { CronInput } from "@/components/cron-input";

<CronInput
  defaultValue="0 9 * * 1-5"
  onValueChange={(expression) => setSchedule(expression)}
/>
```

## Schedules your users choose

Each workspace picks its own schedule and zone, so they can’t be fixed triggers in your config. Save them through the same parser, then let one route that runs every minute ask nextRuns whether this minute is due for each.

```tsx
// app/settings/actions.ts
"use server";
import { parseCron } from "@/components/cron-parse";

export async function saveSchedule({ cron, timeZone }: { cron: string; timeZone: string }) {
  const parsed = parseCron(cron);
  if (!parsed.ok) return { error: parsed.message };
  await db.workspace.update({ where: { id: await workspaceId() }, data: { digestCron: parsed.schedule.expression, digestZone: timeZone } });
  return { ok: true };
}

// app/settings/schedule.tsx
"use client";
import { CronInput } from "@/components/cron-input";

export function DigestSchedule({ cron, timeZone }: { cron: string; timeZone: string }) {
  const schedule = { cron, timeZone };
  return (
    <CronInput
      label="Digest schedule"
      defaultValue={cron}
      defaultTimeZone={timeZone}
      timeZones={["UTC", "Europe/London", "America/New_York", "Asia/Kolkata"]}
      onValueChange={(next) => saveSchedule({ ...schedule, cron: (schedule.cron = next) })}
      onTimeZoneChange={(zone) => saveSchedule({ ...schedule, timeZone: (schedule.timeZone = zone) })}
    />
  );
}

// app/api/cron/route.ts: runs every minute (vercel.json: "crons": [{ "path": "/api/cron", "schedule": "* * * * *" }])
import { nextRuns, parseCron } from "@/components/cron-parse";

export async function GET() {
  const minute = Math.floor(Date.now() / 60_000) * 60_000;
  const workspaces = await db.workspace.findMany({ where: { digestCron: { not: null } } });
  for (const w of workspaces) {
    const parsed = parseCron(w.digestCron!);
    if (!parsed.ok) continue;
    // Due when its first run after the previous minute is this minute.
    const [next] = nextRuns(parsed.schedule, { from: new Date(minute - 1), count: 1, timeZone: w.digestZone });
    if (next?.at.getTime() === minute) await sendDigest(w.id);
  }
  return Response.json({ ok: true });
}
```

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `value / defaultValue` | `string` | `"0 9 * * 1-5"` | The schedule as cron. defaultValue may also be English ("every weekday at 9am"); the field starts with the words. |
| `onValueChange` | `(expression, schedule) => void` | – | Each valid expression (tidied to single spaces, @macros expanded) and its parsed fields. |
| `timeZone / defaultTimeZone` | `string` | `"UTC"` | The IANA zone the schedule runs in; the runs are listed in it. Many schedulers (Vercel, GitHub Actions) only run in UTC. |
| `timeZones` | `string[]` | – | Zones to choose from. With two or more, the zone is a dropdown (Pill select) showing each zone's current offset. |
| `onTimeZoneChange` | `(timeZone) => void` | – | Called when a zone is picked. |
| `runs` | `number` | `5` | How many upcoming runs to list; 0 hides them. |
| `presets` | `{ label, expression }[]` | `CRON_PRESETS` | Shortcuts above the field; [] hides them. |
| `english` | `boolean` | `true` | Read schedules typed in English. |
| `hour12` | `boolean` | `false` | Write times as 1:30pm. |
| `label` | `string` | `"Schedule"` | Accessible name of the field. |
| `name` | `string` | – | Submits the expression with a form. |

## States

- **typing**: The parts are spread with word spacing so their names (10.5px) fit under them. The name under the part your caret is in turns to the text colour and a 1px line glides to it (300ms); names follow their parts as the text changes (340ms).
- **meaning**: The sentence under the field crossfades whole when it changes: the old one lifts out through a 4px blur in 180ms while the new one rises in (380ms, 60ms later).
- **runs**: The next runs list day, time and how long until each. Hours and minutes roll in the direction they changed (700ms), dates and "in 3 h" morph letter by letter, and the list updates on each new minute.
- **english**: Text with words that aren't cron names is read as English. The part names fold away (320ms), the sentence says what it understood and which cron it makes, and Enter or leaving the field morphs the words into the expression.
- **preset**: A pill highlight slides to the preset that matches the expression (380ms). Picking one morphs the expression into the preset: characters both share glide, the rest rise in through a blur.
- **invalid**: The part at fault gets a red line and a red name, the sentence says why ("The hour goes from 0 to 23, not 24.") and the runs stay at 45% opacity until the text is valid again. Escape morphs back to the last valid expression.
- **clocks**: In a zone with daylight saving, a run at a time the clocks skip or repeat gets an amber mark and a line saying which.

## What it handles

Most schedule fields are a bare text box and a link to a cron cheat sheet, so people save schedules they can’t read and find out in March that a job never ran. This field explains itself as you type.

- **Every part is named**: Minute, hour, day, month and weekday sit under their parts, and the part you’re editing lights up.
- **It says it in words**: “At 09:00, Monday to Friday”, updated with every keystroke.
- **It shows the next runs**: The next five times it fires, in the schedule’s own time zone, before anything is saved.
- **It reads plain English**: Type “every weekday at 9am” or “last friday of the month at 17:30” and Enter turns it into cron.
- **It points at mistakes**: The wrong part turns red and says why: “The hour goes from 0 to 23, not 24.”
- **It catches clock changes**: Runs that daylight saving skips or repeats are flagged, in London, New York or any other zone.

## Replaces

a cron cheat sheet, cron-parser, cronstrue, time-zone maths, a presets dropdown

## Where it fits

- **Scheduled jobs**: Let people choose when a sync, an export or a backup runs.
- **Report emails**: Weekly digests and reports that go out at the time each customer picks.
- **Workflow triggers**: The “run on a schedule” step in automation tools and workflow builders.
- **Developer platforms**: Cron settings for functions and workers, with the next runs shown before anything is saved.

## Notes

- Five-field Unix cron: minute, hour, day, month, weekday, with names (MON, JAN), lists, ranges, steps, @daily-style shortcuts, L for the last day of the month, and FRI#2 or 5L for the nth or last weekday. 0 and 7 are Sunday. When both day and weekday are set, either one matching runs it, as in Unix cron; the sentence says so.
- English it reads: every N minutes or hours, from 9am to 5pm, weekdays, weekends, named days and ranges of them, the 1st and 15th, the last day, the second Tuesday, the last Friday, months, and times like 9, 9:30, 5pm, noon and midnight. What it can't say in cron ("9:30 and 17:00") it doesn't guess at.
- parseCron, describeCron, nextRuns and parseSchedule come from cron-parse.ts, which has no React in it, so a server or a worker can check and describe schedules the same way.
- The input has the field's label and is described by the sentence under it; errors mark it aria-invalid. The meaning is announced once typing pauses, never on each keystroke. Each run has its full text for screen readers; the rolling digits are hidden from them.
- Runs are worked out in the zone with Intl, day by day for up to six years, so a schedule that never fires (31 February) says so instead of hanging.
- Reduced motion: no glides, rolls or morphs; text and numbers change in place.

License: MIT (https://hairlineui.com/license#components)
