sveltekit-view-transition

repository·main·Indexed 41 days ago

https://github.com/paoloricciuti/sveltekit-view-transition

A library for integrating the Web View Transition API into SvelteKit applications to enable smooth animations between pages. Version 0.5.3 provides tools to enable default transitions via setupViewTransition(), a transition action for element-specific morphing, and a lifecycle event system (on/off) to execute code at specific stages of the navigation process.

Tokens
5.6K
Snippets
19
Records
24
Agent score
34%

What's inside sveltekit-view-transition

  1. Enable default view transitions in SvelteKit

    main

    To enable view transitions for all navigations in your application, call setupViewTransition() within your root layout file (e.g., src/routes/+layout.svelte). This provides a baseline transition for every page change.

    <script>
    	import { setupViewTransition } from 'sveltekit-view-transition';
    
    	setupViewTransition();
    </script>
    
    <slot />
  2. Develop and build the Svelte project

    main

    After creating the project and installing dependencies (via npm install, pnpm install, or yarn), use the following commands to manage the application lifecycle:

    • Development: Start a local development server.
    • Production Build: Compile the application for production.
    • Preview: Run a local server to preview the production build.
    # Start development server
    npm run dev
    
    # Start development server and open in browser
    npm run dev -- --open
    
    # Build for production
    npm run build
    
    # Preview production build
    npm run preview
  3. Develop and build the list-and-details example

    main

    After creating the project and installing dependencies (npm install, pnpm install, or yarn), use the following scripts to manage the application lifecycle:

    Development

    Start the development server:

    npm run dev

    To start the server and automatically open the app in a new browser tab:

    npm run dev -- --open

    Production

    Build the application for production:

    npm run build

    To preview the production build locally:

    npm run preview

    Note: To deploy, you may need to install a SvelteKit adapter for your target environment.

    npm run dev
    npm run build
    npm run preview
  4. Modify default transition styles

    main

    You can customize the default root transition by targeting the ::view-transition-old(root) and ::view-transition-new(root) pseudo-elements in your global CSS.

    To disable the default crossfade, set view-transition-name: none on the :root. To change the animation speed, adjust the animation-duration.

    <script>
    	import { setupViewTransition } from 'sveltekit-view-transition';
    
    	setupViewTransition();
    </script>
    
    <slot />
    
    <style>
    	/* Disable default crossfade. */
    	:root {
    		view-transition-name: none;
    	}
    
    	/* Or, just modify the duration. */
    	:global(::view-transition-old(root)),
    	:global(::view-transition-new(root)) {
    		animation-duration: 2s;
    	}
    </style>
  5. Subscribe to view transition lifecycle events with `on`

    main

    The on function, returned by setupViewTransition, allows you to execute code at specific stages of the view transition lifecycle. It accepts three arguments:

    1. eventName: The name of the event to subscribe to.
    2. callback: The function to run when the event occurs.
    3. options (optional): An object to configure the listener behavior.

    Event Names

    Events are called in this order:

    • before-start-view-transition: Called before the transition starts. Modifying the DOM here includes changes in the transition 'screenshot'.
    • before-navigation: Called before SvelteKit handles the navigation.
    • before-navigation-complete: Called after SvelteKit starts handling navigation but before it completes (transition is still active).
    • after-navigation-complete: Called after SvelteKit navigation completes (transition is still active).
    • transition-ready: Called when the view-transition is ready and pseudo-elements are created.
    • update-callback-done: Called when the view-transition callback has finished running.
    • transition-finished: Called when the transition finishes and the new view is in place.

    Options

    • registerDuringTransition (boolean): If true, the listener is added immediately, even if a transition is currently running. Useful for modifying state after an incoming transition.
    • autoWrap (boolean): If false, prevents the listener from being wrapped in afterNavigate (which normally ensures listeners are reassigned during same-page navigation).
    • autoClean (boolean): If true (default), the listener is automatically removed after being applied. If false, manual cleaning is required.

    Calling the function returned by on will unregister the callback.

  6. Configure advanced transition options in `transition` action

    main

    Instead of passing a simple string, you can pass an object to the transition action to customize how view transitions are applied. This allows for conditional logic based on navigation state, viewport visibility, and specific element properties.

    Supported options:

    • name (required): The view-transition-name. Can be a string or a function.
    • classes: Classnames applied to the target element during transition. Can be an array of strings or a function returning an array.
    • applyImmediately: Whether the transition name is applied immediately or only during navigation. Can be a boolean or a function.
    • shouldApply: Whether the transition should be applied at all. Can be a boolean or a function.
    <div use:transition={{ 
      name: 'my-element', 
      classes: ['active'], 
      applyImmediately: true, 
      shouldApply: true 
    }}></div>
  7. Add CSS classes to `:root` during navigation with `classes`

    main

    The classes function, returned by setupViewTransition, allows you to add specific CSS classes to the :root element during a navigation. This is useful for styling the transition state globally.

    It can be called directly in a Svelte <script> tag. It accepts either:

    1. An array of strings.
    2. A function that returns an array of strings. This function receives a navigation object (similar to the one provided by the transition action).

    By default, classes is wrapped in afterNavigate to ensure it works during same-page navigation. You can pass false as a second parameter to disable this wrapping.

    <script>
    	import { setupViewTransition } from 'sveltekit-view-transition';
    
    	const { classes } = setupViewTransition();
    
    	classes(({ navigation }) => {
    		if (navigation.to?.route.id === '/') {
    			return ['back'];
    		}
    	});
    </script>
  8. Use the transition action for specific elements

    main

    To give specific elements unique view transitions (allowing them to morph or animate independently during navigation), use the transition action returned by setupViewTransition().

    Pass a string to the use:transition directive; this string becomes the element's view-transition-name. You can then target this specific transition in your CSS using ::view-transition-old(<name>) and ::view-transition-new(<name>).

    <script>
    	import { setupViewTransition } from 'sveltekit-view-transition';
    
    	const { transition } = setupViewTransition();
    </script>
    
    <header use:transition={'header'}>
    	<!-- links -->
    </header>
    
    <slot />
    
    <style>
    	:global(::view-transition-old(header)),
    	:global(::view-transition-new(header)) {
    		/* ... */
    	}
    </style>
  9. Unsubscribe from events with `off`

    main

    The off function allows you to manually unsubscribe a specific handle from a specific event. While on returns an unsubscribe function that is usually preferred, off is available for manual management.

    By default, off is wrapped in afterNavigate to handle same-page navigation. You can pass false as a second parameter to avoid this wrapping.