diff --git a/client/src/components/Journey/JourneyDetailPageDatePicker.test.tsx b/client/src/components/Journey/JourneyDetailPageDatePicker.test.tsx index 77fb7ccee..4cd1f244c 100644 --- a/client/src/components/Journey/JourneyDetailPageDatePicker.test.tsx +++ b/client/src/components/Journey/JourneyDetailPageDatePicker.test.tsx @@ -1,4 +1,4 @@ -// FE-COMP-JOURNEYDATEPICKER-001 to FE-COMP-JOURNEYDATEPICKER-007 +// FE-COMP-JOURNEYDATEPICKER-001 to FE-COMP-JOURNEYDATEPICKER-008 import React from 'react' import { describe, it, expect, vi } from 'vitest' import { render, screen } from '../../../tests/helpers/render' @@ -119,3 +119,28 @@ describe('DatePicker week start (#2029)', () => { } }) }) + +describe('DatePicker inside a clipping dialog (#2858)', () => { + it('FE-COMP-JOURNEYDATEPICKER-008: the calendar floats over the page, stays inside the viewport and closes on a click away', async () => { + const user = userEvent.setup() + const { container } = render( +
+ +
, + ) + const trigger = screen.getByText(/Mar 15, 2026/).closest('button') as HTMLButtonElement + // A trigger at the right edge, where the end date of a range sits. + trigger.getBoundingClientRect = () => ({ top: 100, bottom: 136, left: window.innerWidth - 120, right: window.innerWidth - 8, width: 112, height: 36, x: 0, y: 0, toJSON: () => ({}) }) + await user.click(trigger) + + const day31 = screen.getByRole('button', { name: '31' }) + // Rendered outside the dialog that would clip it. + expect(container.contains(day31)).toBe(false) + const panel = day31.closest('.fixed') as HTMLElement + expect(panel.style.top).toBe('140px') + expect(parseFloat(panel.style.left)).toBe(window.innerWidth - 280 - 8) + + await user.click(document.querySelector('[role="presentation"]') as HTMLElement) + expect(screen.queryByRole('button', { name: '31' })).toBeNull() + }) +}) diff --git a/client/src/components/Journey/JourneyDetailPageDatePicker.tsx b/client/src/components/Journey/JourneyDetailPageDatePicker.tsx index 9df012055..8031ada56 100644 --- a/client/src/components/Journey/JourneyDetailPageDatePicker.tsx +++ b/client/src/components/Journey/JourneyDetailPageDatePicker.tsx @@ -1,9 +1,11 @@ -import { useState } from 'react' +import { useRef, useState } from 'react' +import { createPortal } from 'react-dom' import { localIsoDate } from '../../utils/localDate' import { ArrowLeft, ChevronRight, Calendar } from 'lucide-react' import { useTranslation } from '../../i18n' import { useWeekStartDay } from '../../hooks/useWeekStartDay' import { leadingBlanks, weekdayLabels } from '../../utils/calendarWeek' +import { useAnchoredPosition } from '../../hooks/useAnchoredPosition' /** * The header steps up a level on each click, days → months → years, and a @@ -14,6 +16,9 @@ import { leadingBlanks, weekdayLabels } from '../../utils/calendarWeek' */ type CalendarView = 'days' | 'months' | 'years' const YEAR_PAGE_SIZE = 12 +/** The calendar's width; `w-[280px]` below. */ +const PANEL_WIDTH = 280 +const VIEWPORT_PAD = 8 export function DatePicker({ value, onChange, tripDates }: { value: string @@ -23,6 +28,13 @@ export function DatePicker({ value, onChange, tripDates }: { const { t, locale } = useTranslation() const weekStart = useWeekStartDay() const [open, setOpen] = useState(false) + const triggerRef = useRef(null) + // The calendar used to hang absolutely under its trigger, inside dialogs that clip + // their content: the photo picker cut off the last week and, for the end date in + // the right-hand column, the last weekday (#2858). It now floats over the page, + // pinned to the trigger, flipped above it when below is too short and kept inside + // the viewport sideways. + const anchored = useAnchoredPosition(triggerRef, open, { estimatedHeight: 340, matchWidth: false }) const [view, setView] = useState('days') const [yearPageStart, setYearPageStart] = useState(0) const [viewMonth, setViewMonth] = useState(() => { @@ -107,6 +119,7 @@ export function DatePicker({ value, onChange, tripDates }: { return (
- {open && ( + {open && anchored && createPortal( <> {/* Click-away catcher — no semantics of its own; the trigger button above closes the popover again from the keyboard. */} -
setOpen(false)} /> -
+
setOpen(false)} /> +
{/* Header: arrows step the current view, the label climbs to the next one up */}
) @@ -209,7 +230,8 @@ export function DatePicker({ value, onChange, tripDates }: { )}
- + , + document.body, )}
) diff --git a/client/src/components/Journey/JourneyDetailPageProviderPicker.test.tsx b/client/src/components/Journey/JourneyDetailPageProviderPicker.test.tsx index 071ec2c98..3ccc30ac9 100644 --- a/client/src/components/Journey/JourneyDetailPageProviderPicker.test.tsx +++ b/client/src/components/Journey/JourneyDetailPageProviderPicker.test.tsx @@ -375,11 +375,12 @@ describe('ProviderPicker', () => { await user.click(screen.getByRole('button', { name: 'Search' })) expect(ranges).toHaveLength(0) + // The calendar opens in a portal, so its days are looked up on the page, not under the trigger. const [fromPicker, toPicker] = Array.from(baseElement.querySelectorAll('.flex-1 > .relative')) as HTMLElement[] await user.click(within(fromPicker).getByRole('button')) - await user.click(within(fromPicker).getByRole('button', { name: '3' })) + await user.click(screen.getByRole('button', { name: '3' })) await user.click(within(toPicker).getByRole('button')) - await user.click(within(toPicker).getByRole('button', { name: '9' })) + await user.click(screen.getByRole('button', { name: '9' })) await user.click(screen.getByRole('button', { name: 'Search' })) await waitFor(() => expect(ranges).toHaveLength(1))