1
0
Fork 0
mirror of https://github.com/cheeaun/phanpy.git synced 2025-03-11 00:18:51 +01:00

Auto-close account sheet when location path changes

Test this on account sheet first, probably useful for other sheets too
This commit is contained in:
Lim Chee Aun 2023-10-19 10:15:54 +08:00
parent 106cd16e41
commit c0c2bb45fe

View file

@ -1,4 +1,5 @@
import { useEffect } from 'preact/hooks'; import { useEffect, useRef } from 'preact/hooks';
import { useLocation } from 'react-router-dom';
import { api } from '../utils/api'; import { api } from '../utils/api';
import states from '../utils/states'; import states from '../utils/states';
@ -16,17 +17,28 @@ function AccountSheet({ account, instance: propInstance, onClose }) {
} }
}, [account]); }, [account]);
const location = useLocation();
const currentLocationRef = useRef(location.pathname);
useEffect(() => {
if (
currentLocationRef.current &&
location.pathname !== currentLocationRef.current
) {
onClose?.();
}
}, [location.pathname]);
return ( return (
<div <div
class="sheet" class="sheet"
onClick={(e) => { // onClick={(e) => {
const accountBlock = e.target.closest('.account-block'); // const accountBlock = e.target.closest('.account-block');
if (accountBlock) { // if (accountBlock) {
onClose({ // onClose({
destination: 'account-statuses', // destination: 'account-statuses',
}); // });
} // }
}} // }}
> >
{!!onClose && ( {!!onClose && (
<button type="button" class="sheet-close outer" onClick={onClose}> <button type="button" class="sheet-close outer" onClick={onClose}>