Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

Auto-scroll time grid to opening hour on layout (#5909)

Jacob Beckerman155cdf2ff6b2
diff --git a/apps/web/src/features/calendar/components/CalendarGrid.tsx b/apps/web/src/features/calendar/components/CalendarGrid.tsx
index b1576f2215..428e6b53c6 100644
--- a/apps/web/src/features/calendar/components/CalendarGrid.tsx
+++ b/apps/web/src/features/calendar/components/CalendarGrid.tsx
@@ -18,6 +18,7 @@ import {
type JSX,
Show,
} from 'solid-js';
+import { useTimeGridOpeningScroll } from '../hooks/use-time-grid-opening-scroll';
import {
type CalendarEvent,
type CalendarPeriodView,
@@ -39,6 +40,7 @@ import {
formatCalendarTime,
formatCompactCalendarTime,
} from '../utils/time-format';
+import { TIME_GRID_OPENING_SCROLL_TIME } from '../utils/time-grid-scroller';
import { EventContent } from './EventContent';
import '../calendar.css';
@@ -68,19 +70,6 @@ function CurrentTimeAxisIndicator(props: {
);
}
-function getLocalScrollTime() {
- const now = new Date();
- const minutesSinceMidnight = now.getHours() * 60 + now.getMinutes();
- const scrollMinutes = Math.max(
- 0,
- Math.floor((minutesSinceMidnight - 60) / 30) * 30
- );
- const hours = Math.floor(scrollMinutes / 60);
- const minutes = scrollMinutes % 60;
-
- return `${String(hours).padStart(2, '0')}:${String(minutes).padStart(2, '0')}:00`;
-}
-
export interface CalendarGridHandle extends FullCalendarContextValue {
element: Accessor<HTMLDivElement | undefined>;
eventElements: Map<string, HTMLElement>;
@@ -133,6 +122,8 @@ function CalendarGridHost(props: {
const calendar = useFullCalendar();
const [element, setElement] = createSignal<HTMLDivElement>();
+ useTimeGridOpeningScroll(element, calendar.api);
+
const handle: CalendarGridHandle = {
api: calendar.api,
dateInfo: calendar.dateInfo,
@@ -224,7 +215,7 @@ export function CalendarGrid(props: CalendarGridProps) {
allDayText="All day"
nowIndicator
headerToolbar={false}
- scrollTime={getLocalScrollTime()}
+ scrollTime={TIME_GRID_OPENING_SCROLL_TIME}
scrollTimeReset={false}
weekends={props.settings.showWeekends}
firstDay={props.settings.weekStartsOn}
diff --git a/apps/web/src/features/calendar/hooks/use-time-grid-opening-scroll.ts b/apps/web/src/features/calendar/hooks/use-time-grid-opening-scroll.ts
new file mode 100644
index 0000000000..29c468aa8c
--- /dev/null
+++ b/apps/web/src/features/calendar/hooks/use-time-grid-opening-scroll.ts
@@ -0,0 +1,48 @@
+import type { Calendar } from '@fullcalendar/core';
+import { createResizeObserver } from '@solid-primitives/resize-observer';
+import { type Accessor, createEffect, on, onCleanup } from 'solid-js';
+import {
+ applyTimeGridOpeningScroll,
+ TIME_GRID_OPENING_SCROLL_TIME,
+} from '../utils/time-grid-scroller';
+
+/**
+ * Opens a time grid on {@link TIME_GRID_OPENING_SCROLL_TIME} even when
+ * FullCalendar rendered it before it had layout.
+ *
+ * FullCalendar positions the hour scroller once, while it renders, so a grid
+ * that mounts hidden or unsized stays at midnight after it appears. Watching
+ * the grid for its first laid-out frame lets the opening hour be applied then
+ * instead.
+ */
+export function useTimeGridOpeningScroll(
+ root: Accessor<HTMLElement | undefined>,
+ api: Accessor<Calendar | undefined>
+) {
+ let hasLaidOut = false;
+ let frame: number | undefined;
+
+ const applyOpeningScroll = () => {
+ frame = undefined;
+ hasLaidOut = applyTimeGridOpeningScroll(
+ root(),
+ api(),
+ TIME_GRID_OPENING_SCROLL_TIME
+ );
+ };
+
+ const scheduleOpeningScroll = () => {
+ if (hasLaidOut || frame !== undefined) return;
+
+ frame = requestAnimationFrame(applyOpeningScroll);
+ };
+
+ // A grid mounted with layout is measured on the frame after it renders; one
+ // mounted without layout is measured when becoming visible resizes it.
+ createEffect(on([root, api], scheduleOpeningScroll));
+ createResizeObserver(root, scheduleOpeningScroll);
+
+ onCleanup(() => {
+ if (frame !== undefined) cancelAnimationFrame(frame);
+ });
+}
diff --git a/apps/web/src/features/calendar/utils/time-grid-scroller.test.ts b/apps/web/src/features/calendar/utils/time-grid-scroller.test.ts
new file mode 100644
index 0000000000..120cb6f894
--- /dev/null
+++ b/apps/web/src/features/calendar/utils/time-grid-scroller.test.ts
@@ -0,0 +1,99 @@
+import type { Calendar } from '@fullcalendar/core';
+import { describe, expect, it, vi } from 'vitest';
+import {
+ applyTimeGridOpeningScroll,
+ TIME_GRID_OPENING_SCROLL_TIME,
+} from './time-grid-scroller';
+
+const calendarStub = () =>
+ ({
+ scrollToTime: vi.fn(),
+ updateSize: vi.fn(),
+ }) as unknown as Calendar & {
+ scrollToTime: ReturnType<typeof vi.fn>;
+ updateSize: ReturnType<typeof vi.fn>;
+ };
+
+/** A grid root shaped like FullCalendar's, with a measurable hour scroller. */
+const gridRoot = ({
+ clientHeight,
+ scrollTop,
+}: {
+ clientHeight: number;
+ scrollTop: number;
+}) => {
+ const root = document.createElement('div');
+ const timeGrid = document.createElement('div');
+ timeGrid.className = 'fc-timegrid';
+ const harness = document.createElement('div');
+ harness.className = 'fc-scroller-harness-liquid';
+ const scroller = document.createElement('div');
+ scroller.className = 'fc-scroller';
+
+ harness.append(scroller);
+ timeGrid.append(harness);
+ root.append(timeGrid);
+
+ Object.defineProperty(scroller, 'clientHeight', { value: clientHeight });
+ scroller.scrollTop = scrollTop;
+
+ return root;
+};
+
+describe('applyTimeGridOpeningScroll', () => {
+ it('scrolls a grid that rendered before it had layout', () => {
+ const calendar = calendarStub();
+
+ expect(
+ applyTimeGridOpeningScroll(
+ gridRoot({ clientHeight: 600, scrollTop: 0 }),
+ calendar,
+ TIME_GRID_OPENING_SCROLL_TIME
+ )
+ ).toBe(true);
+ expect(calendar.updateSize).toHaveBeenCalledOnce();
+ expect(calendar.scrollToTime).toHaveBeenCalledWith(
+ TIME_GRID_OPENING_SCROLL_TIME
+ );
+ });
+
+ it('leaves an already scrolled grid alone', () => {
+ const calendar = calendarStub();
+
+ expect(
+ applyTimeGridOpeningScroll(
+ gridRoot({ clientHeight: 600, scrollTop: 289 }),
+ calendar,
+ TIME_GRID_OPENING_SCROLL_TIME
+ )
+ ).toBe(true);
+ expect(calendar.updateSize).not.toHaveBeenCalled();
+ expect(calendar.scrollToTime).not.toHaveBeenCalled();
+ });
+
+ it('waits for a grid that has not laid out yet', () => {
+ const calendar = calendarStub();
+
+ expect(
+ applyTimeGridOpeningScroll(
+ gridRoot({ clientHeight: 0, scrollTop: 0 }),
+ calendar,
+ TIME_GRID_OPENING_SCROLL_TIME
+ )
+ ).toBe(false);
+ expect(calendar.scrollToTime).not.toHaveBeenCalled();
+ });
+
+ it('ignores a view without an hour scroller', () => {
+ const calendar = calendarStub();
+
+ expect(
+ applyTimeGridOpeningScroll(
+ document.createElement('div'),
+ calendar,
+ TIME_GRID_OPENING_SCROLL_TIME
+ )
+ ).toBe(false);
+ expect(calendar.scrollToTime).not.toHaveBeenCalled();
+ });
+});
diff --git a/apps/web/src/features/calendar/utils/time-grid-scroller.ts b/apps/web/src/features/calendar/utils/time-grid-scroller.ts
index 1ff4f08854..c95225b21f 100644
--- a/apps/web/src/features/calendar/utils/time-grid-scroller.ts
+++ b/apps/web/src/features/calendar/utils/time-grid-scroller.ts
@@ -1,3 +1,5 @@
+import type { Calendar } from '@fullcalendar/core';
+
/** FullCalendar's liquid time-grid scroller — the element that scrolls hours. */
const TIME_GRID_SCROLLER_SELECTOR =
'.fc-timegrid .fc-scroller-harness-liquid > .fc-scroller';
@@ -42,3 +44,37 @@ export function scrollEventChipIntoView(
);
scroller.scrollTop += chipBox.top - scrollerBox.top - centeringOffset;
}
+
+/**
+ * Time of day a time grid opens on, an early-morning start that puts the
+ * workday in view without scrolling.
+ */
+export const TIME_GRID_OPENING_SCROLL_TIME = '07:00:00';
+
+/**
+ * Re-applies the opening scroll position of a grid that rendered without
+ * layout.
+ *
+ * FullCalendar resolves `scrollTime` from slot coordinates measured while it
+ * renders. A grid that mounts hidden, detached, or inside a container that has
+ * not been sized yet measures zeros, treats the scroll as done, and leaves the
+ * scroller at midnight once it becomes visible. Re-measuring and scrolling
+ * again on the first laid-out frame restores the intended opening hour.
+ *
+ * Returns whether the grid has laid out, so callers can stop retrying. A grid
+ * that already scrolled itself, and one a reader has scrolled, are left alone.
+ */
+export function applyTimeGridOpeningScroll(
+ root: HTMLElement | undefined,
+ calendar: Calendar | undefined,
+ scrollTime: string
+): boolean {
+ const scroller = timeGridScroller(root);
+ // Month views have no hour scroller and no opening hour to restore.
+ if (!calendar || !scroller || scroller.clientHeight === 0) return false;
+ if (scroller.scrollTop !== 0) return true;
+
+ calendar.updateSize();
+ calendar.scrollToTime(scrollTime);
+ return true;
+}