Skip to content

Commit

Permalink
Hijack browser back event in BackButton
Browse files Browse the repository at this point in the history
  • Loading branch information
jmrossy committed Dec 8, 2024
1 parent 01aeef8 commit 5984a1c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/components/buttons/BackButton.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
import { ArrowIcon, Button } from '@hyperlane-xyz/widgets';
import { ComponentProps } from 'react';
import { ComponentProps, useEffect } from 'react';
import { CardPage } from '../../flows/CardPage';
import { useCardNav } from '../../flows/hooks';
import { Color } from '../../styles/Color';

export function BackButton({ page, ...rest }: ComponentProps<typeof Button> & { page: CardPage }) {
const { setPage } = useCardNav();

// TODO instead of hijacking the back event, a more idiomatic approach would be to
// migrate the app to the AppRouter structure and then use pages instead of CardNav
useEffect(() => {
history.pushState(null, '', location.href);
const onBackEvent = () => {
history.pushState(null, '', location.href);
setPage(page);
};
window.addEventListener('popstate', onBackEvent);
return () => window.removeEventListener('popstate', onBackEvent);
}, [page, setPage]);

return (
<Button onClick={() => setPage(page)} {...rest} className="flex items-center gap-0.5">
<ArrowIcon direction="w" width={30} height={20} color={Color.accent['500']} />
Expand Down
3 changes: 3 additions & 0 deletions src/flows/CardFlow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import { CardPage } from './CardPage';
import { LandingPage } from './LandingCard';
import { useCardNav } from './hooks';

// TODO instead of this somewhat custom approach, a more idiomatic approach would be to
// migrate the app to the AppRouter structure and then use pages instead of CardNav.
// But animations may be more difficult and the AppRouter is tricky for SPAs.
export function CardFlow() {
const { page, direction } = useCardNav();

Expand Down

0 comments on commit 5984a1c

Please sign in to comment.