Skip to content

A platform for community discussion. Free, open, simple.

FIX: Use getBoundingClientRect for welcome banner viewport detection (#37204)

Régis Hanole391f663b4a9
diff --git a/frontend/discourse/app/components/welcome-banner.gjs b/frontend/discourse/app/components/welcome-banner.gjs
index 735e9652a67..4a7d895e8a1 100644
--- a/frontend/discourse/app/components/welcome-banner.gjs
+++ b/frontend/discourse/app/components/welcome-banner.gjs
@@ -8,7 +8,6 @@ import PluginOutlet from "discourse/components/plugin-outlet";
import SearchMenu from "discourse/components/search-menu";
import bodyClass from "discourse/helpers/body-class";
import concatClass from "discourse/helpers/concat-class";
-import isElementInViewport from "discourse/lib/is-element-in-viewport";
import { prioritizeNameFallback } from "discourse/lib/settings";
import { sanitize } from "discourse/lib/text";
import { defaultHomepage, escapeExpression } from "discourse/lib/utilities";
@@ -34,18 +33,24 @@ export default class WelcomeBanner extends Component {
@service search;
checkViewport = modifier((element) => {
- const observer = new IntersectionObserver(
- ([entry]) => {
- this.search.welcomeBannerSearchInViewport = entry.isIntersecting;
- },
- { threshold: 1.0 }
- );
+ const checkVisibility = () => {
+ // Use getBoundingClientRect for reliable visibility detection.
+ // IntersectionObserver's isIntersecting can return stale values during
+ // SPA navigation, but getBoundingClientRect is always accurate.
+ const { top, bottom } = element.getBoundingClientRect();
+ const isFullyVisible = top >= 0 && bottom <= window.innerHeight;
+ this.search.welcomeBannerSearchInViewport = isFullyVisible;
+ };
+ // Use IntersectionObserver only as a trigger for when to check visibility,
+ // not to determine actual visibility state.
+ const threshold = 1.0;
+ const observer = new IntersectionObserver(checkVisibility, { threshold });
observer.observe(element);
- // Synchronously check if the element is in the viewport on initial setup
- // to avoid a flash of the header search before the async IntersectionObserver callback fires
- this.search.welcomeBannerSearchInViewport = isElementInViewport(element);
+ // Set to true immediately when the modifier sets up, since the welcome banner
+ // is always rendered at the top of the page.
+ this.search.welcomeBannerSearchInViewport = true;
return () => {
observer.disconnect();