Skip to content

Scheduling infrastructure for absolutely everyone.

fix: improve overlapping events with dynamic offsets and widths (#25310)

Eunjae Lee251d29f65bf1
diff --git a/apps/web/app/(use-page-wrapper)/settings/(admin-layout)/admin/playground/weekly-calendar/page.tsx b/apps/web/app/(use-page-wrapper)/settings/(admin-layout)/admin/playground/weekly-calendar/page.tsx
index b172faae309..9e3b3f08a8c 100644
--- a/apps/web/app/(use-page-wrapper)/settings/(admin-layout)/admin/playground/weekly-calendar/page.tsx
+++ b/apps/web/app/(use-page-wrapper)/settings/(admin-layout)/admin/playground/weekly-calendar/page.tsx
@@ -5,17 +5,23 @@ import { useState } from "react";
import dayjs from "@calcom/dayjs";
import { Calendar } from "@calcom/features/calendars/weeklyview";
import type { CalendarEvent } from "@calcom/features/calendars/weeklyview/types/events";
-import type { CalendarComponentProps } from "@calcom/features/calendars/weeklyview/types/state";
+import type { CalendarComponentProps, Hours } from "@calcom/features/calendars/weeklyview/types/state";
const makeDate = (dayOffset: number, hour: number, minute: number = 0) => {
return dayjs("2025-01-06").add(dayOffset, "day").hour(hour).minute(minute).second(0).toDate();
};
-const getBaseProps = (events: CalendarEvent[]): CalendarComponentProps => ({
+const getBaseProps = ({
+ events,
+ startHour = 6,
+}: {
+ events: CalendarEvent[];
+ startHour?: Hours;
+}): CalendarComponentProps => ({
startDate: dayjs("2025-01-06").toDate(), // Monday
endDate: dayjs("2025-01-12").toDate(), // Sunday
events,
- startHour: 6,
+ startHour,
endHour: 18,
gridCellsPerHour: 4,
timezone: "UTC",
@@ -23,6 +29,7 @@ const getBaseProps = (events: CalendarEvent[]): CalendarComponentProps => ({
showBorder: false,
hideHeader: true,
borderColor: "subtle",
+ scrollToCurrentTime: false,
});
type Scenario = {
@@ -31,6 +38,7 @@ type Scenario = {
description: string;
expected: string;
events: CalendarEvent[];
+ startHour: Hours;
};
const scenarios: Scenario[] = [
@@ -39,7 +47,8 @@ const scenarios: Scenario[] = [
title: "Two Overlapping Events",
description: "Two events with overlapping time ranges on the same day",
expected:
- "Second event should be offset 8% to the right, both 80% width. Hover should bring event to front.",
+ "First event 80% width at left edge (0%), second event 50% width aligned to right edge (49.5% offset). Events spread across full width for maximum visual distinction. Hover should bring event to front.",
+ startHour: 9,
events: [
{
id: 1,
@@ -62,7 +71,8 @@ const scenarios: Scenario[] = [
title: "Three Overlapping Events (Cascading)",
description: "Three events that overlap, creating a cascading effect",
expected:
- "Events should cascade with offsets 0%, 8%, 16%. Z-index should increment. Hover brings any to top.",
+ "Events spread across full width with variable widths (55%, ~42%, 33%). Offsets: 0%, ~35%, 66.5% (last event aligned to right edge). Right edges evenly distributed for maximum scatter. Z-index should increment. Hover brings any to top.",
+ startHour: 9,
events: [
{
id: 3,
@@ -91,7 +101,8 @@ const scenarios: Scenario[] = [
id: "non-overlapping",
title: "Non-Overlapping Events",
description: "Events that don't overlap should not cascade",
- expected: "Both events at 0% offset (separate groups), no cascade. Both should be 80% width.",
+ expected: "Both events at 0% offset (separate groups), no cascade. Both should be 100% width.",
+ startHour: 9,
events: [
{
id: 6,
@@ -113,7 +124,9 @@ const scenarios: Scenario[] = [
id: "same-start-time",
title: "Same Start Time, Different Durations",
description: "Multiple events starting at the same time with varying lengths",
- expected: "Longest event first (base of cascade), shorter ones offset 8%, 16%. All start at 10:00.",
+ expected:
+ "Longest event first (base of cascade), spread across full width with variable widths (55%, ~42%, 33%). Last event aligned to right edge. All start at 10:00.",
+ startHour: 9,
events: [
{
id: 8,
@@ -139,16 +152,18 @@ const scenarios: Scenario[] = [
],
},
{
- id: "chain-overlaps",
- title: "Chain Overlaps (A→B→C)",
- description: "Events where A overlaps B, and B overlaps C",
- expected: "Single overlap group with cascading offsets 0%, 8%, 16%.",
+ id: "four-overlapping",
+ title: "Four Overlapping Events",
+ description: "Four events that overlap simultaneously",
+ expected:
+ "Events spread across full width with variable widths (40%, ~33%, ~28%, 25%). Last event aligned to right edge. Right edges evenly distributed for maximum scatter.",
+ startHour: 9,
events: [
{
id: 11,
title: "Event A",
start: makeDate(4, 10, 0),
- end: makeDate(4, 11, 0),
+ end: makeDate(4, 12, 0),
options: { status: "ACCEPTED", color: "#3b82f6" },
},
{
@@ -162,9 +177,16 @@ const scenarios: Scenario[] = [
id: 13,
title: "Event C",
start: makeDate(4, 11, 0),
- end: makeDate(4, 12, 0),
+ end: makeDate(4, 12, 30),
options: { status: "ACCEPTED", color: "#10b981" },
},
+ {
+ id: 50,
+ title: "Event D",
+ start: makeDate(4, 11, 15),
+ end: makeDate(4, 12, 15),
+ options: { status: "PENDING", color: "#8b5cf6" },
+ },
],
},
{
@@ -173,6 +195,7 @@ const scenarios: Scenario[] = [
description: "A very busy day with many overlapping events",
expected:
"Visually tight stack with multiple cascading levels. Right edge should not overflow. Hover should still work.",
+ startHour: 8,
events: [
{
id: 14,
@@ -327,7 +350,9 @@ const scenarios: Scenario[] = [
id: "touching-events",
title: "Touching Events (Edge Case)",
description: "Events that touch exactly at boundaries",
- expected: "Separate groups; no cascade; both at 0% offset. Events touching at 11:00 should not overlap.",
+ expected:
+ "Separate groups; no cascade; both at 0% offset. Both should be 100% width. Events touching at 11:00 should not overlap.",
+ startHour: 9,
events: [
{
id: 25,
@@ -351,6 +376,7 @@ const scenarios: Scenario[] = [
description: "Events with different booking statuses",
expected:
"Visual styling should differ by status (ACCEPTED, PENDING, CANCELLED). Cascade should still work.",
+ startHour: 13,
events: [
{
id: 27,
@@ -381,6 +407,7 @@ const scenarios: Scenario[] = [
description: "Events with different durations to test layout logic (eventDuration > 30 changes flex-col)",
expected:
"Events ≤30min show horizontal layout (title and time inline). Events >30min show vertical layout (title and time stacked).",
+ startHour: 8,
events: [
{
id: 40,
@@ -465,7 +492,7 @@ function ScenarioCard({ scenario }: { scenario: Scenario }) {
</div>
<div className="h-[600px] overflow-hidden rounded border">
- <Calendar {...getBaseProps(scenario.events)} />
+ <Calendar {...getBaseProps({ events: scenario.events, startHour: scenario.startHour })} />
</div>
<button
diff --git a/packages/features/calendars/weeklyview/components/Calendar.tsx b/packages/features/calendars/weeklyview/components/Calendar.tsx
index 5b45f5081c9..450e47e53ba 100644
--- a/packages/features/calendars/weeklyview/components/Calendar.tsx
+++ b/packages/features/calendars/weeklyview/components/Calendar.tsx
@@ -34,6 +34,7 @@ function CalendarInner(props: CalendarComponentProps) {
const showBackgroundPattern = useCalendarStore((state) => state.showBackgroundPattern);
const showBorder = useCalendarStore((state) => state.showBorder ?? true);
const borderColor = useCalendarStore((state) => state.borderColor ?? "default");
+ const scrollToCurrentTime = useCalendarStore((state) => state.scrollToCurrentTime ?? true);
const days = useMemo(() => getDaysBetweenDates(startDate, endDate), [startDate, endDate]);
@@ -74,7 +75,7 @@ function CalendarInner(props: CalendarComponentProps) {
borderColor={borderColor}
/>
<div className="relative flex flex-auto">
- <CurrentTime timezone={timezone} />
+ <CurrentTime timezone={timezone} scrollToCurrentTime={scrollToCurrentTime} />
<div
className={classNames(
"bg-default dark:bg-muted ring-muted sticky left-0 z-10 w-16 flex-none ring-1",
diff --git a/packages/features/calendars/weeklyview/components/DateValues/index.tsx b/packages/features/calendars/weeklyview/components/DateValues/index.tsx
index e7e2c6e7126..3cd232f13f1 100644
--- a/packages/features/calendars/weeklyview/components/DateValues/index.tsx
+++ b/packages/features/calendars/weeklyview/components/DateValues/index.tsx
@@ -52,7 +52,7 @@ export function DateValues({ showBorder, borderColor, days, containerNavRef }: P
<div
ref={containerNavRef}
className={classNames(
- "bg-default dark:bg-default sticky top-[var(--calendar-dates-sticky-offset,0px)] z-[80] flex-none border-b sm:pr-8",
+ "bg-default dark:bg-default sticky top-[var(--calendar-dates-sticky-offset,0px)] z-[80] flex-none border-b",
borderColor === "subtle" ? "border-b-subtle" : "border-b-default",
showBorder && (borderColor === "subtle" ? "border-r-subtle border-r" : "border-r-default border-r")
)}>
diff --git a/packages/features/calendars/weeklyview/components/currentTime/index.tsx b/packages/features/calendars/weeklyview/components/currentTime/index.tsx
index f8e1d78ba8a..533a907b9c6 100644
--- a/packages/features/calendars/weeklyview/components/currentTime/index.tsx
+++ b/packages/features/calendars/weeklyview/components/currentTime/index.tsx
@@ -11,7 +11,13 @@ function calculateMinutesFromStart(startHour: number, currentHour: number, curre
return currentMinuteOfDay - startMinute;
}
-export function CurrentTime({ timezone }: { timezone: string }) {
+export function CurrentTime({
+ timezone,
+ scrollToCurrentTime = true,
+}: {
+ timezone: string;
+ scrollToCurrentTime?: boolean;
+}) {
const { timeFormat } = useTimePreferences();
const currentTimeRef = useRef<HTMLDivElement>(null);
const [scrolledIntoView, setScrolledIntoView] = useState(false);
@@ -36,14 +42,14 @@ export function CurrentTime({ timezone }: { timezone: string }) {
const minutesFromStart = calculateMinutesFromStart(startHour, currentHour, currentMinute);
setCurrentTimePos(minutesFromStart);
- if (!currentTimeRef.current || scrolledIntoView) return;
+ if (!scrollToCurrentTime || !currentTimeRef.current || scrolledIntoView) return;
// Within a small timeout so element has time to render.
setTimeout(() => {
// Doesn't seem to cause any issue. Put it under condition if needed
currentTimeRef?.current?.scrollIntoView({ block: "center" });
setScrolledIntoView(true);
}, 100);
- }, [startHour, endHour, scrolledIntoView, timezone]);
+ }, [startHour, endHour, scrolledIntoView, timezone, scrollToCurrentTime]);
return (
<div
diff --git a/packages/features/calendars/weeklyview/components/grid/index.tsx b/packages/features/calendars/weeklyview/components/grid/index.tsx
index 682e84f26d2..deeacef8e49 100644
--- a/packages/features/calendars/weeklyview/components/grid/index.tsx
+++ b/packages/features/calendars/weeklyview/components/grid/index.tsx
@@ -14,7 +14,7 @@ export const SchedulerColumns = React.forwardRef<HTMLOListElement, Props>(functi
return (
<ol
ref={ref}
- className="scheduler-grid-row-template col-start-1 col-end-2 row-start-1 grid auto-cols-auto text-[0px] sm:pr-8"
+ className="scheduler-grid-row-template col-start-1 col-end-2 row-start-1 grid auto-cols-auto text-[0px]"
style={{ marginTop: offsetHeight || "var(--gridDefaultSize)", zIndex }}
data-gridstopsperday={gridStopsPerDay}>
{children}
diff --git a/packages/features/calendars/weeklyview/components/verticalLines/index.tsx b/packages/features/calendars/weeklyview/components/verticalLines/index.tsx
index b021bdb0f40..8bc5577655d 100644
--- a/packages/features/calendars/weeklyview/components/verticalLines/index.tsx
+++ b/packages/features/calendars/weeklyview/components/verticalLines/index.tsx
@@ -19,7 +19,7 @@ export const VerticalLines = ({ days, borderColor }: { days: dayjs.Dayjs[]; bord
return (
<div
className={classNames(
- "pointer-events-none relative z-[60] col-start-1 col-end-2 row-start-1 grid auto-cols-auto grid-rows-1 divide-x sm:pr-8",
+ "pointer-events-none relative z-[60] col-start-1 col-end-2 row-start-1 grid auto-cols-auto grid-rows-1 divide-x",
borderColor === "subtle" ? "divide-subtle" : "divide-default"
)}
dir={direction}
diff --git a/packages/features/calendars/weeklyview/utils/overlap.test.ts b/packages/features/calendars/weeklyview/utils/overlap.test.ts
index 78fbc0bd9bd..d062e46869f 100644
--- a/packages/features/calendars/weeklyview/utils/overlap.test.ts
+++ b/packages/features/calendars/weeklyview/utils/overlap.test.ts
@@ -1,20 +1,30 @@
import { describe, expect, it } from "vitest";
import type { CalendarEvent } from "../types/events";
-import {
- buildOverlapGroups,
- calculateEventLayouts,
- createLayoutMap,
- sortEvents,
-} from "./overlap";
+import { buildOverlapGroups, calculateEventLayouts, createLayoutMap, sortEvents } from "./overlap";
describe("overlap utility", () => {
describe("sortEvents", () => {
it("should sort events by start time ascending", () => {
const events: CalendarEvent[] = [
- { id: 1, title: "Event 1", start: new Date("2024-01-01T10:00:00"), end: new Date("2024-01-01T11:00:00") },
- { id: 2, title: "Event 2", start: new Date("2024-01-01T09:00:00"), end: new Date("2024-01-01T10:00:00") },
- { id: 3, title: "Event 3", start: new Date("2024-01-01T11:00:00"), end: new Date("2024-01-01T12:00:00") },
+ {
+ id: 1,
+ title: "Event 1",
+ start: new Date("2024-01-01T10:00:00"),
+ end: new Date("2024-01-01T11:00:00"),
+ },
+ {
+ id: 2,
+ title: "Event 2",
+ start: new Date("2024-01-01T09:00:00"),
+ end: new Date("2024-01-01T10:00:00"),
+ },
+ {
+ id: 3,
+ title: "Event 3",
+ start: new Date("2024-01-01T11:00:00"),
+ end: new Date("2024-01-01T12:00:00"),
+ },
];
const sorted = sortEvents(events);
@@ -26,9 +36,24 @@ describe("overlap utility", () => {
it("should sort events with same start time by end time descending (longer first)", () => {
const events: CalendarEvent[] = [
- { id: 1, title: "Event 1", start: new Date("2024-01-01T10:00:00"), end: new Date("2024-01-01T11:00:00") },
- { id: 2, title: "Event 2", start: new Date("2024-01-01T10:00:00"), end: new Date("2024-01-01T12:00:00") },
- { id: 3, title: "Event 3", start: new Date("2024-01-01T10:00:00"), end: new Date("2024-01-01T10:30:00") },
+ {
+ id: 1,
+ title: "Event 1",
+ start: new Date("2024-01-01T10:00:00"),
+ end: new Date("2024-01-01T11:00:00"),
+ },
+ {
+ id: 2,
+ title: "Event 2",
+ start: new Date("2024-01-01T10:00:00"),
+ end: new Date("2024-01-01T12:00:00"),
+ },
+ {
+ id: 3,
+ title: "Event 3",
+ start: new Date("2024-01-01T10:00:00"),
+ end: new Date("2024-01-01T10:30:00"),
+ },
];
const sorted = sortEvents(events);
@@ -40,8 +65,18 @@ describe("overlap utility", () => {
it("should not mutate the original array", () => {
const events: CalendarEvent[] = [
- { id: 1, title: "Event 1", start: new Date("2024-01-01T10:00:00"), end: new Date("2024-01-01T11:00:00") },
- { id: 2, title: "Event 2", start: new Date("2024-01-01T09:00:00"), end: new Date("2024-01-01T10:00:00") },
+ {
+ id: 1,
+ title: "Event 1",
+ start: new Date("2024-01-01T10:00:00"),
+ end: new Date("2024-01-01T11:00:00"),
+ },
+ {
+ id: 2,
+ title: "Event 2",
+ start: new Date("2024-01-01T09:00:00"),
+ end: new Date("2024-01-01T10:00:00"),
+ },
];
const originalFirstId = events[0].id;
@@ -59,7 +94,12 @@ describe("overlap utility", () => {
it("should return single group for single event", () => {
const events: CalendarEvent[] = [
- { id: 1, title: "Event 1", start: new Date("2024-01-01T10:00:00"), end: new Date("2024-01-01T11:00:00") },
+ {
+ id: 1,
+ title: "Event 1",
+ start: new Date("2024-01-01T10:00:00"),
+ end: new Date("2024-01-01T11:00:00"),
+ },
];
const groups = buildOverlapGroups(events);
@@ -71,8 +111,18 @@ describe("overlap utility", () => {
it("should group two overlapping events together", () => {
const events: CalendarEvent[] = [
- { id: 1, title: "Event 1", start: new Date("2024-01-01T10:00:00"), end: new Date("2024-01-01T11:00:00") },
- { id: 2, title: "Event 2", start: new Date("2024-01-01T10:30:00"), end: new Date("2024-01-01T11:30:00") },
+ {
+ id: 1,
+ title: "Event 1",
+ start: new Date("2024-01-01T10:00:00"),
+ end: new Date("2024-01-01T11:00:00"),
+ },
+ {
+ id: 2,
+ title: "Event 2",
+ start: new Date("2024-01-01T10:30:00"),
+ end: new Date("2024-01-01T11:30:00"),
+ },
];
const sorted = sortEvents(events);
@@ -86,8 +136,18 @@ describe("overlap utility", () => {
it("should separate non-overlapping events into different groups", () => {
const events: CalendarEvent[] = [
- { id: 1, title: "Event 1", start: new Date("2024-01-01T10:00:00"), end: new Date("2024-01-01T11:00:00") },
- { id: 2, title: "Event 2", start: new Date("2024-01-01T11:00:00"), end: new Date("2024-01-01T12:00:00") },
+ {
+ id: 1,
+ title: "Event 1",
+ start: new Date("2024-01-01T10:00:00"),
+ end: new Date("2024-01-01T11:00:00"),
+ },
+ {
+ id: 2,
+ title: "Event 2",
+ start: new Date("2024-01-01T11:00:00"),
+ end: new Date("2024-01-01T12:00:00"),
+ },
];
const sorted = sortEvents(events);
@@ -102,9 +162,24 @@ describe("overlap utility", () => {
it("should handle chain overlaps (A overlaps B, B overlaps C)", () => {
const events: CalendarEvent[] = [
- { id: 1, title: "Event 1", start: new Date("2024-01-01T10:00:00"), end: new Date("2024-01-01T11:00:00") },
- { id: 2, title: "Event 2", start: new Date("2024-01-01T10:30:00"), end: new Date("2024-01-01T11:30:00") },
- { id: 3, title: "Event 3", start: new Date("2024-01-01T11:00:00"), end: new Date("2024-01-01T12:00:00") },
+ {
+ id: 1,
+ title: "Event 1",
+ start: new Date("2024-01-01T10:00:00"),
+ end: new Date("2024-01-01T11:00:00"),
+ },
+ {
+ id: 2,
+ title: "Event 2",
+ start: new Date("2024-01-01T10:30:00"),
+ end: new Date("2024-01-01T11:30:00"),
+ },
+ {
+ id: 3,
+ title: "Event 3",
+ start: new Date("2024-01-01T11:00:00"),
+ end: new Date("2024-01-01T12:00:00"),
+ },
];
const sorted = sortEvents(events);
@@ -119,10 +194,30 @@ describe("overlap utility", () => {
it("should handle multiple separate overlap groups", () => {
const events: CalendarEvent[] = [
- { id: 1, title: "Event 1", start: new Date("2024-01-01T09:00:00"), end: new Date("2024-01-01T10:00:00") },
- { id: 2, title: "Event 2", start: new Date("2024-01-01T09:30:00"), end: new Date("2024-01-01T10:30:00") },
- { id: 3, title: "Event 3", start: new Date("2024-01-01T11:00:00"), end: new Date("2024-01-01T12:00:00") },
- { id: 4, title: "Event 4", start: new Date("2024-01-01T11:30:00"), end: new Date("2024-01-01T12:30:00") },
+ {
+ id: 1,
+ title: "Event 1",
+ start: new Date("2024-01-01T09:00:00"),
+ end: new Date("2024-01-01T10:00:00"),
+ },
+ {
+ id: 2,
+ title: "Event 2",
+ start: new Date("2024-01-01T09:30:00"),
+ end: new Date("2024-01-01T10:30:00"),
+ },
+ {
+ id: 3,
+ title: "Event 3",
+ start: new Date("2024-01-01T11:00:00"),
+ end: new Date("2024-01-01T12:00:00"),
+ },
+ {
+ id: 4,
+ title: "Event 4",
+ start: new Date("2024-01-01T11:30:00"),
+ end: new Date("2024-01-01T12:30:00"),
+ },
];
const sorted = sortEvents(events);
@@ -139,9 +234,24 @@ describe("overlap utility", () => {
it("should handle events that start at the same time", () => {
const events: CalendarEvent[] = [
- { id: 1, title: "Event 1", start: new Date("2024-01-01T10:00:00"), end: new Date("2024-01-01T12:00:00") },
- { id: 2, title: "Event 2", start: new Date("2024-01-01T10:00:00"), end: new Date("2024-01-01T11:00:00") },
- { id: 3, title: "Event 3", start: new Date("2024-01-01T10:00:00"), end: new Date("2024-01-01T10:30:00") },
+ {
+ id: 1,
+ title: "Event 1",
+ start: new Date("2024-01-01T10:00:00"),
+ end: new Date("2024-01-01T12:00:00"),
+ },
+ {
+ id: 2,
+ title: "Event 2",
+ start: new Date("2024-01-01T10:00:00"),
+ end: new Date("2024-01-01T11:00:00"),
+ },
+ {
+ id: 3,
+ title: "Event 3",
+ start: new Date("2024-01-01T10:00:00"),
+ end: new Date("2024-01-01T10:30:00"),
+ },
];
const sorted = sortEvents(events);
@@ -155,7 +265,12 @@ describe("overlap utility", () => {
describe("calculateEventLayouts", () => {
it("should calculate layout for single event", () => {
const events: CalendarEvent[] = [
- { id: 1, title: "Event 1", start: new Date("2024-01-01T10:00:00"), end: new Date("2024-01-01T11:00:00") },
+ {
+ id: 1,
+ title: "Event 1",
+ start: new Date("2024-01-01T10:00:00"),
+ end: new Date("2024-01-01T11:00:00"),
+ },
];
const layouts = calculateEventLayouts(events);
@@ -163,7 +278,7 @@ describe("overlap utility", () => {
expect(layouts).toHaveLength(1);
expect(layouts[0].event.id).toBe(1);
expect(layouts[0].leftOffsetPercent).toBe(0);
- expect(layouts[0].widthPercent).toBe(80);
+ expect(layouts[0].widthPercent).toBe(99.5);
expect(layouts[0].baseZIndex).toBe(60);
expect(layouts[0].groupIndex).toBe(0);
expect(layouts[0].indexInGroup).toBe(0);
@@ -171,80 +286,118 @@ describe("overlap utility", () => {
it("should calculate cascading layout for two overlapping events", () => {
const events: CalendarEvent[] = [
- { id: 1, title: "Event 1", start: new Date("2024-01-01T10:00:00"), end: new Date("2024-01-01T11:00:00") },
- { id: 2, title: "Event 2", start: new Date("2024-01-01T10:30:00"), end: new Date("2024-01-01T11:30:00") },
+ {
+ id: 1,
+ title: "Event 1",
+ start: new Date("2024-01-01T10:00:00"),
+ end: new Date("2024-01-01T11:00:00"),
+ },
+ {
+ id: 2,
+ title: "Event 2",
+ start: new Date("2024-01-01T10:30:00"),
+ end: new Date("2024-01-01T11:30:00"),
+ },
];
const layouts = calculateEventLayouts(events);
expect(layouts).toHaveLength(2);
-
+
expect(layouts[0].event.id).toBe(1);
expect(layouts[0].leftOffsetPercent).toBe(0);
expect(layouts[0].widthPercent).toBe(80);
expect(layouts[0].baseZIndex).toBe(60);
-
+
expect(layouts[1].event.id).toBe(2);
- expect(layouts[1].leftOffsetPercent).toBe(8);
- expect(layouts[1].widthPercent).toBe(80);
+ expect(layouts[1].leftOffsetPercent).toBe(49.5);
+ expect(layouts[1].widthPercent).toBe(50);
expect(layouts[1].baseZIndex).toBe(61);
});
it("should calculate cascading layout for three overlapping events", () => {
const events: CalendarEvent[] = [
- { id: 1, title: "Event 1", start: new Date("2024-01-01T10:00:00"), end: new Date("2024-01-01T11:00:00") },
- { id: 2, title: "Event 2", start: new Date("2024-01-01T10:30:00"), end: new Date("2024-01-01T11:30:00") },
- { id: 3, title: "Event 3", start: new Date("2024-01-01T11:00:00"), end: new Date("2024-01-01T12:00:00") },
+ {
+ id: 1,
+ title: "Event 1",
+ start: new Date("2024-01-01T10:00:00"),
+ end: new Date("2024-01-01T11:00:00"),
+ },
+ {
+ id: 2,
+ title: "Event 2",
+ start: new Date("2024-01-01T10:30:00"),
+ end: new Date("2024-01-01T11:30:00"),
+ },
+ {
+ id: 3,
+ title: "Event 3",
+ start: new Date("2024-01-01T11:00:00"),
+ end: new Date("2024-01-01T12:00:00"),
+ },
];
const layouts = calculateEventLayouts(events);
expect(layouts).toHaveLength(3);
-
+
expect(layouts[0].leftOffsetPercent).toBe(0);
- expect(layouts[1].leftOffsetPercent).toBe(8);
- expect(layouts[2].leftOffsetPercent).toBe(16);
-
+ expect(layouts[1].leftOffsetPercent).toBeCloseTo(35.315, 1);
+ expect(layouts[2].leftOffsetPercent).toBe(66.5);
+
expect(layouts[0].baseZIndex).toBe(60);
expect(layouts[1].baseZIndex).toBe(61);
expect(layouts[2].baseZIndex).toBe(62);
});
- it("should respect custom configuration", () => {
+ it("should respect custom baseZIndex configuration", () => {
const events: CalendarEvent[] = [
- { id: 1, title: "Event 1", start: new Date("2024-01-01T10:00:00"), end: new Date("2024-01-01T11:00:00") },
- { id: 2, title: "Event 2", start: new Date("2024-01-01T10:30:00"), end: new Date("2024-01-01T11:30:00") },
+ {
+ id: 1,
+ title: "Event 1",
+ start: new Date("2024-01-01T10:00:00"),
+ end: new Date("2024-01-01T11:00:00"),
+ },
+ {
+ id: 2,
+ title: "Event 2",
+ start: new Date("2024-01-01T10:30:00"),
+ end: new Date("2024-01-01T11:30:00"),
+ },
];
const layouts = calculateEventLayouts(events, {
- baseWidthPercent: 70,
- offsetStepPercent: 10,
baseZIndex: 50,
});
- expect(layouts[0].widthPercent).toBe(70);
- expect(layouts[0].leftOffsetPercent).toBe(0);
expect(layouts[0].baseZIndex).toBe(50);
-
- expect(layouts[1].widthPercent).toBe(70);
- expect(layouts[1].leftOffsetPercent).toBe(10);
expect(layouts[1].baseZIndex).toBe(51);
});
it("should handle non-overlapping events in separate groups", () => {
const events: CalendarEvent[] = [
- { id: 1, title: "Event 1", start: new Date("2024-01-01T10:00:00"), end: new Date("2024-01-01T11:00:00") },
- { id: 2, title: "Event 2", start: new Date("2024-01-01T11:00:00"), end: new Date("2024-01-01T12:00:00") },
+ {
+ id: 1,
+ title: "Event 1",
+ start: new Date("2024-01-01T10:00:00"),
+ end: new Date("2024-01-01T11:00:00"),
+ },
+ {
+ id: 2,
+ title: "Event 2",
+ start: new Date("2024-01-01T11:00:00"),
+ end: new Date("2024-01-01T12:00:00"),
+ },
];
const layouts = calculateEventLayouts(events);
expect(layouts).toHaveLength(2);
-
+
expect(layouts[0].groupIndex).toBe(0);
expect(layouts[0].indexInGroup).toBe(0);
expect(layouts[0].leftOffsetPercent).toBe(0);
-
+
expect(layouts[1].groupIndex).toBe(1);
expect(layouts[1].indexInGroup).toBe(0);
expect(layouts[1].leftOffsetPercent).toBe(0);
@@ -252,17 +405,72 @@ describe("overlap utility", () => {
it("should prevent overflow with many overlapping events (dense scenario)", () => {
const events: CalendarEvent[] = [
- { id: 1, title: "Event 1", start: new Date("2024-01-01T09:00:00"), end: new Date("2024-01-01T09:30:00") },
- { id: 2, title: "Event 2", start: new Date("2024-01-01T09:15:00"), end: new Date("2024-01-01T10:00:00") },
- { id: 3, title: "Event 3", start: new Date("2024-01-01T09:45:00"), end: new Date("2024-01-01T11:00:00") },
- { id: 4, title: "Event 4", start: new Date("2024-01-01T10:00:00"), end: new Date("2024-01-01T10:30:00") },
- { id: 5, title: "Event 5", start: new Date("2024-01-01T10:15:00"), end: new Date("2024-01-01T11:30:00") },
- { id: 6, title: "Event 6", start: new Date("2024-01-01T11:00:00"), end: new Date("2024-01-01T12:00:00") },
- { id: 7, title: "Event 7", start: new Date("2024-01-01T11:30:00"), end: new Date("2024-01-01T12:30:00") },
- { id: 8, title: "Event 8", start: new Date("2024-01-01T12:00:00"), end: new Date("2024-01-01T13:30:00") },
- { id: 9, title: "Event 9", start: new Date("2024-01-01T12:30:00"), end: new Date("2024-01-01T13:00:00") },
- { id: 10, title: "Event 10", start: new Date("2024-01-01T13:00:00"), end: new Date("2024-01-01T14:00:00") },
- { id: 11, title: "Event 11", start: new Date("2024-01-01T13:15:00"), end: new Date("2024-01-01T14:00:00") },
+ {
+ id: 1,
+ title: "Event 1",
+ start: new Date("2024-01-01T09:00:00"),
+ end: new Date("2024-01-01T09:30:00"),
+ },
+ {
+ id: 2,
+ title: "Event 2",
+ start: new Date("2024-01-01T09:15:00"),
+ end: new Date("2024-01-01T10:00:00"),
+ },
+ {
+ id: 3,
+ title: "Event 3",
+ start: new Date("2024-01-01T09:45:00"),
+ end: new Date("2024-01-01T11:00:00"),
+ },
+ {
+ id: 4,
+ title: "Event 4",
+ start: new Date("2024-01-01T10:00:00"),
+ end: new Date("2024-01-01T10:30:00"),
+ },
+ {
+ id: 5,
+ title: "Event 5",
+ start: new Date("2024-01-01T10:15:00"),
+ end: new Date("2024-01-01T11:30:00"),
+ },
+ {
+ id: 6,
+ title: "Event 6",
+ start: new Date("2024-01-01T11:00:00"),
+ end: new Date("2024-01-01T12:00:00"),
+ },
+ {
+ id: 7,
+ title: "Event 7",
+ start: new Date("2024-01-01T11:30:00"),
+ end: new Date("2024-01-01T12:30:00"),
+ },
+ {
+ id: 8,
+ title: "Event 8",
+ start: new Date("2024-01-01T12:00:00"),
+ end: new Date("2024-01-01T13:30:00"),
+ },
+ {
+ id: 9,
+ title: "Event 9",
+ start: new Date("2024-01-01T12:30:00"),
+ end: new Date("2024-01-01T13:00:00"),
+ },
+ {
+ id: 10,
+ title: "Event 10",
+ start: new Date("2024-01-01T13:00:00"),
+ end: new Date("2024-01-01T14:00:00"),
+ },
+ {
+ id: 11,
+ title: "Event 11",
+ start: new Date("2024-01-01T13:15:00"),
+ end: new Date("2024-01-01T14:00:00"),
+ },
];
const layouts = calculateEventLayouts(events);
@@ -279,14 +487,14 @@ describe("overlap utility", () => {
const events: CalendarEvent[] = Array.from({ length: 21 }, (_, i) => ({
id: i + 1,
title: `Event ${i + 1}`,
- start: new Date(`2024-01-01T09:${String(i * 2).padStart(2, '0')}:00`),
- end: new Date(`2024-01-01T10:${String(i * 2).padStart(2, '0')}:00`),
+ start: new Date(`2024-01-01T09:${String(i * 2).padStart(2, "0")}:00`),
+ end: new Date(`2024-01-01T10:${String(i * 2).padStart(2, "0")}:00`),
}));
const layouts = calculateEventLayouts(events);
expect(layouts).toHaveLength(21);
-
+
layouts.forEach((layout) => {
const totalWidth = layout.leftOffsetPercent + layout.widthPercent;
expect(totalWidth).toBeLessThanOrEqual(100 - 0.5);
@@ -300,17 +508,17 @@ describe("overlap utility", () => {
const events: CalendarEvent[] = Array.from({ length: 12 }, (_, i) => ({
id: i + 1,
title: `Event ${i + 1}`,
- start: new Date(`2024-01-01T10:${String(i * 5).padStart(2, '0')}:00`),
- end: new Date(`2024-01-01T11:${String(i * 5).padStart(2, '0')}:00`),
+ start: new Date(`2024-01-01T10:${String(i * 5).padStart(2, "0")}:00`),
+ end: new Date(`2024-01-01T11:${String(i * 5).padStart(2, "0")}:00`),
}));
const layouts = calculateEventLayouts(events);
expect(layouts).toHaveLength(12);
-
+
layouts.forEach((layout, index) => {
expect(layout.leftOffsetPercent + layout.widthPercent).toBeLessThanOrEqual(100);
-
+
if (index > 0) {
expect(layout.leftOffsetPercent).toBeGreaterThan(layouts[index - 1].leftOffsetPercent);
}
@@ -320,30 +528,55 @@ describe("overlap utility", () => {
expect(lastLayout.leftOffsetPercent + lastLayout.widthPercent).toBeLessThanOrEqual(100);
});
- it("should maintain full width for small overlap groups", () => {
+ it("should use dynamic width for three overlapping events", () => {
const events: CalendarEvent[] = [
- { id: 1, title: "Event 1", start: new Date("2024-01-01T10:00:00"), end: new Date("2024-01-01T11:00:00") },
- { id: 2, title: "Event 2", start: new Date("2024-01-01T10:30:00"), end: new Date("2024-01-01T11:30:00") },
- { id: 3, title: "Event 3", start: new Date("2024-01-01T11:00:00"), end: new Date("2024-01-01T12:00:00") },
+ {
+ id: 1,
+ title: "Event 1",
+ start: new Date("2024-01-01T10:00:00"),
+ end: new Date("2024-01-01T11:00:00"),
+ },
+ {
+ id: 2,
+ title: "Event 2",
+ start: new Date("2024-01-01T10:30:00"),
+ end: new Date("2024-01-01T11:30:00"),
+ },
+ {
+ id: 3,
+ title: "Event 3",
+ start: new Date("2024-01-01T11:00:00"),
+ end: new Date("2024-01-01T12:00:00"),
+ },
];
const layouts = calculateEventLayouts(events);
- expect(layouts[0].widthPercent).toBe(80);
- expect(layouts[1].widthPercent).toBe(80);
- expect(layouts[2].widthPercent).toBe(80);
-
+ expect(layouts[0].widthPercent).toBe(55);
+ expect(layouts[1].widthPercent).toBeCloseTo(41.9, 0);
+ expect(layouts[2].widthPercent).toBe(33);
+
expect(layouts[0].leftOffsetPercent).toBe(0);
- expect(layouts[1].leftOffsetPercent).toBe(8);
- expect(layouts[2].leftOffsetPercent).toBe(16);
+ expect(layouts[1].leftOffsetPercent).toBeCloseTo(35.315, 1);
+ expect(layouts[2].leftOffsetPercent).toBe(66.5);
});
});
describe("createLayoutMap", () => {
it("should create a map from event ID to layout", () => {
const events: CalendarEvent[] = [
- { id: 1, title: "Event 1", start: new Date("2024-01-01T10:00:00"), end: new Date("2024-01-01T11:00:00") },
- { id: 2, title: "Event 2", start: new Date("2024-01-01T10:30:00"), end: new Date("2024-01-01T11:30:00") },
+ {
+ id: 1,
+ title: "Event 1",
+ start: new Date("2024-01-01T10:00:00"),
+ end: new Date("2024-01-01T11:00:00"),
+ },
+ {
+ id: 2,
+ title: "Event 2",
+ start: new Date("2024-01-01T10:30:00"),
+ end: new Date("2024-01-01T11:30:00"),
+ },
];
const layouts = calculateEventLayouts(events);
@@ -353,7 +586,9 @@ describe("overlap utility", () => {
expect(map.get(1)?.event.id).toBe(1);
expect(map.get(2)?.event.id).toBe(2);
expect(map.get(1)?.leftOffsetPercent).toBe(0);
- expect(map.get(2)?.leftOffsetPercent).toBe(8);
+ expect(map.get(2)?.leftOffsetPercent).toBe(49.5);
+ expect(map.get(1)?.widthPercent).toBe(80);
+ expect(map.get(2)?.widthPercent).toBe(50);
});
it("should handle empty layouts", () => {
diff --git a/packages/features/calendars/weeklyview/utils/overlap.ts b/packages/features/calendars/weeklyview/utils/overlap.ts
index c473222cb13..db092ff249d 100644
--- a/packages/features/calendars/weeklyview/utils/overlap.ts
+++ b/packages/features/calendars/weeklyview/utils/overlap.ts
@@ -3,10 +3,10 @@ import dayjs from "@calcom/dayjs";
import type { CalendarEvent } from "../types/events";
export interface OverlapLayoutConfig {
- baseWidthPercent?: number;
- offsetStepPercent?: number;
baseZIndex?: number;
safetyMarginPercent?: number;
+ minWidthPercent?: number;
+ curveExponent?: number;
}
export interface EventLayout {
@@ -19,12 +19,60 @@ export interface EventLayout {
}
const DEFAULT_CONFIG: Required<OverlapLayoutConfig> = {
- baseWidthPercent: 80,
- offsetStepPercent: 8,
baseZIndex: 60,
safetyMarginPercent: 0.5,
+ minWidthPercent: 25,
+ curveExponent: 1.3,
};
+/**
+ * Calculates variable widths for each event in a cascade based on position
+ * Leftmost (longest) events get more width, rightmost (shortest) get less width
+ * Uses anchor points for 2-4 events and smooth easing curve for 5+ events
+ *
+ * @param groupSize - Number of overlapping events in the group
+ * @param minWidthPercent - Minimum width to maintain readability (default 25%)
+ * @param curveExponent - Easing curve exponent for width distribution (default 1.3)
+ * @returns Array of width percentages, one for each position in the cascade
+ */
+function calculateVariableWidths(
+ groupSize: number,
+ minWidthPercent: number,
+ curveExponent: number
+): number[] {
+ if (groupSize <= 1) {
+ return [100]; // Single event gets full width (100%)
+ }
+
+ // Define anchor points for first and last widths based on group size
+ let wFirst: number;
+ let wLast: number;
+
+ if (groupSize === 2) {
+ wFirst = 80;
+ wLast = 50;
+ } else if (groupSize === 3) {
+ wFirst = 55;
+ wLast = 33;
+ } else if (groupSize === 4) {
+ wFirst = 40;
+ wLast = 25;
+ } else {
+ wFirst = Math.max(30, 40 - 3 * (groupSize - 4));
+ wLast = minWidthPercent;
+ }
+
+ const widths: number[] = [];
+ for (let i = 0; i < groupSize; i++) {
+ const t = groupSize > 1 ? i / (groupSize - 1) : 0;
+ const easedT = Math.pow(1 - t, curveExponent);
+ const width = wLast + (wFirst - wLast) * easedT;
+ widths.push(Math.max(minWidthPercent, width));
+ }
+
+ return widths;
+}
+
/**
* Rounds a number to 3 decimal places using standard rounding
*/
@@ -95,14 +143,13 @@ export function buildOverlapGroups(sortedEvents: CalendarEvent[]): CalendarEvent
/**
* Calculates layout information for all events including position and z-index
- * Dynamically adjusts offset step to prevent overflow when many events overlap
- * Uses safety margin and floor rounding to guarantee no overflow even with CSS box model effects
+ * Uses variable widths and spreads events across full width with last event aligned to right edge
*/
export function calculateEventLayouts(
events: CalendarEvent[],
config: OverlapLayoutConfig = {}
): EventLayout[] {
- const { baseWidthPercent, offsetStepPercent, baseZIndex, safetyMarginPercent } = {
+ const { baseZIndex, safetyMarginPercent, minWidthPercent, curveExponent } = {
...DEFAULT_CONFIG,
...config,
};
@@ -114,30 +161,43 @@ export function calculateEventLayouts(
groups.forEach((group, groupIndex) => {
const groupSize = group.length;
- const allowedOffsetSpace = Math.max(0, 100 - baseWidthPercent - safetyMarginPercent);
- const stepUsed = Math.min(
- offsetStepPercent,
- allowedOffsetSpace / Math.max(1, groupSize - 1)
- );
-
- group.forEach((event, indexInGroup) => {
- const leftRaw = indexInGroup * stepUsed;
- const left = round3(leftRaw);
-
- const maxWidthCap = 100 - left - safetyMarginPercent;
- const widthCap = Math.min(baseWidthPercent, maxWidthCap);
-
- const width = floor3(Math.max(0, widthCap));
-
+
+ const widths = calculateVariableWidths(groupSize, minWidthPercent, curveExponent);
+ const Rmax = 100 - safetyMarginPercent;
+
+ if (groupSize === 1) {
+ const width = floor3(Math.min(widths[0], Rmax));
layouts.push({
- event,
- leftOffsetPercent: left,
+ event: group[0],
+ leftOffsetPercent: 0,
widthPercent: width,
- baseZIndex: baseZIndex + indexInGroup,
+ baseZIndex: baseZIndex,
groupIndex,
- indexInGroup,
+ indexInGroup: 0,
});
- });
+ } else {
+ const Rmin = widths[0];
+
+ group.forEach((event, indexInGroup) => {
+ const t = indexInGroup / (groupSize - 1);
+ const ri = Rmin + (Rmax - Rmin) * t;
+ const leftRaw = ri - widths[indexInGroup];
+ const left = round3(leftRaw);
+
+ const maxWidthCap = Rmax - left;
+ const widthCap = Math.min(widths[indexInGroup], maxWidthCap);
+ const width = floor3(Math.max(0, widthCap));
+
+ layouts.push({
+ event,
+ leftOffsetPercent: left,
+ widthPercent: width,
+ baseZIndex: baseZIndex + indexInGroup,
+ groupIndex,
+ indexInGroup,
+ });
+ });
+ }
});
return layouts;