Calendar
A calendar component that allows users to select a date or a range of dates.
| Su | Mo | Tu | We | Th | Fr | Sa |
|---|---|---|---|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { Calendar } from '@/components/ui/calendar';
import type { DateRange } from '@/components/ui/calendar';
export default class CalendarDemo extends Component {
@tracked date: Date | undefined = new Date();
handleSelect = (date: Date | DateRange | undefined) => {
this.date = date as Date | undefined;
};
<template>
<Calendar
@captionLayout="dropdown"
@class="rounded-md border shadow-sm"
@mode="single"
@onSelect={{this.handleSelect}}
@selected={{this.date}}
/>
</template>
}Installation
Usage
import { Calendar } from '@/components/ui/calendar';<Calendar
@mode="single"
@selected={{this.date}}
@onSelect={{this.setDate}}
@class="rounded-lg border"
/>About
The
Calendar
component is built using
date-fns
for date math and formatting.
Date Picker
You can use the
<Calendar>
component to build a date picker. See the
Date Picker
page for more information.
Selected Date (With TimeZone)
The Calendar component accepts a
@timeZone
argument to ensure dates are displayed and selected in the user's local timezone.
Examples
Basic
A basic calendar component. We used
@class="rounded-lg border"
to style the calendar.
| Su | Mo | Tu | We | Th | Fr | Sa |
|---|---|---|---|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import { Calendar } from '@/components/ui/calendar';
<template><Calendar @class="rounded-lg border" @mode="single" /></template>Range Calendar
Use
@mode="range"
to enable range selection.
| Su | Mo | Tu | We | Th | Fr | Sa |
|---|---|---|---|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| Su | Mo | Tu | We | Th | Fr | Sa |
|---|---|---|---|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { addDays } from 'date-fns';
import { Calendar } from '@/components/ui/calendar';
import { Card, CardContent } from '@/components/ui/card';
import type { DateRange } from '@/components/ui/calendar';
export default class CalendarRange extends Component {
@tracked dateRange: DateRange | undefined = {
from: new Date(new Date().getFullYear(), 0, 12),
to: addDays(new Date(new Date().getFullYear(), 0, 12), 30),
};
handleSelect = (range: Date | DateRange | undefined) => {
this.dateRange = range as DateRange | undefined;
};
isDisabled = (date: Date) => {
return date > new Date() || date < new Date('1900-01-01');
};
<template>
<Card @class="mx-auto w-fit p-0">
<CardContent @class="p-0">
<Calendar
@defaultMonth={{this.dateRange.from}}
@disabled={{this.isDisabled}}
@mode="range"
@numberOfMonths={{2}}
@onSelect={{this.handleSelect}}
@selected={{this.dateRange}}
/>
</CardContent>
</Card>
</template>
}Month and Year Selector
Use
@captionLayout="dropdown"
to show month and year dropdowns.
| Su | Mo | Tu | We | Th | Fr | Sa |
|---|---|---|---|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import { Calendar } from '@/components/ui/calendar';
<template>
<Calendar
@captionLayout="dropdown"
@class="rounded-lg border"
@mode="single"
/>
</template>Presets
| Su | Mo | Tu | We | Th | Fr | Sa |
|---|---|---|---|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import { fn } from '@ember/helper';
import { on } from '@ember/modifier';
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { addDays } from 'date-fns';
import { Button } from '@/components/ui/button';
import { Calendar } from '@/components/ui/calendar';
import { Card, CardContent, CardFooter } from '@/components/ui/card';
import type { DateRange } from '@/components/ui/calendar';
const PRESETS = [
{ label: 'Today', value: 0 },
{ label: 'Tomorrow', value: 1 },
{ label: 'In 3 days', value: 3 },
{ label: 'In a week', value: 7 },
{ label: 'In 2 weeks', value: 14 },
];
export default class CalendarPresets extends Component {
@tracked date: Date | undefined = new Date(new Date().getFullYear(), 1, 12);
@tracked currentMonth: Date = new Date(
new Date().getFullYear(),
new Date().getMonth(),
1
);
handleSelect = (date: Date | DateRange | undefined) => {
this.date = date as Date | undefined;
};
handleMonthChange = (month: Date) => {
this.currentMonth = month;
};
handlePreset = (value: number) => {
const newDate = addDays(new Date(), value);
this.date = newDate;
this.currentMonth = new Date(newDate.getFullYear(), newDate.getMonth(), 1);
};
<template>
<Card @class="mx-auto w-fit max-w-[300px]">
<CardContent>
<Calendar
@class="p-0 [--cell-size:--spacing(9.5)]"
@fixedWeeks={{true}}
@mode="single"
@month={{this.currentMonth}}
@onMonthChange={{this.handleMonthChange}}
@onSelect={{this.handleSelect}}
@selected={{this.date}}
/>
</CardContent>
<CardFooter @class="flex flex-wrap gap-2 border-t">
{{#each PRESETS as |preset|}}
<Button
@class="flex-1"
@size="sm"
@variant="outline"
{{on "click" (fn this.handlePreset preset.value)}}
>
{{preset.label}}
</Button>
{{/each}}
</CardFooter>
</Card>
</template>
}Date and Time Picker
| Su | Mo | Tu | We | Th | Fr | Sa |
|---|---|---|---|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import { on } from '@ember/modifier';
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { Calendar } from '@/components/ui/calendar';
import { Card, CardContent } from '@/components/ui/card';
import { Field, FieldGroup, FieldLabel } from '@/components/ui/field';
import {
InputGroup,
InputGroupAddon,
InputGroupInput,
} from '@/components/ui/input-group';
import type { DateRange } from '@/components/ui/calendar';
import Clock2 from '~icons/lucide/clock-2';
export default class CalendarTime extends Component {
@tracked date: Date | undefined = new Date();
@tracked startTime = '10:30:00';
@tracked endTime = '12:30:00';
handleSelect = (date: Date | DateRange | undefined) => {
this.date = date as Date | undefined;
};
handleStartTimeChange = (event: Event) => {
this.startTime = (event.target as HTMLInputElement).value;
};
handleEndTimeChange = (event: Event) => {
this.endTime = (event.target as HTMLInputElement).value;
};
<template>
<Card @class="mx-auto w-fit max-w-[300px]">
<CardContent>
<Calendar
@class="p-0 [--cell-size:--spacing(9.5)]"
@fixedWeeks={{true}}
@mode="single"
@onSelect={{this.handleSelect}}
@selected={{this.date}}
/>
</CardContent>
<CardContent @class="border-t pt-6">
<FieldGroup>
<Field>
<FieldLabel>Start Time</FieldLabel>
<InputGroup>
<InputGroupInput
step="1"
type="time"
value={{this.startTime}}
{{on "change" this.handleStartTimeChange}}
/>
<InputGroupAddon @align="inline-end">
<Clock2 />
</InputGroupAddon>
</InputGroup>
</Field>
<Field>
<FieldLabel>End Time</FieldLabel>
<InputGroup>
<InputGroupInput
step="1"
type="time"
value={{this.endTime}}
{{on "change" this.handleEndTimeChange}}
/>
<InputGroupAddon @align="inline-end">
<Clock2 />
</InputGroupAddon>
</InputGroup>
</Field>
</FieldGroup>
</CardContent>
</Card>
</template>
}Booked dates
| Su | Mo | Tu | We | Th | Fr | Sa |
|---|---|---|---|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { Calendar } from '@/components/ui/calendar';
import { Card, CardContent } from '@/components/ui/card';
import type { DateRange } from '@/components/ui/calendar';
export default class CalendarBookedDates extends Component {
@tracked date: Date | undefined = new Date(new Date().getFullYear(), 1, 3);
bookedDates = Array.from(
{ length: 15 },
(_, i) => new Date(new Date().getFullYear(), 1, 12 + i)
);
handleSelect = (date: Date | DateRange | undefined) => {
this.date = date as Date | undefined;
};
<template>
<Card @class="mx-auto w-fit p-0">
<CardContent @class="p-0">
<Calendar
@defaultMonth={{this.date}}
@disabled={{this.bookedDates}}
@mode="single"
@modifiers={{this.modifiers}}
@modifiersClassNames={{this.modifiersClassNames}}
@onSelect={{this.handleSelect}}
@selected={{this.date}}
/>
</CardContent>
</Card>
</template>
get modifiers() {
return { booked: this.bookedDates };
}
get modifiersClassNames() {
return { booked: '[&>button]:line-through opacity-100' };
}
}Custom Cell Size
| Su | Mo | Tu | We | Th | Fr | Sa |
|---|---|---|---|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { Calendar } from '@/components/ui/calendar';
import { Card, CardContent } from '@/components/ui/card';
import type { DateRange } from '@/components/ui/calendar';
function isWeekend(date: Date): boolean {
const day = date.getDay();
return day === 0 || day === 6;
}
export default class CalendarCustomDays extends Component {
@tracked dateRange: DateRange | undefined = {
from: new Date(new Date().getFullYear(), 11, 2),
to: new Date(new Date().getFullYear(), 11, 8),
};
handleSelect = (range: Date | DateRange | undefined) => {
this.dateRange = range as DateRange | undefined;
};
<template>
<Card @class="mx-auto w-fit p-0">
<CardContent @class="p-0">
<Calendar
@captionLayout="dropdown"
@class="[--cell-size:--spacing(12)] rounded-lg border p-2 [--cell-radius:var(--radius-md)] md:[--cell-size:--spacing(14)]"
@defaultMonth={{this.dateRange.from}}
@mode="range"
@onSelect={{this.handleSelect}}
@selected={{this.dateRange}}
>
<:day as |day|>
{{day.dayOfMonth}}
{{#unless day.isOutside}}
<span>{{if (isWeekend day.date) "$120" "$100"}}</span>
{{/unless}}
</:day>
</Calendar>
</CardContent>
</Card>
</template>
}
You can customize the size of calendar cells using the
--cell-size
CSS variable. You can also make it responsive by using breakpoint-specific values:
<Calendar
@mode="single"
@selected={{this.date}}
@onSelect={{this.setDate}}
@class="rounded-lg border [--cell-size:--spacing(11)] md:[--cell-size:--spacing(12)]"
/>Or use fixed values:
<Calendar
@mode="single"
@selected={{this.date}}
@onSelect={{this.setDate}}
@class="rounded-lg border [--cell-size:2.75rem] md:[--cell-size:3rem]"
/>Week Numbers
Use
@showWeekNumber
to show week numbers.
| Week number | Su | Mo | Tu | We | Th | Fr | Sa |
|---|---|---|---|---|---|---|---|
|
6
|
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
|
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { Calendar } from '@/components/ui/calendar';
import { Card, CardContent } from '@/components/ui/card';
import type { DateRange } from '@/components/ui/calendar';
export default class CalendarWeekNumbers extends Component {
@tracked date: Date | undefined = new Date(new Date().getFullYear(), 1, 12);
handleSelect = (date: Date | DateRange | undefined) => {
this.date = date as Date | undefined;
};
<template>
<Card @class="mx-auto w-fit p-0">
<CardContent @class="p-0">
<Calendar
@defaultMonth={{this.date}}
@mode="single"
@onSelect={{this.handleSelect}}
@selected={{this.date}}
@showWeekNumber={{true}}
/>
</CardContent>
</Card>
</template>
}Locale
Use
@locale
to localize the calendar. Pass a
date-fns locale
to translate month names, weekday labels, and set the first day of the week. Use
@weekStartsOn
to override the week start day.
import { nl } from 'date-fns/locale';<Calendar
@mode="single"
@locale={{this.nlLocale}}
@selected={{this.date}}
@onSelect={{this.setDate}}
@class="rounded-lg border"
/>| ma | di | wo | do | vr | za | zo |
|---|---|---|---|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { nl } from 'date-fns/locale';
import { Calendar } from '@/components/ui/calendar';
import type { DateRange } from '@/components/ui/calendar';
export default class CalendarLocale extends Component {
locale = nl;
@tracked date: Date | undefined = new Date();
handleSelect = (date: Date | DateRange | undefined) => {
this.date = date as Date | undefined;
};
<template>
<Calendar
@class="rounded-md border shadow-sm"
@locale={{this.locale}}
@mode="single"
@onSelect={{this.handleSelect}}
@selected={{this.date}}
/>
</template>
}