starlight icon indicating copy to clipboard operation
starlight copied to clipboard

feat: ViewTransition Astro component

Open abosch19 opened this issue 2 years ago β€’ 11 comments

What kind of changes does this PR include?

Allow user to set a new user configuration: viewTransitions (boolean) This config sets Astro ViewTransitions component on Page layout if true

  • Changes to Starlight code

Description

  • What does this PR change? User configuration and Page layout

abosch19 avatar Sep 09 '23 12:09 abosch19

⚠️ No Changeset found

Latest commit: 37184197ea7794e266783b37913d2dba72bf294d

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

changeset-bot[bot] avatar Sep 09 '23 12:09 changeset-bot[bot]

Deploy Preview for astro-starlight ready!

Name Link
Latest commit 37184197ea7794e266783b37913d2dba72bf294d
Latest deploy log https://app.netlify.com/sites/astro-starlight/deploys/64fc6a516a047f0008947bda
Deploy Preview https://deploy-preview-694--astro-starlight.netlify.app
Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

Lighthouse
1 paths audited
Performance: 100 (no change from production)
Accessibility: 100 (no change from production)
Best Practices: 100 (no change from production)
SEO: 100 (no change from production)
PWA: -
View the detailed breakdown and full score reports

To edit notification comments on pull requests, go to your Netlify site configuration.

netlify[bot] avatar Sep 09 '23 12:09 netlify[bot]

It looks like this currently breaks the pagefind/search functionality and also needs an astro version update. (checked on a forked version)

turbek avatar Sep 15 '23 14:09 turbek

It looks like this currently breaks the pagefind/search functionality and also needs an astro version update.

Thanks for testing that @turbek! Yes, View Transitions require all client-side scripts to include special handling, so it may be more involved than the changes currently in this PR. We might be able to solve this with transition:persist, but I’m not sure β€” it could also require more complex updates to our scripts.

delucis avatar Oct 24 '23 17:10 delucis

transition:persist="site-search" and a littlebit of extra code for the initialization solves the problem:

document.addEventListener('astro:page-load', () => {
	if (import.meta.env.DEV) return;

	const pageFind = document.querySelector('.pagefind-ui__search-input');

	if (pageFind) return;

	const onIdle = window.requestIdleCallback || ((cb) => setTimeout(cb, 1));
	onIdle(async () => {
		const { PagefindUI } = await import('@pagefind/default-ui');
		new PagefindUI({
			element: '#starlight__search',
			baseUrl: import.meta.env.BASE_URL,
			bundlePath: import.meta.env.BASE_URL.replace(/\/$/, '') + '/pagefind/',
			showImages: false,
			translations,
			showSubResults: true,
		});
	});
}, { once: true });

Although I'm not sure how ideal it is

turbek avatar Oct 25 '23 09:10 turbek

Thanks for testing β€” I’d also assume other client-side scripts are impacted? Like table of contents highlighting and language/theme switching?

delucis avatar Oct 25 '23 10:10 delucis

Actually we removed the language switching because we currently support 1 language and since our website doesn't support dark theme atm, we removed theme switching as well. So we never tested theme/language switching features with the SPA mode.

ToC highlighting seems to work as expected though.

turbek avatar Oct 25 '23 10:10 turbek

I was able to activate View Transitions by overrides Starlight Head.astro component.

I omitted translations since i don't have any.

 npm install @pagefind/default-ui  

Add this custom Head.astro to your custom component in the components configuration option in astro.config.mjs:


---
import Default from '@starlight/components/Head.astro';
import type { Props } from '@starlight/props';
import { ViewTransitions } from 'astro:transitions';
---

<Default {...Astro.props}><slot /></Default>
<ViewTransitions />

<script>
  // Skip PagefindUI on initial page load since it's already run by Search.astro.

  // Flag to track initial page load
  let initialLoad = true;

  document.addEventListener('astro:page-load', () => {
    // Skip PagefindUI on initial page load (and only execute on subsequent loads)
    if (initialLoad) {
      initialLoad = false;
      return;
    }

    // Code to initialize PagefindUI on subsequent loads (if needed)
    if (import.meta.env.DEV) return;

    const pageFind = document.querySelector('.pagefind-ui__search-input');
    if (pageFind) return;

    const onIdle = window.requestIdleCallback || (cb => setTimeout(cb, 1));
    onIdle(async () => {
      // @ts-expect-error β€” Missing types for @pagefind/default-ui package.
      const { PagefindUI } = await import('@pagefind/default-ui');
      new PagefindUI({
        element: '#starlight__search',
        baseUrl: import.meta.env.BASE_URL,
        bundlePath: import.meta.env.BASE_URL.replace(/\/$/, '') + '/pagefind/',
        showImages: false,
        translations: {},
        showSubResults: true,
      });
    });
  });
</script>

onemen avatar Feb 18 '24 08:02 onemen

Super easy to add ViewTransitions to starlight in a similar way to @onemen, just needed to adjust the import paths.

Using @astrojs/starlight/* instead of @starlight/*

---
import Default from '@astrojs/starlight/components/Head.astro'
import type { Props } from '@astrojs/starlight/props'
import { ViewTransitions } from 'astro:transitions'
---

<Default {...Astro.props}><slot /></Default>
<ViewTransitions />
import { defineConfig } from 'astro/config'
import starlight from '@astrojs/starlight'

// https://astro.build/config
export default defineConfig({
  integrations: [
    starlight({
      components: {
        Head: './src/components/Head.astro',
      }
    }),
  ],
})

lukethacoder avatar Mar 26 '24 23:03 lukethacoder

Adding VT to Starlight is quite simple, getting everything to behave correctly when VT is being used is the part that needs more work. Specifically:

  • The theme switcher state gets reset
  • The page theme gets mixed up
  • Logo with light and dark variation appear doubled after a transition
  • Search breaks / Depending on you fix it, the search is reinitialized on every transition and the old instance is not discarded (memory leak)
  • The language switcher state gets weird (especially on 404 pages)

If your Starlight site is unaffected by those or if you think the compromise in those case is fine, then you can enable VT by adding the component to the head.

Fryuni avatar Mar 31 '24 13:03 Fryuni

Adding view transition to a starlight side might involve more than enabling the basic transitions. Beside all the things Fryuni mentioned that must work if you use them, there is a brave new world of extensions you might want to have, once view transitions are enabled:

  • Sidebar behavior that auto-opens categories if closed, e.g. on back navigation or that scrolls the current entry into view if it isn't
  • Reasonable animations when you navigate after you scrolled a page down (this typically results in a large morph animations that looks like wild scrolling
  • Animation direction dependent on the order of your pages

It might be possible to add all this and a lot of options to control that to starlight itself. But this would also bring some complexity to the project.

For all being interested in a community solution for adding view transitions to starlight, have a look at the guide in the πŸ‘œ Bag of Tricks ✨: https://events-3bg.pages.dev/jotter/starlight/guide/

martrapp avatar Apr 14 '24 21:04 martrapp