skeleton icon indicating copy to clipboard operation
skeleton copied to clipboard

Add view transitions when switching themes

Open nanwy opened this issue 1 year ago • 5 comments

Describe the feature in detail (code, mocks, or screenshots encouraged)

Skeleton v3 doc is powered by Astro, and Astro but-ins View Transitions. This means we can use that feature directly:https://docs.astro.build/en/guides/view-transitions/. like this: image

What type of pull request would this be?

New Feature

Provide relevant links or additional information.

https://docs.astro.build/en/guides/view-transitions/.

nanwy avatar Apr 18 '24 04:04 nanwy

I just implemented this. The circle growing animation like the one in the screenshot didn't end up looking very good so I added a line sweep instead

screen.webm

@endigo9740 Should I move forward with a pull request or skip this feature?

Niceadam avatar Aug 24 '24 13:08 Niceadam

@Niceadam no, we're leaving these types of features until the site design is closer to finalized. Feel free to note how you achieved this here or link to a repo that showcases the process though. My plan is to revisit documentation after we complete our current pass through components.

endigo9740 avatar Aug 24 '24 17:08 endigo9740

Alright, leaving the changes here, modify to taste. The new inline script for sites/next.skeleton.dev/src/components/docs/ThemeSwitch.astro:

<script is:inline>
    const elemSelect = document.getElementById('themesList');
    const lsTheme = localStorage.getItem('theme') || 'cerberus';
    
    // Init
    elemSelect.value = lsTheme;
    document.body.dataset.theme = lsTheme;
    
    // Change Theme
    function changeTheme(event) {
        const selectedTheme = event.target.value;
        document.body.dataset.theme = selectedTheme;
        localStorage.setItem('theme', selectedTheme);
    }
    
    // Change Handler
    async function onChangeHandler(event) {
    if (!document.startViewTransition || window.matchMedia('(prefers-reduced-motion: reduce)').matches) {
        changeTheme(event);
        return;
    }
    
    await document.startViewTransition(() => {
         changeTheme(event);
    }).ready
    
    const { top, left, width, height } = event.target.getBoundingClientRect();
    const x = left + width / 2;
    const y = top + height / 2;
    const right = window.innerWidth - left;
    const bottom = window.innerHeight - top;
    const maxRadius = Math.hypot(
      Math.max(left, right),
      Math.max(top, bottom),
    );

    document.documentElement.animate(
      {
        clipPath: [
          `inset(0px 100% 0px 0px)`,
          `inset(0)`,
        ],
      },
      {
        duration: 500,
        easing: 'ease-in-out',
        pseudoElement: '::view-transition-new(root)',
      }
    );
	}

    // Listeners
    elemSelect.addEventListener('change', onChangeHandler);
</script>

and add this in app.css

::view-transition-old(root),
::view-transition-new(root) {
	animation: none;
	mix-blend-mode: normal;
}

Niceadam avatar Aug 24 '24 18:08 Niceadam

@Hugos68 this will be something to look into: https://bsky.app/profile/astro.build/post/3m36c2ryy3f2a

I'm not so concerned with theme transitions, but rather just overall page transitions. We can always refine from there.

Let me know if this will overlap with our other doc plans.

endigo9740 avatar Oct 14 '25 19:10 endigo9740

Note that we technically have support for this as part of today's doc updates. So let's revisit soon.

endigo9740 avatar Nov 10 '25 17:11 endigo9740