scrollIntoView not working when the browser is out of focus, on chromium based browsers

I’m building a react single page app and have noticed that the scrollIntoView function does not work on chrome based browsers (tested on edge&chrome) when the browser is out of focus.
I have a long async operation that involves waiting on a server response. When that response arrives, some elements are being created and the page should scroll them into view. Losing focus causes the scrolling operation to fail.

useEffect(() => {
    const onFocus = () => {
      console.log('Window gained focus')
    }

    const onBlur = () => {
      console.log('Window lost focus: ', currentStage)
    }

    window.addEventListener('focus', onFocus)
    window.addEventListener('blur', onBlur)

    return () => {
      window.removeEventListener('focus', onFocus)
      window.removeEventListener('blur', onBlur)
    }
  }, [])

I use this for debugging purposes ^
and this is the actual scroll logic:

  useLayoutEffect(() => {
    attemptScroll()

    return () => {
      clearTimeout(scrollEndTimer.current)
    }
  }, [currentStage, activeStages])

(the stage changes upon server response)

const attemptScroll = () => {
    console.log(`current stage: ${currentStage.name}`)

    if (!scrollerRef.current) return

    const stageElement = document.getElementById(currentStage?.name)
    if (!stageElement) {
      console.error(`Stage ${currentStage?.name} was not found in the DOM`)
      console.error(currentStage)
      return
    }

    console.log('scrolling to element:', stageElement.current)
    setScrolling(true)
    stageElement.focus()
    stageElement.scrollIntoView({
      behavior: 'smooth',
      block: 'start',
      inline: 'start'
    })

    const checkScrollCompletion = callback => {
      clearTimeout(scrollEndTimer.current)
      const lastScrollLeft = scrollerRef.current.scrollLeft

      scrollEndTimer.current = setTimeout(() => {
        const currentScrollLeft = scrollerRef.current.scrollLeft
        if (currentScrollLeft === lastScrollLeft) {
          callback()
        } else {
          checkScrollCompletion(callback)
        }
      }, 100)
    }

    setScrolling(false)
  }

Just to make is super clear:

  1. server responds
  2. currentStage is updated
  3. use layout effect is called
  4. attemptScroll() is called
  5. all code inside attemptScroll executes, except for scrollIntoView

Even the ‘scrolling to element’ log statement is being written, with the correct element. Everything works as intended, except for the scroll.

I could technically work around this by making the page attempt scrolling when the browser returns to focus, but even after if I ignore the UX hit, I noticed the page loses focus on mobile even when the browser is still open on the screen, so just randomly tapping the screen until something happens is a no go.

Does anybody know why and how I could fix this?

Thanks

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật