From 60db4dda9ca264926e00b7003dc76102366cf922 Mon Sep 17 00:00:00 2001 From: Martin Trapp <94928215+martrapp@users.noreply.github.com> Date: Wed, 25 Sep 2024 15:37:56 +0200 Subject: [PATCH] fix back navigation --- .changeset/dull-seas-attend.md | 5 +++++ src/index.ts | 20 +++++--------------- 2 files changed, 10 insertions(+), 15 deletions(-) create mode 100644 .changeset/dull-seas-attend.md diff --git a/.changeset/dull-seas-attend.md b/.changeset/dull-seas-attend.md new file mode 100644 index 0000000..2d2c0f2 --- /dev/null +++ b/.changeset/dull-seas-attend.md @@ -0,0 +1,5 @@ +--- +'@vtbag/turn-signal': patch +--- + +Fixes a bug where backward animations didn't work as advertised diff --git a/src/index.ts b/src/index.ts index f5b5e91..ef7b29e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -6,7 +6,7 @@ function pageReveal(e: PageRevealEvent) { const pages = allPages(); let hereIdx = 1, fromIdx = 0; - if (act.navigationType === 'traverse' && !allPages) { + if (act.navigationType === 'traverse' && pages.length === 0) { hereIdx = act.entry?.index; fromIdx = act.from?.index; } else { @@ -37,11 +37,7 @@ function pageReveal(e: PageRevealEvent) { } const attributeName = direction.shift()!; value = - hereIdx < fromIdx - ? direction[0] - : hereIdx === fromIdx - ? direction[1] - : direction[2]; + hereIdx < fromIdx ? direction[0] : hereIdx === fromIdx ? direction[1] : direction[2]; if (attributeName && value) { document.documentElement.setAttribute(attributeName, value); e.viewTransition.finished.then(() => @@ -52,7 +48,6 @@ function pageReveal(e: PageRevealEvent) { direction = ['backward', 'same', 'forward']; dir = currentScript!.dataset.directionTypes; if (dir) { - direction = dir.trim().split(/\s*,\s*/); if (direction.length !== 3) { @@ -62,12 +57,7 @@ function pageReveal(e: PageRevealEvent) { return; } } - value = - hereIdx < fromIdx - ? direction[0] - : hereIdx === fromIdx - ? direction[1] - : direction[2]; + value = hereIdx < fromIdx ? direction[0] : hereIdx === fromIdx ? direction[1] : direction[2]; if (value) e.viewTransition.types.add(value); } } @@ -77,8 +67,8 @@ function allPages() { const selector = currentScript!.dataset.selector; return selector ? [...document.querySelectorAll(selector)].map( - (e) => new URL(e.href ?? '.', location.href).pathname - ) + (e) => new URL(e.href ?? '.', location.href).pathname + ) : []; }