Svelte Loading Spinners
This library offers a collection of loading spinner Svelte components. The current stable version is `0.3.6`. It provides a variety of visual spinners, including `Jumper`, `BarLoader`, `Circle`, and `Wave`, each implemented as a standalone Svelte component. Updates are typically released as needed, responding to bug fixes or Svelte ecosystem changes, rather than on a fixed cadence. A key architectural aspect is that these are pure Svelte components, making them reactive and easily integrated into any Svelte application. They are designed to work seamlessly with SvelteKit, particularly demonstrating integration with the `$app/stores.navigating` store for handling page transitions. Each spinner component is highly customizable via common props such as `size`, `color`, `unit`, and `duration`. Specific spinners like `Circle2` and `Circle3` offer additional, unique props for fine-grained control over their appearance. The package includes TypeScript type definitions, providing full type safety for developers using TypeScript in their Svelte projects.
Common errors
-
Cannot find module 'svelte-loading-spinners'
cause The package was not installed, the import path is incorrect, or the module resolution in your build environment is misconfigured.fixEnsure the package is correctly installed via `npm install svelte-loading-spinners` or `yarn add svelte-loading-spinners`. Double-check the import statement for typos: `import { ComponentName } from 'svelte-loading-spinners';`. -
'Jumper' is not exported from 'svelte-loading-spinners'
cause This error occurs when attempting to use a default import for a component that is a named export, or when the component name is misspelled.fixUse a named import with curly braces for all components: `import { Jumper } from 'svelte-loading-spinners';`. Verify the component name is correctly capitalized. -
ReferenceError: $navigating is not defined
cause This issue arises when attempting to access the `$navigating` store, which is part of SvelteKit's `$app/stores`, in an environment where SvelteKit is not active or correctly configured, or if `$app/stores` was not imported.fixIf using SvelteKit, ensure `import { navigating } from '$app/stores';` is present in your script. If not using SvelteKit, you cannot use `$navigating` and must manage spinner visibility using alternative methods (e.g., local component state).
Warnings
- gotcha Some spinner components, specifically `Circle2` and `Circle3`, feature unique sets of props (e.g., `colorOuter`, `ballTopLeft`) that differ from the standard `size`, `color`, `unit`, and `duration` props shared by most other spinners. Assuming universal props will lead to unstyled or incorrectly animated spinners.
- gotcha The primary integration pattern showcased in the README uses `$app/stores.navigating` to display loading animations during page transitions. This store is exclusive to SvelteKit projects. Using `$navigating` in a vanilla Svelte application without SvelteKit will result in a `ReferenceError: $navigating is not defined`.
- gotcha The package's installation instructions suggest installing it as a development dependency (`npm i --save-dev`). While this is often fine for Svelte components that are compiled into the main bundle, in some specific build configurations or deployment scenarios, it might be safer to install it as a regular dependency (`npm install svelte-loading-spinners`) to guarantee its inclusion in production builds.
Install
-
npm install svelte-loading-spinners -
yarn add svelte-loading-spinners -
pnpm add svelte-loading-spinners
Imports
- Jumper
import Jumper from 'svelte-loading-spinners'; // Not a default export
import { Jumper } from 'svelte-loading-spinners'; - BarLoader
const { BarLoader } = require('svelte-loading-spinners'); // Primarily targets ESM environmentsimport { BarLoader } from 'svelte-loading-spinners'; - Circle2
import { Circle2 } from 'svelte-loading-spinners/src/Circle2.svelte'; // Direct component path is not recommended or necessaryimport { Circle2 } from 'svelte-loading-spinners';
Quickstart
<!-- src/routes/+layout.svelte -->
<script lang="ts">
import { Jumper, Wave } from 'svelte-loading-spinners';
import { navigating } from '$app/stores'; // SvelteKit specific store for navigation status
// Simulate a delay or background task for demonstration if not relying solely on SvelteKit navigation
let showManualSpinner = false;
async function simulateLoading() {
showManualSpinner = true;
await new Promise(resolve => setTimeout(resolve, 2000)); // Simulate a 2-second load
showManualSpinner = false;
}
</script>
<nav>
<a href="/">Home</a>
<a href="/about">About</a>
<button on:click={simulateLoading}>Simulate Task</button>
</nav>
<!-- Show spinner during SvelteKit navigation -->
{#if $navigating}
<div class="spinner-overlay kit-nav">
<Jumper size="60" color="#FF3E00" unit="px" duration="1s" />
<p>Loading page...</p>
</div>
{/if}
<!-- Show spinner for manual tasks -->
{#if showManualSpinner}
<div class="spinner-overlay manual-task">
<Wave size="80" color="blue" />
<p>Processing data...</p>
</div>
{/if}
<slot />
<style>
.spinner-overlay {
position: fixed;
top: 0;
left: 0;
width: 100vw;
height: 100vh;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
background-color: rgba(255, 255, 255, 0.9);
z-index: 1000;
}
.kit-nav { background-color: rgba(255, 255, 255, 0.9); }
.manual-task { background-color: rgba(0, 0, 0, 0.7); color: white; }
nav { padding: 1rem; background: #eee; }
nav a, nav button { margin-right: 1rem; }
</style>