Calendar
A monthly calendar supporting single dates, ranges, multiple dates, lunar dates, and event markers. Use it for schedules, shifts, and attendance displayed directly on a page; use DatePicker when filling a date field in a form.
Import
import { HuiCalendar } from '@nscode/handy-ui'
Internal state and controlled updates
modelValue, modelValues, and range independently determine controlled state by prop presence. Omitted props or update listeners alone use internal state; an explicitly supplied prop, including undefined, follows the parent value. A null range is also a controlled empty range. The first range endpoint is an editing draft; an update is requested only after the second click completes it. range=undefined preserves the original mode inference; also set select-mode="range" to select an empty range.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiCalendar, HuiRadioGroup, HuiRadio, HuiSpace, HuiButton } from '@nscode/handy-ui'
const q1Redraw = ref(0)
const q1Last = ref('No request yet')
const q1Mode = ref<'single' | 'multiple' | 'range'>('single')
const q1Single = ref<Date>()
const q1Multiple = ref<Date[]>([])
const q1Range = ref<[Date, Date] | null>(null)
const q1Now = new Date()
const q1FixedSingle = new Date(q1Now.getFullYear(), q1Now.getMonth(), 5)
const q1FixedMultiple = [q1FixedSingle]
const q1FixedRange: [Date, Date] = [
q1FixedSingle,
new Date(q1Now.getFullYear(), q1Now.getMonth(), 6)
]
</script>
<template>
<div class="calendar-q1-demo">
<HuiSpace
><HuiButton class="q1-redraw" @click="q1Redraw++">Parent redraw {{ q1Redraw }}</HuiButton
><HuiRadioGroup v-model="q1Mode"
><HuiRadio value="single">Single</HuiRadio><HuiRadio value="multiple">Multiple</HuiRadio
><HuiRadio value="range">Range</HuiRadio></HuiRadioGroup
></HuiSpace
>
<HuiSpace direction="vertical" fill
><div>
<p>Prop omitted: internal state</p>
<div class="q1-local">
<HuiCalendar v-if="q1Mode === 'single'" select-mode="single" />
<HuiCalendar v-else-if="q1Mode === 'multiple'" select-mode="multiple" />
<HuiCalendar v-else select-mode="range" />
</div>
</div>
<div>
<p>Listener only: internal state</p>
<div class="q1-listener">
<HuiCalendar
v-if="q1Mode === 'single'"
select-mode="single"
@update:model-value="q1Last = JSON.stringify($event)"
/>
<HuiCalendar
v-else-if="q1Mode === 'multiple'"
select-mode="multiple"
@update:model-values="q1Last = JSON.stringify($event)"
/>
<HuiCalendar v-else select-mode="range" @update:range="q1Last = JSON.stringify($event)" />
</div>
</div>
<div>
<p>Explicit undefined: parent value</p>
<div class="q1-empty">
<HuiCalendar v-if="q1Mode === 'single'" select-mode="single" :model-value="undefined" />
<HuiCalendar
v-else-if="q1Mode === 'multiple'"
select-mode="multiple"
:model-values="undefined"
/>
<HuiCalendar v-else select-mode="range" :range="undefined" />
</div>
</div>
<div>
<p>Fixed controlled value: parent rejects updates</p>
<div class="q1-fixed">
<HuiCalendar
v-if="q1Mode === 'single'"
select-mode="single"
:model-value="q1FixedSingle"
@update:model-value="q1Last = JSON.stringify($event)"
/>
<HuiCalendar
v-else-if="q1Mode === 'multiple'"
select-mode="multiple"
:model-values="q1FixedMultiple"
@update:model-values="q1Last = JSON.stringify($event)"
/>
<HuiCalendar
v-else
select-mode="range"
:range="q1FixedRange"
@update:range="q1Last = JSON.stringify($event)"
/>
</div>
</div>
<div>
<p>Two-way binding</p>
<div class="q1-bound">
<HuiCalendar v-if="q1Mode === 'single'" select-mode="single" v-model="q1Single" />
<HuiCalendar
v-else-if="q1Mode === 'multiple'"
select-mode="multiple"
v-model:model-values="q1Multiple"
/>
<HuiCalendar v-else select-mode="range" v-model:range="q1Range" />
</div>
</div>
<p class="q1-notification">Latest request: {{ q1Last }}</p></HuiSpace
>
</div>
</template>
Basic usage
Bind the selected date with v-model. Click a date to select it. Click the year or month in the header to switch to the corresponding view for faster navigation.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiCalendar } from '@nscode/handy-ui'
const value = ref(new Date())
</script>
<template>
<HuiCalendar v-model="value" />
<p style="margin-top: 12px">Selected: {{ value.toLocaleDateString() }}</p>
</template>
Without v-model, the component stores selection internally. Listen to select for the result.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiCalendar } from '@nscode/handy-ui'
const selected = ref('')
</script>
<template>
<HuiCalendar @select="(date) => (selected = date.toLocaleDateString())" />
<p style="margin-top: 12px">{{ selected ? `select event: ${selected}` : 'Click a date' }}</p>
</template>
First day of the week
firstDayOfWeek accepts 1 through 7 for Monday through Sunday. The default is 7 (Sunday). Change the option below to compare weekday headings and date placement:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiCalendar, HuiSegmented, HuiSpace } from '@nscode/handy-ui'
const value = ref(new Date())
const firstDay = ref<string | number>(1)
const options = [
{ label: 'Monday (1)', value: 1 },
{ label: 'Tuesday (2)', value: 2 },
{ label: 'Wednesday (3)', value: 3 },
{ label: 'Thursday (4)', value: 4 },
{ label: 'Friday (5)', value: 5 },
{ label: 'Saturday (6)', value: 6 },
{ label: 'Sunday (7)', value: 7 }
]
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiSegmented v-model="firstDay" :options="options" />
<HuiCalendar v-model="value" :first-day-of-week="Number(firstDay)" />
</HuiSpace>
</template>
Disabled dates
disabledDate is a function; dates for which it returns true cannot be selected. If every date in a month or year is disabled, its cell is also disabled in the month or year view.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiCalendar } from '@nscode/handy-ui'
const value = ref<Date>()
// Dates before today are disabled
const disabledDate = (date: Date) => {
const today = new Date()
today.setHours(0, 0, 0, 0)
return date.getTime() < today.getTime()
}
</script>
<template>
<HuiCalendar v-model="value" :disabled-date="disabledDate" />
</template>
Two common examples: the left disables weekends, and the right allows dates only in the current year. Click the year in the header to open the year view; other years are disabled.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiCalendar, HuiSpace } from '@nscode/handy-ui'
const workday = ref<Date>()
const thisYear = ref<Date>()
const isWeekend = (date: Date) => date.getDay() === 0 || date.getDay() === 6
const notThisYear = (date: Date) => date.getFullYear() !== new Date().getFullYear()
</script>
<template>
<HuiSpace wrap align="start" size="large">
<HuiCalendar v-model="workday" :disabled-date="isWeekend" />
<HuiCalendar v-model="thisYear" :disabled-date="notThisYear" />
</HuiSpace>
</template>
Range selection
With select-mode="range", click twice to select the endpoints. Either chronological order is accepted, and the result is always [start, end]. Ranges can span months: select one endpoint, navigate to another month, then select the other.
Bind the range with v-model:range, or listen to rangeChange.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiCalendar } from '@nscode/handy-ui'
const range = ref<[Date, Date] | null>(null)
</script>
<template>
<HuiCalendar v-model:range="range" select-mode="range" />
<p style="margin-top: 12px">
{{
range
? `${range[0].toLocaleDateString()} to ${range[1].toLocaleDateString()}`
: 'Click two dates to select a range'
}}
</p>
</template>
Without v-model:range, receive the result through rangeChange. Range selection does not emit select.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiCalendar } from '@nscode/handy-ui'
const days = ref(0)
const onRangeChange = (range: [Date, Date] | null) => {
days.value = range ? Math.round((range[1].getTime() - range[0].getTime()) / 86400000) + 1 : 0
}
</script>
<template>
<HuiCalendar select-mode="range" @range-change="onRangeChange" />
<p style="margin-top: 12px">{{ days ? `${days} days total` : 'No range selected' }}</p>
</template>
Multiple selection
With select-mode="multiple", select several dates and click a selected date again to deselect it. Bind using v-model:model-values.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiCalendar } from '@nscode/handy-ui'
const dates = ref<Date[]>([])
</script>
<template>
<HuiCalendar v-model:model-values="dates" select-mode="multiple" />
<p style="margin-top: 12px">
Selected {{ dates.length }} days: {{ dates.map((date) => date.getDate()).join('、') || 'None' }}
</p>
</template>
Lunar dates
Weekdays, year/month labels, and navigation buttons follow the current language. Lunar names use Traditional Chinese in the Traditional Chinese locale; other built-in locales keep Simplified Chinese lunar names.
Set lunar to true to display lunar information below dates. Solar terms take priority. The first lunar day displays the month name, such as the first month or a leap second month; other days display their lunar day names.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiCalendar } from '@nscode/handy-ui'
const value = ref(new Date())
</script>
<template>
<HuiCalendar v-model="value" lunar />
</template>
Event markers
Each events entry displays a small dot below its date. color sets the dot color (primary by default), and title is its hover tooltip. Multiple events on one day produce multiple dots. All events below are in the current month:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiCalendar } from '@nscode/handy-ui'
import type { CalendarEvent } from '@nscode/handy-ui'
const value = ref(new Date())
const year = value.value.getFullYear()
const month = value.value.getMonth()
const events: CalendarEvent[] = [
{ date: new Date(year, month, 3), title: 'Weekly meeting' },
{ date: new Date(year, month, 8), title: 'Release', color: 'var(--hui-color-success)' },
{ date: new Date(year, month, 8), title: 'Review', color: 'var(--hui-color-warning)' },
{ date: new Date(year, month, 16), title: 'Incident review', color: 'var(--hui-color-danger)' },
{ date: new Date(year, month, 22), title: 'Team event', color: 'var(--hui-color-help)' },
{ date: new Date(year, month, 22), title: 'Weekly meeting' },
{ date: new Date(year, month, 22), title: 'Training', color: 'var(--hui-color-info)' }
]
</script>
<template>
<HuiCalendar v-model="value" :events="events" />
</template>
Lunar dates and event markers can be combined:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiCalendar } from '@nscode/handy-ui'
import type { CalendarEvent } from '@nscode/handy-ui'
const value = ref(new Date())
const year = value.value.getFullYear()
const month = value.value.getMonth()
const events: CalendarEvent[] = [
{ date: new Date(year, month, 5), title: 'Pay rent', color: 'var(--hui-color-warning)' },
{ date: new Date(year, month, 20), title: 'Health check', color: 'var(--hui-color-success)' }
]
</script>
<template>
<HuiCalendar v-model="value" lunar :events="events" />
</template>
Custom header
The header slot replaces the entire header and receives the current year, month, and navigation methods: prevMonth, nextMonth, prevYear, nextYear, and goToToday.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiCalendar } from '@nscode/handy-ui'
const value = ref(new Date())
</script>
<template>
<HuiCalendar v-model="value">
<template #header="{ year, month, prevMonth, nextMonth, goToToday }">
<HuiButton size="small" @click="prevMonth">Previous month</HuiButton>
<span>{{ year }} year / {{ month }} month</span>
<HuiButton size="small" @click="nextMonth">Next month</HuiButton>
<HuiButton size="small" @click="goToToday">Today</HuiButton>
</template>
</HuiCalendar>
</template>
Use all navigation methods with spaced controls:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiButtonGroup, HuiCalendar } from '@nscode/handy-ui'
const value = ref(new Date())
</script>
<template>
<HuiCalendar v-model="value">
<template #header="{ year, month, prevMonth, nextMonth, prevYear, nextYear, goToToday }">
<div class="calendar-header">
<strong>{{ year }} year / {{ month }} month</strong>
<HuiButtonGroup size="small">
<HuiButton icon="double-left" aria-label="Previous year" @click="prevYear" />
<HuiButton icon="chevron-left" aria-label="Previous month" @click="prevMonth" />
<HuiButton @click="goToToday">Today</HuiButton>
<HuiButton icon="chevron-right" aria-label="Next month" @click="nextMonth" />
<HuiButton icon="double-right" aria-label="Next year" @click="nextYear" />
</HuiButtonGroup>
</div>
</template>
</HuiCalendar>
</template>
<style scoped>
.calendar-header {
display: flex;
flex: 1;
align-items: center;
justify-content: space-between;
}
</style>
Custom date cells
The cell slot replaces cell content while preserving selected/disabled styling and click behavior. It receives date, text, type, disabled, selected, and events; enabling lunar adds lunar and lunarDisplay, while range mode adds inRange, isStart, and isEnd.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiCalendar } from '@nscode/handy-ui'
import type { CalendarEvent } from '@nscode/handy-ui'
const value = ref(new Date())
const year = value.value.getFullYear()
const month = value.value.getMonth()
const events: CalendarEvent[] = [
{ date: new Date(year, month, 10), title: 'Weekly meeting' },
{ date: new Date(year, month, 10), title: 'Review' },
{ date: new Date(year, month, 18), title: 'Release' }
]
</script>
<template>
<HuiCalendar v-model="value" :events="events">
<template #cell="{ text, date, events: dayEvents }">
<span>{{ text }}</span>
<small v-if="date.getDay() === 0 || date.getDay() === 6">Off</small>
<small v-else-if="dayEvents.length">{{ dayEvents.length }} events</small>
</template>
</HuiCalendar>
</template>
Use isStart and isEnd to mark check-in and check-out dates, with lunarDisplay for lunar text:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiCalendar } from '@nscode/handy-ui'
const range = ref<[Date, Date] | null>(null)
</script>
<template>
<HuiCalendar v-model:range="range" select-mode="range" lunar>
<template #cell="{ text, isStart, isEnd, lunarDisplay }">
<div class="cell">
<span>{{ text }}</span>
<small>{{ isStart ? 'Check-in' : isEnd ? 'Check-out' : lunarDisplay }}</small>
</div>
</template>
</HuiCalendar>
</template>
<style scoped>
.cell {
display: flex;
flex-direction: column;
align-items: center;
line-height: 1.2;
}
</style>
Methods
Call through a ref. prevMonth and nextMonth change the displayed month without changing bound selection. goToToday navigates to today; in single mode it also selects today, while multiple and range modes only move to the current month without changing the selection; selectDate behaves like clicking a date.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiCalendar, HuiSpace } from '@nscode/handy-ui'
import type { CalendarExpose } from '@nscode/handy-ui'
const calendarRef = ref<CalendarExpose>()
const value = ref(new Date())
// Select the 15th of next month
const selectNextMonth15 = () => {
const today = new Date()
calendarRef.value?.selectDate(new Date(today.getFullYear(), today.getMonth() + 1, 15))
}
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiSpace wrap>
<HuiButton size="small" @click="calendarRef?.prevYear()">prevYear</HuiButton>
<HuiButton size="small" @click="calendarRef?.prevMonth()">prevMonth</HuiButton>
<HuiButton size="small" @click="calendarRef?.goToToday()">goToToday</HuiButton>
<HuiButton size="small" @click="calendarRef?.nextMonth()">nextMonth</HuiButton>
<HuiButton size="small" @click="calendarRef?.nextYear()">nextYear</HuiButton>
<HuiButton size="small" @click="selectNextMonth15">selectDate(15th of next month)</HuiButton>
</HuiSpace>
<HuiCalendar ref="calendarRef" v-model="value" />
<span>Bound value: {{ value.toLocaleDateString() }}</span>
</HuiSpace>
</template>
Keyboard interaction
| Key | Action |
|---|---|
Tab | Focus the selected date, or today/the first of the displayed month when nothing is selected |
← → | Previous or next day |
↑ ↓ | Same weekday in the previous or next week |
Enter Space | Select the focused date |
Arrow keys automatically navigate across months without changing bound selection.
Navigation and selection are separate
Navigating months or years emits only panelChange. Selecting a specific date changes the bound selection.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiCalendar, HuiSpace } from '@nscode/handy-ui'
const value = ref(new Date(2026, 10, 18))
const panel = ref('2026-11')
const onPanelChange = (year: number, month: number) => {
panel.value = year + '-' + month
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiCalendar v-model="value" @panel-change="onPanelChange" />
<p>Current panel: {{ panel }}; selected date: {{ value.toLocaleDateString() }}</p>
</HuiSpace>
</template>
Event names and template listeners
Public events are panelChange and rangeChange; legacy kebab-case event names are not emitted in addition. Vue templates may still use @panel-change and @range-change, which resolve to the camelCase declarations. JavaScript render functions use onPanelChange and onRangeChange.
Arguments and ordering are unchanged: month/year navigation emits panelChange(year, month) with a one-based month and does not change selection. Selecting the second range endpoint emits update:range before rangeChange, with the dates sorted chronologically. Disabled dates do not advance range selection, and range mode does not emit select.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiCalendar } from '@nscode/handy-ui'
const eventCalendar = ref<InstanceType<typeof HuiCalendar>>()
const eventDate = ref(new Date(2024, 4, 1))
const eventRange = ref<[Date, Date] | null>(null)
const eventStats = ref({
panels: 0,
ranges: 0,
panel: 'No navigation yet',
range: 'No selection yet'
})
const recordPanel = (year: number, month: number) => {
eventStats.value.panels++
eventStats.value.panel = year + '-' + month
}
const recordRange = (range: [Date, Date] | null) => {
eventStats.value.ranges++
eventStats.value.range = range ? range.map((date) => date.getDate()).join('→') : 'Empty'
}
</script>
<template>
<div
class="calendar-events-demo"
style="display:flex;flex-direction:column;gap:var(--hui-spacing-4);max-width:420px"
>
<button class="calendar-events-next" @click="eventCalendar?.nextMonth()">Next month</button>
<span class="calendar-events-panel-result"
>panelChange:{{ eventStats.panels }};{{ eventStats.panel }}</span
>
<HuiCalendar ref="eventCalendar" v-model="eventDate" @panel-change="recordPanel" />
<span class="calendar-events-range-result"
>rangeChange:{{ eventStats.ranges }};{{ eventStats.range }}</span
>
<div class="calendar-events-range">
<HuiCalendar
v-model:range="eventRange"
:model-value="new Date(2024, 4, 1)"
select-mode="range"
@range-change="recordRange"
/>
</div>
</div>
</template>
Props
| Prop | Type | Default | Description |
|---|---|---|---|
| modelValue | Date | undefined | Selected date in single mode; supports v-model. Controlled when supplied, stored internally when omitted |
| modelValues | Date[] | undefined | Selected dates in multiple mode; supports v-model:model-values. Controlled when supplied, stored internally when omitted |
| range | [Date, Date] | null | undefined | Selected range; supports v-model:range. Controlled when supplied, stored internally when omitted |
| selectMode | 'single' | 'range' | 'multiple' | 'single' | Selection mode; a supplied range (including null) also infers range mode when selectMode is omitted |
| disabledDate | (date: Date) => boolean | - | Dates for which this returns true cannot be selected |
| firstDayOfWeek | number | 7 | First weekday: 1–6 are Monday–Saturday; 7 or 0 is Sunday |
| lunar | boolean | false | Show lunar dates and solar terms |
| events | CalendarEvent[] | - | Event markers |
Events
| Event | Parameters | Description |
|---|---|---|
| update:modelValue | (date: Date) | Emitted after selecting a date in single mode, or selecting today with goToToday, before select; month/year navigation changes only the displayed panel |
| update:modelValues | (dates: Date[]) | Emitted with a new array when multiple selection changes |
| update:range | (range: [Date, Date]) | Emitted when both range endpoints have been selected |
| rangeChange | (range: [Date, Date] | null) | Emitted like update:range when both endpoints have been selected |
| select | (date: Date) | Emitted after a date click in single/multiple mode, or when goToToday selects today in single mode, after the matching update:* event; not emitted in range mode |
| panelChange | (year: number, month: number) | Emitted when the displayed year/month changes; month is 1–12 |
Slots
| Slot | Scope | Description |
|---|---|---|
| header | CalendarHeaderSlotProps: { year, month, prevMonth, nextMonth, prevYear, nextYear, goToToday } | Replace the entire header; month is one-based |
| cell | CalendarCellSlotProps: { date, type, text, disabled, selected, inRange, isStart, isEnd, lunar, lunarDisplay, events } | Replace date cell content |
Methods
Call through the component ref.
| Method | Parameters | Description | | ---------- | -------------- | ----------- | ---------------------------------------------------------------------------------- | | prevMonth | - | void | Navigate to the previous month | | nextMonth | - | void | Navigate to the next month | | prevYear | - | void | Navigate to the previous year, or previous decade in year view | | nextYear | - | void | Navigate to the next year, or next decade in year view | | goToToday | - | void | Navigate to and select today; if today is disabled, only navigate | | selectDate | (date: Date) | void | Handle a date like a click under the current selection mode; ignore disabled dates |
Types
Use CalendarExpose for public methods. The old CalendarInstance method type remains only as a deprecated compatibility alias and will be removed in 1.0.
toLunar(date, names?) and getLunarDisplay(date, showSolarTerm?, names?) can be imported from @nscode/handy-ui. Optional names has type CalendarLunarNames. When omitted, these functions use Simplified Chinese names independently of the global locale. The component passes the names from its active locale.
Public types use a component prefix. Compatibility mapping: LunarDate → CalendarLunarDate. The old name remains importable from @nscode/handy-ui, is marked @deprecated, and will be removed in 1.0.
type CalendarSelectMode = 'single' | 'range' | 'multiple'
interface CalendarEvent {
date: Date
title?: string
color?: string
}
interface CalendarCellSlotProps {
date: Date
type: 'normal' | 'today' | 'prev-month' | 'next-month'
text: number
disabled: boolean
selected: boolean
inRange: boolean
isStart: boolean
isEnd: boolean
lunar?: CalendarLunarDate
lunarDisplay?: string
events: CalendarEvent[]
}
interface CalendarLunarDate {
year: number
month: number
day: number
/** Whether this is a leap month */
isLeap: boolean
/** Lunar month name, such as the first, eleventh, or twelfth month */
monthName: string
/** Lunar day name, such as the first or fifteenth day */
dayName: string
/** Solar term for this day, or null */
solarTerm: string | null
}
interface CalendarLunarNames {
months: readonly string[]
days: readonly string[]
solarTerms: readonly string[]
leap: string
monthSuffix: string
}
interface CalendarExpose {
prevMonth: () => void
nextMonth: () => void
prevYear: () => void
nextYear: () => void
goToToday: () => void
selectDate: (date: Date) => void
}
/** @deprecated Use CalendarExpose; this alias will be removed in 1.0. */
type CalendarInstance = CalendarExpose