Files
cal.diy/apps/web/modules/bookings/components/DatePicker.tsx
T
8c39210a12 fix: scroll issues in Embed (#26583)
* chore: add new view for two step slot selection for embed

* fix: make sure two step slot selection is false by default

* chore: update embed playground to showcase two step slot selection

* chore: add tests

* chore: update embed snippet generator code to include two step slot selection

* chore: update docs

* fix: back button styling

* chore: implement feedback from cubic

* fix: add missing isEnableTwoStepSlotSelectionVisible properties to test mock store

Co-Authored-By: unknown <>

* chore: implement PR feedback

* chore: update slot selection modal

* chore: add prop to hide available times header

* fix: scope two-step slot selection visibility to mobile only

Restores the isMobile check in the timeslot visibility guard to ensure
desktop embeds continue to show the booking UI when two-step slot
selection is enabled. The feature should only hide the timeslot list
on mobile devices.

Addresses Cubic AI review feedback (confidence 9/10).

Co-Authored-By: unknown <>

* fixup: use translations for loading instead of actual word

* fix: type check

* fix: add window.matchMedia mock for jsdom test environment

Co-Authored-By: [email protected] <[email protected]>

* fix: add window.matchMedia mock to packages/testing/src/setupVitest.ts

Co-Authored-By: [email protected] <[email protected]>

* fix: unit tests failing

* add a width style to the two-step slot selection embed container.

* refactor: rename enableTwoStepSlotSelection to useSlotsViewOnSmallScreen

- Rename external-facing embed config option from enableTwoStepSlotSelection to useSlotsViewOnSmallScreen
- Update URL parameter parsing in embed-iframe.ts
- Update type definitions in types.ts and index.d.ts
- Update internal variable names to slotsViewOnSmallScreen for consistency
- Update documentation, playground examples, and tests

Co-Authored-By: [email protected] <[email protected]>

* Add config to prefill all booking fields so that slot has confirm button along with it

* chore: implement PR feedback

* fix: move twoStepSlotSelection embed outside misc-embeds div to fix e2e test

The e2e test was failing because the misc-embeds div content was
intercepting pointer events on mobile viewport. This follows the same
pattern used for skeletonDemo - the embed is now outside misc-embeds
and the div is hidden when testing this namespace.

Co-Authored-By: [email protected] <[email protected]>

* fix: prevent pointer event interception in twoStepSlotSelection embed

Hide heading and note elements and set pointer-events: none on container
elements while keeping pointer-events: auto on the iframe container.
This prevents the container from intercepting clicks on the iframe
during mobile viewport e2e tests.

Co-Authored-By: [email protected] <[email protected]>

* fix: hide all non-essential content for twoStepSlotSelection e2e test

Co-Authored-By: [email protected] <[email protected]>

* fix: disable pointer-events on body for twoStepSlotSelection e2e test

Co-Authored-By: [email protected] <[email protected]>

* fix: remove pointer-events: auto on container to let clicks pass through to iframe

Co-Authored-By: [email protected] <[email protected]>

* fix: explicitly set pointer-events: none on container and inline-embed-container

Co-Authored-By: [email protected] <[email protected]>

* fix: also set pointer-events: none on html element to prevent interception

Co-Authored-By: [email protected] <[email protected]>

* fix: make .place div fill viewport for twoStepSlotSelection e2e test

Co-Authored-By: [email protected] <[email protected]>

* fix: failing embed tests

* fixup

* fixup fixup

* fix: failing tests

* fix: update checks for verifying prefilled date

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: Peer Richelsen <[email protected]>
Co-authored-by: Hariom Balhara <[email protected]>
2026-01-21 15:44:33 -03:00

176 lines
5.6 KiB
TypeScript

import { shallow } from "zustand/shallow";
import type { Dayjs } from "@calcom/dayjs";
import dayjs from "@calcom/dayjs";
import { useBookerStoreContext } from "@calcom/features/bookings/Booker/BookerStoreProvider";
import type { DatePickerClassNames } from "@calcom/features/bookings/Booker/types";
import { DatePicker as DatePickerComponent } from "@calcom/features/calendars/DatePicker";
import { useNonEmptyScheduleDays } from "@calcom/features/schedules/lib/use-schedule/useNonEmptyScheduleDays";
import { weekdayToWeekIndex } from "@calcom/lib/dayjs";
import { useLocale } from "@calcom/lib/hooks/useLocale";
import type { User } from "@calcom/prisma/client";
import type { PeriodData } from "@calcom/types/Event";
import { useSlotsViewOnSmallScreen } from "@calcom/features/bookings/Booker/components/hooks/useSlotsViewOnSmallScreen";
import type { Slots } from "@calcom/features/bookings/types";
const useMoveToNextMonthOnNoAvailability = ({
browsingDate,
nonEmptyScheduleDays,
onMonthChange,
isLoading,
}: {
browsingDate: Dayjs;
nonEmptyScheduleDays: string[];
isLoading: boolean;
onMonthChange: (date: Dayjs) => void;
}) => {
if (isLoading) {
return {
moveToNextMonthOnNoAvailability: () => {
/* return noop until ready */
},
};
}
const nonEmptyScheduleDaysInBrowsingMonth = nonEmptyScheduleDays.filter(
(date) => dayjs(date).isSame(browsingDate, "month")
);
const moveToNextMonthOnNoAvailability = () => {
const currentMonth = dayjs().startOf("month").format("YYYY-MM");
const browsingMonth = browsingDate.format("YYYY-MM");
// Not meeting the criteria to move to next month
// Has to be currentMonth and it must have all days unbookable
if (
currentMonth != browsingMonth ||
nonEmptyScheduleDaysInBrowsingMonth.length
) {
return;
}
onMonthChange(browsingDate.add(1, "month"));
};
return {
moveToNextMonthOnNoAvailability,
};
};
export const DatePicker = ({
event,
slots = {},
isLoading,
classNames,
scrollToTimeSlots,
showNoAvailabilityDialog,
onDateChange,
}: {
event: {
data?: {
subsetOfUsers: Pick<User, "weekStart">[];
periodType?: PeriodData["periodType"];
periodStartDate?: PeriodData["periodStartDate"];
periodEndDate?: PeriodData["periodEndDate"];
periodDays?: PeriodData["periodDays"];
periodCountCalendarDays?: PeriodData["periodCountCalendarDays"];
} | null;
};
slots?: Slots;
isLoading?: boolean;
classNames?: DatePickerClassNames;
scrollToTimeSlots?: () => void;
showNoAvailabilityDialog?: boolean;
onDateChange?: () => void;
}) => {
const { i18n } = useLocale();
const [month, selectedDate, layout] = useBookerStoreContext(
(state) => [state.month, state.selectedDate, state.layout],
shallow
);
const [setSelectedDate, setMonth, setDayCount] = useBookerStoreContext(
(state) => [state.setSelectedDate, state.setMonth, state.setDayCount],
shallow
);
const slotsViewOnSmallScreen = useSlotsViewOnSmallScreen();
const onMonthChange = (date: Dayjs) => {
setMonth(date.format("YYYY-MM"));
if (!slotsViewOnSmallScreen) {
setSelectedDate({ date: date.format("YYYY-MM-DD") });
}
setDayCount(null); // Whenever the month is changed, we nullify getting X days
};
const nonEmptyScheduleDays = useNonEmptyScheduleDays(slots);
const browsingDate = month ? dayjs(month) : dayjs().startOf("month");
const { moveToNextMonthOnNoAvailability } =
useMoveToNextMonthOnNoAvailability({
browsingDate,
nonEmptyScheduleDays,
onMonthChange,
isLoading: isLoading ?? true,
});
moveToNextMonthOnNoAvailability();
// Determine if this is a compact sidebar view based on layout
const isCompact = layout !== "month_view" && layout !== "mobile";
const periodData: PeriodData = {
...{
periodType: "UNLIMITED",
periodStartDate: null,
periodEndDate: null,
periodDays: null,
periodCountCalendarDays: false,
},
...(event?.data && {
periodType: event.data.periodType,
periodStartDate: event.data.periodStartDate,
periodEndDate: event.data.periodEndDate,
periodDays: event.data.periodDays,
periodCountCalendarDays: event.data.periodCountCalendarDays,
}),
};
return (
<DatePickerComponent
customClassNames={{
datePickerTitle: classNames?.datePickerTitle,
datePickerDays: classNames?.datePickerDays,
datePickersDates: classNames?.datePickerDate,
datePickerDatesActive: classNames?.datePickerDatesActive,
datePickerToggle: classNames?.datePickerToggle,
}}
className={classNames?.datePickerContainer}
isLoading={isLoading}
onChange={(date: Dayjs | null, omitUpdatingParams?: boolean) => {
const newDate = date === null ? null : date.format("YYYY-MM-DD");
const previousDate = selectedDate;
const dateChanged = newDate !== previousDate;
setSelectedDate({
date: date === null ? null : date.format("YYYY-MM-DD"),
omitUpdatingParams,
preventMonthSwitching: !isCompact, // Prevent month switching when in monthly view
});
if (dateChanged) {
onDateChange?.();
}
}}
onMonthChange={onMonthChange}
includedDates={nonEmptyScheduleDays}
locale={i18n.language}
browsingDate={month ? dayjs(month) : undefined}
selected={dayjs(selectedDate)}
weekStart={weekdayToWeekIndex(event?.data?.subsetOfUsers?.[0]?.weekStart)}
slots={slots}
scrollToTimeSlots={scrollToTimeSlots}
periodData={periodData}
isCompact={isCompact}
showNoAvailabilityDialog={showNoAvailabilityDialog}
/>
);
};