From 31c52cad6e60801c48999cc204c71d123d22c064 Mon Sep 17 00:00:00 2001 From: 7PH Date: Wed, 17 May 2023 11:30:23 +0200 Subject: SONAR-19236 Implement correct layout for hotspot page sidebar --- .../sonar-web/src/main/js/hooks/useFollowScroll.ts | 42 ++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 server/sonar-web/src/main/js/hooks/useFollowScroll.ts (limited to 'server/sonar-web/src/main/js/hooks') diff --git a/server/sonar-web/src/main/js/hooks/useFollowScroll.ts b/server/sonar-web/src/main/js/hooks/useFollowScroll.ts new file mode 100644 index 00000000000..0692d5e2ab7 --- /dev/null +++ b/server/sonar-web/src/main/js/hooks/useFollowScroll.ts @@ -0,0 +1,42 @@ +/* + * SonarQube + * Copyright (C) 2009-2023 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +import { throttle } from 'lodash'; +import { useEffect, useState } from 'react'; + +const THROTTLE_DELAY = 10; + +export default function useFollowScroll() { + const [left, setLeft] = useState(0); + const [top, setTop] = useState(0); + + useEffect(() => { + const followScroll = throttle(() => { + if (document.documentElement) { + setLeft(document.documentElement.scrollLeft); + setTop(document.documentElement.scrollTop); + } + }, THROTTLE_DELAY); + + document.addEventListener('scroll', followScroll); + return () => document.removeEventListener('scroll', followScroll); + }, []); + + return { left, top }; +} -- cgit v1.2.3