Issue 374 Calendar Popup Ran Off The Screen - edmozley/freeitsm GitHub Wiki
๐ #374 The calendar popup ran off the bottom of the screen
GitHub #374, reported and fixed by @cloudcogsio ยท Calendar, Change Management ยท since the quick-view popups were added, fixed in 5.5.0 (#2991)
What you saw
Calendar โ Month, on a normal laptop-height window: click an event in the last row of the month. The quick-view popup opened below the event and was cut off by the bottom of the window. It could not be moved and the page did not scroll to it, so the details and the Edit button were out of reach. The Change calendar did the same with its popup and its Open change button.
What was wrong
Both popups were placed with one line, measuring only the clicked item:
// assets/js/itsm_calendar.js - showEventPopup() (and the same in change-calendar.js)
const rect = clickEvent.target.getBoundingClientRect();
popup.style.top = `${rect.bottom + 10}px`;
rect.bottom + 10 is always below the item, however close the item is to the bottom of the window. Nothing compared it with window.innerHeight. Left was clamped against the window width; top never was.
Was it more than one place? Two: the Calendar and the Change calendar, each with its own copy. The Tickets calendar (a centred window) and the Tasks calendar (a side panel) place theirs differently and were never affected. On a phone, both popups become a bottom sheet (mobile.css, !important positions), which was also fine.
The fix
@cloudcogsio sent the fix on a branch; it is merged with his authorship.
1. Place it after measuring it. The popup is shown at top: 0 first so it has a real height, then:
const popupHeight = popup.offsetHeight;
let top = rect.bottom + 10; // below, as before...
if (top + popupHeight > window.innerHeight - 8) {
top = rect.top - 10 - popupHeight; // ...or above when there is no room
}
popup.style.top = `${Math.max(8, Math.min(top, window.innerHeight - 8 - popupHeight))}px`;
The last line keeps it inside the window either way, 8px from each edge.
2. A popup taller than the window scrolls inside itself: max-height plus overflow-y: auto on both.
The wrong turn worth knowing about. The Change calendar's popup has padding: 16px and the default box-sizing: content-box, so max-height: calc(100vh - 16px) limited only its content: with the padding it was still 24px taller than the window, and a long change ran off the bottom anyway. The test caught it; the change popup's limit is calc(100vh - 48px) (16px of margin plus 2 ร 16px of padding). Switching it to border-box instead would also have shrunk its min-width and max-width, which changes its look, so it was not done.
๐ Files
| File | Change |
|---|---|
๐ก assets/js/itsm_calendar.js |
showEventPopup() places the popup above when there is no room below |
๐ก assets/js/change-calendar.js |
showChangePopup(), the same |
๐ต assets/css/itsm_calendar.css |
.event-popup max-height and scroll |
๐ต change-management/calendar.php |
.change-popup max-height that allows for its padding, and scroll |
๐ข tests/calendar-popup-position.php |
The real scripts and CSS in headless Chrome |
โช calendar/index.php, calendar/table/index.php |
Cache-busters |
How it was verified
Reproduced first in a 1440ร1000 headless window, with the real itsm_calendar.js, change-calendar.js and their CSS: an event 40px from the bottom put the Calendar popup at 894-1112px in a 904px-high window, and the change popup at 894-1160px.
tests/calendar-popup-position.php: 8 passed. For each popup: an item near the top still opens it below; an item at the bottom keeps the whole popup on screen, above the item and not over it; a popup taller than the window fits and scrolls. Control: on the old code 6 of the 8 fail.
What this means for you
Update to 5.5.0: events low on the screen open their popup above them. Nothing else changes.
For developers: when you place something next to a clicked element, measure the thing you are placing and check both edges of the window, not only the one the layout usually needs. And remember that max-height on a padded content-box element does not include the padding.
Related: Calendar ยท Change Management