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