mirror of
https://github.com/outline/outline.git
synced 2026-12-05 02:44:34 +03:00
fix: Headings at the end of a document never highlight in the contents (#13455)
* fix: Headings at the end of a document never highlight in the contents The highlight was derived only from the scroll position, so a heading in the final viewport — which can never reach the top of the screen — stayed unhighlighted even when clicked directly in the contents. Keep the clicked heading highlighted until the reader scrolls again. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * Release the selected heading on window scroll only Listening for wheel, touch and key events dropped the highlight when the reader typed without scrolling or scrolled the contents panel, and missed scrollbar drags entirely. The document has already been scrolled to the heading by the time the effect runs, so anchor to that position and release once the window moves away from it. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
cdc10b4564
commit
027476b3be
1 file changed
+40
-4
@@ -8,6 +8,7 @@ import breakpoint from "styled-components-breakpoint";
|
||||
import { EmojiText } from "@shared/components/EmojiText";
|
||||
import { EditorStyleHelper } from "@shared/editor/styles/EditorStyleHelper";
|
||||
import { depths, hideScrollbars, s } from "@shared/styles";
|
||||
import { supportsPassiveListener } from "@shared/utils/browser";
|
||||
import { useDocumentContext } from "~/components/DocumentContext";
|
||||
import useWindowScrollPosition from "~/hooks/useWindowScrollPosition";
|
||||
import { patchLocation } from "~/utils/history";
|
||||
@@ -17,13 +18,22 @@ const HEADING_OFFSET = 20;
|
||||
|
||||
function Contents() {
|
||||
const history = useHistory();
|
||||
const [activeSlug, setActiveSlug] = useState<string>();
|
||||
const [scrolledSlug, setScrolledSlug] = useState<string>();
|
||||
const [selectedSlug, setSelectedSlug] = useState<string>();
|
||||
const scrollPosition = useWindowScrollPosition({
|
||||
throttle: 100,
|
||||
});
|
||||
const { headings } = useDocumentContext();
|
||||
const itemRefs = useRef<Record<string, HTMLLIElement | null>>({});
|
||||
|
||||
// A heading chosen from the contents stays highlighted until the reader
|
||||
// scrolls themselves. Headings near the end of a document cannot reach the
|
||||
// top of the viewport, so the scroll position alone would never select them.
|
||||
const activeSlug =
|
||||
selectedSlug && headings.some((heading) => heading.id === selectedSlug)
|
||||
? selectedSlug
|
||||
: scrolledSlug;
|
||||
|
||||
const handleClick = useCallback(
|
||||
(event: React.MouseEvent<HTMLAnchorElement>, id: string) => {
|
||||
// Let modified clicks (open in new tab, copy link, etc.) fall through to
|
||||
@@ -40,11 +50,37 @@ function Contents() {
|
||||
// Navigate via history so the location state (active sidebar context) is
|
||||
// retained rather than dropped by a native hash navigation.
|
||||
event.preventDefault();
|
||||
setSelectedSlug(id);
|
||||
history.push(patchLocation(history.location, { hash: `#${id}` }));
|
||||
},
|
||||
[history]
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
if (!selectedSlug) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Navigating to the heading scrolls the document during the same commit,
|
||||
// so the position here is the one it came to rest at. Any movement away
|
||||
// from it is the reader scrolling, whichever means they use.
|
||||
const restingPosition = window.pageYOffset;
|
||||
|
||||
const handleScroll = () => {
|
||||
if (window.pageYOffset !== restingPosition) {
|
||||
setSelectedSlug(undefined);
|
||||
}
|
||||
};
|
||||
|
||||
window.addEventListener(
|
||||
"scroll",
|
||||
handleScroll,
|
||||
supportsPassiveListener ? { passive: true } : false
|
||||
);
|
||||
|
||||
return () => window.removeEventListener("scroll", handleScroll);
|
||||
}, [selectedSlug]);
|
||||
|
||||
useEffect(() => {
|
||||
let activeId = headings.length > 0 ? headings[0].id : undefined;
|
||||
|
||||
@@ -63,10 +99,10 @@ function Contents() {
|
||||
}
|
||||
}
|
||||
|
||||
if (activeSlug !== activeId) {
|
||||
setActiveSlug(activeId);
|
||||
if (scrolledSlug !== activeId) {
|
||||
setScrolledSlug(activeId);
|
||||
}
|
||||
}, [scrollPosition, headings, activeSlug]);
|
||||
}, [scrollPosition, headings, scrolledSlug]);
|
||||
|
||||
useEffect(() => {
|
||||
const activeItem = activeSlug ? itemRefs.current[activeSlug] : undefined;
|
||||
|
||||
Reference in new issue
Block a user