From 63bc37f2b60cf5b093018ae30a2ae3c51da7d22d Mon Sep 17 00:00:00 2001 From: Martin Trapp <94928215+martrapp@users.noreply.github.com> Date: Wed, 27 Sep 2023 15:30:13 +0200 Subject: [PATCH] API for clientside router (#8571) * refactored CSR into goto() function * first refectoring for router API * added test * added comments to fixture * rename + preliminary changeset * changeset: now 'minor' and featuring Mathew's example from the docs * moved for simpler diff * update after #8617 * fixed ts-errors * more comprehensible handling of intra-page state * sync with main * synch from next_tm --- .changeset/fresh-pots-draw.md | 17 + packages/astro/client.d.ts | 7 + .../astro/components/ViewTransitions.astro | 444 +----------------- .../view-transitions/src/pages/six.astro | 20 + .../view-transitions/src/pages/two.astro | 1 + packages/astro/e2e/view-transitions.test.js | 81 ++++ packages/astro/package.json | 3 +- packages/astro/src/transitions/router.ts | 437 +++++++++++++++++ .../transitions/vite-plugin-transitions.ts | 10 + 9 files changed, 586 insertions(+), 434 deletions(-) create mode 100644 .changeset/fresh-pots-draw.md create mode 100644 packages/astro/e2e/fixtures/view-transitions/src/pages/six.astro create mode 100644 packages/astro/src/transitions/router.ts diff --git a/.changeset/fresh-pots-draw.md b/.changeset/fresh-pots-draw.md new file mode 100644 index 000000000..dde314c42 --- /dev/null +++ b/.changeset/fresh-pots-draw.md @@ -0,0 +1,17 @@ +--- +'astro': minor +--- + +View transitions can now be triggered from JavaScript! + +Import the client-side router from "astro:transitions/client" and enjoy your new remote control for navigation: + +```js +import { navigate } from 'astro:transitions/client'; + +// Navigate to the selected option automatically. +document.querySelector('select').onchange = (ev) => { + let href = ev.target.value; + navigate(href); +}; +``` diff --git a/packages/astro/client.d.ts b/packages/astro/client.d.ts index 5f396b522..c75ae7971 100644 --- a/packages/astro/client.d.ts +++ b/packages/astro/client.d.ts @@ -120,6 +120,13 @@ declare module 'astro:transitions' { export const ViewTransitions: ViewTransitionsModule['default']; } +declare module 'astro:transitions/client' { + type TransitionRouterModule = typeof import('./dist/transitions/router.js'); + export const supportsViewTransitions: TransitionRouterModule['supportsViewTransitions']; + export const transitionEnabledOnThisPage: TransitionRouterModule['transitionEnabledOnThisPage']; + export const navigate: TransitionRouterModule['navigate']; +} + declare module 'astro:middleware' { export * from 'astro/middleware/namespace'; } diff --git a/packages/astro/components/ViewTransitions.astro b/packages/astro/components/ViewTransitions.astro index 230b2f302..65a7af8a4 100644 --- a/packages/astro/components/ViewTransitions.astro +++ b/packages/astro/components/ViewTransitions.astro @@ -11,91 +11,12 @@ const { fallback = 'animate' } = Astro.props as Props; diff --git a/packages/astro/e2e/fixtures/view-transitions/src/pages/six.astro b/packages/astro/e2e/fixtures/view-transitions/src/pages/six.astro new file mode 100644 index 000000000..720378206 --- /dev/null +++ b/packages/astro/e2e/fixtures/view-transitions/src/pages/six.astro @@ -0,0 +1,20 @@ +--- +import Layout from '../components/Layout.astro'; +--- + +

Page 6

+ test +
test content
+ +
diff --git a/packages/astro/e2e/fixtures/view-transitions/src/pages/two.astro b/packages/astro/e2e/fixtures/view-transitions/src/pages/two.astro index af67edbd5..b77679d7f 100644 --- a/packages/astro/e2e/fixtures/view-transitions/src/pages/two.astro +++ b/packages/astro/e2e/fixtures/view-transitions/src/pages/two.astro @@ -4,6 +4,7 @@ import Layout from '../components/Layout.astro';

Page 2

+ go to long page