Skip to content

Commit d191efb

Browse files
authored
Merge pull request #53 from maifeeulasad/feature-scroll-pos
[feature]: added progress on top of page to show location in page;
2 parents e532dc9 + 0ddf083 commit d191efb

File tree

1 file changed

+30
-2
lines changed

1 file changed

+30
-2
lines changed

src/layout/CustomLayout.tsx

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { ReactNode, useState } from 'react';
1+
import React, { ReactNode, useEffect, useState } from 'react';
22
import type { MenuDataItem } from '@ant-design/pro-components';
33
import { PageContainer, ProLayout } from '@ant-design/pro-components';
44
import { useLocation, Link } from 'react-router-dom';
@@ -174,6 +174,20 @@ interface ICustomLayoutProps {
174174
const CustomLayout = ({ children }: ICustomLayoutProps) => {
175175
const location = useLocation();
176176

177+
const [scrollPercent, setScrollPercent] = useState(0);
178+
179+
useEffect(() => {
180+
const handleScroll = () => {
181+
const scrollTop = window.scrollY || document.documentElement.scrollTop;
182+
const scrollHeight = document.documentElement.scrollHeight - window.innerHeight;
183+
const scrolled = (scrollTop / scrollHeight) * 100;
184+
setScrollPercent(scrolled);
185+
};
186+
187+
window.addEventListener('scroll', handleScroll, { passive: true });
188+
return () => window.removeEventListener('scroll', handleScroll);
189+
}, []);
190+
177191
return (
178192
<ProLayout
179193
logo={logo}
@@ -192,7 +206,21 @@ const CustomLayout = ({ children }: ICustomLayoutProps) => {
192206
>
193207
<PageContainer header={{ title: true }}>
194208
<Affix offsetTop={0}>
195-
<SearchBar />
209+
<div style={{ position: 'relative' }}>
210+
<div
211+
style={{
212+
position: 'absolute',
213+
top: 0,
214+
left: 0,
215+
height: 4,
216+
width: `${scrollPercent}%`,
217+
background: '#1890ff',
218+
transition: 'width 0.1s ease-out',
219+
zIndex: 9999,
220+
}}
221+
/>
222+
<SearchBar />
223+
</div>
196224
</Affix>
197225
<div style={{ padding: 16, background: 'transparent' }}>
198226
{children}

0 commit comments

Comments
 (0)