Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/temzasse/vite-plugin-splash-screen

Vite plugin for adding a splash screen to your app ๐Ÿ’ฆ๐Ÿ“ฑ
https://github.com/temzasse/vite-plugin-splash-screen

react splash-screen svelte vite vite-plugin vue

Last synced: 5 days ago
JSON representation

Vite plugin for adding a splash screen to your app ๐Ÿ’ฆ๐Ÿ“ฑ

Awesome Lists containing this project

README

        

vite-plugin-splash-screen


ยท
Framework-agnostic splash screen plugin for Vite
ยท




npm version
npm license




![Demo of vite-plugin-splash-screen with line style loading indicator](media/demo-1.gif)

## ๐Ÿ’ฆ Features

When building a Single-Page-Application (SPA) that is fully rendered on the client side (CSR), it is common to only send a minimal HTML file to the client which only defines a single div as the root of the app and includes links and scripts to load all the necessary assets to render the application.

This common approach will lead to a blank screen for a few seconds while the JS is being loaded and parsed. To improve the user experience we can add a splash screen, which are commonly used in mobile applications, that is displayed while the assets are being loaded and parsed. Then when your application is ready and initialized, the splash screen can be animated out of the way to reveal the application.

With `vite-plugin-splash-screen` you get the following:

- **๐Ÿคน Framework-agnostic**: Works with any frontend framework that uses Vite!
- **๐ŸŽจ Customizable**: You can customize the splash screen with your own logo, change colors, and display a loading indicator.
- **๐Ÿš€ Fast**: The splash screen is inlined to the HTML file at build time.
- **๐Ÿ•น๏ธ Full control**: You can control when the splash screen is hidden.
- **๐Ÿ”ฎ Easy to use**: Just add the plugin to your Vite config and you are good to go.

## ๐Ÿ“ฒ Installation

Install `vite-plugin-splash-screen` with your favorite package manager:

```sh
npm install -D vite-plugin-splash-screen

# yarn
yarn add -D vite-plugin-splash-screen

# pnpm
pnpm add -D vite-plugin-splash-screen
```

## ๐Ÿง‘โ€๐Ÿ’ป Usage

Import the plugin and add it to you Vite config.

The only required option is `logoSrc`, which is the path (relative to the [publicDir](https://vitejs.dev/config/shared-options.html#publicdir)) of the logo that will be displayed on the splash screen.

```js
import { splashScreen } from "vite-plugin-splash-screen";

export default defineConfig({
plugins: [
/* ...other plugins... */
splashScreen({
logoSrc: "logo.svg",
}),
],
});
```

> [!IMPORTANT]
> **Only SVG logos are supported at the moment so that the logo can be inlined to the HTML file**.
>
> Make sure the logo has appropriate dimensions and is optimized for the web. See [SVGOMG](https://jakearchibald.github.io/svgomg/) for optimizing SVG files.

Then in your application code (written in React, Vue, Svelte, whatever), you can hide the splash screen when the application is ready.

```js
import { hideSplashScreen } from "vite-plugin-splash-screen/runtime";

hideSplashScreen();
```

For example in a React app, you can hide the splash screen in the `useEffect` hook.

```jsx
import { useEffect } from "react";
import { hideSplashScreen } from "vite-plugin-splash-screen/runtime";

export function App() {
useEffect(() => {
hideSplashScreen();
}, []);

return

My App
;
}
```

> [!TIP]
> You should wait until your application is fully initialized before hiding the splash screen. This could include setting up a router, initializing a store, loading translations, authenticating the user, or loading data from an API etc.

## ๐ŸŽจ Customization

You can customize the splash screen by providing additional options to the plugin.

### `minDurationMs`

The minimum duration the splash screen should be displayed, even if the `hideSplashScreen` function has been called.
This is useful to prevent the splash screen from flickering in case the app is initialized very quickly.

For example, to display the splash screen for at least 2 seconds:

```js
splashScreen({
logoSrc: "logo.svg",
minDurationMs: 2000,
});
```

### `loaderType`

What type of loading indicator should be displayed below the logo.

Available options: `"line"` (default), `"dots"`, `"none"`.

With `"dots"` you get the following loading indicator:

```js
splashScreen({
logoSrc: "logo.svg",
loaderType: "dots",
});
```

![Demo of vite-plugin-splash-screen with dots style loading indicator](media/demo-2.gif)

Provide `"none"` to hide the loading indicator:

```js
splashScreen({
logoSrc: "logo.svg",
loaderType: "none",
});
```

![Demo of vite-plugin-splash-screen with no loading indicator](media/demo-3.gif)

### `loaderBg`

The background color of the loading indicator (default `#0072f5`).

Example:

```js
splashScreen({
logoSrc: "logo.svg",
loaderType: "line",
loaderBg: "#ff0000",
});
```

### `splashBg`

The background color of the splash screen (default `#ffffff`).

Example:

```js
splashScreen({
logoSrc: "logo.svg",
splashBg: "#000000",
});
```

### Dynamic colors

If your app supports theming (eg. light and dark mode), you can dynamically change the colors of the splash screen using CSS variables.

The following CSS variables are available:

- `--vpss-bg-splash` - Splash screen background color (default `#ffffff`)
- `--vpss-bg-loader` - Loading indicator background color (default `#0072f5`)

โš ๏ธ Note: in order to avoid flickering of colors you should set the CSS variables **before** the splash screen is rendered! You can easily achieve this by utilizing the [vite-plugin-color-scheme](https://github.com/Temzasse/vite-plugin-color-scheme) plugin to inline a small script to setup the CSS variables based on the user's preferred color scheme.

```js
// vite.config.ts
import { splashScreen } from "vite-plugin-splash-screen";
import { colorScheme } from "vite-plugin-color-scheme";

export default defineConfig({
plugins: [
colorScheme({
defaultScheme: "light",
variables: {
light: {
"--vpss-bg-splash": "#ffffff",
"--vpss-bg-loader": "#b18500",
},
dark: {
"--vpss-bg-splash": "#242424",
"--vpss-bg-loader": "#ffcb29",
},
},
}),
splashScreen({
/* ...your options... */
}),
],
});
```