# Weekly hours

> A recurring Mon to Sun operating and availability hours editor with split shifts, aligned 24-hour coverage bars, overlap guards and Copy to Mon-Fri.

Scheduling apps, support desks and storefronts all need recurring weekly hours, yet single start/end fields break the moment someone adds a lunch break or accidentally creates overlapping shifts. WeeklyHours structures Monday through Sunday into one hairline editor: toggle any day on or off, split a day into multiple intervals with +, copy any day schedule across Monday through Friday in one click, and scan an aligned 00:00 to 24:00 proportional coverage bar beside every row. Invalid ranges and overlapping intervals flag immediately in crimson both inline and on the coverage bar, while total weekly open hours stay live in the header.

## Install

```bash
npx shadcn@latest add https://hairlineui.com/r/weekly-hours.json
```

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

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

## Usage

```tsx
import { WeeklyHours, DEFAULT_WEEKLY_SCHEDULE } from "@/components/weekly-hours";

<WeeklyHours
  value={schedule}
  onChange={setSchedule}
  timezone="Europe/London"
/>
```

## Availability settings paired with validation guard

Run validateDaySlots() across the schedule to disable Save whenever a day has overlapping or invalid ranges.

```tsx
"use client";
import { useState } from "react";
import { WeeklyHours, DEFAULT_WEEKLY_SCHEDULE, validateDaySlots, type WeeklySchedule, type WeekdayKey } from "@/components/weekly-hours";

export function AvailabilityForm({ onSave }: { onSave: (s: WeeklySchedule) => Promise<void> }) {
  const [schedule, setSchedule] = useState<WeeklySchedule>(DEFAULT_WEEKLY_SCHEDULE);
  const hasErrors = (Object.keys(schedule) as WeekdayKey[]).some((k) => {
    const v = validateDaySlots(schedule[k]);
    return v.hasInvalid || v.hasOverlap;
  });

  return (
    <div className="space-y-3">
      <WeeklyHours value={schedule} onChange={setSchedule} timezone="Europe/London" />
      <button
        type="button"
        disabled={hasErrors}
        onClick={() => void onSave(schedule)}
        className="h-9 rounded-xl bg-primary px-4 text-xs font-medium text-primary-foreground disabled:opacity-40"
      >
        Save availability
      </button>
    </div>
  );
}
```

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `value / defaultValue / onChange` | `WeeklySchedule / WeeklySchedule / (s) => void` | – | Controlled or uncontrolled Record<WeekdayKey, TimeSlot[]>. |
| `stepMinutes / hour12 / weekStartsOnSunday` | `15 \| 30 \| 60 / boolean / boolean` | `30 / false / false` | Time dropdown step increment, 12-hour AM/PM display and week start day. |
| `timezone / showTimeline / showPresets` | `string / boolean / boolean` | `— / true / true` | Optional header timezone pill, aligned 24-hour coverage bars and quick preset buttons. |
| `title / sound` | `string / boolean` | `"Working hours" / false` | Accessible region label and opt-in Web Audio feedback cues. |

## States

- **standard week**: Monday through Friday open 09:00 to 17:00 with Saturday and Sunday toggled Unavailable.
- **split shifts**: Days with multiple intervals stack cleanly and visually part their 24-hour hairline bar around breaks.
- **conflict detected**: Overlapping or inverted intervals highlight crimson inline with an alert label and red coverage bar.
- **copied to weekdays**: Pressing Copy on any row applies its intervals across Mon to Fri and briefly flashes an emerald Check badge.

## What it handles

A recurring hours form should make editing seven days effortless while making gaps, split breaks and accidental overlaps impossible to miss.

- **Aligned 24-hour coverage hairlines**: Every row draws proportional segments on a shared 00:00 to 24:00 bar with 06/12/18 ticks so lunch breaks and weekend gaps read at a glance.
- **Split shifts with smart next-slot defaults**: Clicking + inserts a fresh interval starting 1 hour after the previous shift ends, with one-click Trash removal per split.
- **One-click Copy to Mon-Fri**: Propagate any day intervals across all weekdays in one click with an inline emerald confirmation.
- **Live overlap and order validation**: Detects inverted ranges (end <= start) and overlapping intervals on every day, tinting both the select border and the 24-hour bar crimson.
- **Quick presets and live weekly total**: Switch between Mon-Fri 9-5 and 24/7 in one tap while the header continuously tallies valid weekly hours and open days.
- **Exported schedule helpers**: Exports validateDaySlots(), calculateWeeklyMinutes(), formatWeeklyDuration() and DEFAULT_WEEKLY_SCHEDULE.

## Replaces

7-row custom hours form, interval overlap validator, copy-to-weekdays button, weekly coverage visualiser

## Where it fits

- **Booking and calendar availability**: Pair with Timezone picker so hosts configure Mon to Sun availability and lunch breaks without leaving settings.
- **Support SLA and on-call policies**: Define live support operating hours with instant 24/7 or Mon-Fri 9-5 presets and total weekly coverage calculation.
- **Local business and storefront hours**: Support split daytime/evening shifts, 12-hour or 24-hour formatting and Sunday-first or Monday-first week ordering.
- **Team shift and dispatch schedules**: Catch overlapping intervals and inverted start/end times client-side via exported validateDaySlots().

## Notes

- Stores canonical 24-hour "HH:MM" strings ("00:00" through "24:00") regardless of whether hour12 display is enabled.
- Time selectors use PillSelect (a theme-aware portaled listbox with keyboard search and arrows) instead of native browser <select> menus.
- Under prefers-reduced-motion: reduce, day toggle thumbs switch immediately without sliding animation.

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