From eb8cb3854c094819e778300f103117aaf4c00e8f Mon Sep 17 00:00:00 2001 From: Jackson Harper Date: Fri, 17 Jun 2022 15:59:41 -0700 Subject: [PATCH] Do all scroll watching on the main window This moves all scrolling from child divs to the main window. This improves our keyboard handling, as focus will be given to the body element, not the child div when navigating by keyboard commands, so arrow keys and space bar will work after navigating to the reader with the keyboard commands. --- packages/appreader/src/index.jsx | 1 - .../web/components/patterns/PrimaryHeader.tsx | 36 ++++++++++--------- .../components/templates/PrimaryLayout.tsx | 16 +++------ .../components/templates/article/Article.tsx | 24 ++----------- .../templates/article/ArticleContainer.tsx | 2 -- .../templates/homeFeed/HomeFeedContainer.tsx | 17 ++++----- packages/web/lib/hooks/useFetchMoreScroll.tsx | 32 ++++------------- packages/web/lib/hooks/useScrollWatcher.tsx | 31 +++------------- .../web/pages/[username]/[slug]/index.tsx | 2 -- .../web/pages/app/[username]/[slug]/index.tsx | 1 - packages/web/pages/home.tsx | 7 ++-- 11 files changed, 47 insertions(+), 122 deletions(-) diff --git a/packages/appreader/src/index.jsx b/packages/appreader/src/index.jsx index 8dde91f96..424bb517e 100644 --- a/packages/appreader/src/index.jsx +++ b/packages/appreader/src/index.jsx @@ -36,7 +36,6 @@ const App = () => { toolbarControl?: JSX.Element alwaysDisplayToolbar?: boolean setShowLogoutConfirmation: (showShareModal: boolean) => void @@ -48,19 +43,26 @@ export function PrimaryHeader(props: HeaderProps): JSX.Element { }) ) - const setScrollWatchedElement = useScrollWatcher( - (changeset: ScrollOffsetChangeset) => { - const isScrolledBeyondMinThreshold = changeset.current.y >= 50 - const isScrollingDown = changeset.current.y > changeset.previous.y +/* + useRegisterActions([ + { + id: 'lightTheme', + section: 'Preferences', + name: 'Change theme (lighter) ', + shortcut: ['v', 'l'], + keywords: 'light theme', + perform: () => lightenTheme(), }, - 0 - ) - - useEffect(() => { - if (props.scrollElementRef) { - setScrollWatchedElement(props.scrollElementRef.current) - } - }, [props.scrollElementRef, setScrollWatchedElement]) + { + id: 'darkTheme', + section: 'Preferences', + name: 'Change theme (darker) ', + shortcut: ['v', 'd'], + keywords: 'dark theme', + perform: () => darkenTheme(), + }, + ]) + */ const initAnalytics = useCallback(() => { setupAnalytics(props.user) diff --git a/packages/web/components/templates/PrimaryLayout.tsx b/packages/web/components/templates/PrimaryLayout.tsx index 973529cf3..295e7e1a8 100644 --- a/packages/web/components/templates/PrimaryLayout.tsx +++ b/packages/web/components/templates/PrimaryLayout.tsx @@ -21,7 +21,6 @@ type PrimaryLayoutProps = { pageTestId: string hideHeader?: boolean pageMetaDataProps?: PageMetaDataProps - scrollElementRef?: MutableRefObject headerToolbarControl?: JSX.Element alwaysDisplayToolbar?: boolean } @@ -63,8 +62,8 @@ export function PrimaryLayout(props: PrimaryLayoutProps): JSX.Element { ) : null} - - scrollElementRef: MutableRefObject articleMutations: ArticleMutations } @@ -89,16 +88,9 @@ export function Article(props: ArticleProps): JSX.Element { } }, [readingProgress]) - const setScrollWatchedElement = useScrollWatcher( + useScrollWatcher( (changeset: ScrollOffsetChangeset) => { - const scrollContainer = props.scrollElementRef.current - if (scrollContainer) { - const newReadingProgress = - (changeset.current.y + scrollContainer.clientHeight) / - scrollContainer.scrollHeight - - debouncedSetReadingProgress(newReadingProgress * 100) - } else if (window && window.document.scrollingElement) { + if (window && window.document.scrollingElement) { const newReadingProgress = window.scrollY / window.document.scrollingElement.scrollHeight const adjustedReadingProgress = @@ -131,10 +123,6 @@ export function Article(props: ArticleProps): JSX.Element { [] ) - useEffect(() => { - setScrollWatchedElement(props.scrollElementRef.current) - }, [props.scrollElementRef, setScrollWatchedElement]) - // Scroll to initial anchor position useEffect(() => { if (typeof window === 'undefined') { @@ -175,17 +163,11 @@ export function Article(props: ArticleProps): JSX.Element { } const calculatedOffset = calculateOffset(anchorElement) - - if (props.scrollElementRef.current) { - props.scrollElementRef.current?.scroll(0, calculatedOffset - 100) - } else { - window.document.documentElement.scroll(0, calculatedOffset - 100) - } + window.document.documentElement.scroll(0, calculatedOffset - 100) } } }, [ props.highlightReady, - props.scrollElementRef, props.initialAnchorIndex, props.initialReadingProgress, shouldScrollToInitialPosition, diff --git a/packages/web/components/templates/article/ArticleContainer.tsx b/packages/web/components/templates/article/ArticleContainer.tsx index 1811e9cf0..a79e415c6 100644 --- a/packages/web/components/templates/article/ArticleContainer.tsx +++ b/packages/web/components/templates/article/ArticleContainer.tsx @@ -24,7 +24,6 @@ type ArticleContainerProps = { article: ArticleAttributes labels: Label[] articleMutations: ArticleMutations - scrollElementRef: MutableRefObject isAppleAppEmbed: boolean highlightBarDisabled: boolean highlightsBaseURL: string @@ -281,7 +280,6 @@ export function ArticleContainer(props: ArticleContainerProps): JSX.Element { articleId={props.article.id} content={props.article.content} initialAnchorIndex={props.article.readingProgressAnchorIndex} - scrollElementRef={props.scrollElementRef} articleMutations={props.articleMutations} />