-
-
Notifications
You must be signed in to change notification settings - Fork 131
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
base: master
Are you sure you want to change the base?
enhance(carousel): Close carousel on browser back navigation #2262
Conversation
58be990
to
7607088
Compare
7607088
to
b76ced7
Compare
There was a problem hiding this 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 callrouter.back()
on close - Updates
Carousel
to re-run the overflow option viasetOptions
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 tobasePath
orpathWithoutHash
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'sclose()
callback when the URL no longer contains#carousel
.
router.push(url, url + '#carousel', { shallow: true })
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: Carousel Hash Management and Component Update Issues
The carousel's URL hash management is flawed: it unconditionally replaces existing hashes with #carousel
upon opening, causing users to lose their original navigation context (e.g., #comment-id
) when navigating back. The modal also fails to close automatically via browser back navigation due to a missing hash change listener. Furthermore, if the initial src
is not found, the CarouselOverflow
component is rendered without props and is never updated because its useEffect
early-exits.
components/carousel.js#L119-L136
stacker.news/components/carousel.js
Lines 119 to 136 in b76ced7
const showCarousel = useCallback(({ src }) => { | |
const url = router.asPath.split('#')[0] | |
if (window.location.hash !== '#carousel') { | |
router.push(url, url + '#carousel', { shallow: true }) | |
} | |
showModal((close, setOptions) => { | |
return <Carousel close={close} mediaArr={Array.from(media.current.entries())} src={src} setOptions={setOptions} /> | |
}, { | |
fullScreen: true, | |
overflow: <CarouselOverflow {...media.current.get(src)} />, | |
onClose: () => { | |
if (window.location.hash === '#carousel') { | |
router.back() | |
} | |
} | |
}) | |
}, [showModal, media.current, router]) |
Was this report helpful? Give feedback by reacting with 👍 or 👎
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. |
Description
Add to browser history stack when opening carousel, so that navigating back on browser should close the carousel modal, not leave the post entirely.
closes #1508
Screenshots
Screen.Recording.2025-07-08.at.18.00.49.mov
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