Calendar
Gregorian month calendar with date-only selection, bounded navigation, localized labels, and scalable touch cells.
Install
First install? Set up the registry in your app.
npx --yes --package=https://native.uipkge.dev/cli/uipkge-native-0.1.1.tgz uipkge-native add calendarSource files
Expand a file to read the source the installer copies into your app.
components/ui/calendar/Calendar.tsx
import * as React from 'react'
import { Pressable, ScrollView, View, useWindowDimensions, type ViewProps } from 'react-native'
import { ChevronLeft, ChevronRight } from 'lucide-react-native'
import { IconButton } from '@/components/ui/icon-button'
import { Text } from '@/components/ui/text'
import { cn } from '@/lib/utils'
import {
calendarDateDisabled,
calendarDateParts,
calendarMonthDays,
calendarMonthParts,
localCalendarDate,
moveCalendarMonth,
type CalendarDate,
type CalendarMonth,
} from './calendar.utils'
export interface CalendarProps extends ViewProps {
className?: string
value?: CalendarDate
defaultValue?: CalendarDate
onValueChange?: (date: CalendarDate) => void
month?: CalendarMonth
defaultMonth?: CalendarMonth
onMonthChange?: (month: CalendarMonth) => void
minDate?: CalendarDate
maxDate?: CalendarDate
disabled?: boolean
locale?: string
weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6
previousMonthLabel?: string
nextMonthLabel?: string
}
export const Calendar = React.forwardRef<React.ComponentRef<typeof View>, CalendarProps>(
(
{
className,
value,
defaultValue,
onValueChange,
month,
defaultMonth,
onMonthChange,
minDate,
maxDate,
disabled = false,
locale,
weekStartsOn = 0,
previousMonthLabel = 'Previous month',
nextMonthLabel = 'Next month',
...props
},
ref,
) => {
const [today] = React.useState(() => localCalendarDate(new Date()))
const [internalValue, setInternalValue] = React.useState(defaultValue)
const [internalMonth, setInternalMonth] = React.useState(() =>
calendarMonthParts(defaultMonth)
? defaultMonth!
: calendarDateParts(value ?? defaultValue)
? (value ?? defaultValue)!.slice(0, 7)
: today.slice(0, 7),
)
const selected = value === undefined ? internalValue : value
const shownMonth = calendarMonthParts(month) ? month! : internalMonth
const minimum = calendarDateParts(minDate) ? minDate : undefined
const maximum = calendarDateParts(maxDate) ? maxDate : undefined
const unavailable = disabled || !!(minimum && maximum && minimum > maximum)
const start = Number.isInteger(weekStartsOn) && weekStartsOn >= 0 && weekStartsOn <= 6 ? weekStartsOn : 0
const parts = calendarMonthParts(shownMonth)!
const days = calendarMonthDays(shownMonth, start)
const { fontScale } = useWindowDimensions()
const cellWidth = Math.max(44, Math.ceil(28 * fontScale))
const previous = moveCalendarMonth(shownMonth, -1)
const next = moveCalendarMonth(shownMonth, 1)
const previousDisabled = unavailable || !previous || !!(minimum && previous < minimum.slice(0, 7))
const nextDisabled = unavailable || !next || !!(maximum && next > maximum.slice(0, 7))
const heading = new Intl.DateTimeFormat(locale, {
month: 'long',
year: 'numeric',
calendar: 'gregory',
timeZone: 'UTC',
})
const fullDate = new Intl.DateTimeFormat(locale, {
weekday: 'long',
year: 'numeric',
month: 'long',
day: 'numeric',
calendar: 'gregory',
timeZone: 'UTC',
})
const dayName = new Intl.DateTimeFormat(locale, { weekday: 'narrow', timeZone: 'UTC' })
const number = new Intl.NumberFormat(locale, { useGrouping: false })
function navigate(nextMonth: CalendarMonth | undefined) {
if (unavailable || !nextMonth) return
if (month === undefined) setInternalMonth(nextMonth)
onMonthChange?.(nextMonth)
}
return (
<View
ref={ref}
{...props}
className={cn(
'gap-[8px] rounded-lg border border-border bg-card p-[8px]',
unavailable && 'opacity-50',
className,
)}
>
<View className="flex-row items-center gap-[8px]">
<IconButton
icon={ChevronLeft}
accessibilityLabel={previousMonthLabel}
disabled={previousDisabled}
onPress={() => {
if (!previousDisabled) navigate(previous)
}}
/>
<Text accessibilityRole="header" className="min-w-0 flex-1 text-center font-semibold">
{heading.format(parts.date)}
</Text>
<IconButton
icon={ChevronRight}
accessibilityLabel={nextMonthLabel}
disabled={nextDisabled}
onPress={() => {
if (!nextDisabled) navigate(next)
}}
/>
</View>
<ScrollView horizontal contentContainerStyle={{ flexGrow: 1 }}>
<View style={{ minWidth: cellWidth * 7, flexGrow: 1 }}>
<View className="flex-row">
{Array.from({ length: 7 }, (_, index) => (
<View
key={index}
className="min-h-[32px] items-center justify-center p-[4px]"
style={{ minWidth: cellWidth, flex: 1 }}
>
<Text size="sm" variant="muted" accessible={false}>
{dayName.format(new Date(Date.UTC(2020, 5, 7 + ((start + index) % 7))))}
</Text>
</View>
))}
</View>
{Array.from({ length: 6 }, (_, week) => (
<View key={week} className="min-h-[48px] flex-row">
{days.slice(week * 7, week * 7 + 7).map((date, index) => {
if (!date) return <View key={`blank-${index}`} style={{ minWidth: cellWidth, flex: 1 }} />
const day = calendarDateParts(date)!
const blocked = unavailable || calendarDateDisabled(date, minimum, maximum)
const checked = selected === date
return (
<Pressable
key={date}
accessibilityRole="button"
accessibilityLabel={fullDate.format(day.date)}
accessibilityState={{ selected: checked, disabled: blocked }}
disabled={blocked}
onPress={() => {
if (blocked || date === selected) return
if (value === undefined) setInternalValue(date)
onValueChange?.(date)
}}
className={cn(
'min-h-[48px] items-center justify-center rounded-md border border-transparent p-[4px] active:bg-muted',
date === today && 'border-primary',
checked && 'bg-primary active:bg-primary',
blocked && 'opacity-40',
)}
style={{ minWidth: cellWidth, flex: 1 }}
>
<Text
className={checked ? 'text-primary-foreground' : undefined}
style={{ fontVariant: ['tabular-nums'] }}
>
{number.format(day.day)}
</Text>
</Pressable>
)
})}
</View>
))}
</View>
</ScrollView>
</View>
)
},
)
Calendar.displayName = 'Calendar'
components/ui/calendar/calendar.utils.ts
export type CalendarDate = string
export type CalendarMonth = string
export function calendarDateParts(value: string | undefined) {
if (!value || !/^\d{4}-\d{2}-\d{2}$/.test(value)) return undefined
const [year, month, day] = value.split('-').map(Number)
const date = new Date(0)
date.setUTCFullYear(year, month - 1, day)
date.setUTCHours(12, 0, 0, 0)
if (year < 1 || date.getUTCFullYear() !== year || date.getUTCMonth() !== month - 1 || date.getUTCDate() !== day) {
return undefined
}
return { year, month, day, date }
}
export function calendarMonthParts(value: string | undefined) {
return value && /^\d{4}-\d{2}$/.test(value) ? calendarDateParts(`${value}-01`) : undefined
}
export function localCalendarDate(date: Date): CalendarDate {
return `${String(date.getFullYear()).padStart(4, '0')}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}`
}
export function moveCalendarMonth(month: CalendarMonth, direction: -1 | 1): CalendarMonth | undefined {
const parts = calendarMonthParts(month)
if (!parts) return undefined
const date = new Date(parts.date)
date.setUTCMonth(date.getUTCMonth() + direction)
const year = date.getUTCFullYear()
if (year < 1 || year > 9999) return undefined
return `${String(year).padStart(4, '0')}-${String(date.getUTCMonth() + 1).padStart(2, '0')}`
}
export function calendarMonthDays(month: CalendarMonth, weekStartsOn: number): Array<CalendarDate | undefined> {
const parts = calendarMonthParts(month)
if (!parts) return []
const offset = (parts.date.getUTCDay() - weekStartsOn + 7) % 7
return Array.from({ length: 42 }, (_, index) => {
const date = new Date(parts.date)
date.setUTCDate(1 + index - offset)
return date.getUTCMonth() === parts.month - 1 ? `${month}-${String(date.getUTCDate()).padStart(2, '0')}` : undefined
})
}
export function calendarDateDisabled(date: CalendarDate, minimum?: CalendarDate, maximum?: CalendarDate) {
return !calendarDateParts(date) || !!(minimum && date < minimum) || !!(maximum && date > maximum)
}
components/ui/calendar/index.ts
export { Calendar, type CalendarProps } from './Calendar'
export type { CalendarDate, CalendarMonth } from './calendar.utils'
Dependencies
Registry items
npm packages
lucide-react-native