### Starting Svelte Development Server with npm
Source: https://github.com/paoloricciuti/sveltekit-view-transition/blob/main/examples/list-and-details/README.md
This snippet shows how to start the development server for a Svelte project using `npm run dev`. It also includes an option to automatically open the application in a new browser tab, facilitating rapid development and testing.
```bash
npm run dev
# or start the server and open the app in a new browser tab
npm run dev -- --open
```
--------------------------------
### Creating a Svelte Project with npm
Source: https://github.com/paoloricciuti/sveltekit-view-transition/blob/main/examples/list-and-details/README.md
This snippet demonstrates how to initialize a new Svelte project using `npm create svelte@latest`. It provides commands for creating a project in the current directory or a specified new directory, setting up the basic project structure.
```bash
# create a new project in the current directory
npm create svelte@latest
# create a new project in my-app
npm create svelte@latest my-app
```
--------------------------------
### Starting SvelteKit Development Server
Source: https://github.com/paoloricciuti/sveltekit-view-transition/blob/main/examples/sveltegram/README.md
This snippet shows how to start the development server for a SvelteKit application. It includes commands to run the server normally or to automatically open the application in a new browser tab upon startup.
```bash
npm run dev
# or start the server and open the app in a new browser tab
npm run dev -- --open
```
--------------------------------
### Building Svelte Production App with npm
Source: https://github.com/paoloricciuti/sveltekit-view-transition/blob/main/examples/list-and-details/README.md
This snippet provides the command to create a production-ready build of the Svelte application using `npm run build`. This command compiles and optimizes the project's assets for deployment, preparing it for a live environment.
```bash
npm run build
```
--------------------------------
### Installing sveltekit-view-transition Package
Source: https://github.com/paoloricciuti/sveltekit-view-transition/blob/main/README.md
This command installs the `sveltekit-view-transition` library as a development dependency using npm. Users can substitute `npm` with `pnpm` or `yarn` based on their preferred package manager to add the latest version of the library to their project.
```Bash
npm i -D sveltekit-view-transition@latest # or pnpm/yarn
```
--------------------------------
### Basic View Transition Setup in SvelteKit Layout
Source: https://github.com/paoloricciuti/sveltekit-view-transition/blob/main/README.md
This Svelte snippet shows the simplest way to enable default view transitions across all navigations in a SvelteKit application. By importing and calling `setupViewTransition()` in the `+layout.svelte` file, the library automatically handles the necessary hooks for smooth page transitions.
```Svelte
```
--------------------------------
### Enabling View Transitions with SvelteKit's onNavigate (Low-Level)
Source: https://github.com/paoloricciuti/sveltekit-view-transition/blob/main/README.md
This snippet demonstrates the low-level SvelteKit primitive for enabling view transitions. It uses the `onNavigate` hook to initiate a view transition before navigation completes, ensuring a smooth visual change between pages. It checks for browser support of `document.startViewTransition` and resolves a promise after the transition starts and navigation completes.
```TypeScript
onNavigate((navigation) => {
if (!document.startViewTransition) return;
return new Promise((resolve) => {
document.startViewTransition(async () => {
resolve();
await navigation.complete;
});
});
});
```
--------------------------------
### Conditionally Applying View Transition with `shouldApply` in SvelteKit
Source: https://github.com/paoloricciuti/sveltekit-view-transition/blob/main/README.md
This Svelte component extends the previous example by incorporating the `shouldApply` option alongside `applyImmediately`. It demonstrates how to apply a view transition name (`name: 'title'`) only when navigating *to* a specific detail page (`/post/[id]`) whose `id` matches the current post. This allows for fine-grained control over when forward transitions are initiated, complementing the `applyImmediately` for backward transitions.
```svelte
```
--------------------------------
### Creating a Svelte Project with npm
Source: https://github.com/paoloricciuti/sveltekit-view-transition/blob/main/examples/sveltegram/README.md
This snippet demonstrates how to initialize a new SvelteKit project using `npm create svelte@latest`. It provides commands for creating a project in the current directory or a specified new directory, `my-app`.
```bash
# create a new project in the current directory
npm create svelte@latest
# create a new project in my-app
npm create svelte@latest my-app
```
--------------------------------
### Building SvelteKit for Production
Source: https://github.com/paoloricciuti/sveltekit-view-transition/blob/main/examples/sveltegram/README.md
This snippet provides the command to create a production-ready build of a SvelteKit application. This command compiles and optimizes the application for deployment.
```bash
npm run build
```
--------------------------------
### Applying Named View Transitions to Elements with Svelte Action
Source: https://github.com/paoloricciuti/sveltekit-view-transition/blob/main/README.md
This snippet illustrates how to apply specific view transitions to individual elements using the `transition` Svelte action returned by `setupViewTransition()`. By assigning a unique `view-transition-name` (e.g., 'header') to an element, its transition can be independently styled using the corresponding `::view-transition-old(name)` and `::view-transition-new(name)` CSS pseudo-elements, allowing for granular control over animations.
```Svelte
```
--------------------------------
### SvelteKit Navigation Interface Definition
Source: https://github.com/paoloricciuti/sveltekit-view-transition/blob/main/README.md
This TypeScript interface defines the structure of the `Navigation` object, which is passed to the `classes` callback in `sveltekit-view-transition`. It details properties like `from` and `to` (containing `params`, `route`, and `url` for source and destination), `type` of navigation (e.g., 'link', 'form', 'popstate'), `willUnload` status, `delta` for history navigation, and a `complete` promise.
```typescript
interface Navigation {
/**
* Where navigation was triggered from
*/
from: {
/**
* Parameters of the target page - e.g. for a route like `/blog/[slug]`, a `{ slug: string }` object.
* Is `null` if the target is not part of the SvelteKit app (could not be resolved to a route).
*/
params: Record | null;
/**
* Info about the target route
*/
route: { id: string | null };
/**
* The URL that is navigated to
*/
url: URL;
} | null;
/**
* Where navigation is going to/has gone to
*/
to: {
/**
* Parameters of the target page - e.g. for a route like `/blog/[slug]`, a `{ slug: string }` object.
* Is `null` if the target is not part of the SvelteKit app (could not be resolved to a route).
*/
params: Record | null;
/**
* Info about the target route
*/
route: { id: string | null };
/**
* The URL that is navigated to
*/
url: URL;
} | null;
/**
* The type of navigation:
* - `form`: The user submitted a `