c/frontend › command
0votes

Scroll-to-Top on Every Navigation

PromptLow risk
submitted by CommandVault editors to c/frontend · 21 uses

This commands ensures that when a user lands on any page of an application, the user gets taken to the very top of that page, and not the position the user was on the previous page.

Prompt · 2 variables
Make every navigation in my app start at the top of the new page.

Stack: {{stack}}
Exceptions that must keep or restore scroll position: {{goal}}

Requirements:
1. On each route change, scroll to (0, 0) before or as the new page paints, so there's no visible jump.
2. Respect the exceptions above. Browser back/forward should restore the previous position unless I said otherwise, and links to #anchors should scroll to the anchor.
3. Handle scrollable containers too, not just window, if the layout scrolls inside a main element.
4. Move keyboard focus to the new page's main heading so screen-reader users know the page changed.
5. Use the framework's built-in mechanism if it has one before writing custom code, and explain why.

Tell me how to test it: three navigation scenarios to click through and what should happen in each.
Exceptions, e.g. 'back/forward navigation and in-page #anchors', or 'none'
Framework, language and versions, e.g. Next.js 16 App Router + TypeScript
0 commentsreport

Try it here

Fill in {{goal}} above first for a better answer, or run it as is and the AI will assume sensible values.

or open in ChatGPT · Claude · Perplexity

How to use this prompt

  1. Fill in the 2 blanks above. The more specific you are, the better the answer.
  2. Copy it and paste it into ChatGPT, Claude, Gemini, Cursor or Codex, or use "Try it here" for a quick test.
  3. Read the answer critically: check anything it claims about your code or data before you rely on it.
  • {{goal}} Exceptions, e.g. 'back/forward navigation and in-page #anchors', or 'none'
  • {{stack}} Framework, language and versions, e.g. Next.js 16 App Router + TypeScript

More from c/frontend

See all of c/frontend →
0 comments

Log in to join the discussion.