- Examples
- Booking calendar
- Team calendar
- Room booking
- Shift scheduler
- Conference agenda
- Calendar in an app layout
- Optimistic server sync
- Lazy-load by date range
- View and date in the URL
- Custom persistence adapter
- ICS import and export
- Custom event rendering
- Multiple time zones
- Linked month and day views
- Recurring events (RRULE)
- Custom ⌘K commands
- Natural-language event entry
Linked month and day views
A month overview beside a day view, both controlled by the same date — click a day on the left, it opens on the right.
Two <Kloq> instances, one useState. Click a date number in the month on
the left and the day view on the right jumps there; step the day view with
‹ › and the month follows. They share the events too, so a block moved on the
right moves on the left.
live·Linked month and day views
"use client";
import { type CalEvent, Kloq, useIsClient } from "kloq";
import { useState } from "react";
import { Bar, Nav } from "../parts";
import { monthEvents } from "./data";
export default function LinkedCalendarsExample() {
const [date, setDate] = useState(() => new Date());
const [events, setEvents] = useState<CalEvent[]>(() => monthEvents(new Date()));
const isClient = useIsClient();
const shared = {
date,
onDateChange: setDate,
events,
onEventsChange: setEvents,
responsive: false,
persistence: false,
designMode: false,
storage: false,
} as const;
return (
<div className="@container flex flex-col">
<Bar className="justify-between">
<span>shared date</span>
<span className="text-foreground tabular-nums">
{isClient
? date.toLocaleDateString("en-US", {
weekday: "short",
month: "short",
day: "numeric",
year: "numeric",
})
: ""}
</span>
<span className="tabular-nums">{events.length} events</span>
</Bar>
{/* `useKloq()` inside `Nav` reads whichever `<Kloq>` it is rendered in */}
<div className="grid @3xl:grid-cols-2">
<div className="h-104 min-w-0 border-b @3xl:border-r @3xl:border-b-0">
<Kloq view="month" {...shared}>
<Kloq.Toolbar>
<Nav />
</Kloq.Toolbar>
</Kloq>
</div>
<div className="h-104 min-w-0">
<Kloq view="day" {...shared}>
<Kloq.Toolbar>
<Nav />
</Kloq.Toolbar>
</Kloq>
</div>
</div>
</div>
);
}
import { type CalEvent, type EventColor, instantAt, resolveView } from "kloq";
interface Plan {
title: string;
/** days from today, minutes from midnight, length in minutes */
day: number;
at: number;
minutes: number;
color: EventColor;
}
const h = (hours: number, minutes = 0) => hours * 60 + minutes;
const PLAN: Plan[] = [
{ title: "Board prep", day: -2, at: h(11), minutes: 120, color: "violet" },
{ title: "Standup", day: -1, at: h(9), minutes: 15, color: "blue" },
{ title: "Standup", day: 0, at: h(9), minutes: 15, color: "blue" },
{ title: "Sprint planning", day: 0, at: h(10), minutes: 60, color: "blue" },
{ title: "Lunch with Sam", day: 0, at: h(12, 30), minutes: 60, color: "emerald" },
{ title: "Design review", day: 0, at: h(15), minutes: 45, color: "violet" },
{ title: "Standup", day: 1, at: h(9), minutes: 15, color: "blue" },
{ title: "1:1 with Priya", day: 1, at: h(9, 30), minutes: 30, color: "blue" },
{ title: "Dentist", day: 2, at: h(8), minutes: 60, color: "rose" },
{ title: "Roadmap", day: 3, at: h(14), minutes: 90, color: "amber" },
{ title: "Offsite", day: 7, at: h(9), minutes: 480, color: "emerald" },
{ title: "Release", day: 9, at: h(16), minutes: 60, color: "amber" },
{ title: "Retro", day: 10, at: h(15), minutes: 45, color: "violet" },
];
/** A month of events around today, as plain timed `CalEvent`s. */
export function monthEvents(now: Date): CalEvent[] {
const view = resolveView("month", 1, now);
return PLAN.map((p, i) => ({
id: `plan-${i}`,
title: p.title,
color: p.color,
start: instantAt(view, view.todayIndex + p.day, p.at),
end: instantAt(view, view.todayIndex + p.day, p.at + p.minutes),
}));
}
"use client";
import { Button } from "@coss/ui/components/button";
import { cn } from "@coss/ui/lib/utils";
import { formatViewTitle, useIsClient, useKloq } from "kloq";
import { ChevronLeft, ChevronRight } from "lucide-react";
import type { ComponentProps } from "react";
/** The strip an example hangs its controls and readouts on; same box as `Kloq.Toolbar`. */
export function Bar({
edge = "bottom",
className,
...props
}: ComponentProps<"div"> & { edge?: "top" | "bottom" }) {
return (
<div
{...props}
className={cn(
"flex min-h-11 shrink-0 flex-wrap items-center gap-x-4 gap-y-2 px-4 py-2.5 font-mono text-xs text-muted-foreground",
edge === "bottom" ? "border-b" : "border-t",
className,
)}
/>
);
}
export function Dot({ live, className, ...props }: ComponentProps<"span"> & { live?: boolean }) {
return (
<span
aria-hidden
{...props}
className={cn(
"size-1.5 shrink-0 rounded-full",
live ? "bg-brand motion-safe:animate-pulse" : "bg-border",
className,
)}
/>
);
}
/** Previous / next / today for a calendar whose view is fixed. Must render inside `<Kloq>`. */
export function Nav({ locale }: { locale?: string }) {
const k = useKloq();
const isClient = useIsClient();
return (
<div className="flex min-w-0 items-center gap-1">
<Button variant="ghost" size="icon-xs" aria-label="Previous" onClick={() => k.step(-1)}>
<ChevronLeft />
</Button>
<Button variant="ghost" size="icon-xs" aria-label="Next" onClick={() => k.step(1)}>
<ChevronRight />
</Button>
<Button
variant="ghost"
size="xs"
className={cn(k.atToday && "text-muted-foreground")}
onClick={k.goToday}
>
Today
</Button>
<span className="ms-1 truncate text-sm font-medium text-foreground tabular-nums sm:text-xs">
{isClient ? formatViewTitle(k.view, locale, { short: k.narrow }) : null}
</span>
</div>
);
}
usesdateonDateChangeeventsonEventsChangeuseKloq
How it works
Both instances get the same date and the same onDateChange, so either one
navigating updates the other — the standard
controlled pattern, applied twice.
view is controlled as well, pinned to "month" on the left and "day" on
the right.
Clicking a date number in a month view calls the calendar's jumpToDay,
which reports onDateChange(date) and then requests onViewChange("day").
The date lands in the shared state; the view request goes nowhere because
view is controlled and no onViewChange is wired — so the month stays a
month and the day view, reading the same date, opens that day.
events / onEventsChange are shared the same way. Controlled events switch
the internal store off, so a drag on the right commits through
onEventsChange into the one array both instances render from.
responsive={false} on both. The views are pinned, so there is nothing for
the responsive collapse to request;
turning it off keeps onViewChange quiet. The month still draws its compact
dot form below 640px — that is a density choice tied to the box width, not a
view change, and it is what makes a 320px month legible.
The strip above each calendar is one component, Nav from parts.tsx,
rendered twice. It calls useKloq() and gets the state of whichever instance it sits inside — step,
goToday, atToday and the resolved view for formatViewTitle. See
toolbar & chrome.
Take it further
Add onViewChange to the right-hand instance and let it switch between
"day" and 3; the month still only ever reports the date. Or mirror the
shared date into the URL, as in View and date in the URL.
Last updated
On This Page