Skip to content

Conversation

gumaerc
Copy link
Contributor

@gumaerc gumaerc commented Dec 17, 2024

What are the relevant tickets?

Closes https://github.com/mitodl/hq/issues/6293

Description (What does it do?)

This PR fixes various scrolling related to the new learning resource drawer. Firstly, clicking the "Show more" link on the description or the start dates were causing the main window to scroll to the top. This is caused by the scroll property on NextLink: https://nextjs.org/docs/pages/api-reference/components/link#scroll. It defaults to true, so whenever you click a NextLink, nextjs scrolls the window to the top. scroll can now be passed as a boolean argument to our Link component, which passes it on to the NextLink component. For the links inside the drawer, we pass false which prevents the scroll to the top from happening.

The second scrolling option here has to do with the cards in the recommendation carousel. Currently clicking them reloads the drawer, but does not scroll the drawer to the top. In order to fix this, code was added to LearningResourceExpandedV2 to reset the scroll position if the resource ID in the query string has changed.

How can this be tested?

  • If you have Posthog set up locally, enable the lr_drawer_v2 flag
  • If you don't have Posthog set up locally, you can force drawerV2 to be true in LearningResourceDrawer.tsx
  • Ensure that you have backpopulated some courses into your local database for testing
  • On line 119 of LearningResourceDrawerV2, temporarily set carousels={[similarResourcesCarousel, similarResourcesCarousel, similarResourcesCarousel]} to create enough scrollable content to demonstrate the second issue detailed above
  • Spin up this branch of mit-learn
  • Visit the search page at http://localhost:8062/search
  • Search for a course with a long description and many start dates, such as "Global Manufacturing Leadership Program"
  • When the search result is returned, scroll to the bottom of the page and then click the card to bring up the drawer
  • Click "Show more" both below the description and near the start dates, verifying that the page behind the drawer does not scroll to the top
  • Scroll down within the drawer and click one of the cards in the recommendation carousels, verifying that the drawer is scrolled to the top

@gumaerc gumaerc changed the title v2 drawer scrolling issues fix v2 drawer scrolling issues Dec 17, 2024
@gumaerc gumaerc added the Needs Review An open Pull Request that is ready for review label Dec 17, 2024
@gumaerc gumaerc force-pushed the cg/v2-drawer-scrolling-issues branch from 6102c38 to 25cd6eb Compare December 18, 2024 16:42
@ChristopherChudzicki ChristopherChudzicki self-assigned this Dec 18, 2024
Copy link
Contributor

@ChristopherChudzicki ChristopherChudzicki left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works well. One question for you about possible simplification.

const outerContainerRef = useRef<HTMLDivElement>(null)
const searchParams = useSearchParams()
const searchParamsRef = useRef(searchParams)
useEffect(() => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this work instead?

  const outerContainerRef = useRef<HTMLDivElement>(null)
  useEffect(() => {
    if (outerContainerRef.current) {
      outerContainerRef.current.scrollTo(0, 0)
    }
  }, [resource?.id])

Only possible quirk i see is that if you scroll while the resource is still loading, it will scroll up when when the resource finishes loading.

If that seems undesirable, maybe pass the ID into v2expanded as a prop. (Alternatively the effect could run when searchParams.get("resource") changes, and eliminate the search params ref, but currently v2expanded has no knowledge of routing, e.g., it doesn't know the "resource" querykey is involved at all, mildly nice to presreve that..)

@gumaerc
Copy link
Contributor Author

gumaerc commented Dec 18, 2024

@ChristopherChudzicki Thanks, this is ready for another look now

@gumaerc gumaerc merged commit 4b543e4 into main Dec 18, 2024
11 checks passed
This was referenced Dec 20, 2024
This was referenced Jan 2, 2025
@rhysyngsun rhysyngsun deleted the cg/v2-drawer-scrolling-issues branch February 7, 2025 20:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Review An open Pull Request that is ready for review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants