Skip to content

Commit 311a907

Browse files
committed
Update readme
1 parent 9a77428 commit 311a907

File tree

2 files changed

+117
-2
lines changed

2 files changed

+117
-2
lines changed

pkg/README.md

Lines changed: 97 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,97 @@
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)

pkg/src/lib/types.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,33 @@
1+
/**
2+
* The store's internal state. Use this value
3+
* to determine if the user has already taken
4+
* any action and if the listener is granted.
5+
*/
16
export type IdleDetectionSubscriptionState =
7+
// No state changes yet.
28
| "init"
9+
// Agent (aka browser) doesn't support the feature.
310
| "not-supported"
11+
// User doesn't allow the API, no permission granted.
412
| "not-permitted"
13+
// Supported + permission granted, but not yet listening.
514
| "ready"
15+
// Listening to change events.
616
| "started"
17+
// Detached listener, no changes will be registered.
718
| "stopped";
819

920
export type SubscribeToIdleDetectionParams = {
21+
/**
22+
* Seconds for timeout window between events.
23+
* Please note that the browser might require a minimum of 60 sec,
24+
* thus 1 minute as threshold to allow the API-call.
25+
*/
1026
threshold?: number;
27+
/**
28+
* Callback for state changes regarding the store-state.
29+
*/
1130
onStateChange: (next: IdleDetectionSubscriptionState) => void;
31+
/** Callback for value changes, which are provided by the API. */
1232
onEventChange: (parmas: Pick<IdleDetector, "userState" | "screenState">) => void;
1333
};

0 commit comments

Comments
 (0)