Skip to content

Commit

Permalink
fix: format
Browse files Browse the repository at this point in the history
  • Loading branch information
himself65 committed Jan 20, 2024
1 parent 49dafad commit ce05d70
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions src/use-next-link-props/index.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
import 'client-only';
import type { LinkProps } from 'next/link'
import { useCallback, useEffect, useMemo, useState } from 'react'
import type { MouseEvent } from 'react'
import { usePathname } from 'next/navigation'
import type { LinkProps } from 'next/link';
import { useCallback, useEffect, useMemo, useState } from 'react';
import type { MouseEvent } from 'react';
import { usePathname } from 'next/navigation';

export type ExtraProps = {
export interface ExtraProps {
isPending: boolean
}

export const useNextLinkProps = (props: LinkProps): LinkProps & ExtraProps => {
const pathname = usePathname();
const [targetPathname, setTargetPathname] = useState(() => pathname);
useEffect(() => {
setTargetPathname(pathname)
}, [pathname])
setTargetPathname(pathname);
}, [pathname]);
const onClickProp = props.onClick;
const onClick = useCallback((event: MouseEvent<HTMLAnchorElement>) => {
setTargetPathname(new URL(event.currentTarget.href).pathname);
return props.onClick?.(event);
}, [props.onClick]);
return onClickProp?.(event);
}, [onClickProp]);
const isPending = targetPathname !== pathname;
return useMemo(() => {
return {
...props,
onClick,
isPending
}
}, [props, onClick, isPending])
}
};
}, [props, onClick, isPending]);
};

0 comments on commit ce05d70

Please sign in to comment.