Skip to content

fix(profiling): Prevent landing page from scrolling on context menu #85273

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

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions static/app/utils/profiling/hooks/useKeyboardNavigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,32 @@ export function useKeyboardNavigation() {

const {tabIndex, setTabIndex, onKeyDown} = useRovingTabIndex(items);

// There's no easy way to subscribe to the position of an element.
// Instead we just check if the menuRef is on screen each render.
//
// This assumes that the menuRef is positioned at (-9999, -9999)
// when it's first rendered.
const boundingRect = menuRef?.getBoundingClientRect();
const offScreen =
!boundingRect ||
(boundingRect.top < 0 &&
boundingRect.bottom < 0 &&
boundingRect.left < 0 &&
boundingRect.right < 0);

useEffect(() => {
if (menuRef) {
if (tabIndex === null) {
menuRef.focus();
}
// Make sure the menu is on screen before trying to focus it.
// Otherwise, it will scroll to the top of the page. This is
// because when the menuRef is first rendered, it is given a
// position at (-9999, -9999) while we try to determine its
// size so it can be properly position.
//
// This has the effect of waiting until the menuRef is correctly
// positioned before trying to focus.
if (menuRef && !offScreen && tabIndex === null) {
menuRef.focus();
}
}, [menuRef, tabIndex]);
}, [offScreen, menuRef, tabIndex]);

function getMenuProps() {
return {
Expand Down
Loading