When viewing code files on GitHub, you can click on a symbol (i.e. identifier) to see a list of places where the symbol is mentioned (i.e. declarations, definitions, references). You can click on any listed location to jump to that location (file and line). On destop, this listing is shown in a sidebar that can be toggled on and off. On mobile, it appears in a dialog.
Unfortunately, on mobile, closing the dialog resets your scroll position to the line of code that you originally opened the dialog from. This is due to mistakes in the dialog implementation:
-
The dialog uses a
useFocusTrap
React hook (in node_modules/@primer/react/lib-esm/hooks/useFocusTrap.js) to trap focus. When the hook is first installed, it checks what element currently has focus, so that when the dialog is dismissed, it can return focus to that element. The hook doesn't know or care whether code on the page has deliberately scrolled to something else while the focus trap is active; during cleanup, the hook will always re-focus the element that had focus before the hook activated. This is what causes the page to forcibly scroll back to the line that originally opened the dialog. -
Even if you find a way to kill
useFocusTrap
's forced focus on cleanup, theCodeNavInfoPanel
(in app/assets/modules/react-code-view/components/blob/BlobContent/CodeNav/CodeNavInfoPanel.tsx) specifically has anonClose
function which attempts to focus#symbols-button
, forcing the scroll position to the top of the page. This button, at the top of the code listing, toggles visibility of the symbol sidebar on desktop. (This focus behavior would make sense when displaying the symbols pane as a sidebar, but is, frankly, dumb when displaying it as a dialog that opens when clicking anywhere in the code view.) The function in question is a React prop, so it's not even defined as part ofCodeNavInfoPanel
; you have to dig upward through the component's users to figure out where the function is even defined (InnerPanelContent
in app/assets/modules/react-code-view/components/PanelContent.tsx).
(The source code links above will probably 404 if accessed directly, but those are the URLs that you'll see in browser devtools, based on GitHub's source maps.)
This issue was reported to GitHub at the start of 2023. It affects desktop versions of Chrome and Firefox when they emulate mobile view via devtools, and it affects Chromium on mobile. As of this writing, it still hasn't been fixed.