Skip to content

enhance(carousel): Close carousel on browser back navigation #2262

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

brymut
Copy link
Contributor

@brymut brymut commented Jul 5, 2025

Description

Add to browser history stack when opening carousel use URL hashes to track modal state, so that navigating back on browser should close the carousel modal, not leave the post entirely.

closes #1508

Screenshots

Screen.Recording.2025-08-18.at.17.36.26.mov

(Updated screen recording)

Additional Context

Was anything unclear during your work on this PR? Anything we should definitely take a closer look at?

Checklist

Are your changes backward compatible? Please answer below:
Y
For example, a change is not backward compatible if you removed a GraphQL field or dropped a database column.

On a scale of 1-10 how well and how have you QA'd this change and any features it might affect? Please answer below:
10

For frontend changes: Tested on mobile, light and dark mode? Please answer below:
Y

Did you introduce any new environment variables? If so, call them out explicitly here:
N

@brymut brymut force-pushed the enhance/browser-back-forward-navigation-carousel branch 3 times, most recently from 7607088 to b76ced7 Compare July 8, 2025 15:06
@brymut brymut marked this pull request as ready for review July 8, 2025 15:06
@brymut brymut requested review from ekzyis, Copilot and huumn and removed request for Copilot July 8, 2025 15:06
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR enhances the carousel modal so that opening it pushes a history state and pressing the browser back button closes the carousel instead of navigating away.

  • Integrates Next.js router in CarouselProvider to push a #carousel hash when opening and call router.back() on close
  • Updates Carousel to re-run the overflow option via setOptions whenever the index or source changes
  • Adds useRouter import and adjusts effect/callback dependencies for proper behavior
Comments suppressed due to low confidence (3)

components/carousel.js:121

  • [nitpick] The variable url is ambiguous—consider renaming it to basePath or pathWithoutHash to clarify that it strips the hash fragment.
    const url = router.asPath.split('#')[0]

components/carousel.js:120

  • [nitpick] The new history‐push and back‐navigation behavior isn’t covered by existing tests. Add a test to simulate opening the carousel and pressing the back button to ensure it closes correctly.
  const showCarousel = useCallback(({ src }) => {

components/carousel.js:123

  • Pushing history state alone won't close the carousel when the user hits the back button. You should listen for popstate or Next.js router events (e.g., router.events.on('routeChangeStart')) and call the modal's close() callback when the URL no longer contains #carousel.
      router.push(url, url + '#carousel', { shallow: true })

cursor[bot]

This comment was marked as outdated.

@brymut
Copy link
Contributor Author

brymut commented Jul 10, 2025

Happy to have a look again based on the cursor feedback, but I can confirm that the modal does close automatically upon navigating backwards, as shown in the PR description screen recording.

@ekzyis ekzyis mentioned this pull request Jul 24, 2025
cursor[bot]

This comment was marked as outdated.

Copy link
Member

@ekzyis ekzyis left a comment

Choose a reason for hiding this comment

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

Closing the carousel now always scrolls us to the top:

2025-07-24.21-37-19.mp4

@brymut
Copy link
Contributor Author

brymut commented Jul 24, 2025

Closing the carousel now always scrolls us to the top:

Let me have a look at that

@brymut brymut force-pushed the enhance/browser-back-forward-navigation-carousel branch from c85d3b1 to df72819 Compare July 25, 2025 23:12
@huumn huumn marked this pull request as draft August 12, 2025 22:18
@brymut brymut force-pushed the enhance/browser-back-forward-navigation-carousel branch from df72819 to 668ade3 Compare August 18, 2025 14:24
@brymut brymut force-pushed the enhance/browser-back-forward-navigation-carousel branch from 668ade3 to 615b2f0 Compare August 18, 2025 14:34
@brymut
Copy link
Contributor Author

brymut commented Aug 18, 2025

Closing the carousel now always scrolls us to the top:

Should work now, switched to checking the URL hash instead, less complicated and seems to not have the scrolling issue.

Screen.Recording.2025-08-18.at.17.36.26.mov

@brymut brymut marked this pull request as ready for review August 18, 2025 14:38
@brymut brymut requested a review from ekzyis August 18, 2025 14:39
@brymut brymut closed this Aug 18, 2025
@brymut brymut deleted the enhance/browser-back-forward-navigation-carousel branch August 18, 2025 15:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Back/forward browser navigation for carousel images
2 participants