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): custom ranges and embed props for DateTimePicker (#68398)

Sam Pennington6814fb2db417
diff --git a/packages/quill/packages/components/AGENTS.md b/packages/quill/packages/components/AGENTS.md
index 154e6f09d7ed..6e7f77fbb54e 100644
--- a/packages/quill/packages/components/AGENTS.md
+++ b/packages/quill/packages/components/AGENTS.md
@@ -59,11 +59,13 @@ import { CUSTOM_RANGE, DateTimePicker, quickRanges } from '@posthog/quill-compon
Rules:
-- `value.range` is one of `quickRanges` (15 presets, "Last 5 minutes" → "Last 1 year") or `CUSTOM_RANGE` for manual selection.
+- `value.range` is one of the offered presets or `CUSTOM_RANGE` for manual selection. Presets default to `quickRanges` (15 presets, "Last 5 minutes" → "Last 1 year"); pass `ranges` to offer your own `DateTimeRange[]` (any `CUSTOM_RANGE` entry is filtered out of the list). `ranges={[]}` hides the quick-ranges column entirely — for hosts that render their own preset list.
+- A `DateTimeRange` computes its start from "now" via `rangeSetter`; the end defaults to "now" unless `endSetter` is given (needed for closed periods like "Last month"). `name` is any string.
- Changes are staged until `onApply` fires — don't treat intermediate calendar clicks as committed.
- Dual-calendar layout appears at the `lg` breakpoint unless `compact` forces a single calendar.
- `minDate`/`maxDate` are day-granular; time inputs are independent of those bounds.
- `weekStartsOn` affects the calendar grid only, not quick-range math.
+- Embedding in a host surface (e.g. inside a popover with the host's own sections): `showHeader={false}` drops the caps header band, `showTime={false}` switches to day-granular mode (no time segments, no "Now", date-only footer readout) — pair with a `className` that strips the card chrome (`shadow-none ring-0`).
## DatePicker
diff --git a/packages/quill/packages/components/src/date-time-picker.stories.tsx b/packages/quill/packages/components/src/date-time-picker.stories.tsx
index 466b09bffc7a..ddd023055799 100644
--- a/packages/quill/packages/components/src/date-time-picker.stories.tsx
+++ b/packages/quill/packages/components/src/date-time-picker.stories.tsx
@@ -1,11 +1,21 @@
import type { Meta, StoryObj } from '@storybook/react'
-import { format, subDays } from 'date-fns'
+import {
+ endOfMonth,
+ endOfQuarter,
+ format,
+ startOfMonth,
+ startOfQuarter,
+ startOfYear,
+ subDays,
+ subMonths,
+ subQuarters,
+} from 'date-fns'
import { Clock } from 'lucide-react'
import * as React from 'react'
import { Button, Popover, PopoverContent, PopoverTrigger } from '@posthog/quill-primitives'
-import { CUSTOM_RANGE } from './date-time-ranges'
+import { CUSTOM_RANGE, type DateTimeRange } from './date-time-ranges'
import { DateTimePicker, type DateTimeValue } from './date-time-picker'
import { quickRanges } from './date-time-ranges'
import { Day } from './use-calendar'
@@ -150,6 +160,47 @@ export const WithSettingsLink: Story = {
},
}
+const calendarRanges: DateTimeRange[] = [
+ { id: 1, name: 'This month', rangeSetter: (d) => startOfMonth(d) },
+ { id: 2, name: 'Last month', rangeSetter: (d) => startOfMonth(subMonths(d, 1)), endSetter: (d) => endOfMonth(subMonths(d, 1)) },
+ { id: 3, name: 'This quarter', rangeSetter: (d) => startOfQuarter(d) },
+ { id: 4, name: 'Last quarter', rangeSetter: (d) => startOfQuarter(subQuarters(d, 1)), endSetter: (d) => endOfQuarter(subQuarters(d, 1)) },
+ { id: 5, name: 'Year to date', rangeSetter: (d) => startOfYear(d) },
+]
+
+export const CustomRanges: Story = {
+ args: baseArgs,
+ render: () => {
+ const [value, setValue] = React.useState<DateTimeValue>({
+ start: startOfMonth(new Date()),
+ end: new Date(),
+ range: calendarRanges[0],
+ })
+ return <DateTimePicker value={value} onApply={setValue} ranges={calendarRanges} />
+ },
+}
+
+export const EmbeddedDayGranular: Story = {
+ args: baseArgs,
+ render: () => {
+ const [value, setValue] = React.useState<DateTimeValue>({
+ start: startOfMonth(new Date()),
+ end: new Date(),
+ range: calendarRanges[0],
+ })
+ return (
+ <DateTimePicker
+ value={value}
+ onApply={setValue}
+ ranges={calendarRanges}
+ showHeader={false}
+ showTime={false}
+ className="shadow-none ring-1"
+ />
+ )
+ },
+}
+
export const WeekStartsThursday: Story = {
args: baseArgs,
render: () => {
diff --git a/packages/quill/packages/components/src/date-time-picker.test.tsx b/packages/quill/packages/components/src/date-time-picker.test.tsx
index 3af7fbbe4a69..e90924513e4b 100644
--- a/packages/quill/packages/components/src/date-time-picker.test.tsx
+++ b/packages/quill/packages/components/src/date-time-picker.test.tsx
@@ -1,7 +1,7 @@
import { cleanup, render, screen } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
-import { CUSTOM_RANGE } from './date-time-ranges'
+import { CUSTOM_RANGE, type DateTimeRange } from './date-time-ranges'
import { DateTimePicker } from './date-time-picker'
// The day grid wraps each Button in a div carrying the data-is-* range flags.
@@ -44,4 +44,36 @@ describe('DateTimePicker', () => {
expect([applied.start.getDate(), applied.end.getDate()]).toEqual([10, 20])
expect(applied.range).toBe(CUSTOM_RANGE)
})
+
+ it('renders the provided ranges instead of the default quick ranges', () => {
+ const ranges: DateTimeRange[] = [
+ { id: 1, name: 'This month', rangeSetter: (d) => d },
+ { id: 2, name: 'Year to date', rangeSetter: (d) => d },
+ ]
+ render(<DateTimePicker value={VALUE} maxDate={MAX} onApply={jest.fn()} ranges={ranges} />)
+
+ expect(screen.getByTitle('This month')).toBeTruthy()
+ expect(screen.getByTitle('Year to date')).toBeTruthy()
+ expect(screen.queryByTitle('Last 7 days')).toBeNull()
+ })
+
+ it('applies both edges from a preset with an endSetter', async () => {
+ const onApply = jest.fn()
+ const lastMonth: DateTimeRange = {
+ id: 1,
+ name: 'Last month',
+ rangeSetter: () => new Date(2022, 11, 1),
+ endSetter: () => new Date(2022, 11, 31, 23, 59, 59),
+ }
+ render(<DateTimePicker value={VALUE} maxDate={MAX} onApply={onApply} ranges={[lastMonth]} />)
+
+ await userEvent.click(screen.getByTitle('Last month'))
+ await userEvent.click(screen.getByLabelText('Apply date range'))
+
+ expect(onApply).toHaveBeenCalledTimes(1)
+ const applied = onApply.mock.calls[0][0]
+ expect(applied.start).toEqual(new Date(2022, 11, 1))
+ expect(applied.end).toEqual(new Date(2022, 11, 31, 23, 59, 59))
+ expect(applied.range).toBe(lastMonth)
+ })
})
diff --git a/packages/quill/packages/components/src/date-time-picker.tsx b/packages/quill/packages/components/src/date-time-picker.tsx
index 5e46965ed4d2..4b099ec57e68 100644
--- a/packages/quill/packages/components/src/date-time-picker.tsx
+++ b/packages/quill/packages/components/src/date-time-picker.tsx
@@ -17,6 +17,12 @@ const DATE_TIME_FORMATS: Record<DateFormatOrder, string> = {
YMD: 'yy-MM-dd HH:mm:ss',
}
+const DATE_FORMATS: Record<DateFormatOrder, string> = {
+ MDY: 'MM/dd/yy',
+ DMY: 'dd/MM/yy',
+ YMD: 'yy-MM-dd',
+}
+
// 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)'
@@ -49,6 +55,12 @@ export interface DateTimePickerProps {
weekStartsOn?: Day
onDateTimeSettings?: () => void
compact?: boolean
+ /** Quick-range presets to offer. Defaults to `quickRanges`; `CUSTOM_RANGE` entries are filtered out. */
+ ranges?: DateTimeRange[]
+ /** Hide the "Choose date range / Quick ranges" header band when embedding in a host surface. */
+ showHeader?: boolean
+ /** Day-granular mode: hides the time segments and "Now", and drops time from the footer readout. */
+ showTime?: boolean
className?: string
}
@@ -63,8 +75,13 @@ export function DateTimePicker({
weekStartsOn,
onDateTimeSettings,
compact = false,
+ ranges = quickRanges,
+ showHeader = true,
+ showTime = true,
className,
}: DateTimePickerProps): React.ReactElement {
+ const presetRanges = ranges.filter((r) => r.id !== CUSTOM_RANGE.id)
+ const hasPresets = presetRanges.length > 0
const maxDate = maxDateProp ?? new Date()
const hasExplicitMaxDate = maxDateProp !== undefined
// The second calendar only renders at `lg`; below it (and in compact) there's
@@ -166,15 +183,16 @@ export function DateTimePicker({
const handleQuickRange = (next: DateTimeRange): void => {
const now = new Date()
const nextStart = next.rangeSetter(now)
+ const nextEnd = next.endSetter?.(now) ?? now
setStart(nextStart)
- setEnd(now)
+ setEnd(nextEnd)
setRange(next)
setLastSet(null)
- setRightViewing(now)
- setLeftViewing(subMonths(now, 1))
+ setRightViewing(nextEnd)
+ setLeftViewing(subMonths(nextEnd, 1))
}
- const dateTimeFormat = DATE_TIME_FORMATS[dateFormat]
+ const dateTimeFormat = showTime ? DATE_TIME_FORMATS[dateFormat] : DATE_FORMATS[dateFormat]
const presentationalStart = format(start, dateTimeFormat)
const presentationalEnd = format(end, dateTimeFormat)
@@ -187,8 +205,8 @@ export function DateTimePicker({
)}
>
{/* Headers */}
- {!compact && (
- <div className="hidden lg:grid lg:grid-cols-[minmax(0,1fr)_9rem]">
+ {!compact && showHeader && (
+ <div className={hasPresets ? 'hidden lg:grid lg:grid-cols-[minmax(0,1fr)_9rem]' : 'hidden lg:grid'}>
<div className="flex items-center gap-2 px-2 py-1 bg-muted/30 border-b border-border rounded-tl-lg">
<span className="text-[10px] text-muted-foreground uppercase tracking-wide">Choose date range</span>
{(minDate || hasExplicitMaxDate) && (
@@ -199,14 +217,16 @@ export function DateTimePicker({
</div>
)}
</div>
- <div className="flex justify-start px-2 py-1 bg-muted/30 border-b border-l border-border rounded-tr-lg">
- <span className="text-[10px] text-muted-foreground uppercase tracking-wide">Quick ranges</span>
- </div>
+ {hasPresets && (
+ <div className="flex justify-start px-2 py-1 bg-muted/30 border-b border-l border-border rounded-tr-lg">
+ <span className="text-[10px] text-muted-foreground uppercase tracking-wide">Quick ranges</span>
+ </div>
+ )}
</div>
)}
{/* Body */}
- <div className={compact
+ <div className={compact || !hasPresets
? 'flex flex-col'
: 'flex flex-col lg:grid lg:grid-cols-[minmax(0,1fr)_9rem]'
}>
@@ -227,18 +247,20 @@ export function DateTimePicker({
<SettingsIcon />
</Button>
)}
- <SegmentedDateInput date={start} maxDate={maxDate} onChange={handleStartChange} dateFormat={dateFormat} showTime />
+ <SegmentedDateInput date={start} maxDate={maxDate} onChange={handleStartChange} dateFormat={dateFormat} showTime={showTime} />
<span className="text-xs text-muted-foreground">to</span>
- <SegmentedDateInput date={end} maxDate={maxDate} onChange={handleEndChange} dateFormat={dateFormat} showTime />
- <Button
- variant="link"
- size="xs"
- onClick={handleNow}
- aria-label="Set end to now"
- title="Set end to now"
- >
- Now
- </Button>
+ <SegmentedDateInput date={end} maxDate={maxDate} onChange={handleEndChange} dateFormat={dateFormat} showTime={showTime} />
+ {showTime && (
+ <Button
+ variant="link"
+ size="xs"
+ onClick={handleNow}
+ aria-label="Set end to now"
+ title="Set end to now"
+ >
+ Now
+ </Button>
+ )}
</div>
</div>
)}
@@ -280,6 +302,7 @@ export function DateTimePicker({
</div>
{/* Quick ranges column */}
+ {hasPresets && (
<div className={compact
? 'order-0 border-b border-border'
: 'order-0 lg:order-none lg:relative lg:border-l lg:border-border border-b border-border lg:border-b-0'
@@ -289,7 +312,7 @@ export function DateTimePicker({
? 'flex flex-row p-2 gap-px max-h-[320px]'
: 'flex flex-row lg:flex-col p-2 gap-px max-h-[320px]'
}>
- {quickRanges.slice(1).map((quick) => (
+ {presetRanges.map((quick) => (
<li key={quick.id} className={compact ? undefined : 'lg:w-full'}>
<Button
variant="default"
@@ -312,6 +335,7 @@ export function DateTimePicker({
</ul>
</ScrollArea>
</div>
+ )}
</div>
<Separator />
@@ -319,7 +343,7 @@ export function DateTimePicker({
{/* Actions */}
<div className="flex justify-end px-3 py-2 items-center gap-2 bg-muted/30">
<span className="text-[10px] text-muted-foreground flex items-center gap-1 tabular-nums mr-auto">
- {range.name === 'Custom' ? <>{presentationalStart} <ArrowRight className="size-3" /> {presentationalEnd}</> : range.name}
+ {range.id === CUSTOM_RANGE.id ? <>{presentationalStart} <ArrowRight className="size-3" /> {presentationalEnd}</> : range.name}
</span>
{onCancel ? (
<Button variant="outline" size="sm" onClick={onCancel} aria-label="Cancel" data-attr="date-time-picker-cancel">
diff --git a/packages/quill/packages/components/src/date-time-ranges.ts b/packages/quill/packages/components/src/date-time-ranges.ts
index 3861b1dec0c3..59cb3acfc970 100644
--- a/packages/quill/packages/components/src/date-time-ranges.ts
+++ b/packages/quill/packages/components/src/date-time-ranges.ts
@@ -1,27 +1,14 @@
import { subDays, subHours, subMinutes, subMonths, subYears } from 'date-fns'
-export type DateTimeRangeName =
- | 'Custom'
- | 'Last 5 minutes'
- | 'Last 15 minutes'
- | 'Last 30 minutes'
- | 'Last 1 hour'
- | 'Last 3 hours'
- | 'Last 6 hours'
- | 'Last 12 hours'
- | 'Last 24 hours'
- | 'Last 2 days'
- | 'Last 7 days'
- | 'Last 30 days'
- | 'Last 90 days'
- | 'Last 6 months'
- | 'Last 1 year'
- | 'Last 2 years'
+export type DateTimeRangeName = string
export interface DateTimeRange {
id: number
name: DateTimeRangeName
+ /** Returns the range's start for a given "now". */
rangeSetter: (date: Date) => Date
+ /** Returns the range's end for a given "now". Defaults to "now" itself. */
+ endSetter?: (date: Date) => Date
}
export const CUSTOM_RANGE: DateTimeRange = {