Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

feat(quill): add single-date DatePicker sharing grid + input with DateTimePicker (#65098)

Paul D'Ambra30c8c8df3186
diff --git a/packages/quill/packages/components/AGENTS.md b/packages/quill/packages/components/AGENTS.md
index 5983e08a75d9..2e2fa1d22619 100644
--- a/packages/quill/packages/components/AGENTS.md
+++ b/packages/quill/packages/components/AGENTS.md
@@ -6,6 +6,7 @@ Quick-reference for AI agents using `@posthog/quill-components` — composed com
- `DataTable` — TanStack Table wired onto quill `Table` + `Pagination`
- `DateTimePicker` — calendar range picker with quick-range presets (`quickRanges`, `CUSTOM_RANGE`)
+- `DatePicker` — single-date picker (one calendar, optional time, no quick ranges)
- `useCalendar` — headless calendar grid hook (`Day`, `Month` enums)
## DataTable
@@ -62,6 +63,30 @@ Rules:
- `minDate`/`maxDate` are day-granular; time inputs are independent of those bounds.
- `weekStartsOn` affects the calendar grid only, not quick-range math.
+## DatePicker
+
+Single-date sibling of `DateTimePicker` — one calendar, no quick ranges, value is a plain `Date`.
+
+```tsx
+import { DatePicker } from '@posthog/quill-components'
+;<DatePicker
+ value={date}
+ onApply={(next) => setDate(next)}
+ onCancel={() => close()}
+ minDate={minDate}
+ maxDate={new Date()}
+ dateFormat="MDY" // or 'DMY' | 'YMD'
+ showTime // adds hour/minute inputs + an "Include time" toggle
+/>
+```
+
+Rules:
+
+- `value`/`onApply` are a single `Date`, not `{ start, end, range }`. Use this for the single-date PostHog callers (currently `LemonCalendarSelect`); reach for `DateTimePicker` only when you need a start→end range.
+- Without `showTime` the applied value is floored to start-of-day. With `showTime`, the "Include time" toggle decides whether the applied value keeps its hour/minute or is floored.
+- Shares the calendar grid and `minDate`/`maxDate` day-granular bounds with `DateTimePicker` (both render `Calendar` from `calendar-grid.tsx`).
+- Always a single calendar; there is no `compact`/dual-calendar mode.
+
## useCalendar
Headless month-grid state for building custom calendar UIs: returns `calendar` (months > weeks > days), view navigation (`viewNextMonth`, `viewToday`, ...), and selection helpers (`select`, `selectRange`, `isSelected`, `toggle`). Selected dates are normalized to midnight. Reach for this only when DateTimePicker doesn't fit.
diff --git a/packages/quill/packages/components/src/calendar-grid.tsx b/packages/quill/packages/components/src/calendar-grid.tsx
new file mode 100644
index 000000000000..94803136b787
--- /dev/null
+++ b/packages/quill/packages/components/src/calendar-grid.tsx
@@ -0,0 +1,275 @@
+import {
+ addMonths,
+ format,
+ getDay,
+ getMonth,
+ getYear,
+ isAfter,
+ isSameDay,
+ isSameMonth,
+ isToday,
+ subMonths,
+} from 'date-fns'
+import { ChevronLeft, ChevronRight } from 'lucide-react'
+import * as React from 'react'
+
+import { Button, Select, SelectContent, SelectGroup, SelectItem, SelectTrigger, SelectValue, cn } from '@posthog/quill-primitives'
+
+import { Day, useCalendar } from './use-calendar'
+
+export const POSTHOG_START_DATE = new Date(2020, 0, 23)
+export const WEEK_DAYS = ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa']
+export const MONTH_NAMES = [
+ 'January',
+ 'February',
+ 'March',
+ 'April',
+ 'May',
+ 'June',
+ 'July',
+ 'August',
+ 'September',
+ 'October',
+ 'November',
+ 'December',
+]
+
+function dayOnly(date: Date): Date {
+ return new Date(date.getFullYear(), date.getMonth(), date.getDate())
+}
+
+interface DayItemProps {
+ day: Date
+ startDate: Date
+ endDate: Date
+ viewing: Date
+ minDate?: Date
+ maxDate: Date
+ onClick: (day: Date) => void
+}
+
+export function DayItem({
+ day,
+ startDate,
+ endDate,
+ viewing,
+ minDate,
+ maxDate,
+ onClick,
+}: DayItemProps): React.ReactElement {
+ const outOfMonth = !isSameMonth(day, viewing)
+ const dayDate = dayOnly(day)
+ const afterMax = dayDate.getTime() > dayOnly(maxDate).getTime()
+ const beforeMin = minDate ? dayDate.getTime() < dayOnly(minDate).getTime() : false
+ const disabled = outOfMonth || afterMax || beforeMin
+
+ const isStart = !disabled && isSameDay(startDate, day)
+ const isEnd = !disabled && isSameDay(endDate, day)
+ const sameDay = isSameDay(startDate, endDate)
+ const isBetween = !disabled && !isStart && !isEnd && isAfter(day, startDate) && !isAfter(day, endDate)
+ const today = isToday(day)
+
+ const label = format(day, 'dd')
+
+ return (
+ <div
+ data-is-between={isBetween}
+ data-is-start={isStart}
+ data-is-end={isEnd}
+ data-is-today={today}
+ data-is-same-day={sameDay}
+ className={cn(
+ 'w-8 h-8 flex items-center justify-center',
+ isBetween && 'bg-fill-selected',
+ isStart && !sameDay && 'bg-fill-selected rounded-l-full',
+ isEnd && !sameDay && 'bg-fill-selected rounded-r-full'
+ )}
+ >
+ <Button
+ variant={isStart || isEnd ? 'primary' : 'default'}
+ size="icon-sm"
+ disabled={disabled}
+ aria-label={`Select ${format(day, 'PP')}`}
+ title={disabled ? undefined : `Select ${format(day, 'PP')}`}
+ onClick={() => onClick(day)}
+ className={cn(
+ 'w-full h-full !rounded-full p-0 text-[11px] tabular-nums',
+ today && !isStart && !isEnd && 'border border-primary',
+ disabled && 'opacity-20'
+ )}
+ >
+ {label}
+ </Button>
+ </div>
+ )
+}
+
+interface CalendarProps {
+ defaultViewing: Date
+ startDate: Date
+ endDate: Date
+ minDate?: Date
+ maxDate: Date
+ onSelect: (day: Date) => void
+ onViewChange: (month: Date) => void
+ siblingViewing?: Date
+ weekStartsOn?: Day
+}
+
+export function Calendar({
+ defaultViewing,
+ startDate,
+ endDate,
+ minDate,
+ maxDate,
+ onSelect,
+ onViewChange,
+ siblingViewing,
+ weekStartsOn,
+}: CalendarProps): React.ReactElement {
+ const { calendar, viewing, setViewing, viewPreviousMonth, viewNextMonth } = useCalendar({
+ viewing: defaultViewing,
+ weekStartsOn,
+ })
+
+ const lastDefaultRef = React.useRef(defaultViewing)
+ React.useEffect(() => {
+ if (!isSameMonth(lastDefaultRef.current, defaultViewing)) {
+ setViewing(defaultViewing)
+ lastDefaultRef.current = defaultViewing
+ }
+ }, [defaultViewing, setViewing])
+
+ const handlePrev = (): void => {
+ viewPreviousMonth()
+ onViewChange(subMonths(viewing, 1))
+ }
+ const handleNext = (): void => {
+ viewNextMonth()
+ onViewChange(addMonths(viewing, 1))
+ }
+
+ const floorDate = minDate && minDate.getTime() > POSTHOG_START_DATE.getTime() ? minDate : POSTHOG_START_DATE
+ const minYearVal = getYear(floorDate)
+ const minMonthAtMinYear = getMonth(floorDate)
+ const floorKey = minYearVal * 12 + minMonthAtMinYear
+ const ceilKey = getYear(maxDate) * 12 + getMonth(maxDate)
+ const viewingKey = getYear(viewing) * 12 + getMonth(viewing)
+
+ const disableNext =
+ viewingKey >= ceilKey ||
+ (!!siblingViewing &&
+ getMonth(siblingViewing) === getMonth(addMonths(viewing, 1)) &&
+ getYear(siblingViewing) === getYear(addMonths(viewing, 1)))
+
+ const disablePrev =
+ viewingKey <= floorKey ||
+ (!!siblingViewing &&
+ getMonth(siblingViewing) === getMonth(subMonths(viewing, 1)) &&
+ getYear(siblingViewing) === getYear(subMonths(viewing, 1)))
+ const maxYearVal = getYear(maxDate)
+ const maxMonthAtMaxYear = getMonth(maxDate)
+ const currentYear = getYear(viewing)
+ const currentMonth = getMonth(viewing)
+
+ const monthKey = (year: number, month: number): number => year * 12 + month
+ const currentKey = monthKey(currentYear, currentMonth)
+ const siblingKey = siblingViewing ? monthKey(getYear(siblingViewing), getMonth(siblingViewing)) : null
+
+ const monthOptions: { key: number; year: number; month: number }[] = []
+ for (let y = minYearVal; y <= maxYearVal; y++) {
+ const startMonth = y === minYearVal ? minMonthAtMinYear : 0
+ const endMonth = y === maxYearVal ? maxMonthAtMaxYear : 11
+ for (let m = startMonth; m <= endMonth; m++) {
+ monthOptions.push({ key: monthKey(y, m), year: y, month: m })
+ }
+ }
+
+ const handleMonthYearSelect = (next: number): void => {
+ const year = Math.floor(next / 12)
+ const month = next % 12
+ const nextDate = new Date(year, month, 1)
+ setViewing(nextDate)
+ onViewChange(nextDate)
+ }
+
+ return (
+ <div>
+ <div className="flex justify-center items-center py-1 gap-1">
+ <Button
+ variant="default"
+ size="icon-sm"
+ onClick={handlePrev}
+ disabled={disablePrev}
+ aria-label="Previous month"
+ title={disablePrev ? 'Disabled' : 'Previous month'}
+ className="disabled:cursor-not-allowed"
+ >
+ <ChevronLeft />
+ </Button>
+ <Select
+ value={currentKey}
+ onValueChange={(v) => {
+ if (v !== null) {
+ handleMonthYearSelect(v)
+ }
+ }}
+ >
+ <SelectTrigger size="sm" aria-label="Month and year" className="h-6 px-2 text-xs">
+ <SelectValue>{(v: number) => `${MONTH_NAMES[v % 12]} ${Math.floor(v / 12)}`}</SelectValue>
+ </SelectTrigger>
+ <SelectContent>
+ <SelectGroup>
+ {monthOptions.map(({ key, year, month }) => (
+ <SelectItem key={key} value={key} disabled={key === siblingKey}>
+ {MONTH_NAMES[month]} {year}
+ </SelectItem>
+ ))}
+ </SelectGroup>
+ </SelectContent>
+ </Select>
+ <Button
+ variant="default"
+ size="icon-sm"
+ onClick={handleNext}
+ disabled={disableNext}
+ aria-label="Next month"
+ title={disableNext ? 'Disabled' : 'Next month'}
+ className="disabled:cursor-not-allowed"
+ >
+ <ChevronRight />
+ </Button>
+ </div>
+
+ <div className="grid grid-cols-7">
+ {calendar[0][0].map((day) => (
+ <div
+ key={`h-${getDay(day)}`}
+ className="w-8 h-6 flex items-center justify-center text-[10px] text-muted-foreground uppercase"
+ >
+ {WEEK_DAYS[getDay(day)]}
+ </div>
+ ))}
+ </div>
+
+ <div className="flex flex-col">
+ {calendar[0].map((week, wi) => (
+ <div key={`w-${wi}`} className="grid grid-cols-7">
+ {week.map((day) => (
+ <DayItem
+ key={day.toISOString()}
+ day={day}
+ startDate={startDate}
+ endDate={endDate}
+ viewing={viewing}
+ minDate={minDate}
+ maxDate={maxDate}
+ onClick={onSelect}
+ />
+ ))}
+ </div>
+ ))}
+ </div>
+ </div>
+ )
+}
diff --git a/packages/quill/packages/components/src/date-picker.stories.tsx b/packages/quill/packages/components/src/date-picker.stories.tsx
new file mode 100644
index 000000000000..e552748d66f3
--- /dev/null
+++ b/packages/quill/packages/components/src/date-picker.stories.tsx
@@ -0,0 +1,102 @@
+import type { Meta, StoryObj } from '@storybook/react'
+import { format, subDays } from 'date-fns'
+import { CalendarIcon } from 'lucide-react'
+import * as React from 'react'
+
+import { Button, Popover, PopoverContent, PopoverTrigger } from '@posthog/quill-primitives'
+
+import { DatePicker } from './date-picker'
+import { Day } from './use-calendar'
+
+const meta = {
+ title: 'Components/DatePicker',
+ component: DatePicker,
+ tags: ['autodocs'],
+} satisfies Meta<typeof DatePicker>
+
+export default meta
+type Story = StoryObj<typeof meta>
+
+const initialValue = subDays(new Date(), 3)
+
+const baseArgs = {
+ value: initialValue,
+ onApply: () => undefined,
+}
+
+export const Default: Story = {
+ args: baseArgs,
+ render: () => {
+ const [value, setValue] = React.useState<Date>(initialValue)
+ return <DatePicker value={value} onApply={setValue} />
+ },
+}
+
+export const WithTime: Story = {
+ args: baseArgs,
+ render: () => {
+ const [value, setValue] = React.useState<Date>(initialValue)
+ return <DatePicker value={value} onApply={setValue} showTime />
+ },
+}
+
+export const InPopover: Story = {
+ args: baseArgs,
+ render: () => {
+ const [value, setValue] = React.useState<Date>(initialValue)
+ const [open, setOpen] = React.useState(false)
+ return (
+ <Popover open={open} onOpenChange={setOpen}>
+ <PopoverTrigger
+ render={
+ <Button variant="outline" size="sm">
+ <CalendarIcon />
+ {format(value, 'MMM do, yyyy')}
+ </Button>
+ }
+ />
+ <PopoverContent align="start" className="w-auto p-0">
+ <DatePicker
+ value={value}
+ onApply={(next) => {
+ setValue(next)
+ setOpen(false)
+ }}
+ onCancel={() => setOpen(false)}
+ />
+ </PopoverContent>
+ </Popover>
+ )
+ },
+}
+
+export const MinAndMaxDate: Story = {
+ args: baseArgs,
+ render: () => {
+ const [value, setValue] = React.useState<Date>(initialValue)
+ return (
+ <DatePicker
+ value={value}
+ onApply={setValue}
+ minDate={subDays(new Date(), 30)}
+ maxDate={subDays(new Date(), 1)}
+ />
+ )
+ },
+}
+
+export const EuropeanDateFormat: Story = {
+ args: baseArgs,
+ render: () => {
+ const [value, setValue] = React.useState<Date>(initialValue)
+ return <DatePicker value={value} onApply={setValue} dateFormat="DMY" />
+ },
+}
+
+export const WeekStartsMonday: Story = {
+ args: baseArgs,
+ render: () => {
+ const [value, setValue] = React.useState<Date>(initialValue)
+ return <DatePicker value={value} onApply={setValue} weekStartsOn={Day.MONDAY} />
+ },
+}
diff --git a/packages/quill/packages/components/src/date-picker.tsx b/packages/quill/packages/components/src/date-picker.tsx
new file mode 100644
index 000000000000..da8987cf4f3d
--- /dev/null
+++ b/packages/quill/packages/components/src/date-picker.tsx
@@ -0,0 +1,184 @@
+import { format, getHours, getMinutes, getMonth, getYear, startOfDay } from 'date-fns'
+import { SettingsIcon } from 'lucide-react'
+import * as React from 'react'
+
+import { Badge, Button, Separator, Switch, cn } from '@posthog/quill-primitives'
+
+import { Calendar } from './calendar-grid'
+import { SegmentedDateInput, type DateFormatOrder } from './segmented-date-input'
+import { Day } from './use-calendar'
+
+// Minute precision, no seconds — deliberately differs from DateTimePicker's HH:mm:ss; don't unify.
+const DATE_TIME_FORMATS: Record<DateFormatOrder, string> = {
+ MDY: 'MM/dd/yy HH:mm',
+ DMY: 'dd/MM/yy HH:mm',
+ YMD: 'yy-MM-dd HH:mm',
+}
+const DATE_ONLY_FORMATS: Record<DateFormatOrder, string> = {
+ MDY: 'MM/dd/yy',
+ DMY: 'dd/MM/yy',
+ YMD: 'yy-MM-dd',
+}
+
+export interface DatePickerProps {
+ value: Date
+ onApply: (value: Date) => void
+ onCancel?: () => void
+ minDate?: Date
+ maxDate?: Date
+ dateFormat?: DateFormatOrder
+ weekStartsOn?: Day
+ onDateTimeSettings?: () => void
+ /** Show hour/minute inputs and an "Include time" toggle. When off, the value is floored to the start of the day. */
+ showTime?: boolean
+ className?: string
+}
+
+
+export function DatePicker({
+ value,
+ onApply,
+ onCancel,
+ minDate,
+ maxDate: maxDateProp,
+ dateFormat = 'MDY',
+ weekStartsOn,
+ onDateTimeSettings,
+ showTime = false,
+ className,
+}: DatePickerProps): React.ReactElement {
+ const maxDate = maxDateProp ?? new Date()
+ const hasExplicitMaxDate = maxDateProp !== undefined
+ const [selected, setSelected] = React.useState<Date>(value)
+ const [includeTime, setIncludeTime] = React.useState<boolean>(showTime)
+ const [viewing, setViewing] = React.useState<Date>(new Date(getYear(value), getMonth(value), 1))
+ // Instance-scoped so two DatePickers on one page don't share a label association.
+ const includeTimeId = React.useId()
+
+ const handleSelect = (date: Date): void => {
+ const next = new Date(
+ date.getFullYear(),
+ date.getMonth(),
+ date.getDate(),
+ includeTime ? getHours(selected) : 0,
+ includeTime ? getMinutes(selected) : 0
+ )
+ setSelected(next)
+ }
+
+ const handleInputChange = (next: Date): void => {
+ // The segmented input caps its segments at maxDate but has no lower bound,
+ // so a typed value below minDate would otherwise bypass the advertised bound.
+ const clamped = minDate && next.getTime() < minDate.getTime() ? minDate : next
+ if (clamped.getTime() === selected.getTime()) {
+ return
+ }
+ setSelected(clamped)
+ setViewing(new Date(getYear(clamped), getMonth(clamped), 1))
+ }
+
+ const handleApply = (): void => {
+ onApply(includeTime ? selected : startOfDay(selected))
+ }
+
+ const presentational = format(selected, includeTime ? DATE_TIME_FORMATS[dateFormat] : DATE_ONLY_FORMATS[dateFormat])
+
+ return (
+ <div
+ className={cn(
+ 'bg-card text-foreground rounded-lg shadow-md ring-1 ring-foreground/10 overflow-hidden w-[15rem]',
+ className
+ )}
+ >
+ <div className="flex items-center gap-2 px-2 py-1 bg-muted/30 border-b border-border rounded-t-lg">
+ <span className="text-[10px] text-muted-foreground uppercase tracking-wide">Choose date</span>
+ {(minDate || hasExplicitMaxDate) && (
+ <div className="flex items-center gap-1 ml-auto">
+ {minDate && (
+ <Badge variant="default" className="text-[10px] px-1.5 py-0">
+ Min: {format(minDate, 'MMM d, yy')}
+ </Badge>
+ )}
+ {hasExplicitMaxDate && (
+ <Badge variant="default" className="text-[10px] px-1.5 py-0">
+ Max: {format(maxDate, 'MMM d, yy')}
+ </Badge>
+ )}
+ </div>
+ )}
+ </div>
+
+ <div className="flex justify-center items-center px-3 pt-3 pb-1">
+ <div className="flex items-center gap-1.5">
+ {onDateTimeSettings && (
+ <Button
+ size="icon-xs"
+ onClick={onDateTimeSettings}
+ aria-label="Date and time settings"
+ title="Date and time settings"
+ className="text-muted-foreground hover:text-foreground"
+ >
+ <SettingsIcon />
+ </Button>
+ )}
+ <SegmentedDateInput
+ date={selected}
+ maxDate={maxDate}
+ onChange={handleInputChange}
+ dateFormat={dateFormat}
+ showTime={includeTime}
+ />
+ </div>
+ </div>
+
+ <div className="p-2 flex justify-center">
+ <Calendar
+ defaultViewing={viewing}
+ startDate={selected}
+ endDate={selected}
+ minDate={minDate}
+ maxDate={maxDate}
+ onSelect={handleSelect}
+ onViewChange={setViewing}
+ weekStartsOn={weekStartsOn}
+ />
+ </div>
+
+ {showTime && (
+ <div className="flex items-center gap-2 px-3 py-1.5 border-t border-border">
+ <Switch
+ checked={includeTime}
+ onCheckedChange={setIncludeTime}
+ aria-label="Include time"
+ id={includeTimeId}
+ data-attr="date-picker-include-time"
+ />
+ <label htmlFor={includeTimeId} className="text-xs text-muted-foreground select-none">
+ Include time
+ </label>
+ </div>
+ )}
+
+ <Separator />
+
+ <div className="flex justify-end px-3 py-2 items-center gap-2 bg-muted/30">
+ <span className="text-[10px] text-muted-foreground tabular-nums mr-auto">{presentational}</span>
+ {onCancel ? (
+ <Button variant="outline" size="sm" onClick={onCancel} aria-label="Cancel" data-attr="date-picker-cancel">
+ Cancel
+ </Button>
+ ) : null}
+ <Button
+ variant="primary"
+ size="sm"
+ aria-label="Apply date"
+ title="Apply date"
+ onClick={handleApply}
+ data-attr="date-picker-apply"
+ >
+ Apply
+ </Button>
+ </div>
+ </div>
+ )
+}
diff --git a/packages/quill/packages/components/src/date-time-picker.tsx b/packages/quill/packages/components/src/date-time-picker.tsx
index 529cdac1869b..5e46965ed4d2 100644
--- a/packages/quill/packages/components/src/date-time-picker.tsx
+++ b/packages/quill/packages/components/src/date-time-picker.tsx
@@ -1,28 +1,15 @@
-import {
- addMonths,
- endOfDay,
- format,
- getDate,
- getDay,
- getDaysInMonth,
- getHours,
- getMinutes,
- getMonth,
- getYear,
- isAfter,
- isSameDay,
- isSameMonth,
- isToday,
- startOfDay,
- subMonths,
-} from 'date-fns'
-import { ArrowRight, ChevronLeft, ChevronRight, SettingsIcon } from 'lucide-react'
+import { endOfDay, format, getMonth, getYear, startOfDay, subMonths } from 'date-fns'
+import { ArrowRight, SettingsIcon } from 'lucide-react'
import * as React from 'react'
-import { Badge, Button, InputGroup, InputGroupNumberInput, ScrollArea, Select, SelectContent, SelectGroup, SelectItem, SelectTrigger, SelectValue, Separator, Tooltip, TooltipContent, TooltipProvider, TooltipTrigger, cn } from '@posthog/quill-primitives'
+import { Badge, Button, ScrollArea, Separator, cn } from '@posthog/quill-primitives'
+import { Calendar } from './calendar-grid'
import { CUSTOM_RANGE, type DateTimeRange, quickRanges } from './date-time-ranges'
-import { Day, useCalendar } from './use-calendar'
+import { SegmentedDateInput, type DateFormatOrder } from './segmented-date-input'
+import { Day } from './use-calendar'
+
+export type { DateFormatOrder } from './segmented-date-input'
const DATE_TIME_FORMATS: Record<DateFormatOrder, string> = {
MDY: 'MM/dd/yy HH:mm:ss',
@@ -30,28 +17,6 @@ const DATE_TIME_FORMATS: Record<DateFormatOrder, string> = {
YMD: 'yy-MM-dd HH:mm:ss',
}
-const DATE_FORMAT_LABELS: Record<DateFormatOrder, string> = {
- MDY: 'MM/DD/YY',
- DMY: 'DD/MM/YY',
- YMD: 'YY-MM-DD',
-}
-const POSTHOG_START_DATE = new Date(2020, 0, 23)
-const WEEK_DAYS = ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa']
-const MONTH_NAMES = [
- 'January',
- 'February',
- 'March',
- 'April',
- 'May',
- 'June',
- 'July',
- 'August',
- 'September',
- 'October',
- 'November',
- 'December',
-]
-
// Tailwind `lg` breakpoint — matches the `lg:` classes that switch this picker
// between a single calendar and the side-by-side dual-calendar layout.
const LG_QUERY = '(min-width: 64rem)'
@@ -74,8 +39,6 @@ export interface DateTimeValue {
range: DateTimeRange
}
-export type DateFormatOrder = 'MDY' | 'DMY' | 'YMD'
-
export interface DateTimePickerProps {
value: DateTimeValue
onApply: (value: DateTimeValue) => void
@@ -89,394 +52,6 @@ export interface DateTimePickerProps {
className?: string
}
-interface DayItemProps {
- day: Date
- startDate: Date
- endDate: Date
- viewing: Date
- minDate?: Date
- maxDate: Date
- onClick: (day: Date) => void
-}
-
-function DayItem({ day, startDate, endDate, viewing, minDate, maxDate, onClick }: DayItemProps): React.ReactElement {
- const outOfMonth = !isSameMonth(day, viewing)
- const dayDate = new Date(day.getFullYear(), day.getMonth(), day.getDate())
- const maxDay = new Date(maxDate.getFullYear(), maxDate.getMonth(), maxDate.getDate())
- const afterMax = dayDate.getTime() > maxDay.getTime()
- const beforeMin = minDate
- ? dayDate.getTime() < new Date(minDate.getFullYear(), minDate.getMonth(), minDate.getDate()).getTime()
- : false
- const disabled = outOfMonth || afterMax || beforeMin
-
- const isStart = !disabled && isSameDay(startDate, day)
- const isEnd = !disabled && isSameDay(endDate, day)
- const sameDay = isSameDay(startDate, endDate)
- const isBetween =
- !disabled &&
- !isStart &&
- !isEnd &&
- isAfter(day, startDate) &&
- !isAfter(day, endDate)
- const today = isToday(day)
-
- const label = format(day, 'dd')
-
- return (
- <div
- data-is-between={isBetween}
- data-is-start={isStart}
- data-is-end={isEnd}
- data-is-today={today}
- data-is-same-day={sameDay}
- className={cn(
- 'w-8 h-8 flex items-center justify-center',
- isBetween && 'bg-fill-selected',
- isStart && !sameDay && 'bg-fill-selected rounded-l-full',
- isEnd && !sameDay && 'bg-fill-selected rounded-r-full'
- )}
- >
- <Button
- variant={isStart || isEnd ? 'primary' : 'default'}
- size="icon-sm"
- disabled={disabled}
- aria-label={`Select ${format(day, 'PP')}`}
- title={disabled ? undefined : `Select ${format(day, 'PP')}`}
- onClick={() => onClick(day)}
- className={cn(
- 'w-full h-full !rounded-full p-0 text-[11px] tabular-nums',
- today && !isStart && !isEnd && 'border border-primary',
- disabled && 'opacity-20'
- )}
- >
- {label}
- </Button>
- </div>
- )
-}
-
-interface CalendarProps {
- defaultViewing: Date
- startDate: Date
- endDate: Date
- minDate?: Date
- maxDate: Date
- onSelect: (day: Date) => void
- onViewChange: (month: Date) => void
- siblingViewing?: Date
- weekStartsOn?: Day
-}
-
-function Calendar({
- defaultViewing,
- startDate,
- endDate,
- minDate,
- maxDate,
- onSelect,
- onViewChange,
- siblingViewing,
- weekStartsOn,
-}: CalendarProps): React.ReactElement {
- const { calendar, viewing, setViewing, viewPreviousMonth, viewNextMonth } = useCalendar({
- viewing: defaultViewing,
- weekStartsOn,
- })
-
- // Keep viewing in sync if parent-controlled defaultViewing changes (e.g. when
- // quick ranges reset both calendars).
- const lastDefaultRef = React.useRef(defaultViewing)
- React.useEffect(() => {
- if (!isSameMonth(lastDefaultRef.current, defaultViewing)) {
- setViewing(defaultViewing)
- lastDefaultRef.current = defaultViewing
- }
- }, [defaultViewing, setViewing])
-
- const handlePrev = (): void => {
- viewPreviousMonth()
- onViewChange(subMonths(viewing, 1))
- }
- const handleNext = (): void => {
- viewNextMonth()
- onViewChange(addMonths(viewing, 1))
- }
-
- const floorDate = minDate && minDate.getTime() > POSTHOG_START_DATE.getTime() ? minDate : POSTHOG_START_DATE
- const minYearVal = getYear(floorDate)
- const minMonthAtMinYear = getMonth(floorDate)
- const floorKey = minYearVal * 12 + minMonthAtMinYear
- const viewingKey = getYear(viewing) * 12 + getMonth(viewing)
-
- const disableNext =
- (getMonth(viewing) === getMonth(new Date()) && getYear(viewing) === getYear(new Date())) ||
- (!!siblingViewing &&
- getMonth(siblingViewing) === getMonth(addMonths(viewing, 1)) &&
- getYear(siblingViewing) === getYear(addMonths(viewing, 1)))
-
- const disablePrev =
- viewingKey <= floorKey ||
- (!!siblingViewing &&
- getMonth(siblingViewing) === getMonth(subMonths(viewing, 1)) &&
- getYear(siblingViewing) === getYear(subMonths(viewing, 1)))
- const maxYearVal = getYear(maxDate)
- const maxMonthAtMaxYear = getMonth(maxDate)
- const currentYear = getYear(viewing)
- const currentMonth = getMonth(viewing)
-
- const monthKey = (year: number, month: number): number => year * 12 + month
- const currentKey = monthKey(currentYear, currentMonth)
- const siblingKey = siblingViewing ? monthKey(getYear(siblingViewing), getMonth(siblingViewing)) : null
-
- const monthOptions: { key: number; year: number; month: number }[] = []
- for (let y = minYearVal; y <= maxYearVal; y++) {
- const startMonth = y === minYearVal ? minMonthAtMinYear : 0
- const endMonth = y === maxYearVal ? maxMonthAtMaxYear : 11
- for (let m = startMonth; m <= endMonth; m++) {
- monthOptions.push({ key: monthKey(y, m), year: y, month: m })
- }
- }
-
- const handleMonthYearSelect = (next: number): void => {
- const year = Math.floor(next / 12)
- const month = next % 12
- const nextDate = new Date(year, month, 1)
- setViewing(nextDate)
- onViewChange(nextDate)
- }
-
- return (
- <div>
- <div className="flex justify-center items-center py-1 gap-1">
- <Button
- variant="default"
- size="icon-sm"
- onClick={handlePrev}
- disabled={disablePrev}
- aria-label="Previous month"
- title={disablePrev ? 'Disabled' : 'Previous month'}
- className="disabled:cursor-not-allowed"
- >
- <ChevronLeft />
- </Button>
- <Select
- value={currentKey}
- onValueChange={(v) => {
- if (v !== null) {
- handleMonthYearSelect(v)
- }
- }}
- >
- <SelectTrigger size="sm" aria-label="Month and year" className="h-6 px-2 text-xs">
- <SelectValue>
- {(v: number) => `${MONTH_NAMES[v % 12]} ${Math.floor(v / 12)}`}
- </SelectValue>
- </SelectTrigger>
- <SelectContent>
- <SelectGroup>
- {monthOptions.map(({ key, year, month }) => (
- <SelectItem key={key} value={key} disabled={key === siblingKey}>
- {MONTH_NAMES[month]} {year}
- </SelectItem>
- ))}
- </SelectGroup>
- </SelectContent>
- </Select>
- <Button
- variant="default"
- size="icon-sm"
- onClick={handleNext}
- disabled={disableNext}
- aria-label="Next month"
- title={disableNext ? 'Disabled' : 'Next month'}
- className="disabled:cursor-not-allowed"
- >
- <ChevronRight />
- </Button>
- </div>
-
- <div className="grid grid-cols-7">
- {calendar[0][0].map((day) => (
- <div
- key={`h-${getDay(day)}`}
- className="w-8 h-6 flex items-center justify-center text-[10px] text-muted-foreground uppercase"
- >
- {WEEK_DAYS[getDay(day)]}
- </div>
- ))}
- </div>
-
- <div className="flex flex-col">
- {calendar[0].map((week, wi) => (
- <div key={`w-${wi}`} className="grid grid-cols-7">
- {week.map((day) => (
- <DayItem
- key={day.toISOString()}
- day={day}
- startDate={startDate}
- endDate={endDate}
- viewing={viewing}
- minDate={minDate}
- maxDate={maxDate}
- onClick={onSelect}
- />
- ))}
- </div>
- ))}
- </div>
- </div>
- )
-}
-
-interface DateTimeInputProps {
- date: Date
- maxDate: Date
- onChange: (date: Date) => void
- dateFormat: DateFormatOrder
-}
-
-const PAD_2 = { minimumIntegerDigits: 2 } as const
-
-function DateTimeInput({ date, maxDate, onChange, dateFormat }: DateTimeInputProps): React.ReactElement {
- const [month, setMonth] = React.useState(getMonth(date) + 1)
- const [day, setDay] = React.useState(getDate(date))
- const [year, setYear] = React.useState(getYear(date) % 100)
- const [hour, setHour] = React.useState(getHours(date))
- const [minute, setMinute] = React.useState(getMinutes(date))
-
- const onChangeRef = React.useRef(onChange)
- React.useEffect(() => {
- onChangeRef.current = onChange
- }, [onChange])
-
- React.useEffect(() => {
- setMonth(getMonth(date) + 1)
- setDay(getDate(date))
- setYear(getYear(date) % 100)
- setHour(getHours(date))
- setMinute(getMinutes(date))
- }, [date])
-
- const touched = React.useRef(false)
- React.useEffect(() => {
- if (!touched.current) {
- return
- }
- const handle = setTimeout(() => {
- onChangeRef.current(new Date(2000 + year, month - 1, day, hour, minute))
- touched.current = false
- }, 400)
- return () => clearTimeout(handle)
- }, [month, day, year, hour, minute])
-
- const set = (setter: React.Dispatch<React.SetStateAction<number>>) => (v: number | null) => {
- if (v === null) {
- return
- }
- touched.current = true
- setter(v)
- }
-
- const segmentClass = 'w-7 flex-none text-center tabular-nums p-0'
- const separatorClass = 'text-xs text-muted-foreground select-none'
- const sep = dateFormat === 'YMD' ? '-' : '/'
-
- const maxYear = getYear(maxDate) % 100
- const atMaxYear = year === maxYear
- const maxMonth = atMaxYear ? getMonth(maxDate) + 1 : 12
- const atMaxMonth = atMaxYear && month === maxMonth
- const maxDay = atMaxMonth ? getDate(maxDate) : getDaysInMonth(new Date(2000 + year, month - 1))
-
- const monthSegment = (
- <InputGroupNumberInput
- key="month"
- aria-label="Month"
- value={month}
- onValueChange={set(setMonth)}
- min={1}
- max={maxMonth}
- format={PAD_2}
- className={segmentClass}
- />
- )
- const daySegment = (
- <InputGroupNumberInput
- key="day"
- aria-label="Day"
- value={day}
- onValueChange={set(setDay)}
- min={1}
- max={maxDay}
- format={PAD_2}
- className={segmentClass}
- />
- )
- const yearSegment = (
- <InputGroupNumberInput
- key="year"
- aria-label="Year"
- value={year}
- onValueChange={set(setYear)}
- min={0}
- max={maxYear}
- format={PAD_2}
- className={segmentClass}
- />
- )
-
- const dateSegments =
- dateFormat === 'DMY'
- ? [daySegment, monthSegment, yearSegment]
- : dateFormat === 'YMD'
- ? [yearSegment, monthSegment, daySegment]
- : [monthSegment, daySegment, yearSegment]
-
- return (
- <TooltipProvider>
- <div className="flex items-center gap-2">
- <Tooltip>
- <TooltipTrigger render={
- <InputGroup className="w-auto px-1.5">
- {dateSegments.map((segment, i) => (
- <React.Fragment key={i}>
- {i > 0 && <span className={separatorClass}>{sep}</span>}
- {segment}
- </React.Fragment>
- ))}
- </InputGroup>
- }/>
- <TooltipContent>
- {DATE_FORMAT_LABELS[dateFormat]}
- </TooltipContent>
- </Tooltip>
-
-
- <InputGroup className="w-auto px-1.5">
- <InputGroupNumberInput
- aria-label="Hour"
- value={hour}
- onValueChange={set(setHour)}
- min={0}
- max={23}
- format={PAD_2}
- className={segmentClass}
- />
- <span className={separatorClass}>:</span>
- <InputGroupNumberInput
- aria-label="Minute"
- value={minute}
- onValueChange={set(setMinute)}
- min={0}
- max={59}
- format={PAD_2}
- className={segmentClass}
- />
- </InputGroup>
- </div>
- </TooltipProvider>
-
- )
-}
export function DateTimePicker({
value,
@@ -652,9 +227,9 @@ export function DateTimePicker({
<SettingsIcon />
</Button>
)}
- <DateTimeInput date={start} maxDate={maxDate} onChange={handleStartChange} dateFormat={dateFormat} />
+ <SegmentedDateInput date={start} maxDate={maxDate} onChange={handleStartChange} dateFormat={dateFormat} showTime />
<span className="text-xs text-muted-foreground">to</span>
- <DateTimeInput date={end} maxDate={maxDate} onChange={handleEndChange} dateFormat={dateFormat} />
+ <SegmentedDateInput date={end} maxDate={maxDate} onChange={handleEndChange} dateFormat={dateFormat} showTime />
<Button
variant="link"
size="xs"
diff --git a/packages/quill/packages/components/src/index.ts b/packages/quill/packages/components/src/index.ts
index 9e390aedb3a8..4d03c3d8fad6 100644
--- a/packages/quill/packages/components/src/index.ts
+++ b/packages/quill/packages/components/src/index.ts
@@ -10,5 +10,6 @@
export { DataTable, type DataTableProps } from './data-table'
export { DateTimePicker, type DateTimePickerProps, type DateTimeValue, type DateFormatOrder } from './date-time-picker'
+export { DatePicker, type DatePickerProps } from './date-picker'
export { quickRanges, CUSTOM_RANGE, type DateTimeRange, type DateTimeRangeName } from './date-time-ranges'
export { useCalendar, Day, Month, type UseCalendarOptions, type UseCalendarReturn } from './use-calendar'
diff --git a/packages/quill/packages/components/src/segmented-date-input.tsx b/packages/quill/packages/components/src/segmented-date-input.tsx
new file mode 100644
index 000000000000..5eb82ec7a861
--- /dev/null
+++ b/packages/quill/packages/components/src/segmented-date-input.tsx
@@ -0,0 +1,184 @@
+import { getDate, getDaysInMonth, getHours, getMinutes, getMonth, getYear } from 'date-fns'
+import * as React from 'react'
+
+import {
+ InputGroup,
+ InputGroupNumberInput,
+ Tooltip,
+ TooltipContent,
+ TooltipProvider,
+ TooltipTrigger,
+} from '@posthog/quill-primitives'
+
+export type DateFormatOrder = 'MDY' | 'DMY' | 'YMD'
+
+export const DATE_FORMAT_LABELS: Record<DateFormatOrder, string> = {
+ MDY: 'MM/DD/YY',
+ DMY: 'DD/MM/YY',
+ YMD: 'YY-MM-DD',
+}
+
+const PAD_2 = { minimumIntegerDigits: 2 } as const
+
+export interface SegmentedDateInputProps {
+ date: Date
+ maxDate: Date
+ onChange: (date: Date) => void
+ dateFormat: DateFormatOrder
+ /** Show hour/minute segments alongside the date. */
+ showTime: boolean
+}
+
+/**
+ * Segmented numeric date (+ optional time) entry shared by DatePicker and DateTimePicker.
+ * Edits are debounced before committing so partially-typed values don't fire onChange.
+ * Time-format follow-ups (12/24h, granularity) belong here so both pickers inherit them.
+ */
+export function SegmentedDateInput({
+ date,
+ maxDate,
+ onChange,
+ dateFormat,
+ showTime,
+}: SegmentedDateInputProps): React.ReactElement {
+ const [month, setMonth] = React.useState(getMonth(date) + 1)
+ const [day, setDay] = React.useState(getDate(date))
+ const [year, setYear] = React.useState(getYear(date) % 100)
+ const [hour, setHour] = React.useState(getHours(date))
+ const [minute, setMinute] = React.useState(getMinutes(date))
+
+ const onChangeRef = React.useRef(onChange)
+ React.useEffect(() => {
+ onChangeRef.current = onChange
+ }, [onChange])
+
+ React.useEffect(() => {
+ setMonth(getMonth(date) + 1)
+ setDay(getDate(date))
+ setYear(getYear(date) % 100)
+ setHour(getHours(date))
+ setMinute(getMinutes(date))
+ }, [date])
+
+ const touched = React.useRef(false)
+ React.useEffect(() => {
+ if (!touched.current) {
+ return
+ }
+ const handle = setTimeout(() => {
+ // Two-digit year input: 2000-2099 only. Acceptable for analytics date ranges;
+ // a value outside that window can't be entered via the segments (silent, not an error).
+ onChangeRef.current(new Date(2000 + year, month - 1, day, hour, minute))
+ touched.current = false
+ }, 400)
+ return () => clearTimeout(handle)
+ }, [month, day, year, hour, minute])
+
+ const set = (setter: React.Dispatch<React.SetStateAction<number>>) => (v: number | null) => {
+ if (v === null) {
+ return
+ }
+ touched.current = true
+ setter(v)
+ }
+
+ const segmentClass = 'w-7 flex-none text-center tabular-nums p-0'
+ const separatorClass = 'text-xs text-muted-foreground select-none'
+ const sep = dateFormat === 'YMD' ? '-' : '/'
+
+ const maxYear = getYear(maxDate) % 100
+ const atMaxYear = year === maxYear
+ const maxMonth = atMaxYear ? getMonth(maxDate) + 1 : 12
+ const atMaxMonth = atMaxYear && month === maxMonth
+ const maxDay = atMaxMonth ? getDate(maxDate) : getDaysInMonth(new Date(2000 + year, month - 1))
+
+ const monthSegment = (
+ <InputGroupNumberInput
+ key="month"
+ aria-label="Month"
+ value={month}
+ onValueChange={set(setMonth)}
+ min={1}
+ max={maxMonth}
+ format={PAD_2}
+ className={segmentClass}
+ />
+ )
+ const daySegment = (
+ <InputGroupNumberInput
+ key="day"
+ aria-label="Day"
+ value={day}
+ onValueChange={set(setDay)}
+ min={1}
+ max={maxDay}
+ format={PAD_2}
+ className={segmentClass}
+ />
+ )
+ const yearSegment = (
+ <InputGroupNumberInput
+ key="year"
+ aria-label="Year"
+ value={year}
+ onValueChange={set(setYear)}
+ min={0}
+ max={maxYear}
+ format={PAD_2}
+ className={segmentClass}
+ />
+ )
+
+ const dateSegments =
+ dateFormat === 'DMY'
+ ? [daySegment, monthSegment, yearSegment]
+ : dateFormat === 'YMD'
+ ? [yearSegment, monthSegment, daySegment]
+ : [monthSegment, daySegment, yearSegment]
+
+ return (
+ <TooltipProvider>
+ <div className="flex items-center gap-2">
+ <Tooltip>
+ <TooltipTrigger
+ render={
+ <InputGroup className="w-auto px-1.5">
+ {dateSegments.map((segment, i) => (
+ <React.Fragment key={i}>
+ {i > 0 && <span className={separatorClass}>{sep}</span>}
+ {segment}
+ </React.Fragment>
+ ))}
+ </InputGroup>
+ }
+ />
+ <TooltipContent>{DATE_FORMAT_LABELS[dateFormat]}</TooltipContent>
+ </Tooltip>
+
+ {showTime && (
+ <InputGroup className="w-auto px-1.5">
+ <InputGroupNumberInput
+ aria-label="Hour"
+ value={hour}
+ onValueChange={set(setHour)}
+ min={0}
+ max={23}
+ format={PAD_2}
+ className={segmentClass}
+ />
+ <span className={separatorClass}>:</span>
+ <InputGroupNumberInput
+ aria-label="Minute"
+ value={minute}
+ onValueChange={set(setMinute)}
+ min={0}
+ max={59}
+ format={PAD_2}
+ className={segmentClass}
+ />
+ </InputGroup>
+ )}
+ </div>
+ </TooltipProvider>
+ )
+}