Dismiss share modals when clicking links

This commit is contained in:
Sam Becker 2025-01-18 22:41:49 -06:00
parent 3875c1ab1e
commit 18b33389b5
2 changed files with 17 additions and 0 deletions

View File

@ -11,6 +11,7 @@ import { PiXLogo } from 'react-icons/pi';
import { SHOW_SOCIAL } from '@/site/config';
import { generateXPostText } from '@/utility/social';
import { useAppState } from '@/state/AppState';
import useOnPathChange from '@/utility/useOnPathChange';
export default function ShareModal({
title,
@ -44,6 +45,8 @@ export default function ShareModal({
{icon}
</div>;
useOnPathChange(() => setShareModalProps?.(undefined));
return (
<Modal onClose={() => setShareModalProps?.(undefined)}>
<div className="space-y-3 md:space-y-4 w-full">

View File

@ -0,0 +1,14 @@
import { usePathname } from 'next/navigation';
import { useEffect, useRef } from 'react';
export default function useOnPathChange(onPathChange: () => void) {
const path = usePathname();
const initialPath = useRef(path);
useEffect(() => {
if (initialPath.current !== path) {
onPathChange();
}
}, [path, onPathChange]);
}