Skip to content

Commit

Permalink
feat: upgraded appHistory
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Milfred committed Dec 2, 2024
1 parent b624665 commit 9ce73f9
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions composables/useAppHistory.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
let metaPosition = undefined;
let navigationDirection = 0;

export const useAppHistory = () => {
const route = useRoute();
const router = useRouter();
Expand All @@ -10,6 +13,7 @@ export const useAppHistory = () => {
currentUrl: route.fullPath,
forwardUrl: null,
position: null,
navigationDirection: 0,

// And additionals
get hasHistory() {
Expand All @@ -26,18 +30,24 @@ export const useAppHistory = () => {
},
});

onBeforeMount(() => {
if (import.meta.client) {
dataObject.ready = true;

metaPosition ??= router.options.history.state.position;
update();
});
}
watch(() => route.fullPath, update);

function update() {
if (metaPosition !== router.options.history.state.position) {
navigationDirection = Math.sign(router.options.history.state.position - metaPosition);
metaPosition = router.options.history.state.position;
}

dataObject.backUrl = router.options.history.state.back;
dataObject.currentUrl = router.options.history.state.current;
dataObject.forwardUrl = router.options.history.state.forward;
dataObject.position = router.options.history.state.position;
dataObject.navigationDirection = navigationDirection;
}

return dataObject;
Expand Down

0 comments on commit 9ce73f9

Please sign in to comment.