diff --git a/app/components/ContentEditable.tsx b/app/components/ContentEditable.tsx
index c3ad930c27..dd714bc539 100644
--- a/app/components/ContentEditable.tsx
+++ b/app/components/ContentEditable.tsx
@@ -1,5 +1,6 @@
import isPrintableKeyEvent from "is-printable-key-event";
import * as React from "react";
+import { useMergeRefs } from "react-merge-refs";
import styled from "styled-components";
import { s } from "@shared/styles";
import { TextHelper } from "@shared/utils/TextHelper";
@@ -137,7 +138,8 @@ function ContentEditable({
// case the component may be rendered with display: none. React 18 may solve
// this in the future by delaying useEffect hooks:
// https://github.com/facebook/react/issues/14536#issuecomment-861980492
- const isVisible = useOnScreen(contentRef);
+ const [onScreenRef, isVisible] = useOnScreen();
+ const mergedRef = useMergeRefs([contentRef, onScreenRef]);
React.useEffect(() => {
if (autoFocus && isVisible && !disabled && !readOnly) {
@@ -195,7 +197,7 @@ function ContentEditable({
{children}
(null);
const [topShadowVisible, setTopShadow] = React.useState(false);
const [bottomShadowVisible, setBottomShadow] = React.useState(false);
@@ -53,40 +52,51 @@ function Scrollable({
const trackTop = !!(shadow || topShadow || fadeTop);
const trackBottom = !!(shadow || bottomShadow || fadeBottom);
- const updateShadows = React.useCallback(() => {
- const c = localRef.current;
- if (!c) {
- return;
- }
- const scrollTop = c.scrollTop;
- setTopShadow(trackTop && scrollTop > 0);
+ const updateShadows = React.useCallback(
+ (c: HTMLDivElement) => {
+ const scrollTop = c.scrollTop;
+ setTopShadow(trackTop && scrollTop > 0);
- const wrapperHeight = c.scrollHeight - c.clientHeight;
- setBottomShadow(trackBottom && wrapperHeight - scrollTop > 1);
- }, [trackTop, trackBottom]);
+ const wrapperHeight = c.scrollHeight - c.clientHeight;
+ setBottomShadow(trackBottom && wrapperHeight - scrollTop > 1);
+ },
+ [trackTop, trackBottom]
+ );
- React.useEffect(() => {
- const c = localRef.current;
- if (!c) {
- return;
- }
+ const handleScroll = React.useCallback(
+ (event: React.UIEvent) => {
+ updateShadows(event.currentTarget);
+ },
+ [updateShadows]
+ );
- updateShadows();
+ // Observe the container and its children for size changes.
+ const observeRef = React.useCallback(
+ (c: HTMLDivElement | null) => {
+ if (!c) {
+ return;
+ }
- const observer = new ResizeObserver(updateShadows);
- observer.observe(c);
+ updateShadows(c);
- for (const child of Array.from(c.children)) {
- observer.observe(child);
- }
+ const observer = new ResizeObserver(() => updateShadows(c));
+ observer.observe(c);
- return () => observer.disconnect();
- }, [updateShadows]);
+ for (const child of Array.from(c.children)) {
+ observer.observe(child);
+ }
+
+ return () => observer.disconnect();
+ },
+ [updateShadows]
+ );
+
+ const mergedRef = useMergeRefs([observeRef, ref]);
return (
(null);
+ // Hold the scroll element in state so the SidebarScrollProvider can
+ // re-render descendants with it.
const [scrollArea, setScrollArea] = useState(null);
- useEffect(() => {
- setScrollArea(scrollRef.current);
- }, []);
const sectionOrder = normalizeSidebarSectionOrder(
user.getPreference(UserPreference.SidebarSectionOrder, [])
@@ -139,7 +133,7 @@ function AppSidebar() {
{can.createDocument && }
-
+
{sectionOrder.map((section) => (
diff --git a/app/components/Sidebar/components/DocumentLink.tsx b/app/components/Sidebar/components/DocumentLink.tsx
index ef5e0459d8..34e2660dde 100644
--- a/app/components/Sidebar/components/DocumentLink.tsx
+++ b/app/components/Sidebar/components/DocumentLink.tsx
@@ -1,6 +1,7 @@
import type { Location } from "history";
import { observer } from "mobx-react";
import * as React from "react";
+import { useMergeRefs } from "react-merge-refs";
import { useTranslation } from "react-i18next";
import type { match } from "react-router";
import { useHistory } from "react-router-dom";
@@ -154,7 +155,8 @@ const DocumentLink = observer(function DocumentLink(props: Props) {
() => ({ root: scrollRoot, rootMargin: ROOT_MARGIN }),
[scrollRoot]
);
- const isOnScreen = useOnScreen(placeholderRef, observerOptions);
+ const [onScreenRef, isOnScreen] = useOnScreen(observerOptions);
+ const mergedRef = useMergeRefs([placeholderRef, onScreenRef]);
const isDragActive = useIsDragActive();
const [mounted, setMounted] = React.useState(false);
@@ -189,7 +191,7 @@ const DocumentLink = observer(function DocumentLink(props: Props) {
return (
<>
-
+
{mounted ? (
0} />
) : null}
diff --git a/app/hooks/useOnScreen.ts b/app/hooks/useOnScreen.ts
index 5189e0c74f..ee73370150 100644
--- a/app/hooks/useOnScreen.ts
+++ b/app/hooks/useOnScreen.ts
@@ -19,14 +19,15 @@ function parseRootMargin(
}
/**
- * Hook to return if a given ref is visible on screen.
+ * Hook to track whether an element is visible on screen.
*
- * @returns boolean if the node is visible
+ * @param options - intersection observer options.
+ * @returns a tuple of a ref callback to attach to the element and a boolean
+ * that is true when the element is visible.
*/
export default function useOnScreen(
- ref: React.RefObject,
options?: IntersectionObserverInit
-) {
+): [React.RefCallback, boolean] {
const root = options?.root;
const rootMargin = options?.rootMargin;
const threshold = Array.isArray(options?.threshold)
@@ -35,47 +36,47 @@ export default function useOnScreen(
const [isIntersecting, setIntersecting] = React.useState(!isSupported);
- React.useLayoutEffect(() => {
- const element = ref.current;
- if (!element) {
- return undefined;
- }
+ const ref = React.useCallback(
+ (element: HTMLElement | null) => {
+ if (!element) {
+ return;
+ }
- // Synchronous initial check so the first paint is correct.
- const [mt, mr, mb, ml] = parseRootMargin(rootMargin);
- const rect = element.getBoundingClientRect();
- const rootRect =
- root instanceof Element
- ? root.getBoundingClientRect()
- : {
- top: 0,
- left: 0,
- bottom: window.innerHeight,
- right: window.innerWidth,
- };
- const initialVisible =
- rect.bottom >= rootRect.top - mt &&
- rect.top <= rootRect.bottom + mb &&
- rect.right >= rootRect.left - ml &&
- rect.left <= rootRect.right + mr;
+ // Synchronous initial check so the first paint is correct.
+ const [mt, mr, mb, ml] = parseRootMargin(rootMargin);
+ const rect = element.getBoundingClientRect();
+ const rootRect =
+ root instanceof Element
+ ? root.getBoundingClientRect()
+ : {
+ top: 0,
+ left: 0,
+ bottom: window.innerHeight,
+ right: window.innerWidth,
+ };
+ const initialVisible =
+ rect.bottom >= rootRect.top - mt &&
+ rect.top <= rootRect.bottom + mb &&
+ rect.right >= rootRect.left - ml &&
+ rect.left <= rootRect.right + mr;
- setIntersecting(initialVisible);
+ setIntersecting(initialVisible);
- if (!isSupported) {
- return undefined;
- }
+ if (!isSupported) {
+ return;
+ }
- const observer = new IntersectionObserver(([entry]) => {
- setIntersecting(entry.isIntersecting);
- }, options);
- observer.observe(element);
+ const observer = new IntersectionObserver(([entry]) => {
+ setIntersecting(entry.isIntersecting);
+ }, options);
+ observer.observe(element);
- return () => {
- observer.unobserve(element);
- };
+ return () => observer.disconnect();
+ },
// Re-create when option primitives change; options object identity ignored
// eslint-disable-next-line react-hooks/exhaustive-deps
- }, [ref, root, rootMargin, threshold]);
+ [root, rootMargin, threshold]
+ );
- return isIntersecting;
+ return [ref, isIntersecting];
}
diff --git a/shared/editor/components/PDF.tsx b/shared/editor/components/PDF.tsx
index 8a0503bd83..ace65776ec 100644
--- a/shared/editor/components/PDF.tsx
+++ b/shared/editor/components/PDF.tsx
@@ -1,4 +1,5 @@
-import React, { useEffect, useRef } from "react";
+import React, { useCallback, useRef } from "react";
+import { useMergeRefs } from "react-merge-refs";
import styled from "styled-components";
import useDragResize from "./hooks/useDragResize";
import { ResizeLeft, ResizeRight } from "./ResizeHandle";
@@ -39,48 +40,53 @@ export default function PdfViewer(props: Props) {
});
// force embed to reload, so the content fits the new size.
- useEffect(() => {
- // firefox handles resizing on its own
- // and forced reload causes the parent to collapse while resizing
- if (isFirefox || !ref.current) {
- return;
- }
-
- const observer = new ResizeObserver(() => {
- if (dragging) {
+ const observeRef = useCallback(
+ (element: HTMLDivElement | null) => {
+ // firefox handles resizing on its own
+ // and forced reload causes the parent to collapse while resizing
+ if (isFirefox || !element) {
return;
}
- if (debounceTimerRef.current) {
- clearTimeout(debounceTimerRef.current);
- }
-
- debounceTimerRef.current = setTimeout(() => {
- if (embedRef.current) {
- embedRef.current.src = "";
- requestAnimationFrame(() => {
- if (embedRef.current) {
- embedRef.current.src = sanitizeUrl(href) ?? "";
- }
- });
+ const observer = new ResizeObserver(() => {
+ if (dragging) {
+ return;
}
- }, 250);
- });
- observer.observe(ref.current);
+ if (debounceTimerRef.current) {
+ clearTimeout(debounceTimerRef.current);
+ }
- return () => {
- observer.disconnect();
- if (debounceTimerRef.current) {
- clearTimeout(debounceTimerRef.current);
- }
- };
- }, [dragging, href]);
+ debounceTimerRef.current = setTimeout(() => {
+ if (embedRef.current) {
+ embedRef.current.src = "";
+ requestAnimationFrame(() => {
+ if (embedRef.current) {
+ embedRef.current.src = sanitizeUrl(href) ?? "";
+ }
+ });
+ }
+ }, 250);
+ });
+
+ observer.observe(element);
+
+ return () => {
+ observer.disconnect();
+ if (debounceTimerRef.current) {
+ clearTimeout(debounceTimerRef.current);
+ }
+ };
+ },
+ [dragging, href]
+ );
+
+ const mergedRef = useMergeRefs([ref, observeRef]);
return (