You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
> :bulb: Please note that this package only works with [Vite](https://vitejs.dev/) and [Nuxt](https://nuxt.com/) setups. Usage without a build-step is not supported. Testing against legacy bundlers (Vue CLI, Webpack) has not been performed.
12
+
13
+
<br />
14
+
9
15
## Intro
10
16
11
17
I find it useful to display global loaders in single-page apps. For example:
@@ -14,7 +20,7 @@ I find it useful to display global loaders in single-page apps. For example:
14
20
- When navigating to an internal page after a critical operation such as sign-in/sign-out
15
21
- When navigating to an internal page with plenty of data
16
22
17
-
Since I was creating and re-creating the same logic and markup over and over again, I decided to publish a package for it.
23
+
Since I was re-creating the same logic and markup over and over again, I decided to publish a package for it.
18
24
19
25
## Features
20
26
@@ -24,7 +30,24 @@ This package simplifies the usage of a single, top-level global loader by:
24
30
- Providing a practical API customizable via few key props (get started in 10 seconds)
25
31
- Properly disable user interactions with the rest of the app while the loader is displayed
26
32
- Announcing a screen reader message when the loader is displayed
27
-
- Dynamically update global options from anywhere in the Vue app and set scoped options for the current loader
33
+
- Dynamically update options from anywhere in the app and set options scoped to the current loader
There's no need to style the spinners (e.g. the spinner should be 300px wide on desktop, 160px wide on low-res mobile devices, etc). This is already taken care for you.
210
+
There's no need to style the spinners (e.g. the spinner should be 140px wide on desktop, 110px wide on mobile devices, animations should be disabled if users prefer reduced motion, etc). This is already taken care for you.
197
211
198
-
Each spinner already has its own CSS and inherits the `foreground` option specified in your config. You can append a class to override its styles, but it's not recommended and it's better to use a custom spinner.
212
+
Each spinner already has its own CSS and inherits the `foregroundColor` option specified in your config. You can append a class to override its styles, but it's not recommended and it's better to use a custom spinner.
199
213
200
214
#### Custom Spinners
201
215
@@ -218,7 +232,7 @@ To use your own spinner, pass a custom SVG (or whatever) to the default slot:
218
232
219
233
<style>
220
234
.MySpinner {
221
-
fill: var(--v-gl-fg-color); /* Value of the 'foreground' prop */
235
+
fill: var(--v-gl-fg-color); /* Value of the 'foregroundColor' prop */
222
236
width: 100px;
223
237
height: 100px;
224
238
@@ -235,14 +249,14 @@ To use your own spinner, pass a custom SVG (or whatever) to the default slot:
For convenience, you can set new defaults from anywhere in your Vue app using `updateOptions`:
288
302
303
+
> :bulb: No need to state the imports if using **Nuxt** (everything is auto-imported)
304
+
289
305
```vue
290
306
<script setup>
291
307
import { watch } from 'vue'
@@ -310,6 +326,35 @@ watch(
310
326
</script>
311
327
```
312
328
329
+
### Callbacks / Transitions Lifecycle
330
+
331
+
The `GlobalLoader` lifecycle is handled using Vue's [Transition](https://vuejs.org/guide/built-ins/transition.html) hooks. For convenience, `displayLoader` and `destroyLoader` include some syntactic sugar to make it easier to execute code after the fade transition is completed.
332
+
333
+
#### `displayLoader`
334
+
335
+
This function returns a promise that resolves after the enter transition is completed or cancelled.
336
+
337
+
```ts
338
+
const { displayLoader } =useGlobalLoader()
339
+
const router =useRouter()
340
+
341
+
awaitdisplayLoader() // Wait for the fade transition to complete...
342
+
awaitsignOut() // ...mutate the underlying UI
343
+
router.push('/') // ...navigate to the homepage and call 'destroyLoader' there
344
+
```
345
+
346
+
#### `destroyLoader`
347
+
348
+
This function doesn't return a promise but instead, it accepts a callback that is executed after the loader is destroyed.
@@ -342,17 +387,29 @@ Use it when you think it's better for the user to not interact with the rest of
342
387
343
388
### When to not use it
344
389
390
+
- To display a loader while your app JS is loading. Use the [SPA Loading Templates](#spa-loading-templates) in plain HTML for that (see below).
345
391
- Server-side rendered pages: they are already meant to send the proper content to the client and avoid spinners.
346
392
- Non-critical async operations that are quick and obvious, in such case a local loader is better (e.g. spinner in the newsletter form submit button).
347
393
- Async operations meant to feed the content of small sub-components, in such case [Suspense](https://vuejs.org/guide/built-ins/suspense.html) is the way to go.
348
-
- To display a loader while your app JS is loading. In this case:
349
-
-**Vite SPA** - Add the loader markup directly to the `index.html` file (e.g. `<div id="spa-loader">`) and remove it in a top-level (App.vue) _onMounted_ hook via `document.getElementById('spa-loader').remove()`.
350
-
-**Nuxt** - Use the built-in [SPA loading indicator](https://nuxt.com/blog/v3-6#spa-loading-indicator).
394
+
395
+
## SPA Loading Templates
396
+
397
+
For convenience, ready-made HTML templates for each spinner shipped with this package are available in [this folder](https://github.com/smastrom/vue-global-loader/tree/main/spa-loading-templates).
398
+
399
+
They can be used to display a global loader that has the same appearance of the one used in your app to be displayed while the app JS is loading.
400
+
401
+
### Vite SPA
402
+
403
+
Paste the markup as a direct child of the `body` in the `index.html` file and remove it in a top-level (App.vue) _onMounted_ hook via `document.getElementById('spa_loading_template').remove()`.
404
+
405
+
### Nuxt
406
+
407
+
Download the desired html file, rename it and place it in `@/app/spa-loading-template.html`.
351
408
352
409
## Thanks
353
410
354
411
[@n3r4zzurr0](https://github.com/n3r4zzurr0) for the awesome spinners.
0 commit comments