/* * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ import React, { useEffect } from "react"; import { NavHashLink } from "react-router-hash-link"; interface WidgetNameId { name: string; id: string; isInsideSection: boolean; sectionWithTabs: string; } interface Props { stickyPosition: number; offset: number; widgetNameIds: Array; activeWidgetId: string; onBottomOfThePage: Function; isTop: boolean; area: number; marginRight: number; displayTableOfContents: boolean; onClick?: Function; } function Navigation({ stickyPosition, offset, widgetNameIds, activeWidgetId, onBottomOfThePage, isTop, area, displayTableOfContents, onClick, marginRight, }: Props) { // forcefully highlight the last tab in table of contents when user reaches // the bottom of the page const handleScroll = () => { const isBottom = Math.ceil(window.innerHeight + window.scrollY) >= document.documentElement.scrollHeight; if (isBottom && widgetNameIds.length) { onBottomOfThePage(widgetNameIds[widgetNameIds.length - 1].id); } }; useEffect(() => { window.addEventListener("scroll", handleScroll, { passive: true, }); return () => { window.removeEventListener("scroll", handleScroll); }; }, []); const scrollWithOffset = (el: HTMLElement) => { const yCoordinate = el.getBoundingClientRect().top + window.scrollY; const yOffset = -offset; window.scrollTo({ top: yCoordinate + yOffset, behavior: "smooth" }); }; const onClickHandler = (active: string) => { if (onClick) { onClick(active); } }; if (!isTop) { return ( ); } else { return ( ); } } export default Navigation;