From 6fd20bd1696769530bbef0010bd3d34ebf294956 Mon Sep 17 00:00:00 2001 From: Alex Andreev Date: Fri, 12 Mar 2021 12:51:08 +0300 Subject: [PATCH] Convert ScrollSpy to observable component Signed-off-by: Alex Andreev --- .../components/scroll-spy/scroll-spy.tsx | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/src/renderer/components/scroll-spy/scroll-spy.tsx b/src/renderer/components/scroll-spy/scroll-spy.tsx index 9b57368d30..0a47abe85c 100644 --- a/src/renderer/components/scroll-spy/scroll-spy.tsx +++ b/src/renderer/components/scroll-spy/scroll-spy.tsx @@ -1,3 +1,4 @@ +import { observer } from "mobx-react"; import React, { useEffect, useRef, useState } from "react"; import { useMutationObserver } from "../../hooks"; import { NavigationTree } from "../tree-view"; @@ -7,13 +8,7 @@ interface Props extends React.DOMAttributes { rootMargin?: string // https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API#creating_an_intersection_observer } -ScrollSpy.defaultProps = { - // Shrinking root area from the bottom - // Allows to fire observer event only if target scrolled up to top of the page) - rootMargin: "0px 0px -85%" -}; - -export function ScrollSpy(props: Props) { +export const ScrollSpy = observer(({ render, rootMargin = "0px 0px -85%" }: Props) => { const parent = useRef(); const sections = useRef>(); const [tree, setTree] = useState([]); @@ -70,7 +65,7 @@ export function ScrollSpy(props: Props) { const observeSections = () => { const options: IntersectionObserverInit = { threshold: [0], - rootMargin: props.rootMargin, + rootMargin }; sections.current.forEach((section) => { @@ -94,8 +89,7 @@ export function ScrollSpy(props: Props) { return (
- {props.render(tree)} + {render(tree)}
); -} - +});