|
1 | | -# svelte-idle-detection |
2 | | -Idle Detetection API for SvelteKit |
| 1 | +# Svelte Idle Detection API |
| 2 | + |
| 3 | +This library provides a readable Svelte store to use a PWA's access to the [`Idle Detection API`](https://developer.mozilla.org/en-US/docs/Web/API/Idle_Detection_API), available on the window. It allows you to read certain state-values, defined by the API. As this API requires user permission, relevant helper functions are also included |
| 4 | + |
| 5 | +> Note: This feature is currently experimental and only supported in a subset of Chromium-browsers. |
| 6 | +
|
| 7 | +This package is part of a collection of PWA-related svelte-packages: |
| 8 | + |
| 9 | +- [🖥️ Screen Wake Lock](https://www.npmjs.com/package/svelte-screen-wake-lock) |
| 10 | +- [🔋 Battery Status](https://www.npmjs.com/package/svelte-battery-status) |
| 11 | +- [📡 Network Information](https://www.npmjs.com/package/svelte-network-information) |
| 12 | +- [🦥 Idle Detection API](https://www.npmjs.com/package/svelte-idle-detection) |
| 13 | + |
| 14 | +## Install |
| 15 | + |
| 16 | +```text |
| 17 | +npm i -D svelte-idle-detection |
| 18 | +``` |
| 19 | + |
| 20 | +## Usage |
| 21 | + |
| 22 | +### Basic |
| 23 | + |
| 24 | +The following example shows a simple, complete usage of the `idleDetectionStore`. It reads the store's `state` as well as `userState` and `screenState`, which come from the API. All possible helper functions are implemented to show a common use case for this library. |
| 25 | + |
| 26 | +```svelte |
| 27 | +<script lang="ts"> |
| 28 | + import { idleDetectionStore as store } from "svelte-idle-detection"; |
| 29 | +
|
| 30 | + const { |
| 31 | + state, |
| 32 | + userState, |
| 33 | + screenState, |
| 34 | + requestPermission, |
| 35 | + requestPermissionAndStart, |
| 36 | + start, |
| 37 | + stop |
| 38 | + } = store; |
| 39 | +
|
| 40 | + // Unsubscribe if the component unmounts. |
| 41 | + onDestroy(stop); |
| 42 | +</script> |
| 43 | +
|
| 44 | +<h1>Welcome to SvelteKit</h1> |
| 45 | +<p>Visit <a href="https://kit.svelte.dev">kit.svelte.dev</a> to read the documentation</p> |
| 46 | +
|
| 47 | +<ul> |
| 48 | + <li>State: {$state}</li> |
| 49 | + <li>UserState: {$userState}</li> |
| 50 | + <li>ScreenState: {$screenState}</li> |
| 51 | +</ul> |
| 52 | +
|
| 53 | +{#if $state === "init"} |
| 54 | + <button on:click={requestPermission}>Check permission</button> |
| 55 | + <button on:click={() => requestPermissionAndStart()}>Check permission & start</button> |
| 56 | +{/if} |
| 57 | +
|
| 58 | +{#if $state === "not-permitted"} |
| 59 | + <span>Not permitted</span> |
| 60 | +{/if} |
| 61 | +
|
| 62 | +{#if $state === "ready" || $state === "stopped"} |
| 63 | + <button on:click={() => start()}>Start</button> |
| 64 | +{/if} |
| 65 | +
|
| 66 | +{#if $state === "started"} |
| 67 | + <button on:click={stop}>Stop</button> |
| 68 | +{/if} |
| 69 | +
|
| 70 | +``` |
| 71 | + |
| 72 | +### Derived |
| 73 | + |
| 74 | +To subscribe to changes for only a specific selection of values, simply create a `derived` store. |
| 75 | + |
| 76 | +```svelte |
| 77 | +<script lang="ts"> |
| 78 | + import { idleDetectionStore } from 'svelte-battery-status'; |
| 79 | + import { derived } from 'svelte/store'; |
| 80 | +
|
| 81 | + const state = derived(idleDetectionStore, ($store) => $store.state); |
| 82 | +</script> |
| 83 | +
|
| 84 | +state: {$state} |
| 85 | +``` |
| 86 | + |
| 87 | +## API |
| 88 | + |
| 89 | +Please see the [types](https://github.com/flaming-codes/svelte-idle-detection/blob/main/pkg/src/lib/types.ts) for an API-documentation. |
| 90 | + |
| 91 | +## Svelte Development Help |
| 92 | + |
| 93 | +- [MSW w/ SvelteKit for local development](https://flaming.codes/posts/msw-in-sveltekit-for-local-development) |
| 94 | +- [License generator for SvelteKit-projects](https://flaming.codes/posts/license-generator-for-dependencies-in-sveltekit) |
| 95 | +- [Lazy-loading modules in SvelteKit](https://flaming.codes/posts/lazy-loading-modules-in-svelte-to-import-components-on-demand) |
| 96 | +- [Custom $lib-folder in SvelteKit](https://flaming.codes/posts/custom-lib-folder-with-path-alias-in-sveltekit)) |
| 97 | +- [HMR for SvelteKit w/ Gitpod](https://flaming.codes/posts/setup-hmr-for-sveltekit-with-gitpod) |
0 commit comments