source: src/components/progress-bar/progress-bar.tsx@ 5d6f37a

main
Last change on this file since 5d6f37a was 5d6f37a, checked in by Naum Shapkarovski <naumshapkarovski@…>, 7 weeks ago

add customer

  • Property mode set to 100644
File size: 1.4 KB
Line 
1'use client';
2
3import { useEffect } from 'react';
4import NProgress from 'nprogress';
5import StyledProgressBar from './styles';
6
7type PushStateInput = [data: any, unused: string, url?: string | URL | null | undefined];
8
9export default function ProgressBar() {
10 useEffect(() => {
11 NProgress.configure({ showSpinner: false });
12
13 const handleAnchorClick = (event: MouseEvent) => {
14 const targetUrl = (event.currentTarget as HTMLAnchorElement).href;
15 const currentUrl = window.location.href;
16 if (targetUrl !== currentUrl) {
17 NProgress.start();
18 }
19 };
20
21 const handleMutation = () => {
22 const anchorElements: NodeListOf<HTMLAnchorElement> = document.querySelectorAll('a[href]');
23
24 const filteredAnchors = Array.from(anchorElements).filter((element) => {
25 const href = element.getAttribute('href');
26 return href && href.startsWith('/');
27 });
28
29 filteredAnchors.forEach((anchor) => anchor.addEventListener('click', handleAnchorClick));
30 };
31
32 const mutationObserver = new MutationObserver(handleMutation);
33
34 mutationObserver.observe(document, { childList: true, subtree: true });
35
36 window.history.pushState = new Proxy(window.history.pushState, {
37 apply: (target, thisArg, argArray: PushStateInput) => {
38 NProgress.done();
39 return target.apply(thisArg, argArray);
40 },
41 });
42 });
43
44 return <StyledProgressBar />;
45}
Note: See TracBrowser for help on using the repository browser.