Make detail back buttons history-aware
This commit is contained in:
34
src/components/BackButton.tsx
Normal file
34
src/components/BackButton.tsx
Normal file
@@ -0,0 +1,34 @@
|
||||
import { ArrowLeft } from 'lucide-react';
|
||||
import { useLocation, useNavigate } from 'react-router-dom';
|
||||
|
||||
type BackButtonProps = {
|
||||
fallbackTo: string;
|
||||
label?: string;
|
||||
};
|
||||
|
||||
const BackButton = ({ fallbackTo, label = 'Voltar' }: BackButtonProps) => {
|
||||
const navigate = useNavigate();
|
||||
const location = useLocation();
|
||||
|
||||
const handleClick = () => {
|
||||
if (location.key !== 'default' && window.history.length > 1) {
|
||||
navigate(-1);
|
||||
return;
|
||||
}
|
||||
|
||||
navigate(fallbackTo, { replace: true });
|
||||
};
|
||||
|
||||
return (
|
||||
<button
|
||||
type="button"
|
||||
onClick={handleClick}
|
||||
className="inline-flex w-fit items-center text-sm font-bold text-zinc-400 dark:text-dark-muted transition-colors hover:text-zinc-900 dark:hover:text-dark-text"
|
||||
>
|
||||
<ArrowLeft className="mr-2 h-4 w-4" />
|
||||
{label}
|
||||
</button>
|
||||
);
|
||||
};
|
||||
|
||||
export default BackButton;
|
||||
Reference in New Issue
Block a user